BREAKING CHANGE(filter design): filters now are congruent with their data types. Static extensions of doc base class now are fully typed with automatic reference to their child classes.
This commit is contained in:
parent
1330d03af2
commit
302e51a77f
19231
package-lock.json
generated
19231
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
18
test/test.ts
18
test/test.ts
@ -111,7 +111,7 @@ tap.test('expect to get instance of Car with shallow match', async () => {
|
|||||||
let counter = 0;
|
let counter = 0;
|
||||||
do {
|
do {
|
||||||
const timeStart = Date.now();
|
const timeStart = Date.now();
|
||||||
const myCars = await Car.getInstances<Car>({
|
const myCars = await Car.getInstances({
|
||||||
brand: 'Renault',
|
brand: 'Renault',
|
||||||
});
|
});
|
||||||
if (counter % 10 === 0) {
|
if (counter % 10 === 0) {
|
||||||
@ -132,9 +132,11 @@ tap.test('expect to get instance of Car with deep match', async () => {
|
|||||||
let counter = 0;
|
let counter = 0;
|
||||||
do {
|
do {
|
||||||
const timeStart = Date.now();
|
const timeStart = Date.now();
|
||||||
const myCars2 = await Car.getInstances<Car>({
|
const myCars2 = await Car.getInstances({
|
||||||
'deepData.sodeep': 'yes',
|
deepData: {
|
||||||
} as any);
|
sodeep: 'yes'
|
||||||
|
},
|
||||||
|
});
|
||||||
if (counter % 10 === 0) {
|
if (counter % 10 === 0) {
|
||||||
console.log(
|
console.log(
|
||||||
`performed ${counter} of ${totalQueryCycles} total query cycles: took ${
|
`performed ${counter} of ${totalQueryCycles} total query cycles: took ${
|
||||||
@ -158,7 +160,7 @@ 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 myCars = await Car.getInstances<Car>({
|
const myCars = await Car.getInstances({
|
||||||
brand: 'Volvo',
|
brand: 'Volvo',
|
||||||
color: 'blue',
|
color: 'blue',
|
||||||
});
|
});
|
||||||
@ -198,13 +200,15 @@ 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<Truck>({ color: 'blue' });
|
const myTruck = await Truck.getInstance({ color: 'blue' });
|
||||||
myTruck.id = 'foo';
|
myTruck.id = 'foo';
|
||||||
await myTruck.save();
|
await myTruck.save();
|
||||||
const myTruck2 = await Truck.getInstance<Truck>({ color: 'blue' });
|
const myTruck2 = await Truck.getInstance({ color: 'blue' });
|
||||||
console.log(myTruck2);
|
console.log(myTruck2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('should ', async () => {})
|
||||||
|
|
||||||
// =======================================
|
// =======================================
|
||||||
// close the database connection
|
// close the database connection
|
||||||
// =======================================
|
// =======================================
|
||||||
|
@ -79,13 +79,35 @@ export class SmartDataDbDoc<T extends TImplements, TImplements> {
|
|||||||
constructor() {}
|
constructor() {}
|
||||||
|
|
||||||
public static async getInstances<T>(
|
public static async getInstances<T>(
|
||||||
|
this: plugins.tsclass.typeFest.Class<T>,
|
||||||
filterArg: plugins.tsclass.typeFest.PartialDeep<T>
|
filterArg: plugins.tsclass.typeFest.PartialDeep<T>
|
||||||
): Promise<T[]> {
|
): Promise<T[]> {
|
||||||
const foundDocs = await this.collection.find(filterArg);
|
const convertedFilter: any = {};
|
||||||
|
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 item of foundDocs) {
|
||||||
const newInstance = new this();
|
const newInstance = new this();
|
||||||
newInstance.creationStatus = 'db';
|
(newInstance as any).creationStatus = 'db';
|
||||||
for (const key of Object.keys(item)) {
|
for (const key of Object.keys(item)) {
|
||||||
newInstance[key] = item[key];
|
newInstance[key] = item[key];
|
||||||
}
|
}
|
||||||
@ -95,9 +117,10 @@ export class SmartDataDbDoc<T extends TImplements, TImplements> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static async getInstance<T>(
|
public static async getInstance<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.getInstances<T>(filterArg);
|
const result = await (this as any).getInstances(filterArg);
|
||||||
if (result && result.length > 0) {
|
if (result && result.length > 0) {
|
||||||
return result[0];
|
return result[0];
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user