From 552b344914e93b7ed0bc34b8ee8a63cb1781beb7 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Mon, 15 Apr 2024 18:34:13 +0200 Subject: [PATCH] feat(SmartDataDbDoc): add static .getCount({}) method --- test/test.ts | 8 +++++++- ts/00_commitinfo_data.ts | 2 +- ts/smartdata.classes.collection.ts | 5 +++++ ts/smartdata.classes.doc.ts | 11 +++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/test/test.ts b/test/test.ts index a06d970..e819013 100644 --- a/test/test.ts +++ b/test/test.ts @@ -199,12 +199,18 @@ tap.test('should store a new Truck', async () => { const truck = new Truck('blue', 'MAN'); await truck.save(); const myTruck2 = await Truck.getInstance({ color: 'blue' }); + expect(myTruck2.color).toEqual('blue'); myTruck2.color = 'red'; await myTruck2.save(); const myTruck3 = await Truck.getInstance({ color: 'blue' }); - console.log(myTruck3); + expect(myTruck3).toBeNull(); }); +tap.test('should return a count', async () => { + const truckCount = await Truck.getCount(); + expect(truckCount).toEqual(1); +}) + tap.test('should use a cursor', async () => { const cursor = await Car.getCursor({}); let counter = 0; diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 7e67d00..eb76ed8 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartdata', - version: '5.1.2', + version: '5.2.0', description: 'An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.' } diff --git a/ts/smartdata.classes.collection.ts b/ts/smartdata.classes.collection.ts index 26fa7f5..8f0f610 100644 --- a/ts/smartdata.classes.collection.ts +++ b/ts/smartdata.classes.collection.ts @@ -273,6 +273,11 @@ export class SmartdataCollection { await this.mongoDbCollection.deleteOne(identifiableObject); } + public async getCount(filterObject: any) { + await this.init(); + return this.mongoDbCollection.countDocuments(filterObject); + } + /** * checks a Doc for constraints * if this.objectValidation is not set it passes. diff --git a/ts/smartdata.classes.doc.ts b/ts/smartdata.classes.doc.ts index dd66242..d31a347 100644 --- a/ts/smartdata.classes.doc.ts +++ b/ts/smartdata.classes.doc.ts @@ -181,6 +181,17 @@ export class SmartDataDbDoc( + this: plugins.tsclass.typeFest.Class, + filterArg: plugins.tsclass.typeFest.PartialDeep = ({} as any) + ) { + const collection: SmartdataCollection = (this as any).collection; + return await collection.getCount(filterArg); + } + // INSTANCE /**