UNPKG

nest-arango

Version:

ArangoDB driver module for NestJS with a built-in CLI tool for creating and running migration scripts

727 lines 40.4 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ArangoRepository = void 0; const common_1 = require("@nestjs/common"); const arangojs_1 = require("arangojs"); const error_1 = require("arangojs/error"); const __1 = require(".."); const repository_types_1 = require("../interfaces/repository.types"); const event_metadata_storage_1 = require("../metadata/storages/event-metadata.storage"); const type_metadata_storage_1 = require("../metadata/storages/type-metadata.storage"); const listener_type_1 = require("../metadata/types/listener.type"); let ArangoRepository = class ArangoRepository { get collectionName() { return this.targetMetadata.collection; } constructor(arangoManager, target) { this.arangoManager = arangoManager; this.targetMetadata = type_metadata_storage_1.TypeMetadataStorage.getMetadata(target.constructor.name); this.eventListeners = event_metadata_storage_1.EventListenerMetadataStorage.getMetadata(target.constructor.name); this.collection = this.arangoManager.database.collection(this.collectionName); } getIdFor(_key) { return `${this.collectionName}/${_key}`; } getKeyFrom(_id) { return _id.split('/')[1]; } documentExists(key, documentExistsOptions = {}) { return __awaiter(this, void 0, void 0, function* () { if (documentExistsOptions.transaction) { return yield documentExistsOptions.transaction.step(() => this.collection.documentExists(key)); } else { return yield this.collection.documentExists(key, documentExistsOptions); } }); } documentsExist(documents, documentsExistOptions = {}) { return __awaiter(this, void 0, void 0, function* () { const keys = documents.map((selector) => { const curSelector = selector; if (curSelector['_id']) return curSelector._id.split('/')[1]; if (curSelector['_key']) return curSelector._key; const split = curSelector.split('/'); if (split.length > 1) return split[1]; return curSelector; }); const aqlQuery = ` FOR key IN @keys RETURN !IS_NULL(DOCUMENT(CONCAT('${this.collection.name}', '/', key))) `; const bindVars = { keys: keys }; if (documentsExistOptions.transaction) { const cursor = yield documentsExistOptions.transaction.step(() => this.arangoManager.query(aqlQuery, bindVars)); return yield documentsExistOptions.transaction.step(() => cursor.all()); } else { const cursor = yield this.arangoManager.query(aqlQuery, bindVars); return yield cursor.all(); } }); } getDocumentCountBy(bindVars, findManyByOptions = {}) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function* () { const filter = (_a = Object.entries(bindVars)) === null || _a === void 0 ? void 0 : _a.map(([k]) => `FILTER d.${k} == @${k}`).join(' '); const aqlQuery = `WITH ${this.collection.name} FOR d IN ${this.collection.name} ${filter} COLLECT WITH COUNT INTO length RETURN length`; if (findManyByOptions.transaction) { const cursor = yield findManyByOptions.transaction.step(() => this.arangoManager.query(aqlQuery, bindVars)); return ((_b = (yield findManyByOptions.transaction.step(() => cursor.next()))) !== null && _b !== void 0 ? _b : 0); } else { const cursor = yield this.arangoManager.query(aqlQuery, bindVars); return (_c = (yield cursor.next())) !== null && _c !== void 0 ? _c : 0; } }); } findOne(key, findOneOptions = {}) { return __awaiter(this, void 0, void 0, function* () { if (findOneOptions.transaction) { return yield findOneOptions.transaction.step(() => this.collection.document(key, { graceful: true })); } else { return yield this.collection.document(key, { graceful: true }); } }); } findOneBy(bindVars, findOneByOptions = {}) { var _a; return __awaiter(this, void 0, void 0, function* () { const filter = (_a = Object.entries(bindVars)) === null || _a === void 0 ? void 0 : _a.map(([k]) => `FILTER d.${k} == @${k}`).join(' '); const aqlQuery = `WITH ${this.collection.name} FOR d IN ${this.collection.name} ${filter} RETURN d`; if (findOneByOptions.transaction) { const cursor = yield findOneByOptions.transaction.step(() => this.arangoManager.query(aqlQuery, bindVars)); return yield findOneByOptions.transaction.step(() => cursor.next()); } else { const cursor = yield this.arangoManager.query(aqlQuery, bindVars); return yield cursor.next(); } }); } findMany(keys, findManyOptions = {}) { return __awaiter(this, void 0, void 0, function* () { if (findManyOptions.transaction) { const docs = yield findManyOptions.transaction.step(() => this.collection.documents(keys)); return this.findManyInternal(docs); } else { return this.findManyInternal(yield this.collection.documents(keys)); } }); } findManyInternal(docs) { return docs.filter((doc) => { if (doc.error) { return doc.errorNum !== 1202; } else { return true; } }); } findManyBy(bindVars, findManyByOptions = {}) { var _a, _b, _c, _d, _e, _f, _g; return __awaiter(this, void 0, void 0, function* () { const filter = (_a = Object.entries(bindVars)) === null || _a === void 0 ? void 0 : _a.map(([k]) => `FILTER d.${k} == @${k}`).join(' '); let limit = ''; if (findManyByOptions.page !== undefined && findManyByOptions.pageSize !== undefined) { limit = `LIMIT ${findManyByOptions.page * findManyByOptions.pageSize}, ${findManyByOptions.pageSize}`; } const sort = (_c = Object.entries((_b = findManyByOptions.sort) !== null && _b !== void 0 ? _b : {})) === null || _c === void 0 ? void 0 : _c.map(([k, v]) => `SORT d.${k} ${v}`).join(' '); const aqlQuery = `WITH ${this.collection.name} FOR d IN ${this.collection.name} ${filter} ${limit} ${sort} RETURN d`; if (findManyByOptions.transaction) { const cursor = yield findManyByOptions.transaction.step(() => this.arangoManager.query(aqlQuery, bindVars, { fullCount: true, })); const results = yield findManyByOptions.transaction.step(() => cursor.all()); return { totalCount: (_e = (_d = cursor.extra.stats) === null || _d === void 0 ? void 0 : _d.fullCount) !== null && _e !== void 0 ? _e : results.length, results: results, }; } else { const cursor = yield this.arangoManager.query(aqlQuery, bindVars, { fullCount: true }); const results = yield cursor.all(); return { totalCount: (_g = (_f = cursor.extra.stats) === null || _f === void 0 ? void 0 : _f.fullCount) !== null && _g !== void 0 ? _g : results.length, results: results, }; } }); } findAll(findAllOptions = {}) { var _a, _b, _c, _d, _e, _f; return __awaiter(this, void 0, void 0, function* () { let limit = ''; if (findAllOptions.page !== undefined && findAllOptions.pageSize !== undefined) { limit = `LIMIT ${findAllOptions.page * findAllOptions.pageSize}, ${findAllOptions.pageSize}`; } const sort = (_b = Object.entries((_a = findAllOptions.sort) !== null && _a !== void 0 ? _a : {})) === null || _b === void 0 ? void 0 : _b.map(([k, v]) => `SORT d.${k} ${v}`).join(' '); const aqlQuery = `WITH ${this.collection.name} FOR d IN ${this.collection.name} ${limit} ${sort} RETURN d`; if (findAllOptions.transaction) { const cursor = yield findAllOptions.transaction.step(() => this.arangoManager.query(aqlQuery, {}, { fullCount: true, })); const results = yield findAllOptions.transaction.step(() => cursor.all()); return { totalCount: (_d = (_c = cursor.extra.stats) === null || _c === void 0 ? void 0 : _c.fullCount) !== null && _d !== void 0 ? _d : results.length, results: results, }; } else { const cursor = yield this.arangoManager.query(aqlQuery, {}, { fullCount: true }); const results = yield cursor.all(); return { totalCount: (_f = (_e = cursor.extra.stats) === null || _e === void 0 ? void 0 : _e.fullCount) !== null && _f !== void 0 ? _f : results.length, results: results, }; } }); } save(document, saveOptions = {}) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function* () { saveOptions = Object.assign({ emitEvents: true }, saveOptions); let context; if (saveOptions === null || saveOptions === void 0 ? void 0 : saveOptions.emitEvents) { context = { database: this.arangoManager.database, transaction: saveOptions === null || saveOptions === void 0 ? void 0 : saveOptions.transaction, info: { current: 0, }, data: saveOptions === null || saveOptions === void 0 ? void 0 : saveOptions.data, repository: this, }; yield ((_b = (_a = this.eventListeners) === null || _a === void 0 ? void 0 : _a.get(listener_type_1.EventListenerType.BEFORE_SAVE)) === null || _b === void 0 ? void 0 : _b.call(document, context)); } let newEntity; if (saveOptions.transaction) { newEntity = (yield saveOptions.transaction.step(() => this.collection.save(document, { returnNew: true, }))).new; } else { newEntity = (yield this.collection.save(document, { returnNew: true, })).new; } if (saveOptions === null || saveOptions === void 0 ? void 0 : saveOptions.emitEvents) { context.new = newEntity; yield ((_d = (_c = this.eventListeners) === null || _c === void 0 ? void 0 : _c.get(listener_type_1.EventListenerType.AFTER_SAVE)) === null || _d === void 0 ? void 0 : _d.call(document, context)); } return newEntity; }); } saveAll(documents, saveAllOptions = {}) { var _a; return __awaiter(this, void 0, void 0, function* () { saveAllOptions = Object.assign({ emitEvents: true }, saveAllOptions); let context; if (saveAllOptions === null || saveAllOptions === void 0 ? void 0 : saveAllOptions.emitEvents) { context = { database: this.arangoManager.database, transaction: saveAllOptions === null || saveAllOptions === void 0 ? void 0 : saveAllOptions.transaction, info: { current: 0, }, data: saveAllOptions === null || saveAllOptions === void 0 ? void 0 : saveAllOptions.data, repository: this, }; if ((_a = this.eventListeners) === null || _a === void 0 ? void 0 : _a.get(listener_type_1.EventListenerType.BEFORE_SAVE)) { yield Promise.all(documents.map((document, index) => __awaiter(this, void 0, void 0, function* () { var _b, _c; context.info.current = index; yield ((_c = (_b = this.eventListeners) === null || _b === void 0 ? void 0 : _b.get(listener_type_1.EventListenerType.BEFORE_SAVE)) === null || _c === void 0 ? void 0 : _c.call(document, context)); }))); } } let result; if (saveAllOptions.transaction) { result = yield saveAllOptions.transaction.step(() => { return this.collection.saveAll(documents, { returnNew: true, }); }); } else { result = yield this.collection.saveAll(documents, { returnNew: true, }); } return Promise.all(result.map((document, index) => __awaiter(this, void 0, void 0, function* () { var _d, _e; if (saveAllOptions === null || saveAllOptions === void 0 ? void 0 : saveAllOptions.emitEvents) { context.info.current = index; context.new = document.new; yield ((_e = (_d = this.eventListeners) === null || _d === void 0 ? void 0 : _d.get(listener_type_1.EventListenerType.AFTER_SAVE)) === null || _e === void 0 ? void 0 : _e.call(document, context)); } return document.new; }))); }); } update(document, updateOptions = {}) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function* () { updateOptions = Object.assign({ returnOld: true, emitEvents: true }, updateOptions); const { transaction, emitEvents, data } = updateOptions, options = __rest(updateOptions, ["transaction", "emitEvents", "data"]); let context; if (emitEvents) { context = { database: this.arangoManager.database, transaction: transaction, info: { current: 0, }, data: data, repository: this, }; yield ((_b = (_a = this.eventListeners) === null || _a === void 0 ? void 0 : _a.get(listener_type_1.EventListenerType.BEFORE_UPDATE)) === null || _b === void 0 ? void 0 : _b.call(document, context)); } let result; if (transaction) { result = yield transaction.step(() => this.collection.update(document, document, Object.assign({ returnNew: true }, updateOptions))); } else { result = yield this.collection.update(document, document, Object.assign({ returnNew: true }, updateOptions)); } if (updateOptions === null || updateOptions === void 0 ? void 0 : updateOptions.emitEvents) { context.new = result.new; context.old = result.old; yield ((_d = (_c = this.eventListeners) === null || _c === void 0 ? void 0 : _c.get(listener_type_1.EventListenerType.AFTER_UPDATE)) === null || _d === void 0 ? void 0 : _d.call(document, context)); } return new repository_types_1.ArangoNewOldResult(result === null || result === void 0 ? void 0 : result.new, result === null || result === void 0 ? void 0 : result.old); }); } updateWithAql(document, updateOptions = {}) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function* () { updateOptions = Object.assign({ returnOld: true, emitEvents: true }, updateOptions); const { transaction, emitEvents, data } = updateOptions, options = __rest(updateOptions, ["transaction", "emitEvents", "data"]); let context; if (emitEvents) { context = { database: this.arangoManager.database, transaction: transaction, info: { current: 0, }, data: data, repository: this, }; yield ((_b = (_a = this.eventListeners) === null || _a === void 0 ? void 0 : _a.get(listener_type_1.EventListenerType.BEFORE_UPDATE)) === null || _b === void 0 ? void 0 : _b.call(document, context)); } let result; const _aql = (0, __1.aqlConcat)((0, __1.aqlPart) `WITH ${this.collection} `, `UPDATE ${JSON.stringify({ _key: document._key, _id: document._id, })} WITH `, (0, __1.documentAQLBuilder)(document), (0, __1.aqlPart) ` IN ${this.collection} `, `OPTIONS ${JSON.stringify(options)} RETURN { 'new': NEW, 'old': OLD }`); const aqlQuery = (0, arangojs_1.aql)(_aql.templateStrings, ..._aql.args); let cursor; if (transaction) { cursor = yield transaction.step(() => this.arangoManager.query(aqlQuery)); result = yield transaction.step(() => cursor.next()); } else { cursor = yield this.arangoManager.query(aqlQuery); result = yield cursor.next(); } if (!result) { throw new error_1.ArangoError({ code: 404, error: true, errorMessage: 'document not found', errorNum: 1202, }); } if (updateOptions === null || updateOptions === void 0 ? void 0 : updateOptions.emitEvents) { context.new = result.new; context.old = result.old; yield ((_d = (_c = this.eventListeners) === null || _c === void 0 ? void 0 : _c.get(listener_type_1.EventListenerType.AFTER_UPDATE)) === null || _d === void 0 ? void 0 : _d.call(document, context)); } return new repository_types_1.ArangoNewOldResult(result === null || result === void 0 ? void 0 : result.new, result === null || result === void 0 ? void 0 : result.old); }); } updateAll(documents, updateAllOptions = {}) { var _a; return __awaiter(this, void 0, void 0, function* () { updateAllOptions = Object.assign({ returnOld: true, emitEvents: true }, updateAllOptions); let context; if (updateAllOptions === null || updateAllOptions === void 0 ? void 0 : updateAllOptions.emitEvents) { context = { database: this.arangoManager.database, transaction: updateAllOptions === null || updateAllOptions === void 0 ? void 0 : updateAllOptions.transaction, info: { current: 0, }, data: updateAllOptions === null || updateAllOptions === void 0 ? void 0 : updateAllOptions.data, repository: this, }; if ((_a = this.eventListeners) === null || _a === void 0 ? void 0 : _a.get(listener_type_1.EventListenerType.BEFORE_UPDATE)) { yield Promise.all(documents.map((document, index) => __awaiter(this, void 0, void 0, function* () { var _b, _c; context.info.current = index; yield ((_c = (_b = this.eventListeners) === null || _b === void 0 ? void 0 : _b.get(listener_type_1.EventListenerType.BEFORE_UPDATE)) === null || _c === void 0 ? void 0 : _c.call(document, context)); }))); } } let results; if (updateAllOptions.transaction) { results = yield updateAllOptions.transaction.step(() => this.collection.updateAll(documents, Object.assign({ returnNew: true }, updateAllOptions))); } else { results = yield this.collection.updateAll(documents, Object.assign({ returnNew: true }, updateAllOptions)); } return yield Promise.all(results.map((document, index) => __awaiter(this, void 0, void 0, function* () { var _d, _e; if (updateAllOptions === null || updateAllOptions === void 0 ? void 0 : updateAllOptions.emitEvents) { context.new = document.new; context.old = document.old; context.info.current = index; yield ((_e = (_d = this.eventListeners) === null || _d === void 0 ? void 0 : _d.get(listener_type_1.EventListenerType.AFTER_UPDATE)) === null || _e === void 0 ? void 0 : _e.call(document, context)); } return new repository_types_1.ArangoNewOldResult(document.new, document.old); }))); }); } replace(selector, document, replaceOptions = {}) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function* () { replaceOptions = Object.assign({ returnOld: true, emitEvents: true }, replaceOptions); let context; if (replaceOptions === null || replaceOptions === void 0 ? void 0 : replaceOptions.emitEvents) { context = { database: this.arangoManager.database, transaction: replaceOptions === null || replaceOptions === void 0 ? void 0 : replaceOptions.transaction, info: { current: 0, }, data: replaceOptions === null || replaceOptions === void 0 ? void 0 : replaceOptions.data, repository: this, }; yield ((_b = (_a = this.eventListeners) === null || _a === void 0 ? void 0 : _a.get(listener_type_1.EventListenerType.BEFORE_REPLACE)) === null || _b === void 0 ? void 0 : _b.call(document, context)); } let result; if (replaceOptions.transaction) { result = yield replaceOptions.transaction.step(() => this.collection.replace(selector, document, Object.assign({ returnNew: true }, replaceOptions))); } else { result = yield this.collection.replace(selector, document, Object.assign({ returnNew: true }, replaceOptions)); } if (replaceOptions === null || replaceOptions === void 0 ? void 0 : replaceOptions.emitEvents) { context.new = result.new; context.old = result.old; yield ((_d = (_c = this.eventListeners) === null || _c === void 0 ? void 0 : _c.get(listener_type_1.EventListenerType.AFTER_REPLACE)) === null || _d === void 0 ? void 0 : _d.call(document, context)); } return new repository_types_1.ArangoNewOldResult(result.new, result.old); }); } replaceAll(documents, replaceAllOptions = {}) { var _a; return __awaiter(this, void 0, void 0, function* () { replaceAllOptions = Object.assign({ returnOld: true, emitEvents: true }, replaceAllOptions); let context; if (replaceAllOptions === null || replaceAllOptions === void 0 ? void 0 : replaceAllOptions.emitEvents) { context = { database: this.arangoManager.database, transaction: replaceAllOptions === null || replaceAllOptions === void 0 ? void 0 : replaceAllOptions.transaction, info: { current: 0, }, data: replaceAllOptions === null || replaceAllOptions === void 0 ? void 0 : replaceAllOptions.data, repository: this, }; if ((_a = this.eventListeners) === null || _a === void 0 ? void 0 : _a.get(listener_type_1.EventListenerType.BEFORE_REPLACE)) { yield Promise.all(documents.map((document, index) => __awaiter(this, void 0, void 0, function* () { var _b, _c; context.info.current = index; yield ((_c = (_b = this.eventListeners) === null || _b === void 0 ? void 0 : _b.get(listener_type_1.EventListenerType.BEFORE_REPLACE)) === null || _c === void 0 ? void 0 : _c.call(document, context)); }))); } } let results; if (replaceAllOptions.transaction) { results = yield replaceAllOptions.transaction.step(() => this.collection.replaceAll(documents, Object.assign({ returnNew: true }, replaceAllOptions))); } else { results = yield this.collection.replaceAll(documents, Object.assign({ returnNew: true }, replaceAllOptions)); } return yield Promise.all(results.map((document, index) => __awaiter(this, void 0, void 0, function* () { var _d, _e; if (replaceAllOptions === null || replaceAllOptions === void 0 ? void 0 : replaceAllOptions.emitEvents) { context.new = document.new; context.old = document.old; context.info.current = index; yield ((_e = (_d = this.eventListeners) === null || _d === void 0 ? void 0 : _d.get(listener_type_1.EventListenerType.AFTER_REPLACE)) === null || _e === void 0 ? void 0 : _e.call(document, context)); } return new repository_types_1.ArangoNewOldResult(document.new, document.old); }))); }); } upsert(upsert, insert, update, upsertOptions = {}) { var _a, _b, _c, _d, _e, _f, _g, _h; return __awaiter(this, void 0, void 0, function* () { upsertOptions = Object.assign({ emitEvents: true }, upsertOptions); const { transaction, emitEvents, data } = upsertOptions, options = __rest(upsertOptions, ["transaction", "emitEvents", "data"]); let context; if (emitEvents) { context = { database: this.arangoManager.database, transaction: transaction, info: { current: 0, }, data: data, repository: this, }; yield ((_b = (_a = this.eventListeners) === null || _a === void 0 ? void 0 : _a.get(listener_type_1.EventListenerType.BEFORE_SAVE)) === null || _b === void 0 ? void 0 : _b.call(insert, context)); yield ((_d = (_c = this.eventListeners) === null || _c === void 0 ? void 0 : _c.get(listener_type_1.EventListenerType.BEFORE_UPDATE)) === null || _d === void 0 ? void 0 : _d.call(update, context)); } let result; let cursor; const _aql = (0, __1.aqlConcat)((0, __1.aqlPart) `WITH ${this.collection} `, (0, __1.aqlPart) `UPSERT ${upsert} `, (0, __1.aqlPart) `INSERT ${insert} `, (0, __1.aqlPart) `UPDATE ${update} `, (0, __1.aqlPart) `IN ${this.collection} `, `OPTIONS ${JSON.stringify(options)} `, `RETURN { 'new': NEW, 'old': OLD }`); const aqlQuery = (0, arangojs_1.aql)(_aql.templateStrings, ..._aql.args); if (transaction) { cursor = yield transaction.step(() => this.arangoManager.query(aqlQuery)); result = yield transaction.step(() => cursor.next()); } else { cursor = yield this.arangoManager.query(aqlQuery); result = yield cursor.next(); } if (!result) { throw new error_1.ArangoError({ code: 404, error: true, errorMessage: 'document not found', errorNum: 1202, }); } if (emitEvents) { context.new = result.new; context.old = result.old; if (result.old) { yield ((_f = (_e = this.eventListeners) === null || _e === void 0 ? void 0 : _e.get(listener_type_1.EventListenerType.AFTER_UPDATE)) === null || _f === void 0 ? void 0 : _f.call(update, context)); } else { yield ((_h = (_g = this.eventListeners) === null || _g === void 0 ? void 0 : _g.get(listener_type_1.EventListenerType.AFTER_SAVE)) === null || _h === void 0 ? void 0 : _h.call(insert, context)); } } return new repository_types_1.ArangoNewOldResult(result.new, result.old); }); } upsertWithAql(upsert, insert, update, upsertOptions = {}) { var _a, _b, _c, _d, _e, _f, _g, _h; return __awaiter(this, void 0, void 0, function* () { upsertOptions = Object.assign({ emitEvents: true }, upsertOptions); const { transaction, emitEvents, data } = upsertOptions, options = __rest(upsertOptions, ["transaction", "emitEvents", "data"]); let context; if (emitEvents) { context = { database: this.arangoManager.database, transaction: transaction, info: { current: 0, }, data: data, repository: this, }; yield ((_b = (_a = this.eventListeners) === null || _a === void 0 ? void 0 : _a.get(listener_type_1.EventListenerType.BEFORE_SAVE)) === null || _b === void 0 ? void 0 : _b.call(insert, context)); yield ((_d = (_c = this.eventListeners) === null || _c === void 0 ? void 0 : _c.get(listener_type_1.EventListenerType.BEFORE_UPDATE)) === null || _d === void 0 ? void 0 : _d.call(update, context)); } let result; let cursor; const _aql = (0, __1.aqlConcat)((0, __1.aqlPart) `WITH ${this.collection} `, (0, __1.aqlPart) `UPSERT ${upsert} `, (0, __1.aqlPart) `INSERT ${insert} `, `UPDATE `, (0, __1.documentAQLBuilder)(update), (0, __1.aqlPart) `IN ${this.collection} `, `OPTIONS ${JSON.stringify(options)} `, `RETURN { 'new': NEW, 'old': OLD }`); const aqlQuery = (0, arangojs_1.aql)(_aql.templateStrings, ..._aql.args); if (transaction) { cursor = yield transaction.step(() => this.arangoManager.query(aqlQuery)); result = yield transaction.step(() => cursor.next()); } else { cursor = yield this.arangoManager.query(aqlQuery); result = yield cursor.next(); } if (!result) { throw new error_1.ArangoError({ code: 404, error: true, errorMessage: 'document not found', errorNum: 1202, }); } if (emitEvents) { context.new = result.new; context.old = result.old; if (result.old) { yield ((_f = (_e = this.eventListeners) === null || _e === void 0 ? void 0 : _e.get(listener_type_1.EventListenerType.AFTER_UPDATE)) === null || _f === void 0 ? void 0 : _f.call(update, context)); } else { yield ((_h = (_g = this.eventListeners) === null || _g === void 0 ? void 0 : _g.get(listener_type_1.EventListenerType.AFTER_SAVE)) === null || _h === void 0 ? void 0 : _h.call(insert, context)); } } return new repository_types_1.ArangoNewOldResult(result.new, result.old); }); } remove(key, removeOptions = {}) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { removeOptions = Object.assign({ emitEvents: true }, removeOptions); let context; if (removeOptions === null || removeOptions === void 0 ? void 0 : removeOptions.emitEvents) { context = { database: this.arangoManager.database, transaction: removeOptions === null || removeOptions === void 0 ? void 0 : removeOptions.transaction, info: { current: 0, }, data: removeOptions === null || removeOptions === void 0 ? void 0 : removeOptions.data, repository: this, }; } let result; if (removeOptions.transaction) { result = (yield removeOptions.transaction.step(() => this.collection.remove(key, { returnOld: true }))).old; } else { result = (yield this.collection.remove(key, { returnOld: true })).old; } if (removeOptions === null || removeOptions === void 0 ? void 0 : removeOptions.emitEvents) { context.old = result; yield ((_b = (_a = this.eventListeners) === null || _a === void 0 ? void 0 : _a.get(listener_type_1.EventListenerType.AFTER_REMOVE)) === null || _b === void 0 ? void 0 : _b.call(result, context)); } return result; }); } /** * Removes documents containing matching fields with the provided bind variables. * * @remarks * This method does NOT emit the event that invokes `@BeforeRemove` decorated methods. */ removeBy(bindVars, removeByOptions = {}) { return __awaiter(this, void 0, void 0, function* () { removeByOptions = Object.assign({ emitEvents: true }, removeByOptions); const entries = Object.entries(bindVars); if ((entries === null || entries === void 0 ? void 0 : entries.length) < 1) { throw new Error('No bindVars were specified!'); } let context; if (removeByOptions === null || removeByOptions === void 0 ? void 0 : removeByOptions.emitEvents) { context = { database: this.arangoManager.database, transaction: removeByOptions === null || removeByOptions === void 0 ? void 0 : removeByOptions.transaction, info: { current: 0, }, data: removeByOptions === null || removeByOptions === void 0 ? void 0 : removeByOptions.data, repository: this, }; } let results; const filter = entries === null || entries === void 0 ? void 0 : entries.map(([k]) => `FILTER d.${k} == @${k}`).join(' '); const aqlQuery = `WITH ${this.collection.name} FOR d IN ${this.collection.name} ${filter} REMOVE d IN ${this.collection.name} RETURN OLD`; if (removeByOptions.transaction) { const cursor = yield removeByOptions.transaction.step(() => this.arangoManager.query(aqlQuery, bindVars)); results = yield removeByOptions.transaction.step(() => cursor.all()); } else { const cursor = yield this.arangoManager.query(aqlQuery, bindVars); results = yield cursor.all(); } return yield Promise.all(results.map((document, index) => __awaiter(this, void 0, void 0, function* () { var _a, _b; if (removeByOptions === null || removeByOptions === void 0 ? void 0 : removeByOptions.emitEvents) { context.old = document; context.info.current = index; yield ((_b = (_a = this.eventListeners) === null || _a === void 0 ? void 0 : _a.get(listener_type_1.EventListenerType.AFTER_REMOVE)) === null || _b === void 0 ? void 0 : _b.call(document, context)); } return document; }))); }); } removeAll(keys, removeAllOptions = {}) { return __awaiter(this, void 0, void 0, function* () { removeAllOptions = Object.assign({ emitEvents: true }, removeAllOptions); let context; if (removeAllOptions === null || removeAllOptions === void 0 ? void 0 : removeAllOptions.emitEvents) { context = { database: this.arangoManager.database, transaction: removeAllOptions === null || removeAllOptions === void 0 ? void 0 : removeAllOptions.transaction, info: { current: 0, }, data: removeAllOptions === null || removeAllOptions === void 0 ? void 0 : removeAllOptions.data, repository: this, }; } let results; if (removeAllOptions.transaction) { results = (yield removeAllOptions.transaction.step(() => this.collection.removeAll(keys, { returnOld: true }))).map((item) => { return item.old; }); } else { results = (yield this.collection.removeAll(keys, { returnOld: true })).map((item) => { return item.old; }); } return results.map((document, index) => { var _a, _b; if (removeAllOptions === null || removeAllOptions === void 0 ? void 0 : removeAllOptions.emitEvents) { context.old = document; context.info.current = index; (_b = (_a = this.eventListeners) === null || _a === void 0 ? void 0 : _a.get(listener_type_1.EventListenerType.AFTER_REMOVE)) === null || _b === void 0 ? void 0 : _b.call(document, context); } return document; }); }); } truncate(truncateOptions = {}) { return __awaiter(this, void 0, void 0, function* () { if (truncateOptions.transaction) { yield truncateOptions.transaction.step(() => this.collection.truncate()); } else { yield this.collection.truncate(); } }); } }; exports.ArangoRepository = ArangoRepository; exports.ArangoRepository = ArangoRepository = __decorate([ (0, common_1.Injectable)() ], ArangoRepository); //# sourceMappingURL=arango.repository.js.map