How to view or retrieve data from hyperledger fabric - couchdb

I have modified fabcar example to store my own data which have keys like this 5e57b8dbb9b30e3575f45d75,5e57b8dbb9b30e3575f45d76.
I used getStateByRange to get data, but it retrieves only certain data between the range. Is there any way to retrieve all the data or based on the timestamp of data
Also how can to view the GUI of couchDB which is running in the fabcar example.

Yes, You can view a CouchDB GUI on http://127.0.0.1:5984/_utils URL, If you run your fabric locally if you have any server then check docker ps command output and get your couchDB port number.
And in fabric you want query for all data then use getQueryResult method it will give you all data.
for example:
let queryString = {
"selector": {}
}
let resultsIterator = await ctx.stub.getQueryResult(JSON.stringify(queryString));
Hope it will help you :)

Related

Hyperledger Fabric Version 2: How to query Block Header such as data hash, previous hash by using Fabric Node SDK 2.2

I am trying to build a decentralized app that able to do show the block header like data hash, the previous hash of the block when a user submits a new transaction. However, It seems like the new version of Fabric Node SDK 2.2 removes the function queryblock.
I refer on the documentation at https://hyperledger.github.io/fabric-sdk-node/release-2.2/module-fabric-network.html and currently still looking for some workaround to show the block info of the user's transaction.
The ideal output that I wish to achieved is almost similar to what Hyperledger Explorer provide.The reference can be check at:
https://github.com/hyperledger/blockchain-explorer
which show the information such as Number of Blocks, Data Hash, Previous Hash and other Block Information regarding the hash.
Some information that I able to gather
There are some information like BlockEvent that have the name blockData in the interface BlockEvent. However, I can't seem to find it when npm install i fabric-network.
https://hyperledger.github.io/fabric-sdk-node/release-2.2/module-fabric-network.BlockEvent.html
Since the user can query and check their hash with the ledger, a key or hash need to be returned to the user upon transaction success. Is there any API function for this? So far, I able to found there are getTransactionId() in the class Transaction. But is this one that I need to use?
https://hyperledger.github.io/fabric-sdk-node/release-2.2/module-fabric-network.Transaction.html
StackOverflow reference that I had gone over:
All of the reference since to deal with the old version of node SDK which is version 1.4.
Hyperledger Fabric : How to Query blocks using fabric NodeSDK
HyperLedger Fabric Get Block Info - using node.js
how to display current hash,previous hash using node js on view pages for hyperledger fabric
How do you calculate the Block Hash for the Current Block in Hyperledger Fabric with javascript?
How to get history of asset with block hash in hyperledger fabric using node sdk
You can query block number by call function GetBlockByNumber of qscc contract. Example:
const contract = network.getContract('qscc');
const resultByte = await contract.evaluateTransaction(
'GetBlockByNumber',
channelName,
String(blockNum)
);
const resultJson = BlockDecoder.decode(resultByte);
logger.debug('queryBlock', resultJson);

How to apply a pagination on IBM vs code chaincode for fabric?

I'm using an IBM-Blockchain platform VS code extension for one of my POC development. I want pagination on the Query function in fabric. Here is my demo example:
let queryString =
{
"selector": {
"isLink": isLink,
"f_id":f_id
}
}
How can I put pagination (like when 5 data comes on the first page and then continue like another page) on this chaincode selector query which is created on the IBM-Blockchain platform? Can anyone help me?
Thanks.
pagination is done using a pagination api, so suggest you take a look here
https://hyperledger.github.io/fabric-chaincode-node/release-1.4/api/fabric-shim.ChaincodeStub.html#getQueryResultWithPagination__anchor
For the api reference. Sorry I don't know if there is any example of use of this in any of the fabric samples, if not then hopefully there are some examples in the test suite for fabric-chaincode-node on github.

Mongoose - Keep local database in sync with remote database

I have access to two separate databases that I'd like to keep in sync, a new one and an existing one, which will be in separate physical locations. The new one is going to be used to service an external API, so to cut down on request time, I think it makes sense to only query the local database for API requests.
My initial approach was to use mongoose.createConnection and limit the local collection to minor metadata and directly access the remote collection, but that's what I'm now looking to avoid.
Another approach might be to use mongoose.createConnection to periodically query the remote db and update the local one, but it could be costly if I want to do make frequent updates.
There are ways to cut down the cost - for example, there is a lastUpdated property in the relevant collection on the existing database, which could be used to limit the remote query to recently updated records such as:
RemoteCollection.find({
lastUpdated: {$gte: Date.now() - lookbackPeriod}
})
But I'm wondering if there's any native functionality of mongoose/mongoDB that can be used more efficiently make the updates. I also thought about mongodump and mongorestore to keep a full local copy of the records I needed, but that also seems costly.
Any help is appreciated.
After a bit of reading and thanks to Jake's comment, it looks like it's working. I need to do some more setup, but the code below should work and is based on this section from the docs:
https://mongoosejs.com/docs/models.html#change-streams
The first step would be start mongod with the --replSet flag:
mongod --replSet "rs0" --bind_ip localhost,<hostname(s)|ip address(es)>
Then close and restart mongo and run rs.initiate() on the database. You can then check the status of the replica set with rs.status(). If that command works and returns a result, the replica set functionality should be there.
Then within Node, you can do something like this:
// The docs reference creating a new model but you can just import an existing one
const RemotePerson = require('./models/RemotePerson');
const LocalPerson = require('./models/LocalPerson');
RemotePerson.watch().on('change', data => {
if (data.operationType === "insert") {
LocalPerson.create(data.fullDocument);
} else if (data.operationType === "update") {
LocalPerson.findByIdAndUpdate(data.documentKey, {
$set: data.updateDescription.updatedFields
});
}
});

Syncing Azure Easy Table with WHERE clause

I'm developing a Xamarin.Forms app which uses an Azure app service with SQL database linked through EasyTables. I've run the samples and successfully tested querying tables etc on the server and enabled offline sync so as a localdb is created.
I've created the store, defined the table & sync'd it, however I want to be able to query it somehow with a where clause - is that possible? Can I add a where clause to the client.GetSyncTable line?
var store = new MobileServiceSQLiteStore("localstore.db");
store.DefineTable<Journey_Stages>();
client.SyncContext.InitializeAsync(store);
tbl_Stages = client.GetSyncTable<Journey_Stages>();
Some of the tables I'm pulling down will grow over time & are linked to individual user profiles, so I only want data which belongs to that user and I don't want to be bringing down masses of data each time, preferably let the server handle that and only bring down what I need on a user by user basis.
Thanks,
Steve
You should add this filtering logic on the server side, so that each user's data isn't exposed to all your other users. See for example this sample if you are using the Node.js backend -- line 17 adds a WHERE clause for the table read query. If you have the .Net backend, similar logic would go in your table controller.
// Configure specific code when the client does a request
// READ - only return records belonging to the authenticated user
table.read(function (context) {
context.query.where({ userId: context.user.id });
return context.execute();
});

CouchDB longpoll without _changes

I want to use the longpoll option in couchDB, but without the _changes view.
The database has this view:
function (doc) {
if(doc.job)
emit(doc._id, 1);
}
So it emits all documents that have a tag “job” in them. Note that I have a bunch of other stuff in this DB. Now I want to use a longpoll where the connection stays open as long as there are no documents in the DB with the job tag. As soon as there is a document with the job tag, the db send it to the client and the connection should be closed. The client can now do the job, send a delete command to the db to delete the job, and start listening again.
So my idea is to call the view somehow like this:
http://mycouch/mydb/_design/visualize/_view/get_jobs?feed=longpoll&include_docs=true
However, it seems it is only possible to use longpoll with the _changes view. Any workaround?
Thanks for any help
_changes is the feed you need to use in your case. Yo can use it filtering the feed by receiving changes only for "job" tagged documents. You can do this using your view map function.
http://mycouch/_changes?filter=_view&view=visualize/get_jobs&feed=logpoll&include_docs=true
You'll receive through this feed any update (create/update/delete) over "job" tagged docs in your database.

Resources