smartdata/ts/smartdata.classes.dbdoc.ts

101 lines
2.3 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.
*/
creationStatus: TDocCreation = 'new'
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
/**
* primary id in the database
*/
dbId: 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
*/
async save () {
let self: any = this
switch (this.creationStatus) {
2017-02-25 10:37:05 +00:00
case 'db':
await this.collection.update(self)
2017-02-25 10:37:05 +00:00
break
case 'new':
let writeResult = await this.collection.insert(self)
this.creationStatus = 'db'
break;
default:
console.error('neither new nor in db?')
2017-02-25 10:37:05 +00:00
}
}
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
}
createSavableObject () {
let saveableObject: any = {} // is not exposed to outside, so any is ok here
for (let propertyNameString of this.saveableProperties) {
saveableObject[ propertyNameString ] = this[ propertyNameString ]
}
return saveableObject
}
2016-09-13 20:53:21 +00:00
}