now allows adding a saveable decorator

This commit is contained in:
Philipp Kunz 2016-11-18 00:42:25 +01:00
parent 9cbdf317dc
commit e49811eecd
6 changed files with 109 additions and 6 deletions

View File

@ -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' | 'data' | 'mixed';
/**
* sva - 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;
} }

View File

@ -1,12 +1,39 @@
"use strict"; "use strict";
/**
* sva - 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];
}
} }
/**
* also store any referenced objects to DB
* better for data consistency
*/
saveDeep() { saveDeep() {
this.save();
} }
} }
exports.DbDoc = DbDoc; exports.DbDoc = DbDoc;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRkYXRhLmNsYXNzZXMuZGJkb2MuanMiLCJzb3VyY2VS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Q0FBQyxDQUFDLElBQUksa0JBQWtCLElBQUksSUFBSSxDQUFDLGtCQUFrQixDQUFDLENBQUMsQ0FBQztZQUNyRCxjQUFjLENBQUMsa0JBQWtCLENBQUMsR0FBRyxJQUFJLENBQUMsa0JBQWtCLENBQUMsQ0FBQTtRQUNqRSxDQUFDO0lBQ0wsQ0FBQztJQUVEOzs7T0FHRztJQUNILFFBQVE7UUFDSixJQUFJLENBQUMsSUFBSSxFQUFFLENBQUE7SUFDZixDQUFDO0NBQ0o7QUExQ0Qsc0JBMENDIn0=

File diff suppressed because one or more lines are too long

View File

@ -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)

View File

@ -5,17 +5,55 @@ import { DbCollection } from './smartdata.classes.dbcollection'
export type TDocCreation = 'db' | 'data' | 'mixed' export type TDocCreation = 'db' | 'data' | 'mixed'
/**
* sva - 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]
}
} }
saveDeep() {
/**
* also store any referenced objects to DB
* better for data consistency
*/
saveDeep() {
this.save()
} }
} }

5
tsconfig.json Normal file
View File

@ -0,0 +1,5 @@
{
"compilerOptions": {
"experimentalDecorators": true
}
}