Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
3f591ff9d8 | |||
7b33347b4c | |||
3f11cbf595 | |||
cf1ec7f9eb | |||
54060deb8f | |||
48cffb5ac2 | |||
8301eb79a2 | |||
ad6366a294 | |||
cb6c03ebfe | |||
551916fe5c |
8244
package-lock.json
generated
8244
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
22
package.json
22
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "4.0.17",
|
"version": "4.0.22",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "do more with data",
|
"description": "do more with data",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@ -21,27 +21,27 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartdata#README",
|
"homepage": "https://gitlab.com/pushrocks/smartdata#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/lik": "^4.0.20",
|
"@pushrocks/lik": "^5.0.0",
|
||||||
"@pushrocks/smartlog": "^2.0.39",
|
"@pushrocks/smartlog": "^2.0.44",
|
||||||
"@pushrocks/smartpromise": "^3.1.5",
|
"@pushrocks/smartpromise": "^3.1.5",
|
||||||
"@pushrocks/smartstring": "^3.0.24",
|
"@pushrocks/smartstring": "^3.0.24",
|
||||||
"@pushrocks/smartunique": "^3.0.3",
|
"@pushrocks/smartunique": "^3.0.3",
|
||||||
"@tsclass/tsclass": "^3.0.33",
|
"@tsclass/tsclass": "^3.0.36",
|
||||||
"@types/lodash": "^4.14.169",
|
"@types/lodash": "^4.14.176",
|
||||||
"@types/mongodb": "^3.6.12",
|
"@types/mongodb": "^4.0.7",
|
||||||
"lodash": "^4.17.21",
|
"lodash": "^4.17.21",
|
||||||
"mongodb": "^3.6.6",
|
"mongodb": "^4.1.4",
|
||||||
"runtime-type-checks": "0.0.4"
|
"runtime-type-checks": "0.0.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.25",
|
"@gitzone/tsbuild": "^2.1.28",
|
||||||
"@gitzone/tstest": "^1.0.54",
|
"@gitzone/tstest": "^1.0.60",
|
||||||
"@pushrocks/qenv": "^4.0.10",
|
"@pushrocks/qenv": "^4.0.10",
|
||||||
"@pushrocks/tapbundle": "^3.2.14",
|
"@pushrocks/tapbundle": "^3.2.14",
|
||||||
"@types/mongodb-memory-server": "^2.3.0",
|
"@types/mongodb-memory-server": "^2.3.0",
|
||||||
"@types/node": "^15.3.0",
|
"@types/node": "^16.11.7",
|
||||||
"@types/shortid": "0.0.29",
|
"@types/shortid": "0.0.29",
|
||||||
"mongodb-memory-server": "^6.9.6",
|
"mongodb-memory-server": "^8.0.2",
|
||||||
"tslint": "^6.1.3",
|
"tslint": "^6.1.3",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
|
@ -20,22 +20,26 @@ let smartdataOptions: smartdata.IMongoDescriptor;
|
|||||||
let mongod: mongoPlugin.MongoMemoryServer;
|
let mongod: mongoPlugin.MongoMemoryServer;
|
||||||
|
|
||||||
tap.test('should create a testinstance as database', async () => {
|
tap.test('should create a testinstance as database', async () => {
|
||||||
mongod = new mongoPlugin.MongoMemoryServer({});
|
mongod = await mongoPlugin.MongoMemoryServer.create();
|
||||||
console.log('created mongod instance');
|
console.log('created mongod instance');
|
||||||
await mongod._startUpInstance().catch((err) => {
|
|
||||||
console.log(err);
|
|
||||||
});
|
|
||||||
console.log('mongod started');
|
console.log('mongod started');
|
||||||
smartdataOptions = {
|
smartdataOptions = {
|
||||||
mongoDbName: await mongod.getDbName(),
|
mongoDbUrl: mongod.getUri(),
|
||||||
mongoDbPass: '',
|
|
||||||
mongoDbUrl: await mongod.getUri(),
|
|
||||||
};
|
};
|
||||||
console.log(smartdataOptions);
|
console.log(smartdataOptions);
|
||||||
testDb = new smartdata.SmartdataDb(smartdataOptions);
|
testDb = new smartdata.SmartdataDb(smartdataOptions);
|
||||||
await testDb.init();
|
await testDb.init();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.skip.test('should connect to atlas', async (tools) => {
|
||||||
|
const databaseName = `test-smartdata-${smartunique.shortId()}`;
|
||||||
|
testDb = new smartdata.SmartdataDb({
|
||||||
|
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGO_URL'),
|
||||||
|
mongoDbName: databaseName,
|
||||||
|
});
|
||||||
|
await testDb.init();
|
||||||
|
});
|
||||||
|
|
||||||
let easyStore: smartdata.EasyStore<{
|
let easyStore: smartdata.EasyStore<{
|
||||||
key1: string;
|
key1: string;
|
||||||
key2: string;
|
key2: string;
|
||||||
@ -49,11 +53,8 @@ tap.test('should create an easystore', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
tap.test('close', async () => {
|
tap.test('close', async () => {
|
||||||
testDb.close();
|
await mongod.stop();
|
||||||
mongod.stop();
|
await testDb.close();
|
||||||
setTimeout(() => {
|
|
||||||
process.exit(0);
|
|
||||||
}, 1000);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
39
test/test.ts
39
test/test.ts
@ -21,31 +21,24 @@ let mongod: mongoPlugin.MongoMemoryServer;
|
|||||||
|
|
||||||
const totalCars = 2000;
|
const totalCars = 2000;
|
||||||
|
|
||||||
tap.skip.test('should create a testinstance as database', async () => {
|
tap.test('should create a testinstance as database', async () => {
|
||||||
mongod = new mongoPlugin.MongoMemoryServer({});
|
mongod = await mongoPlugin.MongoMemoryServer.create();
|
||||||
console.log('created mongod instance');
|
console.log('created mongod instance');
|
||||||
await mongod._startUpInstance().catch((err) => {
|
|
||||||
console.log(err);
|
|
||||||
});
|
|
||||||
console.log('mongod started');
|
console.log('mongod started');
|
||||||
smartdataOptions = {
|
smartdataOptions = {
|
||||||
mongoDbName: await mongod.getDbName(),
|
mongoDbUrl: mongod.getUri(),
|
||||||
mongoDbPass: '',
|
|
||||||
mongoDbUrl: await mongod.getUri(),
|
|
||||||
};
|
};
|
||||||
console.log(smartdataOptions);
|
console.log(smartdataOptions);
|
||||||
testDb = new smartdata.SmartdataDb(smartdataOptions);
|
testDb = new smartdata.SmartdataDb(smartdataOptions);
|
||||||
|
await testDb.init();
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should connect to atlas', async (tools) => {
|
tap.skip.test('should connect to atlas', async (tools) => {
|
||||||
const databaseName = `test-smartdata-${smartunique.shortId()}`;
|
const databaseName = `test-smartdata-${smartunique.shortId()}`;
|
||||||
testDb = new smartdata.SmartdataDb({
|
testDb = new smartdata.SmartdataDb({
|
||||||
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGO_URL'),
|
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGO_URL'),
|
||||||
mongoDbName: databaseName,
|
mongoDbName: databaseName,
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should establish a connection to mongod', async () => {
|
|
||||||
await testDb.init();
|
await testDb.init();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -200,24 +193,26 @@ class Truck extends smartdata.SmartDataDbDoc<Car, Car> {
|
|||||||
tap.test('should store a new Truck', async () => {
|
tap.test('should store a new Truck', async () => {
|
||||||
const truck = new Truck('blue', 'MAN');
|
const truck = new Truck('blue', 'MAN');
|
||||||
await truck.save();
|
await truck.save();
|
||||||
const myTruck = await Truck.getInstance({ color: 'blue' });
|
|
||||||
myTruck.id = 'foo';
|
|
||||||
await myTruck.save();
|
|
||||||
const myTruck2 = await Truck.getInstance({ color: 'blue' });
|
const myTruck2 = await Truck.getInstance({ color: 'blue' });
|
||||||
console.log(myTruck2);
|
myTruck2.color = 'red';
|
||||||
|
await myTruck2.save();
|
||||||
|
const myTruck3 = await Truck.getInstance({ color: 'blue' });
|
||||||
|
console.log(myTruck3);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should ', async () => {});
|
tap.test('should use a cursor', async () => {
|
||||||
|
const cursor = await Truck.getCursor({});
|
||||||
|
cursor.forEach(async truckArg => {
|
||||||
|
console.log(truckArg.id);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// =======================================
|
// =======================================
|
||||||
// close the database connection
|
// close the database connection
|
||||||
// =======================================
|
// =======================================
|
||||||
tap.test('should close the database connection', async (tools) => {
|
tap.test('close', async () => {
|
||||||
await testDb.mongoDb.dropDatabase();
|
await mongod.stop();
|
||||||
await testDb.close();
|
await testDb.close();
|
||||||
try {
|
|
||||||
await mongod.stop();
|
|
||||||
} catch (e) {}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start({ throwOnError: true });
|
tap.start({ throwOnError: true });
|
||||||
|
@ -21,31 +21,24 @@ let mongod: mongoPlugin.MongoMemoryServer;
|
|||||||
|
|
||||||
const totalCars = 2000;
|
const totalCars = 2000;
|
||||||
|
|
||||||
tap.skip.test('should create a testinstance as database', async () => {
|
tap.test('should create a testinstance as database', async () => {
|
||||||
mongod = new mongoPlugin.MongoMemoryServer({});
|
mongod = await mongoPlugin.MongoMemoryServer.create();
|
||||||
console.log('created mongod instance');
|
console.log('created mongod instance');
|
||||||
await mongod._startUpInstance().catch((err) => {
|
|
||||||
console.log(err);
|
|
||||||
});
|
|
||||||
console.log('mongod started');
|
console.log('mongod started');
|
||||||
smartdataOptions = {
|
smartdataOptions = {
|
||||||
mongoDbName: await mongod.getDbName(),
|
mongoDbUrl: mongod.getUri(),
|
||||||
mongoDbPass: '',
|
|
||||||
mongoDbUrl: await mongod.getUri(),
|
|
||||||
};
|
};
|
||||||
console.log(smartdataOptions);
|
console.log(smartdataOptions);
|
||||||
testDb = new smartdata.SmartdataDb(smartdataOptions);
|
testDb = new smartdata.SmartdataDb(smartdataOptions);
|
||||||
|
await testDb.init();
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should connect to atlas', async (tools) => {
|
tap.skip.test('should connect to atlas', async (tools) => {
|
||||||
const databaseName = `test-smartdata-${smartunique.shortId()}`;
|
const databaseName = `test-smartdata-${smartunique.shortId()}`;
|
||||||
testDb = new smartdata.SmartdataDb({
|
testDb = new smartdata.SmartdataDb({
|
||||||
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGO_URL'),
|
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGO_URL'),
|
||||||
mongoDbName: databaseName,
|
mongoDbName: databaseName,
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should establish a connection to mongod', async () => {
|
|
||||||
await testDb.init();
|
await testDb.init();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -99,12 +92,9 @@ tap.test('should get a car', async () => {
|
|||||||
// =======================================
|
// =======================================
|
||||||
// close the database connection
|
// close the database connection
|
||||||
// =======================================
|
// =======================================
|
||||||
tap.test('should close the database connection', async (tools) => {
|
tap.test('close', async () => {
|
||||||
await testDb.mongoDb.dropDatabase();
|
await mongod.stop();
|
||||||
await testDb.close();
|
await testDb.close();
|
||||||
try {
|
|
||||||
await mongod.stop();
|
|
||||||
} catch (e) {}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start({ throwOnError: true });
|
tap.start({ throwOnError: true });
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import * as plugins from './smartdata.plugins';
|
import * as plugins from './smartdata.plugins';
|
||||||
import { SmartdataDb } from './smartdata.classes.db';
|
import { SmartdataDb } from './smartdata.classes.db';
|
||||||
|
import { SmartdataDbCursor } from './smartdata.classes.cursor';
|
||||||
import { SmartDataDbDoc } from './smartdata.classes.doc';
|
import { SmartDataDbDoc } from './smartdata.classes.doc';
|
||||||
import { CollectionFactory } from './smartdata.classes.collectionfactory';
|
import { CollectionFactory } from './smartdata.classes.collectionfactory';
|
||||||
|
|
||||||
@ -170,9 +171,28 @@ export class SmartdataCollection<T> {
|
|||||||
/**
|
/**
|
||||||
* finds an object in the DbCollection
|
* finds an object in the DbCollection
|
||||||
*/
|
*/
|
||||||
public async find(filterObject: any): Promise<any> {
|
public async findOne(filterObject: any): Promise<any> {
|
||||||
await this.init();
|
await this.init();
|
||||||
const result = await this.mongoDbCollection.find(filterObject).toArray();
|
const cursor = this.mongoDbCollection.find(filterObject);
|
||||||
|
const result = await cursor.next();
|
||||||
|
cursor.close();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getCursor(filterObject: any): Promise<SmartdataDbCursor<any>> {
|
||||||
|
await this.init();
|
||||||
|
const cursor = this.mongoDbCollection.find(filterObject);
|
||||||
|
return new SmartdataDbCursor(cursor);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* finds an object in the DbCollection
|
||||||
|
*/
|
||||||
|
public async findAll(filterObject: any): Promise<any[]> {
|
||||||
|
await this.init();
|
||||||
|
const cursor = this.mongoDbCollection.find(filterObject);
|
||||||
|
const result = await cursor.toArray();
|
||||||
|
cursor.close();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,9 +235,7 @@ export class SmartdataCollection<T> {
|
|||||||
await this.init();
|
await this.init();
|
||||||
await this.checkDoc(dbDocArg);
|
await this.checkDoc(dbDocArg);
|
||||||
const identifiableObject = await dbDocArg.createIdentifiableObject();
|
const identifiableObject = await dbDocArg.createIdentifiableObject();
|
||||||
await this.mongoDbCollection.deleteOne(identifiableObject, {
|
await this.mongoDbCollection.deleteOne(identifiableObject);
|
||||||
w: 1,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
39
ts/smartdata.classes.cursor.ts
Normal file
39
ts/smartdata.classes.cursor.ts
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import * as plugins from './smartdata.plugins';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* a wrapper for the native mongodb cursor. Exposes better
|
||||||
|
*/
|
||||||
|
export class SmartdataDbCursor<T = any> {
|
||||||
|
// STATIC
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
public mongodbCursor: plugins.mongodb.FindCursor<T>;
|
||||||
|
constructor(cursorArg: plugins.mongodb.FindCursor<T>) {
|
||||||
|
this.mongodbCursor = cursorArg
|
||||||
|
};
|
||||||
|
|
||||||
|
public async next(closeAtEnd = true) {
|
||||||
|
const result = await this.mongodbCursor.next();
|
||||||
|
if (!result && closeAtEnd) {
|
||||||
|
await this.close();
|
||||||
|
};
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
public async forEach(forEachFuncArg: (itemArg: T) => Promise<any>, closeCursorAtEnd = true) {
|
||||||
|
let currentValue: T;
|
||||||
|
do {
|
||||||
|
currentValue = await this.mongodbCursor.next();
|
||||||
|
if (currentValue) {
|
||||||
|
await forEachFuncArg(currentValue);
|
||||||
|
}
|
||||||
|
} while (currentValue);
|
||||||
|
if (closeCursorAtEnd) {
|
||||||
|
await this.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async close() {
|
||||||
|
await this.mongodbCursor.close();
|
||||||
|
}
|
||||||
|
}
|
@ -47,8 +47,6 @@ export class SmartdataDb {
|
|||||||
.replace('<dbname>', this.smartdataOptions.mongoDbName);
|
.replace('<dbname>', this.smartdataOptions.mongoDbName);
|
||||||
|
|
||||||
this.mongoDbClient = await plugins.mongodb.MongoClient.connect(finalConnectionUrl, {
|
this.mongoDbClient = await plugins.mongodb.MongoClient.connect(finalConnectionUrl, {
|
||||||
useNewUrlParser: true,
|
|
||||||
useUnifiedTopology: true,
|
|
||||||
maxPoolSize: 100,
|
maxPoolSize: 100,
|
||||||
maxIdleTimeMS: 10,
|
maxIdleTimeMS: 10,
|
||||||
});
|
});
|
||||||
@ -78,7 +76,7 @@ export class SmartdataDb {
|
|||||||
* @returns DbTable
|
* @returns DbTable
|
||||||
*/
|
*/
|
||||||
public async getSmartdataCollectionByName<T>(nameArg: string): Promise<SmartdataCollection<T>> {
|
public async getSmartdataCollectionByName<T>(nameArg: string): Promise<SmartdataCollection<T>> {
|
||||||
const resultCollection = this.smartdataCollectionMap.find((dbTableArg) => {
|
const resultCollection = await this.smartdataCollectionMap.find(async (dbTableArg) => {
|
||||||
return dbTableArg.collectionName === nameArg;
|
return dbTableArg.collectionName === nameArg;
|
||||||
});
|
});
|
||||||
return resultCollection;
|
return resultCollection;
|
||||||
|
@ -3,6 +3,7 @@ import * as plugins from './smartdata.plugins';
|
|||||||
import { ObjectMap } from '@pushrocks/lik';
|
import { ObjectMap } from '@pushrocks/lik';
|
||||||
|
|
||||||
import { SmartdataDb } from './smartdata.classes.db';
|
import { SmartdataDb } from './smartdata.classes.db';
|
||||||
|
import { SmartdataDbCursor } from './smartdata.classes.cursor';
|
||||||
import { IManager, SmartdataCollection } from './smartdata.classes.collection';
|
import { IManager, SmartdataCollection } from './smartdata.classes.collection';
|
||||||
|
|
||||||
export type TDocCreation = 'db' | 'new' | 'mixed';
|
export type TDocCreation = 'db' | 'new' | 'mixed';
|
||||||
@ -41,6 +42,31 @@ export function unI() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const convertFilterForMongoDb = (filterArg: { [key: string]: any }) => {
|
||||||
|
const convertedFilter: { [key: string]: any } = {};
|
||||||
|
const convertFilterArgument = (keyPathArg2: string, filterArg2: any) => {
|
||||||
|
if (typeof filterArg2 === 'object') {
|
||||||
|
for (const key of Object.keys(filterArg2)) {
|
||||||
|
if (key.startsWith('$')) {
|
||||||
|
convertedFilter[keyPathArg2] = filterArg2;
|
||||||
|
return;
|
||||||
|
} else if (key.includes('.')) {
|
||||||
|
throw new Error('keys cannot contain dots');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const key of Object.keys(filterArg2)) {
|
||||||
|
convertFilterArgument(`${keyPathArg2}.${key}`, filterArg2[key]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
convertedFilter[keyPathArg2] = filterArg2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
for (const key of Object.keys(filterArg)) {
|
||||||
|
convertFilterArgument(key, filterArg[key]);
|
||||||
|
}
|
||||||
|
return convertedFilter;
|
||||||
|
};
|
||||||
|
|
||||||
export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends IManager = any> {
|
export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends IManager = any> {
|
||||||
/**
|
/**
|
||||||
* the collection object an Doc belongs to
|
* the collection object an Doc belongs to
|
||||||
@ -81,54 +107,81 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
|
|||||||
*/
|
*/
|
||||||
constructor() {}
|
constructor() {}
|
||||||
|
|
||||||
|
public static createInstanceFromMongoDbNativeDoc<T>(
|
||||||
|
this: plugins.tsclass.typeFest.Class<T>,
|
||||||
|
mongoDbNativeDocArg: any
|
||||||
|
): T {
|
||||||
|
const newInstance = new this();
|
||||||
|
(newInstance as any).creationStatus = 'db';
|
||||||
|
for (const key of Object.keys(mongoDbNativeDocArg)) {
|
||||||
|
newInstance[key] = mongoDbNativeDocArg[key];
|
||||||
|
}
|
||||||
|
return newInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets all instances as array
|
||||||
|
* @param this
|
||||||
|
* @param filterArg
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
public static async getInstances<T>(
|
public static async getInstances<T>(
|
||||||
this: plugins.tsclass.typeFest.Class<T>,
|
this: plugins.tsclass.typeFest.Class<T>,
|
||||||
filterArg: plugins.tsclass.typeFest.PartialDeep<T>
|
filterArg: plugins.tsclass.typeFest.PartialDeep<T>
|
||||||
): Promise<T[]> {
|
): Promise<T[]> {
|
||||||
const convertedFilter: any = {};
|
const foundDocs = await (this as any).collection.findAll(convertFilterForMongoDb(filterArg));
|
||||||
const convertFilterArgument = (keyPathArg: string, filterArg2: any) => {
|
|
||||||
if (typeof filterArg2 === 'object') {
|
|
||||||
for (const key of Object.keys(filterArg2)) {
|
|
||||||
if (key.startsWith('$')) {
|
|
||||||
convertedFilter[keyPathArg] = filterArg2;
|
|
||||||
return;
|
|
||||||
} else if (key.includes('.')) {
|
|
||||||
throw new Error('keys cannot contain dots');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (const key of Object.keys(filterArg2)) {
|
|
||||||
convertFilterArgument(`${keyPathArg}.${key}`, filterArg2[key]);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
convertedFilter[keyPathArg] = filterArg2;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
for (const key of Object.keys(filterArg)) {
|
|
||||||
convertFilterArgument(key, filterArg[key]);
|
|
||||||
}
|
|
||||||
const foundDocs = await (this as any).collection.find(convertedFilter);
|
|
||||||
const returnArray = [];
|
const returnArray = [];
|
||||||
for (const item of foundDocs) {
|
for (const foundDoc of foundDocs) {
|
||||||
const newInstance = new this();
|
const newInstance: T = (this as any).createInstanceFromMongoDbNativeDoc(foundDoc);
|
||||||
(newInstance as any).creationStatus = 'db';
|
|
||||||
for (const key of Object.keys(item)) {
|
|
||||||
newInstance[key] = item[key];
|
|
||||||
}
|
|
||||||
returnArray.push(newInstance);
|
returnArray.push(newInstance);
|
||||||
}
|
}
|
||||||
return returnArray;
|
return returnArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets the first matching instance
|
||||||
|
* @param this
|
||||||
|
* @param filterArg
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
public static async getInstance<T>(
|
public static async getInstance<T>(
|
||||||
this: plugins.tsclass.typeFest.Class<T>,
|
this: plugins.tsclass.typeFest.Class<T>,
|
||||||
filterArg: plugins.tsclass.typeFest.PartialDeep<T>
|
filterArg: plugins.tsclass.typeFest.PartialDeep<T>
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
const result = await (this as any).getInstances(filterArg);
|
const foundDoc = await (this as any).collection.findOne(convertFilterForMongoDb(filterArg));
|
||||||
if (result && result.length > 0) {
|
if (foundDoc) {
|
||||||
return result[0];
|
const newInstance: T = (this as any).createInstanceFromMongoDbNativeDoc(foundDoc);
|
||||||
|
return newInstance;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get cursor
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
public static async getCursor<T>(
|
||||||
|
this: plugins.tsclass.typeFest.Class<T>,
|
||||||
|
filterArg: plugins.tsclass.typeFest.PartialDeep<T>
|
||||||
|
) {
|
||||||
|
const cursor: SmartdataDbCursor<T> = await (this as any).collection.getCursor(convertFilterForMongoDb(filterArg));
|
||||||
|
return cursor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* run a function for all instances
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
public static async forEach<T>(
|
||||||
|
this: plugins.tsclass.typeFest.Class<T>,
|
||||||
|
filterArg: plugins.tsclass.typeFest.PartialDeep<T>,
|
||||||
|
forEachFunction: (itemArg: T) => Promise<any>
|
||||||
|
) {
|
||||||
|
const cursor: SmartdataDbCursor<T> = await (this as any).getCursor(filterArg);
|
||||||
|
await cursor.forEach(forEachFunction)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* saves this instance but not any connected items
|
* saves this instance but not any connected items
|
||||||
* may lead to data inconsistencies, but is faster
|
* may lead to data inconsistencies, but is faster
|
||||||
|
Reference in New Issue
Block a user