ottoman
Version:
Ottoman Couchbase ODM
81 lines • 4.11 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.storeLifeCycle = void 0;
const hooks_1 = require("../../utils/hooks");
const exec_hooks_1 = require("../hooks/exec-hooks");
const schema_1 = require("../../schema");
const handler_1 = require("../../handler");
const update_refdoc_indexes_1 = require("./update-refdoc-indexes");
const cast_strategy_1 = require("../../utils/cast-strategy");
const couchbase_1 = require("couchbase");
const ottoman_errors_1 = require("../../exceptions/ottoman-errors");
/**
* Store lifecycle including hooks and validations.
* @ignore
*/
const storeLifeCycle = ({ key, id, data, options, metadata, refKeys }) => __awaiter(void 0, void 0, void 0, function* () {
const { schema, collection, modelKey, ID_KEY, ottoman } = metadata;
let document = data;
const _colleciton = collection();
yield (0, exec_hooks_1.execHooks)(schema, 'preHooks', hooks_1.HOOKS.VALIDATE, document);
document = (0, schema_1.validate)(document, schema, {
strict: schema.options.strict,
strategy: cast_strategy_1.CAST_STRATEGY.THROW,
skip: [modelKey.split('.')[0], ID_KEY],
});
// enforceRefCheck logic
let enforceRefCheck = schema.options.enforceRefCheck;
if (options.hasOwnProperty('enforceRefCheck')) {
enforceRefCheck = options.enforceRefCheck;
}
if (enforceRefCheck) {
for (const key in schema.fields) {
const fieldType = schema.fields[key];
if (fieldType instanceof schema_1.ReferenceType) {
const RefModel = ottoman.getModel(fieldType.refModel);
try {
yield RefModel.findById(document[fieldType.name], { transactionContext: options.transactionContext });
}
catch (e) {
if (e instanceof couchbase_1.DocumentNotFoundError) {
switch (enforceRefCheck) {
case true:
console.warn(`Reference to '${fieldType.name}' field with value = '${document[fieldType.name]}' was not found!`);
break;
case 'throw':
throw new ottoman_errors_1.InvalidModelReferenceError(`Reference to '${fieldType.name}' field with value = '${document[fieldType.name]}' was not found!`);
}
}
}
}
}
}
yield (0, exec_hooks_1.execHooks)(schema, 'postHooks', hooks_1.HOOKS.VALIDATE, document);
if (options.cas) {
yield (0, exec_hooks_1.execHooks)(schema, 'preHooks', hooks_1.HOOKS.UPDATE, document);
}
else {
yield (0, exec_hooks_1.execHooks)(schema, 'preHooks', hooks_1.HOOKS.SAVE, document);
}
const result = yield (0, handler_1.store)(key, document, options, _colleciton);
// After storing the document update the index refdocs
yield (0, update_refdoc_indexes_1.updateRefdocIndexes)(refKeys, id, _colleciton, options.transactionContext);
if (options.cas) {
yield (0, exec_hooks_1.execHooks)(schema, 'postHooks', hooks_1.HOOKS.UPDATE, document);
}
else {
yield (0, exec_hooks_1.execHooks)(schema, 'postHooks', hooks_1.HOOKS.SAVE, document);
}
return { result, document };
});
exports.storeLifeCycle = storeLifeCycle;
//# sourceMappingURL=store-life-cycle.js.map