fix(core): update

This commit is contained in:
Philipp Kunz 2019-01-08 18:45:30 +01:00
parent e715cc6d37
commit 1800273b25
2 changed files with 35 additions and 7 deletions

View File

@ -109,7 +109,6 @@ export class SmartdataCollection<T> {
await this.checkDoc(dbDocArg); await this.checkDoc(dbDocArg);
this.markUniqueIndexes(dbDocArg.uniqueIndexes); this.markUniqueIndexes(dbDocArg.uniqueIndexes);
const saveableObject = await dbDocArg.createSavableObject(); const saveableObject = await dbDocArg.createSavableObject();
console.log(saveableObject);
const result = await this.mongoDbCollection.insertOne(saveableObject); const result = await this.mongoDbCollection.insertOne(saveableObject);
return result; return result;
} }
@ -117,11 +116,19 @@ export class SmartdataCollection<T> {
/** /**
* inserts object into the DbCollection * inserts object into the DbCollection
*/ */
async update(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> { public async update(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
await this.init(); await this.init();
await this.checkDoc(dbDocArg); await this.checkDoc(dbDocArg);
const identifiableObject = await dbDocArg.createIdentifiableObject();
const saveableObject = await dbDocArg.createSavableObject(); const saveableObject = await dbDocArg.createSavableObject();
this.mongoDbCollection.updateOne(saveableObject.dbDocUniqueId, saveableObject); this.mongoDbCollection.updateOne(identifiableObject, saveableObject);
}
public async delete (dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
await this.init();
await this.checkDoc(dbDocArg);
const identifiableObject = await dbDocArg.createIdentifiableObject();
this.mongoDbCollection.deleteOne(identifiableObject);
} }
/** /**
@ -129,7 +136,7 @@ export class SmartdataCollection<T> {
* if this.objectValidation is not set it passes. * if this.objectValidation is not set it passes.
*/ */
private checkDoc(docArg: T): Promise<void> { private checkDoc(docArg: T): Promise<void> {
let done = plugins.smartq.defer<void>(); const done = plugins.smartq.defer<void>();
let validationResult = true; let validationResult = true;
if (this.objectValidation) { if (this.objectValidation) {
validationResult = this.objectValidation(docArg); validationResult = this.objectValidation(docArg);

View File

@ -139,6 +139,13 @@ export class SmartDataDbDoc<T> {
} }
} }
/**
* deletes a document from the database
*/
async delete() {
}
/** /**
* also store any referenced objects to DB * also store any referenced objects to DB
* better for data consistency * better for data consistency
@ -157,11 +164,25 @@ export class SmartDataDbDoc<T> {
} }
} }
async createSavableObject() { /**
let saveableObject: any = {}; // is not exposed to outside, so any is ok here * creates a saveable object so the instance can be persisted as json in the database
for (let propertyNameString of this.saveableProperties) { */
public async createSavableObject() {
const saveableObject: any = {}; // is not exposed to outside, so any is ok here
for (const propertyNameString of this.saveableProperties) {
saveableObject[propertyNameString] = this[propertyNameString]; saveableObject[propertyNameString] = this[propertyNameString];
} }
return saveableObject; return saveableObject;
} }
/**
* creates an identifiable object for operations that require filtering
*/
public async createIdentifiableObject() {
const identifiableObject: any = {}; // is not exposed to outside, so any is ok here
for (const propertyNameString of this.uniqueIndexes) {
identifiableObject[propertyNameString] = this[propertyNameString];
}
return identifiableObject;
}
} }