Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
63ceded1b8 | |||
b45e7ee206 | |||
715709f3ec | |||
0b8adea736 | |||
90d9a25b3f | |||
e49811eecd |
@ -63,8 +63,8 @@ So to get to get access to a specific collection you document
|
|||||||
|
|
||||||
@Collection(myDb1)
|
@Collection(myDb1)
|
||||||
class myObject extends smartdata.DbDoc<myObject> { // read the next block about DbDoc
|
class myObject extends smartdata.DbDoc<myObject> { // read the next block about DbDoc
|
||||||
property1:string
|
@smartdata.saveable property1: string // @smartdata.saveable marks the property for db save
|
||||||
property2:number
|
property2: number // this one is not marked, so it won't be save upon calling this.save()
|
||||||
constructor(optionsArg:{
|
constructor(optionsArg:{
|
||||||
property1: string,
|
property1: string,
|
||||||
property2: number
|
property2: number
|
||||||
|
27
dist/smartdata.classes.dbdoc.d.ts
vendored
27
dist/smartdata.classes.dbdoc.d.ts
vendored
@ -1,9 +1,34 @@
|
|||||||
import { DbCollection } from './smartdata.classes.dbcollection';
|
import { DbCollection } from './smartdata.classes.dbcollection';
|
||||||
export declare type TDocCreation = 'db' | 'data' | 'mixed';
|
export declare type TDocCreation = 'db' | 'new' | 'mixed';
|
||||||
|
/**
|
||||||
|
* saveable - saveable decorator to be used on class properties
|
||||||
|
*/
|
||||||
|
export declare function saveable(target: DbDoc<any>, key: string): void;
|
||||||
export declare class DbDoc<T> {
|
export declare class DbDoc<T> {
|
||||||
|
/**
|
||||||
|
* the collection object an Doc belongs to
|
||||||
|
*/
|
||||||
collection: DbCollection<T>;
|
collection: DbCollection<T>;
|
||||||
|
/**
|
||||||
|
* how the Doc in memory was created, may prove useful later.
|
||||||
|
*/
|
||||||
creationType: TDocCreation;
|
creationType: TDocCreation;
|
||||||
|
/**
|
||||||
|
* an array of saveable properties of a doc
|
||||||
|
*/
|
||||||
|
saveableProperties: string[];
|
||||||
|
/**
|
||||||
|
* class constructor
|
||||||
|
*/
|
||||||
constructor();
|
constructor();
|
||||||
|
/**
|
||||||
|
* saves this instance but not any connected items
|
||||||
|
* may lead to data inconsistencies, but is faster
|
||||||
|
*/
|
||||||
save(): void;
|
save(): void;
|
||||||
|
/**
|
||||||
|
* also store any referenced objects to DB
|
||||||
|
* better for data consistency
|
||||||
|
*/
|
||||||
saveDeep(): void;
|
saveDeep(): void;
|
||||||
}
|
}
|
||||||
|
36
dist/smartdata.classes.dbdoc.js
vendored
36
dist/smartdata.classes.dbdoc.js
vendored
@ -1,12 +1,46 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
/**
|
||||||
|
* saveable - saveable decorator to be used on class properties
|
||||||
|
*/
|
||||||
|
function saveable(target, key) {
|
||||||
|
console.log('called sva');
|
||||||
|
if (!target.saveableProperties) {
|
||||||
|
target.saveableProperties = [];
|
||||||
|
}
|
||||||
|
target.saveableProperties.push(key);
|
||||||
|
}
|
||||||
|
exports.saveable = saveable;
|
||||||
class DbDoc {
|
class DbDoc {
|
||||||
|
/**
|
||||||
|
* class constructor
|
||||||
|
*/
|
||||||
constructor() {
|
constructor() {
|
||||||
this.collection = this.constructor['dbCollection'];
|
this.collection = this.constructor['dbCollection'];
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* saves this instance but not any connected items
|
||||||
|
* may lead to data inconsistencies, but is faster
|
||||||
|
*/
|
||||||
save() {
|
save() {
|
||||||
|
let saveableObject = {};
|
||||||
|
for (let propertyNameString of this.saveableProperties) {
|
||||||
|
saveableObject[propertyNameString] = this[propertyNameString];
|
||||||
}
|
}
|
||||||
|
switch (this.creationType) {
|
||||||
|
case 'db':
|
||||||
|
this.collection; // TODO implement collection.update()
|
||||||
|
break;
|
||||||
|
case 'new':
|
||||||
|
this.collection.insertOne(saveableObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* also store any referenced objects to DB
|
||||||
|
* better for data consistency
|
||||||
|
*/
|
||||||
saveDeep() {
|
saveDeep() {
|
||||||
|
this.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.DbDoc = DbD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UFDLGNBQWMsQ0FBQyxDQUFBO0lBQ3RELENBQUM7SUFFRDs7O09BR0c7SUFDSCxJQUFJO1FBQ0EsSUFBSSxjQUFjLEdBQUcsRUFBRSxDQUFBO1FBQ3ZCLEdBQUcsQ0FBQyxDQUFDLElBQUksa0JBQWtCLElBQUksSUFBSSxDQUFDLGtCQUFrQixDQUFDLENBQUMsQ0FBQztZQUNyRCxjQUFjLENBQUMsa0JBQWtCLENBQUMsR0FBRyxJQUFJLENBQUMsa0JBQWtCLENBQUMsQ0FBQTtRQUNqRSxDQUFDO1FBQ0QsTUFBTSxDQUFDLENBQUMsSUFBSSxDQUFDLFlBQVksQ0FBQyxDQUFDLENBQUM7WUFDeEIsS0FBSyxJQUFJO2dCQUNMLElBQUksQ0FBQyxVQUFVLENBQUEsQ0FBQyxxQ0FBcUM7Z0JBQ3JELEtBQUssQ0FBQTtZQUNULEtBQUssS0FBSztnQkFDTixJQUFJLENBQUMsVUFBVSxDQUFDLFNBQVMsQ0FBQyxjQUFjLENBQUMsQ0FBQTtRQUNqRCxDQUFDO0lBQ0wsQ0FBQztJQUVEOzs7T0FHRztJQUNILFFBQVE7UUFDSixJQUFJLENBQUMsSUFBSSxFQUFFLENBQUE7SUFDZixDQUFDO0NBQ0o7QUFqREQsc0JBaURDIn0=
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "smartdata",
|
"name": "smartdata",
|
||||||
"version": "1.0.18",
|
"version": "1.0.21",
|
||||||
"description": "do more with data",
|
"description": "do more with data",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
|
File diff suppressed because one or more lines are too long
@ -72,6 +72,7 @@ describe('smartdata', function () {
|
|||||||
it('should create an extended class', function () {
|
it('should create an extended class', function () {
|
||||||
@smartdata.Collection(testDb)
|
@smartdata.Collection(testDb)
|
||||||
class TestCar extends smartdata.DbDoc<TestCar> {
|
class TestCar extends smartdata.DbDoc<TestCar> {
|
||||||
|
@smartdata.saveable
|
||||||
color: string
|
color: string
|
||||||
constructor(optionsArg: {
|
constructor(optionsArg: {
|
||||||
color: string,
|
color: string,
|
||||||
@ -85,6 +86,8 @@ describe('smartdata', function () {
|
|||||||
color: 'red',
|
color: 'red',
|
||||||
property2: 2
|
property2: 2
|
||||||
})
|
})
|
||||||
|
|
||||||
|
should(testCarInstance.saveableProperties[0]).equal('color')
|
||||||
console.log(TestCar)
|
console.log(TestCar)
|
||||||
should(testCarInstance.collection).be.instanceof(smartdata.DbCollection)
|
should(testCarInstance.collection).be.instanceof(smartdata.DbCollection)
|
||||||
should(testCarInstance).be.instanceof(smartdata.DbDoc)
|
should(testCarInstance).be.instanceof(smartdata.DbDoc)
|
||||||
|
@ -3,19 +3,64 @@ import * as plugins from './smartdata.plugins'
|
|||||||
import { Db } from './smartdata.classes.db'
|
import { Db } from './smartdata.classes.db'
|
||||||
import { DbCollection } from './smartdata.classes.dbcollection'
|
import { DbCollection } from './smartdata.classes.dbcollection'
|
||||||
|
|
||||||
export type TDocCreation = 'db' | 'data' | 'mixed'
|
export type TDocCreation = 'db' | 'new' | 'mixed'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* saveable - saveable decorator to be used on class properties
|
||||||
|
*/
|
||||||
|
export function saveable(target: DbDoc<any>, key: string) {
|
||||||
|
console.log('called sva')
|
||||||
|
if (!target.saveableProperties) { target.saveableProperties = [] }
|
||||||
|
target.saveableProperties.push(key)
|
||||||
|
}
|
||||||
|
|
||||||
export class DbDoc<T> {
|
export class DbDoc<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the collection object an Doc belongs to
|
||||||
|
*/
|
||||||
collection: DbCollection<T>
|
collection: DbCollection<T>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* how the Doc in memory was created, may prove useful later.
|
||||||
|
*/
|
||||||
creationType: TDocCreation
|
creationType: TDocCreation
|
||||||
|
|
||||||
|
/**
|
||||||
|
* an array of saveable properties of a doc
|
||||||
|
*/
|
||||||
|
saveableProperties: string[]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class constructor
|
||||||
|
*/
|
||||||
constructor() {
|
constructor() {
|
||||||
this.collection = this.constructor['dbCollection']
|
this.collection = this.constructor['dbCollection']
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* saves this instance but not any connected items
|
||||||
|
* may lead to data inconsistencies, but is faster
|
||||||
|
*/
|
||||||
save() {
|
save() {
|
||||||
|
let saveableObject = {}
|
||||||
|
for (let propertyNameString of this.saveableProperties) {
|
||||||
|
saveableObject[propertyNameString] = this[propertyNameString]
|
||||||
}
|
}
|
||||||
|
switch (this.creationType) {
|
||||||
|
case 'db':
|
||||||
|
this.collection // TODO implement collection.update()
|
||||||
|
break
|
||||||
|
case 'new':
|
||||||
|
this.collection.insertOne(saveableObject)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* also store any referenced objects to DB
|
||||||
|
* better for data consistency
|
||||||
|
*/
|
||||||
saveDeep() {
|
saveDeep() {
|
||||||
|
this.save()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
5
tsconfig.json
Normal file
5
tsconfig.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user