From 4388e6d610566f7023be91afb46f05ae96fada23 Mon Sep 17 00:00:00 2001 From: Gerald Baulig Date: Fri, 6 Feb 2026 18:03:47 +0100 Subject: [PATCH] fix(resource-base-interface): make equal operator optional again --- .../src/database/provider/arango/base.ts | 23 +-- .../src/database/provider/arango/common.ts | 28 ++-- packages/chassis-srv/test/database.spec.ts | 140 +++++++++--------- .../chassis-srv/test/microservice.spec.ts | 66 ++++----- .../src/core/ResourcesAPI.ts | 2 +- packages/resource-base-interface/src/index.ts | 7 +- .../resource-base-interface/test/crud.spec.ts | 2 +- 7 files changed, 138 insertions(+), 130 deletions(-) diff --git a/packages/chassis-srv/src/database/provider/arango/base.ts b/packages/chassis-srv/src/database/provider/arango/base.ts index 51fc8c716..e70bba2f6 100644 --- a/packages/chassis-srv/src/database/provider/arango/base.ts +++ b/packages/chassis-srv/src/database/provider/arango/base.ts @@ -64,13 +64,13 @@ export class Arango implements DatabaseProvider { } } - if (isEmptyish(filterQuery)) { - filterQuery = true; + if (isNullish(filterQuery) || isEmptyish(filterQuery)) { + filterQuery = ''; } else { filterQuery = toArray(filterQuery); filterResult = buildFilter(filterQuery); - filterQuery = filterResult.q; + filterQuery = `FILTER ${filterResult.q}`; } // if search options are set build search query @@ -104,7 +104,7 @@ export class Arango implements DatabaseProvider { if (!isEmptyish(searchQueries)) { queryStrings.push(`SEARCH ${searchQueries.join(' OR ')}`); } - queryStrings.push(`FILTER ${filterQuery}`); + queryStrings.push(filterQuery); queryStrings.push(...customFilters); queryStrings.push(sortQuery, limitQuery, returnQuery); @@ -122,7 +122,7 @@ export class Arango implements DatabaseProvider { if (!isEmptyish(customFilters) && opts.customArguments) { bindVars.customArguments = opts.customArguments; } - const queryString = queryStrings.filter(s => !isEmptyish(s)).join(' '); + const queryString = queryStrings.filter(s => !isNullish(s) && !isEmptyish(s)).join(' '); this.logger.info(queryString); const res = searchQueries ? await this.db.query(queryString, bindVars) @@ -197,8 +197,11 @@ export class Arango implements DatabaseProvider { async update(collectionName: string, updateDocuments: any): Promise { const documents = clone(updateDocuments) as ArangoDocument; const updateDocsResponse = []; - if (isNullish(collectionName) || - !isString(collectionName) || isEmptyish(collectionName)) { + if ( + isNullish(collectionName) + || !isString(collectionName) + || isEmptyish(collectionName) + ) { throw new LoggedError(this.logger, 'invalid or missing collection argument for update operation'); } if (isNullish(documents)) { @@ -250,8 +253,10 @@ export class Arango implements DatabaseProvider { * @param {Object|Array.Object} documents */ async upsert(collectionName: string, documents: any): Promise { - if (isNullish(collectionName) || - !isString(collectionName) || isEmptyish(collectionName)) { + if (isNullish(collectionName) + || !isString(collectionName) + || isEmptyish(collectionName) + ) { throw new LoggedError(this.logger, 'invalid or missing collection argument for upsert operation'); } if (isNullish(documents)) { diff --git a/packages/chassis-srv/src/database/provider/arango/common.ts b/packages/chassis-srv/src/database/provider/arango/common.ts index 2ef5c1f76..79bd06500 100644 --- a/packages/chassis-srv/src/database/provider/arango/common.ts +++ b/packages/chassis-srv/src/database/provider/arango/common.ts @@ -171,39 +171,39 @@ export const buildComparison = (filter: any, op: string, index: number, export const buildField = (key: any, value: any, index: number, bindVarsMap: any): string => { const bindValueVar = `@value${index}`; const bindValueVarWithOutPrefix = `value${index}`; - if (isString(value) || isBoolean(value) || isNumber(value || isDate(value))) { + if (isString(value) || isBoolean(value) || isNumber(value) || isDate(value)) { bindVarsMap[bindValueVarWithOutPrefix] = autoCastValue(key, value); return autoCastKey(key, value) + ' == ' + bindValueVar; } - if (!isNullish(value.$eq)) { + if (value?.$eq) { bindVarsMap[bindValueVarWithOutPrefix] = autoCastValue(key, value.$eq); return autoCastKey(key, value) + ' == ' + bindValueVar; } - if (value.$gt) { + if (value?.$gt) { bindVarsMap[bindValueVarWithOutPrefix] = autoCastValue(key, value.$gt); return autoCastKey(key, value) + ' > ' + bindValueVar; } - if (value.$gte) { + if (value?.$gte) { bindVarsMap[bindValueVarWithOutPrefix] = autoCastValue(key, value.$gte); return autoCastKey(key, value) + ' >= ' + bindValueVar; } - if (value.$lt) { + if (value?.$lt) { bindVarsMap[bindValueVarWithOutPrefix] = autoCastValue(key, value.$lt); return autoCastKey(key, value) + ' < ' + bindValueVar; } - if (value.$lte) { + if (value?.$lte) { bindVarsMap[bindValueVarWithOutPrefix] = autoCastValue(key, value.$lte); return autoCastKey(key, value) + ' <= ' + bindValueVar; } - if (!isNullish(value.$ne)) { + if (value?.$ne) { bindVarsMap[bindValueVarWithOutPrefix] = autoCastValue(key, value.$ne); return autoCastKey(key, value) + ' != ' + bindValueVar; } - if (value.$inVal) { + if (value?.$inVal) { bindVarsMap[bindValueVarWithOutPrefix] = autoCastValue(key, value.$inVal); return bindValueVar + ' IN ' + autoCastKey(key, value); } - if (value.$in) { + if (value?.$in) { bindVarsMap[bindValueVarWithOutPrefix] = autoCastValue(key, value.$in); if (isString(value.$in)) { // if it is a field which should be an array @@ -213,16 +213,16 @@ export const buildField = (key: any, value: any, index: number, bindVarsMap: any // assuming it is a list of provided values return autoCastKey(key, value) + ' IN ' + bindValueVar; } - if (value.$nin) { + if (value?.$nin) { bindVarsMap[bindValueVarWithOutPrefix] = autoCastValue(key, value.$nin); return autoCastKey(key, value) + ' NOT IN ' + bindValueVar; } - if (value.$iLike) { + if (value?.$iLike) { bindVarsMap[bindValueVarWithOutPrefix] = autoCastValue(key, value.$iLike); // @param 'true' is for case insensitive return ' LIKE (' + autoCastKey(key, value) + ',' + bindValueVar + ', true)'; } - if (!isNullish(value.$not)) { + if (value?.$not) { const temp = buildField(key, value.$not, index, bindVarsMap); return `!(${temp})`; } @@ -231,7 +231,7 @@ export const buildField = (key: any, value: any, index: number, bindVarsMap: any // will always search for an empty string return autoCastKey(key, '') + ' == ' + bindValueVar; } - if (!isNullish((value as any).$startswith)) { + if (value?.$startswith) { const bindValueVar1 = `@value${index + 1}`; const bindValueVarWithOutPrefix1 = `value${index + 1}`; const k = autoCastKey(key); @@ -240,7 +240,7 @@ export const buildField = (key: any, value: any, index: number, bindVarsMap: any bindVarsMap[bindValueVarWithOutPrefix1] = v; return `LEFT(${k}, LENGTH(${bindValueVar})) == ${bindValueVar1}`; } - if (!isNullish((value as any).$endswith)) { + if (value?.$endswith) { const bindValueVar1 = `@value${index + 1}`; const bindValueVarWithOutPrefix1 = `value${index + 1}`; const k = autoCastKey(key); diff --git a/packages/chassis-srv/test/database.spec.ts b/packages/chassis-srv/test/database.spec.ts index 68baf7842..5855590a3 100644 --- a/packages/chassis-srv/test/database.spec.ts +++ b/packages/chassis-srv/test/database.spec.ts @@ -31,14 +31,14 @@ const providers = [ const dbName: string = cfg.get('database:arango:database'); const db = new Database('http://' + dbHost + ':' + dbPort); - await db.dropDatabase(dbName); + await db!.dropDatabase(dbName); }, custom: () => { describe('testing custom queries', () => { it('should register a custom query', () => { const script = 'return "Hello World"'; - db.registerCustomQuery('helloWorld', script, 'query'); - const queries = db.listCustomQueries(); + db!.registerCustomQuery!('helloWorld', script, 'query'); + const queries = db!.listCustomQueries!(); should.exist(queries); queries.should.have.length(1); should.exist(queries[0][0]); @@ -52,20 +52,20 @@ const providers = [ }); it('should unregister a custom query', async () => { const script = 'return "Hello World";'; - db.registerCustomQuery('helloWorld', script, 'query'); - let functions = db.listCustomQueries(); + db!.registerCustomQuery!('helloWorld', script, 'query'); + let functions = db!.listCustomQueries!(); should.exist(functions); functions.should.have.length(1); - db.unregisterCustomQuery('helloWorld'); - functions = db.listCustomQueries(); + db!.unregisterCustomQuery!('helloWorld'); + functions = db!.listCustomQueries!(); should.exist(functions); functions.should.have.length(0); }); it('should execute a custom query', async () => { const script = `return "Hello World"`; - await db.registerCustomQuery('helloWorld', script, 'query'); - const result = await db.find('test', {}, { + await db!.registerCustomQuery!('helloWorld', script, 'query'); + const result = await db!.find('test', {}, { customQueries: ['helloWorld'] }); should.exist(result); @@ -74,8 +74,8 @@ const providers = [ }); it('should execute a custom query with custom parameters', async () => { const script = `return @param`; - await db.registerCustomQuery('helloWorld', script, 'query'); - const result = await db.find('test', {}, { + await db!.registerCustomQuery!('helloWorld', script, 'query'); + const result = await db!.find('test', {}, { customQueries: ['helloWorld'], customArguments: { param: 'Hello World' @@ -87,8 +87,8 @@ const providers = [ }); it('should execute a custom query which accesses the database', async () => { const script = `for t in test return t`; - await db.registerCustomQuery('script', script, 'query'); - const result = await db.find('test', {}, { + await db!.registerCustomQuery!('script', script, 'query'); + const result = await db!.find('test', {}, { customQueries: ['script'] }); should.exist(result); @@ -96,8 +96,8 @@ const providers = [ }); it('should apply a custom filter within a `find` query', async () => { const script = `filter node.id == @customArguments.param`; - await db.registerCustomQuery('script', script, 'filter'); - const result = await db.find('test', {}, { + await db!.registerCustomQuery!('script', script, 'filter'); + const result = await db!.find('test', {}, { customQueries: ['script'], customArguments: { param: '/test/sort0' @@ -116,8 +116,8 @@ const providers = [ }); it('should combine a custom filter with normal filters', async () => { const script = `filter node.value != @customArguments.param`; - await db.registerCustomQuery('script', script, 'filter'); - const result = await db.find('test', { + await db!.registerCustomQuery!('script', script, 'filter'); + const result = await db!.find('test', { include: { $eq: true } @@ -190,13 +190,13 @@ const testProvider = (providerCfg) => { ]; beforeEach(async () => { db = await providerCfg.init(); - await db.insert(collection, testData); + await db!.insert(collection, testData); should.exist(db); - const result = await db.count(collection, {}); + const result = await db!.count(collection, {}); // insert user collection for full text search testcase if (providerCfg.name === 'arango') { - await db.insert(userCollection, userData); - await db.insert(addressCollection, addressData); + await db!.insert(userCollection, userData); + await db!.insert(addressCollection, addressData); } }); @@ -209,11 +209,11 @@ const testProvider = (providerCfg) => { id: '/test/testupsert', name: 'test', }; - let result = await db.upsert(collection, newDoc); + let result = await db!.upsert(collection, newDoc); should.exist(result); result.should.deepEqual([newDoc]); newDoc.name = 'changed'; - result = await db.upsert(collection, newDoc); + result = await db!.upsert(collection, newDoc); result.should.deepEqual([newDoc]); }); it('should update existing document with upsert operation', async () => { @@ -221,7 +221,7 @@ const testProvider = (providerCfg) => { id: '/test/testupsert', name: 'changedAgain', }; - let result = await db.upsert(collection, newDoc); + let result = await db!.upsert(collection, newDoc); should.exist(result); result.should.deepEqual([newDoc]); }); @@ -229,33 +229,33 @@ const testProvider = (providerCfg) => { describe('count', () => { it(`should return the number of documents in the collection with blank filter`, async () => { - const result = await db.count(collection, {}); + const result = await db!.count(collection, {}); should.exist(result); result.should.equal(testData.length); }); it('should return one for filtering based on id', async () => { - const result = await db.count(collection, { id: testData[0].id }); + const result = await db!.count(collection, { id: testData[0].id }); should.exist(result); result.should.equal(1); }); }); describe('truncate', () => { it('should delete all collection', async () => { - await db.truncate(); - const result = await db.count(collection, {}); + await db!.truncate(); + const result = await db!.count(collection, {}); should.exist(result); result.should.equal(0); }); it('should delete all documents in provided collection', async () => { - await db.truncate(collection); - const result = await db.count(collection, {}); + await db!.truncate(collection); + const result = await db!.count(collection, {}); should.exist(result); result.should.equal(0); }); }); describe('findByID', () => { it('should find documents', async () => { - const result = await db.findByID(collection, document.id); + const result = await db!.findByID(collection, document.id); should.exist(result); result.should.be.length(1); result[0].should.deepEqual(document); @@ -264,7 +264,7 @@ const testProvider = (providerCfg) => { describe('find', () => { describe('with id filter', () => { it('should return a document', async () => { - const result = await db.find(collection, { + const result = await db!.find(collection, { id: document.id, }); result.should.be.length(1); @@ -276,7 +276,7 @@ const testProvider = (providerCfg) => { describe('with iLike filter', () => { it('should return one filtering based on iLike', async () => { - const result = await db.find('test', { + const result = await db!.find('test', { id: { $iLike: '%sOrT%' } @@ -293,7 +293,7 @@ const testProvider = (providerCfg) => { if (providerCfg.name == 'arango') { sortOrderKey = 'ASC'; } - const result = await db.find(collection, + const result = await db!.find(collection, { include: true }, { sort: { value: sortOrderKey } }); // sort ascending should.exist(result); @@ -305,7 +305,7 @@ const testProvider = (providerCfg) => { if (providerCfg.name == 'arango') { sortOrderKey = 'DESC'; } - const result = await db.find(collection, + const result = await db!.find(collection, { include: true }, { sort: { value: sortOrderKey } }); // sort descending should.exist(result); @@ -314,20 +314,20 @@ const testProvider = (providerCfg) => { }); describe('with field limiting', () => { it('should return documents with selected fields', async () => { - const result = await db.find(collection, + const result = await db!.find(collection, { include: true }, // 0 is exclude and 1 is to include that particular key { fields: { include: 0 } }); // exclude field 'include' should.exist(result); - const resultKeep = await db.find(collection, + const resultKeep = await db!.find(collection, { include: true }, { fields: { id: 1, value: 1 } }); // include only id and value fields resultKeep.should.deepEqual(result); // Not to modify the original data which is used in next test case // to add and delete in beforeEach and afterEach const clonedData = clone([testData[3], testData[4], testData[0]]); - const compareData = clonedData.map((e) => { - delete e['include']; + const compareData = clonedData.map((e: any) => { + delete e.include; return e; }); sortBy(result, prop('id')).should.deepEqual(sortBy(compareData, prop('id'))); @@ -335,7 +335,7 @@ const testProvider = (providerCfg) => { }); describe('with limit', () => { it('should return one document', async () => { - const result: Object = await db.find(collection, { + const result: Object = await db!.find(collection, { id: document.id, }, { @@ -349,7 +349,7 @@ const testProvider = (providerCfg) => { }); describe('with filter operator', () => { it('should return a document', async () => { - let result = await db.find(collection, { + let result = await db!.find(collection, { $or: [ { id: document.id }, { value: 'new' } @@ -359,7 +359,7 @@ const testProvider = (providerCfg) => { result.should.be.length(1); result[0].should.deepEqual(document); - result = await db.find(collection, { + result = await db!.find(collection, { $or: [ { id: document.id, @@ -386,7 +386,7 @@ const testProvider = (providerCfg) => { result.should.be.length(1); result[0].should.deepEqual(document); - result = await db.find(collection, { + result = await db!.find(collection, { id: document.id, }, { @@ -395,14 +395,14 @@ const testProvider = (providerCfg) => { }); result.should.be.empty(); - result = await db.find(collection, { + result = await db!.find(collection, { id: { $startswith: '/test', }, }); result.should.be.length(testData.length); - result = await db.find(collection, { + result = await db!.find(collection, { id: { $endswith: '0', }, @@ -410,7 +410,7 @@ const testProvider = (providerCfg) => { result.should.be.length(1); result[0].should.deepEqual(testData[0]); - result = await db.find(collection, { + result = await db!.find(collection, { value: { $isEmpty: null, }, @@ -425,7 +425,7 @@ const testProvider = (providerCfg) => { id: 'testnew', name: 'test', }; - let insertResp = await db.insert(collection, newDoc); + let insertResp = await db!.insert(collection, newDoc); insertResp[0].should.deepEqual(newDoc); }); it('should return an error response when inserting same document twice', async () => { @@ -434,9 +434,9 @@ const testProvider = (providerCfg) => { id: 'testnew', name: 'test', }; - let insertResp = await db.insert(collection, newDoc); + let insertResp = await db!.insert(collection, newDoc); insertResp[0].should.deepEqual(newDoc); - insertResp = await db.insert(collection, newDoc); + insertResp = await db!.insert(collection, newDoc); should.exist(insertResp); insertResp[0].error.should.equal(true); }); @@ -445,14 +445,14 @@ const testProvider = (providerCfg) => { it('should update document', async () => { const newDoc = clone(document); newDoc.value = 'new'; - await db.update(collection, [newDoc]); - let result = await db.findByID(collection, document.id); + await db!.update(collection, [newDoc]); + let result = await db!.findByID(collection, document.id); result = result[0]; result.should.deepEqual(newDoc); }); it('should return error response when updating document which does not exist', async () => { const invalidDoc = { id: 'invlaid', include: false }; - let updateResp = await db.update(collection, [invalidDoc]); + let updateResp = await db!.update(collection, [invalidDoc]); should.exist(updateResp); updateResp[0].error.should.equal(true); updateResp[0].errorMessage.should.equal('document not found'); @@ -460,12 +460,12 @@ const testProvider = (providerCfg) => { }); describe('delete', () => { it('should delete document and also return a response for missing / invalid doc ID', async () => { - let deleteResp = await db.delete(collection, [document.id, 'invalid']); + let deleteResp = await db!.delete(collection, [document.id, 'invalid']); should.exist(deleteResp); deleteResp.should.be.length(2); should.exist(deleteResp[0]._id); deleteResp[1].error.should.equal(true); - const result = await db.findByID(collection, document.id); + const result = await db!.findByID(collection, document.id); result.should.be.Array(); result.should.be.length(0); }); @@ -481,9 +481,9 @@ const testProvider = (providerCfg) => { { id: 'b', created: timeStamp2 }, { id: 'c', created: timeStamp3 } ]; - await db.insert(collection, timeData); + await db!.insert(collection, timeData); // should return first two documents - let result = await db.find(collection, { + let result = await db!.find(collection, { $and: [ { created: { @@ -504,13 +504,13 @@ const testProvider = (providerCfg) => { result = sortBy(result, (o) => { return o.id; }); result.should.deepEqual(timeData); // truncate test DB - await db.truncate(); + await db!.truncate(); }); if (providerCfg.name === 'arango') { describe('full text search', () => { it('should return all test docs ignorning search string when analayzer or view config not set', async () => { - let testDocs = await db.find(collection, {}, { search: { search: 'test' } }); + let testDocs = await db!.find(collection, {}, { search: { search: 'test' } }); testDocs.length.should.equal(8); }); @@ -519,7 +519,7 @@ const testProvider = (providerCfg) => { await new Promise((resolve, reject) => { setTimeout(resolve, 2000); }); - let usersFound = await db.find(userCollection, {}, { search: { search: 'Ich oWd' } }); + let usersFound = await db!.find(userCollection, {}, { search: { search: 'Ich oWd' } }); usersFound.length.should.equal(3); usersFound[0].id.should.equal('4'); usersFound[0].first_name.should.equal('Michael'); @@ -537,7 +537,7 @@ const testProvider = (providerCfg) => { await new Promise((resolve, reject) => { setTimeout(resolve, 2000); }); - let addressFound = await db.find(addressCollection, {}, { search: { search: 'ber man' } }); + let addressFound = await db!.find(addressCollection, {}, { search: { search: 'ber man' } }); addressFound.length.should.equal(4); addressFound[0].city.should.equal('Berlin'); // Berlin, Germany (both terms match) addressFound[1].city.should.equal('Bern'); @@ -550,9 +550,9 @@ const testProvider = (providerCfg) => { await new Promise((resolve, reject) => { setTimeout(resolve, 2000); }); - let usersFound = await db.find(userCollection, {}, { search: { search: 'Ich oWd', case_sensitive: true } }); + let usersFound = await db!.find(userCollection, {}, { search: { search: 'Ich oWd', case_sensitive: true } }); usersFound.length.should.equal(0); - usersFound = await db.find(userCollection, {}, { search: { search: 'Mic Bow', case_sensitive: true } }); + usersFound = await db!.find(userCollection, {}, { search: { search: 'Mic Bow', case_sensitive: true } }); usersFound.length.should.equal(3); usersFound[0].id.should.equal('4'); usersFound[0].first_name.should.equal('Michael'); @@ -570,7 +570,7 @@ const testProvider = (providerCfg) => { await new Promise((resolve, reject) => { setTimeout(resolve, 2000); }); - let usersFound = await db.find(userCollection, {}, { search: { search: 'müll' } }); + let usersFound = await db!.find(userCollection, {}, { search: { search: 'müll' } }); usersFound.length.should.equal(1); usersFound[0].first_name.should.equal('David'); usersFound[0].last_name.should.equal('Müller'); @@ -581,7 +581,7 @@ const testProvider = (providerCfg) => { await new Promise((resolve, reject) => { setTimeout(resolve, 2000); }); - let usersFound = await db.find(userCollection, {}, { search: { search: 'does not exist' } }); + let usersFound = await db!.find(userCollection, {}, { search: { search: 'does not exist' } }); usersFound.length.should.equal(0); }, 5000); @@ -590,7 +590,7 @@ const testProvider = (providerCfg) => { await new Promise((resolve, reject) => { setTimeout(resolve, 2000); }); - let usersFound = await db.find(userCollection, { last_name: { $iLike: '%bow%' } }, { search: { search: 'mic' } }); + let usersFound = await db!.find(userCollection, { last_name: { $iLike: '%bow%' } }, { search: { search: 'mic' } }); usersFound.length.should.equal(1); usersFound[0].first_name.should.equal('Michael'); usersFound[0].last_name.should.equal('Bowden'); @@ -601,7 +601,7 @@ const testProvider = (providerCfg) => { await new Promise((resolve, reject) => { setTimeout(resolve, 2000); }); - let resp = await db.deleteAnalyzer(['trigram', 'trigram_norm']); + let resp = await db!.deleteAnalyzer(['trigram', 'trigram_norm']); resp.length.should.equal(2); resp[0].id.should.equal('trigram'); resp[0].code.should.equal(409); @@ -616,7 +616,7 @@ const testProvider = (providerCfg) => { await new Promise((resolve, reject) => { setTimeout(resolve, 2000); }); - let resp = await db.dropView(['test']); + let resp = await db!.dropView(['test']); resp.length.should.equal(1); resp[0].id.should.equal('test'); resp[0].code.should.equal(404); @@ -628,7 +628,7 @@ const testProvider = (providerCfg) => { await new Promise((resolve, reject) => { setTimeout(resolve, 2000); }); - let resp = await db.dropView(['users_view']); + let resp = await db!.dropView(['users_view']); resp.length.should.equal(1); resp[0].id.should.equal('users_view'); resp[0].code.should.equal(200); @@ -641,8 +641,8 @@ const testProvider = (providerCfg) => { setTimeout(resolve, 2000); }); // drop view and then analyzer - await db.dropView(['users_view', 'addresss_view']); - let resp = await db.deleteAnalyzer(['trigram', 'trigram_norm']); + await db!.dropView(['users_view', 'addresss_view']); + let resp = await db!.deleteAnalyzer(['trigram', 'trigram_norm']); resp.length.should.equal(2); resp[0].id.should.equal('trigram'); resp[0].code.should.equal(200); diff --git a/packages/chassis-srv/test/microservice.spec.ts b/packages/chassis-srv/test/microservice.spec.ts index d802235d2..a682b0637 100644 --- a/packages/chassis-srv/test/microservice.spec.ts +++ b/packages/chassis-srv/test/microservice.spec.ts @@ -26,7 +26,7 @@ const status = { export const testService: TestServiceImplementation = { test: async (request) => { - request.value.should.be.equal('hello'); + request!.value!.should.be.equal('hello'); return { result: 'welcome', status @@ -34,11 +34,11 @@ export const testService: TestServiceImplementation = { }, create: async (request) => { return { - items: request.items.map(item => ({ + items: request!.items!.map(item => ({ payload: item, status })), - total_count: request.items.length, + total_count: request!.items!.length, operation_status: status }; }, @@ -72,8 +72,8 @@ const streamService: StreamServiceImplementation = { }, async* responseStream(request) { should.exist(request); - should.exist(request.value); - request.value.should.equal('ping'); + should.exist(request?.value); + request!.value!.should.equal('ping'); for (let i = 0; i < 3; i += 1) { yield {result: `${i}`}; } @@ -82,7 +82,7 @@ const streamService: StreamServiceImplementation = { for await (const item of request) { should.exist(item); should.exist(item.value); - item.value.should.equal('ping'); + item!.value!.should.equal('ping'); } yield {result: 'pong'}; } @@ -154,10 +154,10 @@ describe('microservice.Server', () => { // --- 'test' endpoint --- const testResult = await testClient.test({value: 'hello'}); should.exist(testResult.status); - testResult.status.code.should.equal(200); - testResult.status.message.should.equal('success'); + testResult.status!.code!.should.equal(200); + testResult.status!.message!.should.equal('success'); should.exist(testResult.result); - testResult.result.should.be.equal('welcome'); + testResult.result!.should.be.equal('welcome'); // --- 'testCreate' endpoint --- const msg: any = { @@ -171,26 +171,26 @@ describe('microservice.Server', () => { }] }); should.exist(createResult.operationStatus); - createResult.operationStatus.code.should.equal(200); - createResult.operationStatus.message.should.equal('success'); + createResult.operationStatus!.code!.should.equal(200); + createResult.operationStatus!.message!.should.equal('success'); should.exist(createResult.items); // verify decoded google.protobuf.any buffered response - createResult.items[0].payload.value.should.equal('helloWorld123'); - const decodedBuffResp = JSON.parse(createResult.items[0].payload.data.value.toString()); + createResult.items![0].payload!.value!.should.equal('helloWorld123'); + const decodedBuffResp = JSON.parse(createResult.items![0].payload!.data!.value!.toString()); decodedBuffResp.testKey.should.equal('testVal'); // --- 'throw' endpoint --- const throwResult = await testClient.throw({value: 'hello'}); should.exist(throwResult.status); - throwResult.status.code.should.equal(500); - throwResult.status.message.should.equal('forced error'); + throwResult.status!.code!.should.equal(500); + throwResult.status!.message!.should.equal('forced error'); should.not.exist(throwResult.result); // --- 'notFound' endpoint --- const notFoundResult = await testClient.notFound({value: 'hello'}); should.exist(notFoundResult.status); - notFoundResult.status.code.should.equal(404); - notFoundResult.status.message.should.equal('test not found'); + notFoundResult.status!.code!.should.equal(404); + notFoundResult.status!.message!.should.equal('test not found'); should.not.exist(notFoundResult.result); // 'requestStream' @@ -203,11 +203,11 @@ describe('microservice.Server', () => { value: 'ping' }])); should.exist(streamResult.status); - streamResult.status.code.should.equal(200); - streamResult.status.message.should.equal('success'); + streamResult.status!.code!.should.equal(200); + streamResult.status!.message!.should.equal('success'); should.exist(streamResult); should.exist(streamResult.result); - streamResult.result.should.be.equal('pong'); + streamResult.result!.should.be.equal('pong'); // 'responseStream' const responseStreamRequest = streamClient.responseStream({ @@ -215,7 +215,7 @@ describe('microservice.Server', () => { }); let concatDataResp = []; for await (const response of responseStreamRequest) { - concatDataResp.push(response.result); + concatDataResp.push(response.result!); } concatDataResp.should.deepEqual(['0', '1', '2']); @@ -224,7 +224,7 @@ describe('microservice.Server', () => { value: 'ping' }])); for await (const response of biStreamRequest) { - response.result.should.be.equal('pong'); + response.result!.should.be.equal('pong'); } }); }); @@ -252,12 +252,12 @@ describe('microservice.Server', () => { const resps = await Promise.all(reqs); for (let i = 0; i < resps.length; i += 1) { - const response = await resps[i]; + const response: any = await resps[i]; should.exist(response.status); - response.status.code.should.equal(200); - response.status.message.should.equal('success'); + response.status!.code!.should.equal(200); + response.status!.message!.should.equal('success'); should.exist(response.result); - response.result.should.be.equal('welcome'); + response.result!.should.be.equal('welcome'); } }); }); @@ -319,10 +319,10 @@ describe('microservice.Client', () => { }); should.exist(result); should.exist(result.status); - result.status.code.should.equal(200); - result.status.message.should.equal('success'); + result.status!.code!.should.equal(200); + result.status!.message!.should.equal('success'); should.exist(result.result); - result.result.should.equal('welcome'); + result.result!.should.equal('welcome'); // test with timeout await config.load(process.cwd() + '/test'); @@ -338,10 +338,10 @@ describe('microservice.Client', () => { }); should.exist(result); should.exist(result.status); - result.status.code.should.equal(200); - result.status.message.should.equal('success'); + result.status!.code!.should.equal(200); + result.status!.message!.should.equal('success'); should.exist(result.result); - result.result.should.equal('welcome'); + result.result!.should.equal('welcome'); }); }); describe('end', () => { @@ -374,7 +374,7 @@ describe('microservice.Client', () => { }); } catch (err) { should.exist(err); - err.message.should.equal('/test.Test/Test DEADLINE_EXCEEDED: Deadline exceeded'); + err.message!.should.equal('/test.Test/Test DEADLINE_EXCEEDED: Deadline exceeded'); } }); }); diff --git a/packages/resource-base-interface/src/core/ResourcesAPI.ts b/packages/resource-base-interface/src/core/ResourcesAPI.ts index 8bec5a7c2..fc113c138 100644 --- a/packages/resource-base-interface/src/core/ResourcesAPI.ts +++ b/packages/resource-base-interface/src/core/ResourcesAPI.ts @@ -387,7 +387,7 @@ export class ResourcesAPIBase { await this.db.find( this.collectionName, { - _key: { + id: { $in: [...new Set(documents?.map(doc => doc.id).filter(id => id))], }, }, diff --git a/packages/resource-base-interface/src/index.ts b/packages/resource-base-interface/src/index.ts index e826caffc..5b4920aa1 100644 --- a/packages/resource-base-interface/src/index.ts +++ b/packages/resource-base-interface/src/index.ts @@ -59,6 +59,7 @@ const insertFilterFieldOpValue = (filter: Filter, object: any, key: string) => { if (!Array.isArray(object)) { throw new Error('Filter object has to be of type Array'); } + console.log('HELLOO:',filter.operation); filter.operation ??= FilterOperation.eq; // defaults to eq if undefined; switch (filter.operation) { case FilterOperation.eq: @@ -147,10 +148,12 @@ export const convertToObject = (input: any, obj?: any, currentOperator?: string) } convertToObject(filterObj, obj, newOperator); } - } else if (filters.field && (filters.operation || filters.operation === 0) && filters.value !== undefined) { + } + if (filters.field && filters.value !== undefined) { // object contains field, operation and value, update it on obj using convertFilterToObject() obj = convertFilterToObject(obj, currentOperator, filters); - } else if (Array.isArray(filters?.filters)) { + } + if (Array.isArray(filters?.filters)) { for (const filterObj of filters.filters) { const operator = filters.operator ? filters.operator : 'and'; convertToObject(filterObj, obj, operator); diff --git a/packages/resource-base-interface/test/crud.spec.ts b/packages/resource-base-interface/test/crud.spec.ts index d8fcbc0fc..0c74da935 100644 --- a/packages/resource-base-interface/test/crud.spec.ts +++ b/packages/resource-base-interface/test/crud.spec.ts @@ -404,7 +404,7 @@ describe('ServiceBase', () => { const filters = [{ filters: [{ field: 'id', - operation: Filter_Operation.eq, + // operation: Filter_Operation.eq, value: 'test_xy' }] }];