Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
8308d8d03b | |||
97365ddf29 | |||
55d96fa68d | |||
54ec6accdf | |||
fc5d092b25 | |||
dfba057562 |
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@pushrocks/smartdata",
|
||||
"version": "4.0.0",
|
||||
"version": "4.0.3",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@pushrocks/smartdata",
|
||||
"version": "4.0.0",
|
||||
"version": "4.0.3",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@pushrocks/lik": "^4.0.20",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartdata",
|
||||
"version": "4.0.0",
|
||||
"version": "4.0.3",
|
||||
"private": false,
|
||||
"description": "do more with data",
|
||||
"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.
|
||||
|
||||
```typescript
|
||||
// Assuming toplevel await
|
||||
import * as smartdata from 'smartdata';
|
||||
|
||||
const smartdataDb = new smartdata.SmartdataDb({
|
||||
@ -57,7 +58,7 @@ const smartdataDb = new smartdata.SmartdataDb({
|
||||
mongoDbPass: 'mypassword',
|
||||
});
|
||||
|
||||
smartdataDb.connect();
|
||||
await smartdataDb.connect();
|
||||
```
|
||||
|
||||
### 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
|
||||
|
||||
```typescript
|
||||
// Assuming toplevel await
|
||||
// continues from the block before...
|
||||
|
||||
@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
|
||||
@smartdata.svDb()
|
||||
property1: string; // @smartdata.svDb() marks the property for db save
|
||||
@ -87,14 +89,22 @@ class MyObject extends smartdata.DbDoc<MyObject> {
|
||||
|
||||
const localObject = new MyObject({
|
||||
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
|
||||
|
||||
MyObject.getInstance<MyObject>({
|
||||
property: 'hi',
|
||||
// .getInstance is staticly inheritied, yet fully typed static function to get instances with fully typed filters
|
||||
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
|
||||
```
|
||||
|
||||
|
@ -14,7 +14,7 @@ export interface IDocValidationFunc<T> {
|
||||
(doc: T): boolean;
|
||||
}
|
||||
|
||||
export type TDelayedDbCreation = () => SmartdataDb;
|
||||
export type TDelayed<TDelayedArg> = () => TDelayedArg;
|
||||
|
||||
const collectionFactory = new CollectionFactory();
|
||||
|
||||
@ -22,19 +22,67 @@ const collectionFactory = new CollectionFactory();
|
||||
* This is a decorator that will tell the decorated class what dbTable to use
|
||||
* @param dbArg
|
||||
*/
|
||||
export function Collection(dbArg: SmartdataDb | TDelayedDbCreation) {
|
||||
export function Collection(dbArg: SmartdataDb | TDelayed<SmartdataDb>) {
|
||||
return function classDecorator<T extends { new (...args: any[]): {} }>(constructor: T) {
|
||||
return class extends constructor {
|
||||
public static get collection() {
|
||||
if (!(dbArg instanceof SmartdataDb)) {
|
||||
dbArg = dbArg();
|
||||
}
|
||||
return collectionFactory.getCollection(constructor.name, dbArg);
|
||||
}
|
||||
public get collection() {
|
||||
if (!(dbArg instanceof SmartdataDb)) {
|
||||
dbArg = dbArg();
|
||||
}
|
||||
return collectionFactory.getCollection(constructor.name, dbArg);
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
interface IManager {
|
||||
db: SmartdataDb
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a decorator that will tell the decorated class what dbTable to use
|
||||
* @param dbArg
|
||||
*/
|
||||
export function Manager<TManager extends IManager>(managerArg?: TManager | TDelayed<TManager>) {
|
||||
return function classDecorator<T extends { new (...args: any[]): {} }>(constructor: T) {
|
||||
return class extends constructor {
|
||||
public static get collection() {
|
||||
let dbArg: SmartdataDb;
|
||||
if (managerArg['db']) {
|
||||
dbArg = (managerArg as TManager).db
|
||||
} else {
|
||||
dbArg = (managerArg as TDelayed<TManager>)().db;
|
||||
}
|
||||
return collectionFactory.getCollection(constructor.name, dbArg);
|
||||
}
|
||||
public get collection() {
|
||||
let dbArg: SmartdataDb;
|
||||
if (managerArg['db']) {
|
||||
dbArg = (managerArg as TManager).db
|
||||
} else {
|
||||
dbArg = (managerArg as TDelayed<TManager>)().db;
|
||||
}
|
||||
return collectionFactory.getCollection(constructor.name, dbArg);
|
||||
}
|
||||
public get manager() {
|
||||
let manager: TManager;
|
||||
if (managerArg['db']) {
|
||||
manager = (managerArg as TManager);
|
||||
} else {
|
||||
manager = (managerArg as TDelayed<TManager>)();
|
||||
}
|
||||
return manager;
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
// tslint:disable-next-line: max-classes-per-file
|
||||
export class SmartdataCollection<T> {
|
||||
/**
|
||||
|
@ -7,16 +7,13 @@ export class CollectionFactory {
|
||||
|
||||
public getCollection = (
|
||||
nameArg: string,
|
||||
dbArg: SmartdataDb | (() => SmartdataDb)
|
||||
dbArg: SmartdataDb
|
||||
): SmartdataCollection<any> => {
|
||||
if (!this.collections[nameArg]) {
|
||||
this.collections[nameArg] = (() => {
|
||||
if (dbArg instanceof SmartdataDb) {
|
||||
// tslint:disable-next-line: no-string-literal
|
||||
return new SmartdataCollection(nameArg, dbArg);
|
||||
} else {
|
||||
dbArg = dbArg();
|
||||
return new SmartdataCollection(nameArg, dbArg);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
Reference in New Issue
Block a user