Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

feat: return processing cost for queryDocuments #100

Merged
merged 3 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions node/Drive.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Drive {

/**
* @param {boolean} [useTransaction=false]
* @returns {Promise<void>}
* @returns {Promise<[number, number]>}
*/
async createRootTree(useTransaction = false) {
return driveCreateRootTreeAsync.call(this.drive, useTransaction);
Expand All @@ -72,7 +72,7 @@ class Drive {
* @param {DataContract} dataContract
* @param {Date} blockTime
* @param {boolean} [useTransaction=false]
* @returns {Promise<void>}
* @returns {Promise<[number, number]>}
*/
async applyContract(dataContract, blockTime, useTransaction = false) {
return driveApplyContractAsync.call(
Expand All @@ -87,7 +87,7 @@ class Drive {
* @param {Document} document
* @param {Date} blockTime
* @param {boolean} [useTransaction=false]
* @returns {Promise<void>}
* @returns {Promise<[number, number]>}
*/
async createDocument(document, blockTime, useTransaction = false) {
return driveCreateDocumentAsync.call(
Expand All @@ -106,7 +106,7 @@ class Drive {
* @param {Document} document
* @param {Date} blockTime
* @param {boolean} [useTransaction=false]
* @returns {Promise<void>}
* @returns {Promise<[number, number]>}
*/
async updateDocument(document, blockTime, useTransaction = false) {
return driveUpdateDocumentAsync.call(
Expand All @@ -125,7 +125,7 @@ class Drive {
* @param {string} documentType
* @param {Identifier} documentId
* @param {boolean} [useTransaction=false]
* @returns {Promise<void>}
* @returns {Promise<[number, number]>}
*/
async deleteDocument(
dataContract,
Expand Down Expand Up @@ -153,32 +153,37 @@ class Drive {
* @param [query.startAfter]
* @param [query.orderBy]
* @param {Boolean} [useTransaction=false]
* @returns {Promise<Document[]>}
* @returns {Promise<[Document[], number]>}
*/
async queryDocuments(dataContract, documentType, query = {}, useTransaction = false) {
const encodedQuery = await cbor.encodeAsync(query);

const [encodedDocuments] = await driveQueryDocumentsAsync.call(
const [encodedDocuments, , processingFee] = await driveQueryDocumentsAsync.call(
this.drive,
encodedQuery,
dataContract.id.toBuffer(),
documentType,
useTransaction,
);

return encodedDocuments.map((encodedDocument) => {
const documents = encodedDocuments.map((encodedDocument) => {
const [protocolVersion, rawDocument] = decodeProtocolEntity(encodedDocument);

rawDocument.$protocolVersion = protocolVersion;

return new Document(rawDocument, dataContract);
});

return [
documents,
processingFee,
];
}

/**
* @param {DataContract} dataContract
* @param {DataContract} identity
* @param {boolean} [useTransaction=false]
* @returns {Promise<void>}
* @returns {Promise<[number, number]>}
*/
async insertIdentity(identity, useTransaction = false) {
return driveInsertIdentityAsync.call(
Expand Down
13 changes: 10 additions & 3 deletions node/test/Drive.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,35 +195,42 @@ describe('Drive', () => {
await Promise.all(
documents.map((document) => drive.createDocument(document, blockTime)),
);
const fetchedDocuments = await drive.queryDocuments(dataContract, 'indexedDocument', {

const [fetchedDocuments, processingCost] = await drive.queryDocuments(dataContract, 'indexedDocument', {
where: [['lastName', '==', 'Kennedy']],
});

expect(fetchedDocuments).to.have.lengthOf(1);
expect(fetchedDocuments[0]).to.be.an.instanceOf(Document);
expect(fetchedDocuments[0].toObject()).to.deep.equal(documents[4].toObject());

expect(processingCost).to.be.greaterThan(0);
});

it('should query existing documents again', async () => {
// Create documents
await Promise.all(
documents.map((document) => drive.createDocument(document, blockTime)),
);
const fetchedDocuments = await drive.queryDocuments(dataContract, 'indexedDocument', {

const [fetchedDocuments, processingCost] = await drive.queryDocuments(dataContract, 'indexedDocument', {
where: [['lastName', '==', 'Kennedy']],
});

expect(fetchedDocuments).to.have.lengthOf(1);
expect(fetchedDocuments[0]).to.be.an.instanceOf(Document);
expect(fetchedDocuments[0].toObject()).to.deep.equal(documents[4].toObject());

expect(processingCost).to.be.greaterThan(0);
});

it('should return empty array if documents are not exist', async () => {
const fetchedDocuments = await drive.queryDocuments(dataContract, 'indexedDocument', {
const [fetchedDocuments, processingCost] = await drive.queryDocuments(dataContract, 'indexedDocument', {
where: [['lastName', '==', 'Kennedy']],
});

expect(fetchedDocuments).to.have.lengthOf(0);
expect(processingCost).to.be.greaterThan(0);
});
});

Expand Down