retrieve option for TxUpdate

Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
Andrey Platov 2021-10-13 21:54:11 +02:00
parent 002af26680
commit 8013bc086c
No known key found for this signature in database
GPG Key ID: C8787EFEB4B64AF0
4 changed files with 22 additions and 8 deletions

View File

@ -211,7 +211,7 @@ export class ModelDb extends MemDb implements Storage {
}
doc.modifiedBy = tx.modifiedBy
doc.modifiedOn = tx.modifiedOn
return { update: 'here' }
return tx.retrieve === true ? { object: doc } : {}
}
protected async txRemoveDoc (tx: TxRemoveDoc<Doc>): Promise<TxResult> {

View File

@ -155,6 +155,7 @@ export type DocumentUpdate<T extends Doc> = Partial<Data<T>> & PushOptions<T> &
*/
export interface TxUpdateDoc<T extends Doc> extends TxCUD<T> {
operations: DocumentUpdate<T>
retrieve?: boolean
}
/**
@ -273,9 +274,10 @@ export class TxOperations implements Storage {
_class: Ref<Class<T>>,
space: Ref<Space>,
objectId: Ref<T>,
operations: DocumentUpdate<T>
operations: DocumentUpdate<T>,
retrieve?: boolean
): Promise<TxResult> {
const tx = this.txFactory.createTxUpdateDoc(_class, space, objectId, operations)
const tx = this.txFactory.createTxUpdateDoc(_class, space, objectId, operations, retrieve)
return this.storage.tx(tx)
}
@ -346,7 +348,8 @@ export class TxFactory {
_class: Ref<Class<T>>,
space: Ref<Space>,
objectId: Ref<T>,
operations: DocumentUpdate<T>
operations: DocumentUpdate<T>,
retrieve?: boolean
): TxUpdateDoc<T> {
return {
_id: generateId(),
@ -357,7 +360,8 @@ export class TxFactory {
objectId,
objectClass: _class,
objectSpace: space,
operations
operations,
retrieve
}
}

View File

@ -45,7 +45,7 @@
}
function onChange(value: any) {
client.updateDoc(_class, object.space, object._id, { [key]: value }).then(result => console.log('UPDATE RESULT', result))
client.updateDoc(_class, object.space, object._id, { [key]: value }, true).then(result => console.log('UPDATE RESULT', result))
}
</script>

View File

@ -179,10 +179,20 @@ class MongoAdapter extends MongoAdapterBase {
console.log('ops', ops)
return await this.db.collection(domain).bulkWrite(ops as any)
} else {
return await this.db.collection(domain).updateOne({ _id: tx.objectId }, tx.operations)
if (tx.retrieve === true) {
const result = await this.db.collection(domain).findOneAndUpdate({ _id: tx.objectId }, tx.operations, { returnDocument: 'after' })
return { object: result.value }
} else {
return await this.db.collection(domain).updateOne({ _id: tx.objectId }, tx.operations)
}
}
} else {
return await this.db.collection(domain).updateOne({ _id: tx.objectId }, { $set: tx.operations })
if (tx.retrieve === true) {
const result = await this.db.collection(domain).findOneAndUpdate({ _id: tx.objectId }, { $set: tx.operations }, { returnDocument: 'after' })
return { object: result.value }
} else {
return await this.db.collection(domain).updateOne({ _id: tx.objectId }, { $set: tx.operations })
}
}
}