From 1800273b25521600ef54684e02ceebce08f63811 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Tue, 8 Jan 2019 18:45:30 +0100 Subject: [PATCH] fix(core): update --- ts/smartdata.classes.collection.ts | 15 +++++++++++---- ts/smartdata.classes.doc.ts | 27 ++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/ts/smartdata.classes.collection.ts b/ts/smartdata.classes.collection.ts index 2dadd4f..5c0aba5 100644 --- a/ts/smartdata.classes.collection.ts +++ b/ts/smartdata.classes.collection.ts @@ -109,7 +109,6 @@ export class SmartdataCollection { await this.checkDoc(dbDocArg); this.markUniqueIndexes(dbDocArg.uniqueIndexes); const saveableObject = await dbDocArg.createSavableObject(); - console.log(saveableObject); const result = await this.mongoDbCollection.insertOne(saveableObject); return result; } @@ -117,11 +116,19 @@ export class SmartdataCollection { /** * inserts object into the DbCollection */ - async update(dbDocArg: T & SmartDataDbDoc): Promise { + public async update(dbDocArg: T & SmartDataDbDoc): Promise { await this.init(); await this.checkDoc(dbDocArg); + const identifiableObject = await dbDocArg.createIdentifiableObject(); const saveableObject = await dbDocArg.createSavableObject(); - this.mongoDbCollection.updateOne(saveableObject.dbDocUniqueId, saveableObject); + this.mongoDbCollection.updateOne(identifiableObject, saveableObject); + } + + public async delete (dbDocArg: T & SmartDataDbDoc): Promise { + await this.init(); + await this.checkDoc(dbDocArg); + const identifiableObject = await dbDocArg.createIdentifiableObject(); + this.mongoDbCollection.deleteOne(identifiableObject); } /** @@ -129,7 +136,7 @@ export class SmartdataCollection { * if this.objectValidation is not set it passes. */ private checkDoc(docArg: T): Promise { - let done = plugins.smartq.defer(); + const done = plugins.smartq.defer(); let validationResult = true; if (this.objectValidation) { validationResult = this.objectValidation(docArg); diff --git a/ts/smartdata.classes.doc.ts b/ts/smartdata.classes.doc.ts index e13648d..94916d3 100644 --- a/ts/smartdata.classes.doc.ts +++ b/ts/smartdata.classes.doc.ts @@ -139,6 +139,13 @@ export class SmartDataDbDoc { } } + /** + * deletes a document from the database + */ + async delete() { + + } + /** * also store any referenced objects to DB * better for data consistency @@ -157,11 +164,25 @@ export class SmartDataDbDoc { } } - async createSavableObject() { - let saveableObject: any = {}; // is not exposed to outside, so any is ok here - for (let propertyNameString of this.saveableProperties) { + /** + * creates a saveable object so the instance can be persisted as json in the database + */ + 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]; } 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; + } }