smartdata/ts/smartdata.classes.dbdoc.ts

87 lines
2.0 KiB
TypeScript
Raw Normal View History

2016-09-13 20:53:21 +00:00
import * as plugins from './smartdata.plugins'
2017-02-25 10:37:05 +00:00
import { Objectmap } from 'lik'
2016-09-13 20:53:21 +00:00
import { Db } from './smartdata.classes.db'
2018-01-07 16:26:34 +00:00
import { DbTable } from './smartdata.classes.dbcollection'
2016-09-13 20:53:21 +00:00
2016-11-17 23:59:57 +00:00
export type TDocCreation = 'db' | 'new' | 'mixed'
2016-09-13 20:53:21 +00:00
2016-11-17 23:42:25 +00:00
/**
2016-11-17 23:59:57 +00:00
* saveable - saveable decorator to be used on class properties
2016-11-17 23:42:25 +00:00
*/
2016-11-18 12:56:15 +00:00
export function svDb() {
2017-02-25 10:37:05 +00:00
return (target: DbDoc<any>, key: string) => {
console.log('called sva')
if (!target.saveableProperties) { target.saveableProperties = [] }
target.saveableProperties.push(key)
}
2016-11-17 23:42:25 +00:00
}
2016-09-13 20:53:21 +00:00
export class DbDoc<T> {
2016-11-17 23:42:25 +00:00
2017-02-25 10:37:05 +00:00
/**
* the collection object an Doc belongs to
*/
2018-01-07 16:26:34 +00:00
collection: DbTable<T>
2016-11-17 23:42:25 +00:00
2017-02-25 10:37:05 +00:00
/**
* how the Doc in memory was created, may prove useful later.
*/
creationType: TDocCreation
2016-11-17 23:42:25 +00:00
2017-02-25 10:37:05 +00:00
/**
* an array of saveable properties of a doc
*/
saveableProperties: string[]
2016-11-17 23:42:25 +00:00
2017-06-18 17:52:54 +00:00
/**
* name
*/
name: string
2017-02-25 10:37:05 +00:00
/**
* class constructor
*/
constructor () {
this.name = this.constructor['name']
2017-02-25 10:37:05 +00:00
this.collection = this.constructor[ 'dbCollection' ]
}
2016-11-17 23:42:25 +00:00
2017-02-25 10:37:05 +00:00
/**
* saves this instance but not any connected items
* may lead to data inconsistencies, but is faster
*/
2017-09-13 11:47:38 +00:00
save () {
2017-06-18 17:52:54 +00:00
let saveableObject: any = {} // is not exposed to outside, so any is ok here
2017-02-25 10:37:05 +00:00
for (let propertyNameString of this.saveableProperties) {
saveableObject[ propertyNameString ] = this[ propertyNameString ]
2016-09-13 20:53:21 +00:00
}
2017-02-25 10:37:05 +00:00
switch (this.creationType) {
case 'db':
this.collection // TODO implement collection.update()
break
case 'new':
this.collection.insertOne(saveableObject)
}
}
2017-02-25 10:37:05 +00:00
/**
* also store any referenced objects to DB
* better for data consistency
*/
2017-09-13 11:47:38 +00:00
saveDeep (savedMapArg: Objectmap<DbDoc<any>> = null) {
2017-02-25 10:37:05 +00:00
if (!savedMapArg) {
savedMapArg = new Objectmap<DbDoc<any>>()
}
savedMapArg.add(this)
this.save()
for (let propertyKey in this) {
2017-09-13 11:47:38 +00:00
let property: any = this[ propertyKey ]
2017-02-25 10:37:05 +00:00
if (property instanceof DbDoc && !savedMapArg.checkForObject(property)) {
property.saveDeep(savedMapArg)
}
2016-09-13 20:53:21 +00:00
}
2017-02-25 10:37:05 +00:00
}
2016-09-13 20:53:21 +00:00
}