Compare commits

..

4 Commits

5 changed files with 16580 additions and 2724 deletions

19233
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartdata", "name": "@pushrocks/smartdata",
"version": "3.1.56", "version": "4.0.1",
"private": false, "private": false,
"description": "do more with data", "description": "do more with data",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",

View File

@ -49,6 +49,7 @@ How RethinkDB's terms map to the ones of smartdata:
represents a Database. Naturally it has .connect() etc. methods on it. represents a Database. Naturally it has .connect() etc. methods on it.
```typescript ```typescript
// Assuming toplevel await
import * as smartdata from 'smartdata'; import * as smartdata from 'smartdata';
const smartdataDb = new smartdata.SmartdataDb({ const smartdataDb = new smartdata.SmartdataDb({
@ -57,7 +58,7 @@ const smartdataDb = new smartdata.SmartdataDb({
mongoDbPass: 'mypassword', mongoDbPass: 'mypassword',
}); });
smartdataDb.connect(); await smartdataDb.connect();
``` ```
### class DbCollection ### class DbCollection
@ -68,10 +69,11 @@ A collection is defined by the object class (that is extending smartdata.dbdoc)
So to get to get access to a specific collection you document So to get to get access to a specific collection you document
```typescript ```typescript
// Assuming toplevel await
// continues from the block before... // continues from the block before...
@smartdata.Collection(smartdataDb) @smartdata.Collection(smartdataDb)
class MyObject extends smartdata.DbDoc<MyObject> { class MyObject extends smartdata.DbDoc<MyObject /* ,[an optional interface to implement] */> {
// read the next block about DbDoc // read the next block about DbDoc
@smartdata.svDb() @smartdata.svDb()
property1: string; // @smartdata.svDb() marks the property for db save property1: string; // @smartdata.svDb() marks the property for db save
@ -87,14 +89,22 @@ class MyObject extends smartdata.DbDoc<MyObject> {
const localObject = new MyObject({ const localObject = new MyObject({
property1: 'hi', property1: 'hi',
property2: 2, property2: {
deep: 3
},
}); });
localObject.save(); // saves the object to the database await localObject.save(); // saves the object to the database
// start retrieving instances // start retrieving instances
MyObject.getInstance<MyObject>({ // .getInstance is staticly inheritied, yet fully typed static function to get instances with fully typed filters
property: 'hi', const myInstance = await MyObject.getInstance({
property1: 'hi',
property2: {
deep: {
$gt: 2
} as any
}
}); // outputs a new instance of MyObject with the values from db assigned }); // outputs a new instance of MyObject with the values from db assigned
``` ```

View File

@ -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
// ======================================= // =======================================

View File

@ -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];
} }