UNPKG

@webiny/api-form-builder-so-ddb-es

Version:

[![](https://img.shields.io/npm/dw/@webiny/api-form-builder-so-ddb-es.svg)](https://www.npmjs.com/package/@webiny/api-form-builder-so-ddb-es) [![](https://img.shields.io/npm/v/@webiny/api-form-builder-so-ddb-es.svg)](https://www.npmjs.com/package/@webiny

207 lines (205 loc) 7.36 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); var _exportNames = { createFormBuilderStorageOperations: true }; exports.createFormBuilderStorageOperations = void 0; var _filters = _interopRequireDefault(require("@webiny/db-dynamodb/plugins/filters")); var _elasticsearchFields = _interopRequireDefault(require("./operations/form/elasticsearchFields")); var _elasticsearchFields2 = _interopRequireDefault(require("./operations/submission/elasticsearchFields")); var _error = _interopRequireDefault(require("@webiny/error")); var _types = require("./types"); var _table = require("./definitions/table"); var _form = require("./definitions/form"); var _submission = require("./definitions/submission"); var _system = require("./definitions/system"); var _settings = require("./definitions/settings"); var _system2 = require("./operations/system"); var _submission2 = require("./operations/submission"); var _settings2 = require("./operations/settings"); var _form2 = require("./operations/form"); var _tableElasticsearch = require("./definitions/tableElasticsearch"); var _plugins = require("@webiny/plugins"); var _elasticsearch = require("./definitions/elasticsearch"); var _apiElasticsearch = require("@webiny/api-elasticsearch"); var _indices = require("./elasticsearch/indices"); var _createElasticsearchIndex = require("./elasticsearch/createElasticsearchIndex"); var _plugins2 = require("./plugins"); Object.keys(_plugins2).forEach(function (key) { if (key === "default" || key === "__esModule") return; if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; if (key in exports && exports[key] === _plugins2[key]) return; Object.defineProperty(exports, key, { enumerable: true, get: function () { return _plugins2[key]; } }); }); var _createIndexTaskPlugin = require("./tasks/createIndexTaskPlugin"); var _api = require("@webiny/api"); const reservedFields = ["PK", "SK", "index", "data", "TYPE", "__type", "GSI1_PK", "GSI1_SK"]; const isReserved = name => { if (reservedFields.includes(name) === false) { return; } throw new _error.default(`Attribute name "${name}" is not allowed.`, "ATTRIBUTE_NOT_ALLOWED", { name }); }; const createFormBuilderStorageOperations = params => { const { attributes, table: tableName, esTable: esTableName, documentClient, elasticsearch, plugins: userPlugins } = params; if (attributes) { Object.values(attributes).forEach(attrs => { Object.keys(attrs).forEach(isReserved); }); } const plugins = new _plugins.PluginsContainer([ /** * User defined plugins. */ userPlugins || [], /** * Elasticsearch field definitions for the submission record. */ (0, _elasticsearchFields2.default)(), /** * Elasticsearch field definitions for the form record. */ (0, _elasticsearchFields.default)(), /** * DynamoDB filter plugins for the where conditions. */ (0, _filters.default)(), /** * Built-in Elasticsearch index plugins */ (0, _indices.elasticsearchIndexPlugins)()]); const table = (0, _table.createTable)({ tableName, documentClient }); const esTable = (0, _tableElasticsearch.createElasticsearchTable)({ tableName: esTableName, documentClient }); const entities = { /** * Regular entities. */ form: (0, _form.createFormEntity)({ entityName: _types.ENTITIES.FORM, table, attributes: attributes ? attributes[_types.ENTITIES.FORM] : {} }), submission: (0, _submission.createSubmissionEntity)({ entityName: _types.ENTITIES.SUBMISSION, table, attributes: attributes ? attributes[_types.ENTITIES.SUBMISSION] : {} }), system: (0, _system.createSystemEntity)({ entityName: _types.ENTITIES.SYSTEM, table, attributes: attributes ? attributes[_types.ENTITIES.SYSTEM] : {} }), settings: (0, _settings.createSettingsEntity)({ entityName: _types.ENTITIES.SETTINGS, table, attributes: attributes ? attributes[_types.ENTITIES.SETTINGS] : {} }), /** * Elasticsearch entities. */ esForm: (0, _elasticsearch.createElasticsearchEntity)({ entityName: _types.ENTITIES.ES_FORM, table: esTable, attributes: attributes ? attributes[_types.ENTITIES.ES_FORM] : {} }), esSubmission: (0, _elasticsearch.createElasticsearchEntity)({ entityName: _types.ENTITIES.ES_SUBMISSION, table: esTable, attributes: attributes ? attributes[_types.ENTITIES.ES_SUBMISSION] : {} }) }; return { beforeInit: async context => { context.db.registry.register({ item: entities.form, app: "fb", tags: ["regular", "form", entities.form.name] }); context.db.registry.register({ item: entities.esForm, app: "fb", tags: ["es", "form", entities.esForm.name] }); context.db.registry.register({ item: entities.submission, app: "fb", tags: ["regular", "form-submission", entities.submission.name] }); context.db.registry.register({ item: entities.esSubmission, app: "fb", tags: ["es", "form-submission", entities.esSubmission.name] }); const types = [_apiElasticsearch.ElasticsearchQueryBuilderOperatorPlugin.type, // Form Builder _plugins2.FormDynamoDbFieldPlugin.type, _plugins2.FormElasticsearchBodyModifierPlugin.type, _plugins2.FormElasticsearchFieldPlugin.type, _plugins2.FormElasticsearchIndexPlugin.type, _plugins2.FormElasticsearchQueryModifierPlugin.type, _plugins2.FormElasticsearchSortModifierPlugin.type, _plugins2.SubmissionElasticsearchBodyModifierPlugin.type, _plugins2.SubmissionElasticsearchFieldPlugin.type, _plugins2.SubmissionElasticsearchQueryModifierPlugin.type, _plugins2.SubmissionElasticsearchSortModifierPlugin.type, _api.CompressorPlugin.type]; for (const type of types) { plugins.mergeByType(context.plugins, type); } context.plugins.register([(0, _createIndexTaskPlugin.createIndexTaskPlugin)(), (0, _indices.elasticsearchIndexPlugins)()]); }, init: async context => { context.i18n.locales.onLocaleBeforeCreate.subscribe(async ({ locale, tenant }) => { await (0, _createElasticsearchIndex.createElasticsearchIndex)({ elasticsearch, plugins, tenant, locale: locale.code }); }); }, getTable: () => table, getEsTable: () => esTable, getEntities: () => entities, ...(0, _system2.createSystemStorageOperations)({ table, entity: entities.system }), ...(0, _settings2.createSettingsStorageOperations)({ table, entity: entities.settings }), ...(0, _form2.createFormStorageOperations)({ elasticsearch, table, entity: entities.form, esEntity: entities.esForm, plugins }), ...(0, _submission2.createSubmissionStorageOperations)({ elasticsearch, table, entity: entities.submission, esEntity: entities.esSubmission, plugins }) }; }; exports.createFormBuilderStorageOperations = createFormBuilderStorageOperations; //# sourceMappingURL=index.js.map