Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
55d96fa68d | |||
54ec6accdf | |||
fc5d092b25 | |||
dfba057562 | |||
1ad05943b5 | |||
302e51a77f |
19233
package-lock.json
generated
19233
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartdata",
|
"name": "@pushrocks/smartdata",
|
||||||
"version": "3.1.56",
|
"version": "4.0.2",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "do more with data",
|
"description": "do more with data",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
|
22
readme.md
22
readme.md
@ -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
|
||||||
```
|
```
|
||||||
|
|
||||||
|
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
|
||||||
// =======================================
|
// =======================================
|
||||||
|
@ -14,7 +14,7 @@ export interface IDocValidationFunc<T> {
|
|||||||
(doc: T): boolean;
|
(doc: T): boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TDelayedDbCreation = () => SmartdataDb;
|
export type TDelayed<TDelayedArg> = () => TDelayedArg;
|
||||||
|
|
||||||
const collectionFactory = new CollectionFactory();
|
const collectionFactory = new CollectionFactory();
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ const collectionFactory = new CollectionFactory();
|
|||||||
* This is a decorator that will tell the decorated class what dbTable to use
|
* This is a decorator that will tell the decorated class what dbTable to use
|
||||||
* @param dbArg
|
* @param dbArg
|
||||||
*/
|
*/
|
||||||
export function Collection(dbArg: SmartdataDb | TDelayedDbCreation) {
|
export function Collection<TManager>(dbArg: SmartdataDb | TDelayed<SmartdataDb>, managerArg?: TDelayed<TManager>) {
|
||||||
return function classDecorator<T extends { new (...args: any[]): {} }>(constructor: T) {
|
return function classDecorator<T extends { new (...args: any[]): {} }>(constructor: T) {
|
||||||
return class extends constructor {
|
return class extends constructor {
|
||||||
public static get collection() {
|
public static get collection() {
|
||||||
@ -31,6 +31,11 @@ export function Collection(dbArg: SmartdataDb | TDelayedDbCreation) {
|
|||||||
public get collection() {
|
public get collection() {
|
||||||
return collectionFactory.getCollection(constructor.name, dbArg);
|
return collectionFactory.getCollection(constructor.name, dbArg);
|
||||||
}
|
}
|
||||||
|
public get manager() {
|
||||||
|
if (managerArg) {
|
||||||
|
return managerArg();
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -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];
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user