Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
0d3518d990 | |||
fbdde2268c | |||
8b6c26f45a | |||
97e82ed75a | |||
03baffd9fd | |||
f29ae67580 |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "3.1.51",
|
"version": "3.1.54",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "3.1.51",
|
"version": "3.1.54",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "do more with data",
|
"description": "do more with data",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
|
26
test/test.ts
26
test/test.ts
@ -27,7 +27,7 @@ tap.skip.test('should create a testinstance as database', async () => {
|
|||||||
smartdataOptions = {
|
smartdataOptions = {
|
||||||
mongoDbName: await mongod.getDbName(),
|
mongoDbName: await mongod.getDbName(),
|
||||||
mongoDbPass: '',
|
mongoDbPass: '',
|
||||||
mongoDbUrl: await mongod.getConnectionString(),
|
mongoDbUrl: await mongod.getUri(),
|
||||||
};
|
};
|
||||||
console.log(smartdataOptions);
|
console.log(smartdataOptions);
|
||||||
testDb = new smartdata.SmartdataDb(smartdataOptions);
|
testDb = new smartdata.SmartdataDb(smartdataOptions);
|
||||||
@ -68,7 +68,7 @@ class Car extends smartdata.SmartDataDbDoc<Car, Car> {
|
|||||||
|
|
||||||
@smartdata.svDb()
|
@smartdata.svDb()
|
||||||
deepData = {
|
deepData = {
|
||||||
sodeep: 'yes'
|
sodeep: 'yes',
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor(colorArg: string, brandArg: string) {
|
constructor(colorArg: string, brandArg: string) {
|
||||||
@ -85,14 +85,16 @@ tap.test('should save the car to the db', async () => {
|
|||||||
const myCar2 = new Car('red', 'Volvo');
|
const myCar2 = new Car('red', 'Volvo');
|
||||||
await myCar2.save();
|
await myCar2.save();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let counter = 0;
|
let counter = 0;
|
||||||
|
const totalCars = 2000;
|
||||||
do {
|
do {
|
||||||
const myCar3 = new Car('red', 'Renault');
|
const myCar3 = new Car('red', 'Renault');
|
||||||
await myCar3.save();
|
await myCar3.save();
|
||||||
counter++;
|
counter++;
|
||||||
} while (counter < 2000);
|
if (counter % 100 === 0) {
|
||||||
|
console.log(`Filled database with ${counter} of ${totalCars} Cars`);
|
||||||
|
}
|
||||||
|
} while (counter < totalCars);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('expect to get instance of Car with shallow match', async () => {
|
tap.test('expect to get instance of Car with shallow match', async () => {
|
||||||
@ -102,7 +104,7 @@ tap.test('expect to get instance of Car with shallow match', async () => {
|
|||||||
const myCars = await Car.getInstances<Car>({
|
const myCars = await Car.getInstances<Car>({
|
||||||
brand: 'Renault',
|
brand: 'Renault',
|
||||||
});
|
});
|
||||||
console.log(`took ${Date.now() - timeStart}`);
|
console.log(`took ${Date.now() - timeStart}ms to query a set of 2000`);
|
||||||
expect(myCars[0].deepData.sodeep).to.equal('yes');
|
expect(myCars[0].deepData.sodeep).to.equal('yes');
|
||||||
expect(myCars[0].brand).to.equal('Renault');
|
expect(myCars[0].brand).to.equal('Renault');
|
||||||
counter++;
|
counter++;
|
||||||
@ -116,7 +118,7 @@ tap.test('expect to get instance of Car with deep match', async () => {
|
|||||||
const myCars2 = await Car.getInstances<Car>({
|
const myCars2 = await Car.getInstances<Car>({
|
||||||
'deepData.sodeep': 'yes',
|
'deepData.sodeep': 'yes',
|
||||||
} as any);
|
} as any);
|
||||||
console.log(`took ${Date.now() - timeStart}`);
|
console.log(`took ${Date.now() - timeStart}ms to query a set of 2000`);
|
||||||
expect(myCars2[0].deepData.sodeep).to.equal('yes');
|
expect(myCars2[0].deepData.sodeep).to.equal('yes');
|
||||||
expect(myCars2[0].brand).to.equal('Volvo');
|
expect(myCars2[0].brand).to.equal('Volvo');
|
||||||
counter++;
|
counter++;
|
||||||
@ -133,11 +135,15 @@ tap.test('expect to get instance of Car and update it', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should be able to delete an instance of car', async () => {
|
tap.test('should be able to delete an instance of car', async () => {
|
||||||
const myCar = await Car.getInstance<Car>({
|
const myCars = await Car.getInstances<Car>({
|
||||||
brand: 'Volvo',
|
brand: 'Volvo',
|
||||||
|
color: 'blue',
|
||||||
});
|
});
|
||||||
expect(myCar.color).to.equal('blue');
|
console.log(myCars);
|
||||||
await myCar.delete();
|
expect(myCars[0].color).to.equal('blue');
|
||||||
|
for (const myCar of myCars) {
|
||||||
|
await myCar.delete();
|
||||||
|
}
|
||||||
|
|
||||||
const myCar2 = await Car.getInstance<Car>({
|
const myCar2 = await Car.getInstance<Car>({
|
||||||
brand: 'Volvo',
|
brand: 'Volvo',
|
||||||
|
@ -131,11 +131,12 @@ export class SmartdataCollection<T> {
|
|||||||
}
|
}
|
||||||
updateableObject[key] = saveableObject[key];
|
updateableObject[key] = saveableObject[key];
|
||||||
}
|
}
|
||||||
this.mongoDbCollection.updateOne(
|
const result = await this.mongoDbCollection.updateOne(
|
||||||
identifiableObject,
|
identifiableObject,
|
||||||
{ $set: updateableObject },
|
{ $set: updateableObject },
|
||||||
{ upsert: true }
|
{ upsert: true }
|
||||||
);
|
);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async delete(dbDocArg: T & SmartDataDbDoc<T, unknown>): Promise<any> {
|
public async delete(dbDocArg: T & SmartDataDbDoc<T, unknown>): Promise<any> {
|
||||||
|
@ -3,9 +3,12 @@ import { SmartdataCollection } from './smartdata.classes.collection';
|
|||||||
import { SmartdataDb } from './smartdata.classes.db';
|
import { SmartdataDb } from './smartdata.classes.db';
|
||||||
|
|
||||||
export class CollectionFactory {
|
export class CollectionFactory {
|
||||||
public collections: {[key: string]: SmartdataCollection<any>} = {};
|
public collections: { [key: string]: SmartdataCollection<any> } = {};
|
||||||
|
|
||||||
public getCollection = (nameArg: string, dbArg: SmartdataDb | (() => SmartdataDb)): SmartdataCollection<any> => {
|
public getCollection = (
|
||||||
|
nameArg: string,
|
||||||
|
dbArg: SmartdataDb | (() => SmartdataDb)
|
||||||
|
): SmartdataCollection<any> => {
|
||||||
if (!this.collections[nameArg]) {
|
if (!this.collections[nameArg]) {
|
||||||
this.collections[nameArg] = (() => {
|
this.collections[nameArg] = (() => {
|
||||||
if (dbArg instanceof SmartdataDb) {
|
if (dbArg instanceof SmartdataDb) {
|
||||||
@ -18,5 +21,5 @@ export class CollectionFactory {
|
|||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
return this.collections[nameArg];
|
return this.collections[nameArg];
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ export class SmartdataDb {
|
|||||||
useNewUrlParser: true,
|
useNewUrlParser: true,
|
||||||
useUnifiedTopology: true,
|
useUnifiedTopology: true,
|
||||||
maxPoolSize: 100,
|
maxPoolSize: 100,
|
||||||
maxIdleTimeMS: 10
|
maxIdleTimeMS: 10,
|
||||||
});
|
});
|
||||||
this.mongoDb = this.mongoDbClient.db(this.smartdataOptions.mongoDbName);
|
this.mongoDb = this.mongoDbClient.db(this.smartdataOptions.mongoDbName);
|
||||||
this.status = 'connected';
|
this.status = 'connected';
|
||||||
|
@ -41,7 +41,7 @@ export function unI() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SmartDataDbDoc<T, TImplements> {
|
export class SmartDataDbDoc<T extends TImplements, TImplements> {
|
||||||
/**
|
/**
|
||||||
* the collection object an Doc belongs to
|
* the collection object an Doc belongs to
|
||||||
*/
|
*/
|
||||||
@ -110,17 +110,19 @@ export class SmartDataDbDoc<T, TImplements> {
|
|||||||
public async save() {
|
public async save() {
|
||||||
// tslint:disable-next-line: no-this-assignment
|
// tslint:disable-next-line: no-this-assignment
|
||||||
const self: any = this;
|
const self: any = this;
|
||||||
|
let dbResult: any;
|
||||||
switch (this.creationStatus) {
|
switch (this.creationStatus) {
|
||||||
case 'db':
|
case 'db':
|
||||||
await this.collection.update(self);
|
dbResult = await this.collection.update(self);
|
||||||
break;
|
break;
|
||||||
case 'new':
|
case 'new':
|
||||||
const writeResult = await this.collection.insert(self);
|
dbResult = await this.collection.insert(self);
|
||||||
this.creationStatus = 'db';
|
this.creationStatus = 'db';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.error('neither new nor in db?');
|
console.error('neither new nor in db?');
|
||||||
}
|
}
|
||||||
|
return dbResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user