UNPKG

@getanthill/datastore

Version:

Event-Sourced Datastore

311 lines 15.9 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.mergeWithArrays = mergeWithArrays; exports.merge = merge; exports.replaceEntityEncryptedFields = replaceEntityEncryptedFields; exports.replaceEncryptedFields = replaceEncryptedFields; exports.default = buildJsonSchema; exports.mapDateTimeFormatToEitherStringOrObject = mapDateTimeFormatToEitherStringOrObject; const lodash_1 = __importDefault(require("lodash")); const c = __importStar(require("../constants")); const utils_1 = require("../utils"); const PROPERTIES_MODEL_CONFIG_PATH = 'schema.model.properties'; const MERGE_ARRAY_KEYS = Object.freeze(['required', 'indexes']); function mergeWithArrays(objValue, srcValue, key) { if (Array.isArray(objValue) && MERGE_ARRAY_KEYS.includes(key)) { return objValue.concat(srcValue).filter(utils_1.unique); } } function merge(...args) { // @ts-ignore return lodash_1.default.mergeWith.call(null, ...args); } function getDefaultEvents(services, modelConfig, properties) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z; if (modelConfig.with_default_events === false) { return {}; } return { [c.EVENT_TYPE_CREATED]: { '0_0_0': { type: 'object', additionalProperties: (_c = (_b = (_a = modelConfig.schema) === null || _a === void 0 ? void 0 : _a.model) === null || _b === void 0 ? void 0 : _b.additionalProperties) !== null && _c !== void 0 ? _c : false, required: ['type', 'v', ...((_f = (_e = (_d = modelConfig.schema) === null || _d === void 0 ? void 0 : _d.model) === null || _e === void 0 ? void 0 : _e.required) !== null && _f !== void 0 ? _f : [])], properties: { ...properties, ...c.COMPONENT_STATE_PROPERTIES, ...c.COMPONENT_EVENT_PROPERTIES, [services.config.features.properties.is_readonly]: c.COMPONENTS.is_readonly, }, }, }, [c.EVENT_TYPE_UPDATED]: { '0_0_0': { type: 'object', additionalProperties: (_j = (_h = (_g = modelConfig.schema) === null || _g === void 0 ? void 0 : _g.model) === null || _h === void 0 ? void 0 : _h.additionalProperties) !== null && _j !== void 0 ? _j : false, required: ['type', 'v'], properties: { ...properties, ...c.COMPONENT_STATE_PROPERTIES, ...c.COMPONENT_EVENT_PROPERTIES, [services.config.features.properties.is_readonly]: c.COMPONENTS.is_readonly, }, }, }, [c.EVENT_TYPE_PATCHED]: { '0_0_0': { type: 'object', additionalProperties: (_m = (_l = (_k = modelConfig.schema) === null || _k === void 0 ? void 0 : _k.model) === null || _l === void 0 ? void 0 : _l.additionalProperties) !== null && _m !== void 0 ? _m : false, required: ['json_patch'], properties: { json_patch: c.COMPONENT_JSON_PATCH, }, }, }, [c.EVENT_TYPE_ARCHIVED]: { '0_0_0': { type: 'object', additionalProperties: (_q = (_p = (_o = modelConfig.schema) === null || _o === void 0 ? void 0 : _o.model) === null || _p === void 0 ? void 0 : _p.additionalProperties) !== null && _q !== void 0 ? _q : false, properties: { ...properties, [services.config.features.properties.is_readonly]: c.COMPONENTS.is_readonly, [services.config.features.properties.is_archived]: c.COMPONENTS.is_archived, [services.config.features.properties.is_deleted]: c.COMPONENTS.is_deleted, }, }, }, [c.EVENT_TYPE_DELETED]: { '0_0_0': { type: 'object', additionalProperties: (_t = (_s = (_r = modelConfig.schema) === null || _r === void 0 ? void 0 : _r.model) === null || _s === void 0 ? void 0 : _s.additionalProperties) !== null && _t !== void 0 ? _t : false, properties: { ...properties, [services.config.features.properties.is_readonly]: c.COMPONENTS.is_readonly, [services.config.features.properties.is_archived]: c.COMPONENTS.is_archived, [services.config.features.properties.is_deleted]: c.COMPONENTS.is_deleted, }, }, }, [c.EVENT_TYPE_RESTORED]: { '0_0_0': { type: 'object', additionalProperties: (_w = (_v = (_u = modelConfig.schema) === null || _u === void 0 ? void 0 : _u.model) === null || _v === void 0 ? void 0 : _v.additionalProperties) !== null && _w !== void 0 ? _w : false, required: ['type', 'v'], properties: { ...properties, ...c.COMPONENT_STATE_PROPERTIES, ...c.COMPONENT_EVENT_PROPERTIES, [services.config.features.properties.is_readonly]: c.COMPONENTS.is_readonly, [services.config.features.properties.is_archived]: c.COMPONENTS.is_archived, [services.config.features.properties.is_deleted]: c.COMPONENTS.is_deleted, }, }, }, [c.EVENT_TYPE_ROLLBACKED]: { '0_0_0': { type: 'object', additionalProperties: (_z = (_y = (_x = modelConfig.schema) === null || _x === void 0 ? void 0 : _x.model) === null || _y === void 0 ? void 0 : _y.additionalProperties) !== null && _z !== void 0 ? _z : false, required: ['type', 'v'], properties: { ...properties, ...c.COMPONENT_STATE_PROPERTIES, ...c.COMPONENT_EVENT_PROPERTIES, [services.config.features.properties.is_readonly]: c.COMPONENTS.is_readonly, [services.config.features.properties.is_archived]: c.COMPONENTS.is_archived, [services.config.features.properties.is_deleted]: c.COMPONENTS.is_deleted, }, }, }, }; } function extendEventsWithDefaultFields(jsonSchema) { for (const eventType in jsonSchema.events) { for (const eventVersion in jsonSchema.events[eventType]) { lodash_1.default.set(jsonSchema, ['events', eventType, eventVersion], lodash_1.default.mergeWith({}, { type: 'object', additionalProperties: false, required: ['type', 'v'], properties: { type: c.COMPONENTS.type, v: c.COMPONENTS.v, version: c.COMPONENTS.version, json_patch: c.COMPONENT_JSON_PATCH, created_at: c.COMPONENTS.created_at, }, }, jsonSchema.events[eventType][eventVersion], mergeWithArrays)); } } return jsonSchema; } function replaceEventsEncryptedFields(modelConfig, clonedModelConfig) { var _a, _b, _c; for (const eventName in (_a = clonedModelConfig.schema) === null || _a === void 0 ? void 0 : _a.events) { for (const eventVersion in clonedModelConfig.schema.events[eventName]) { for (const field of (_b = modelConfig.encrypted_fields) !== null && _b !== void 0 ? _b : []) { if (lodash_1.default.has(clonedModelConfig.schema.events[eventName][eventVersion].properties, field)) { lodash_1.default.set(clonedModelConfig.schema.events[eventName][eventVersion].properties, field, { description: lodash_1.default.get(clonedModelConfig.schema.events[eventName][eventVersion] .properties, field, { description: '', }).description, anyOf: [ { type: 'object', description: `\`encrypted\` ${(_c = lodash_1.default.get(clonedModelConfig.schema.events[eventName][eventVersion] .properties, field, {}).description) !== null && _c !== void 0 ? _c : ''}`, properties: { hash: { type: 'string', description: '`sha512` value', example: 'b8cccea15437aef415090bda6acb3b0ad3d4cf7d3e4cf816772e4b43e8f9d08af392bb98b8d532e07249f0d1304e6d65e007205c39913ee5db95578be398f4bd', }, encrypted: { type: 'string', description: 'Encrypted value', example: '03d72e:1e9f1a960adfd0815530f7133c97dcd2:648b63622bb5bc3145de4d3f15fe05651ebdedbeab1d11986538214c4a25b834', }, }, }, lodash_1.default.get(clonedModelConfig.schema.events[eventName][eventVersion] .properties, field), ], }); } } } } } function replaceEntityEncryptedFields(modelConfig, clonedModelConfig) { var _a, _b; for (const field of (_a = modelConfig.encrypted_fields) !== null && _a !== void 0 ? _a : []) { lodash_1.default.set(clonedModelConfig.schema.model.properties, field, { description: lodash_1.default.get(clonedModelConfig.schema.model.properties, field, { description: '', }).description, anyOf: [ { type: 'object', description: `\`encrypted\` ${(_b = lodash_1.default.get(clonedModelConfig.schema.model.properties, field, {}) .description) !== null && _b !== void 0 ? _b : ''}`, properties: { hash: { type: 'string', description: '`sha512` value', example: 'b8cccea15437aef415090bda6acb3b0ad3d4cf7d3e4cf816772e4b43e8f9d08af392bb98b8d532e07249f0d1304e6d65e007205c39913ee5db95578be398f4bd', }, encrypted: { type: 'string', description: 'Encrypted value', example: '03d72e:1e9f1a960adfd0815530f7133c97dcd2:648b63622bb5bc3145de4d3f15fe05651ebdedbeab1d11986538214c4a25b834', }, }, }, lodash_1.default.get(clonedModelConfig.schema.model.properties, field), ], }); } return clonedModelConfig; } function replaceEncryptedFields(modelConfig) { const clonedModelConfig = lodash_1.default.cloneDeep(modelConfig); replaceEntityEncryptedFields(modelConfig, clonedModelConfig); replaceEventsEncryptedFields(modelConfig, clonedModelConfig); return clonedModelConfig; } function buildJsonSchema(services, modelConfig) { var _a, _b, _c, _d, _e, _f, _g, _h; const extendedModelConfig = replaceEncryptedFields(modelConfig); const correlationField = extendedModelConfig.correlation_field || c.DEFAULT_CORRELATION_FIELD; const properties = { [correlationField]: c.COMPONENT_CORRELATION_ID, ...lodash_1.default.get(extendedModelConfig, PROPERTIES_MODEL_CONFIG_PATH, {}), }; if (extendedModelConfig.with_blockchain_hash === true) { properties[(_a = extendedModelConfig.current_hash_field) !== null && _a !== void 0 ? _a : 'hash'] = { type: 'string', description: 'Entity hash', }; properties[(_b = extendedModelConfig.previous_hash_field) !== null && _b !== void 0 ? _b : 'prev'] = { type: 'string', description: 'Previous entity hash', }; properties[(_c = extendedModelConfig.nonce_field) !== null && _c !== void 0 ? _c : 'nonce'] = { type: 'integer', description: 'Nonce value', }; } const modelJsonSchema = { $id: 'events', components: c.COMPONENTS, retry_duration: 0, model: { type: 'object', additionalProperties: (_f = (_e = (_d = extendedModelConfig.schema) === null || _d === void 0 ? void 0 : _d.model) === null || _e === void 0 ? void 0 : _e.additionalProperties) !== null && _f !== void 0 ? _f : false, required: (_h = (_g = extendedModelConfig.schema) === null || _g === void 0 ? void 0 : _g.model) === null || _h === void 0 ? void 0 : _h.required, properties: { ...properties, version: c.COMPONENT_EVENT_VERSION, created_at: c.COMPONENTS.created_at, updated_at: c.COMPONENTS.updated_at, [services.config.features.properties.is_readonly]: c.COMPONENTS.is_readonly, [services.config.features.properties.is_archived]: c.COMPONENTS.is_archived, [services.config.features.properties.is_deleted]: c.COMPONENTS.is_deleted, }, }, events: getDefaultEvents(services, extendedModelConfig, properties), }; return extendEventsWithDefaultFields(lodash_1.default.mergeWith({}, modelJsonSchema, extendedModelConfig.schema, mergeWithArrays)); } function mapDateTimeFormatToEitherStringOrObject(schema) { return lodash_1.default.mergeWith({}, schema, (src, obj) => { var _a; if ((obj === null || obj === void 0 ? void 0 : obj.type) && ((_a = obj === null || obj === void 0 ? void 0 : obj.format) === null || _a === void 0 ? void 0 : _a.startsWith('date'))) { return { oneOf: [ obj, { ...obj, type: 'object', }, ], }; } }); } //# sourceMappingURL=schema.js.map