/** * this plugin allows delta-updates with mongo-like-syntax * It's using modifyjs internally * @link https://github.com/lgandecki/modifyjs */ import modifyjs from 'modifyjs'; import type { RxDocument, RxQuery, RxPlugin, UpdateQuery } from 'nxdb-old/src/types'; export function incrementalUpdate( this: RxDocument, updateObj: UpdateQuery ): Promise> { return this.incrementalModify((docData) => { const newDocData = modifyjs(docData, updateObj); return newDocData; }); } export function update( this: RxDocument, updateObj: UpdateQuery ): Promise> { const oldDocData = this._data; const newDocData = modifyjs(oldDocData, updateObj); return this._saveData(newDocData, oldDocData); } export function RxQueryUpdate( this: RxQuery, updateObj: UpdateQuery ): Promise { return this.exec() .then(docs => { if (!docs) { return null; } if (Array.isArray(docs)) { return Promise.all( docs.map(doc => doc.update(updateObj)) ).then(() => docs); } else { // via findOne() return docs.update(updateObj).then(() => docs); } }); } export const NxDBUpdatePlugin: RxPlugin = { name: 'update', nxdb: true, prototypes: { RxDocument: (proto: any) => { proto.update = update; proto.incrementalUpdate = incrementalUpdate; }, RxQuery: (proto: any) => { proto.update = RxQueryUpdate; } } };