@webiny/api-headless-cms-ddb-es 5.15.0-beta.3 → 5.16.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/configurations.js
CHANGED
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
const configurations = {
|
|
8
8
|
db: () => ({
|
|
9
|
-
table: process.env.DB_TABLE_HEADLESS_CMS,
|
|
9
|
+
table: process.env.DB_TABLE_HEADLESS_CMS || process.env.DB_TABLE,
|
|
10
10
|
keys: [{
|
|
11
11
|
primary: true,
|
|
12
12
|
unique: true,
|
package/configurations.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/configurations.ts"],"names":["configurations","db","table","process","env","DB_TABLE_HEADLESS_CMS","keys","primary","unique","name","fields","esDb","DB_TABLE_HEADLESS_CMS_ELASTICSEARCH","DB_TABLE_ELASTICSEARCH","es","context","model","tenant","tenancy","getCurrentTenant","Error","sharedIndex","ELASTICSEARCH_SHARED_INDEXES","locale","cms","getLocale","code","index","id","modelId","join","toLowerCase","prefix","ELASTIC_SEARCH_INDEX_PREFIX"],"mappings":";;;;;;AA4BA,MAAMA,cAA8B,GAAG;AACnCC,EAAAA,EAAE,EAAE,OAAO;AACPC,IAAAA,KAAK,EAAEC,OAAO,CAACC,GAAR,CAAYC,
|
|
1
|
+
{"version":3,"sources":["../src/configurations.ts"],"names":["configurations","db","table","process","env","DB_TABLE_HEADLESS_CMS","DB_TABLE","keys","primary","unique","name","fields","esDb","DB_TABLE_HEADLESS_CMS_ELASTICSEARCH","DB_TABLE_ELASTICSEARCH","es","context","model","tenant","tenancy","getCurrentTenant","Error","sharedIndex","ELASTICSEARCH_SHARED_INDEXES","locale","cms","getLocale","code","index","id","modelId","join","toLowerCase","prefix","ELASTIC_SEARCH_INDEX_PREFIX"],"mappings":";;;;;;AA4BA,MAAMA,cAA8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YJ,MAAM,CAACW,EAA/B,EAAmC,cAAnC,EAAmDL,MAAnD,EAA2DP,KAAK,CAACa,OAAjE,EACTC,IADS,CACJ,GADI,EAETC,WAFS,EAAd;AAIA,UAAMC,MAAM,GAAG9B,OAAO,CAACC,GAAR,CAAY8B,2BAA3B;;AACA,QAAID,MAAJ,EAAY;AACR,aAAO;AAAEL,QAAAA,KAAK,EAAEK,MAAM,GAAGL;AAAlB,OAAP;AACH;;AACD,WAAO;AAAEA,MAAAA;AAAF,KAAP;AACH;;AAzCkC,CAAvC;eA4Ce5B,c","sourcesContent":["import { CmsContentModel, CmsContext } from \"@webiny/api-headless-cms/types\";\n\ninterface DatabaseConfigKeyFields {\n name: string;\n}\n\ninterface DatabaseConfigKeys {\n primary: boolean;\n unique: boolean;\n name: string;\n fields: DatabaseConfigKeyFields[];\n}\n\nexport interface CmsDatabaseConfig {\n table: string;\n keys: DatabaseConfigKeys[];\n}\n\ninterface ElasticsearchConfig {\n index: string;\n}\n\ninterface Configurations {\n db: () => CmsDatabaseConfig;\n esDb: () => CmsDatabaseConfig;\n es: (context: CmsContext, model: CmsContentModel) => ElasticsearchConfig;\n}\n\nconst configurations: Configurations = {\n db: () => ({\n table: process.env.DB_TABLE_HEADLESS_CMS || process.env.DB_TABLE,\n keys: [\n {\n primary: true,\n unique: true,\n name: \"primary\",\n fields: [{ name: \"PK\" }, { name: \"SK\" }]\n }\n ]\n }),\n esDb: () => ({\n table:\n process.env.DB_TABLE_HEADLESS_CMS_ELASTICSEARCH || process.env.DB_TABLE_ELASTICSEARCH,\n keys: [\n {\n primary: true,\n unique: true,\n name: \"primary\",\n fields: [{ name: \"PK\" }, { name: \"SK\" }]\n }\n ]\n }),\n es(context, model) {\n const tenant = context.tenancy.getCurrentTenant();\n if (!tenant) {\n throw new Error(`There is no tenant on \"context.security\".`);\n }\n\n const sharedIndex = process.env.ELASTICSEARCH_SHARED_INDEXES === \"true\";\n const locale = context.cms.getLocale().code;\n const index = [sharedIndex ? \"root\" : tenant.id, \"headless-cms\", locale, model.modelId]\n .join(\"-\")\n .toLowerCase();\n\n const prefix = process.env.ELASTIC_SEARCH_INDEX_PREFIX;\n if (prefix) {\n return { index: prefix + index };\n }\n return { index };\n }\n};\n\nexport default configurations;\n"],"file":"configurations.js"}
|
|
@@ -25,29 +25,10 @@ var _fields = require("./fields");
|
|
|
25
25
|
|
|
26
26
|
var _CmsEntryElasticsearchFieldPlugin = require("../plugins/CmsEntryElasticsearchFieldPlugin");
|
|
27
27
|
|
|
28
|
+
var _where = require("@webiny/api-elasticsearch/where");
|
|
29
|
+
|
|
28
30
|
const specialFields = ["published", "latest"];
|
|
29
31
|
const noKeywordFields = ["date", "number", "boolean"];
|
|
30
|
-
const parseWhereKeyRegExp = new RegExp(/^([a-zA-Z0-9]+)(_[a-zA-Z0-9_]+)?$/);
|
|
31
|
-
|
|
32
|
-
const parseWhereKey = key => {
|
|
33
|
-
const match = key.match(parseWhereKeyRegExp);
|
|
34
|
-
|
|
35
|
-
if (!match) {
|
|
36
|
-
throw new Error(`It is not possible to search by key "${key}"`);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const [, field, operation = "eq"] = match;
|
|
40
|
-
const op = operation.match(/^_/) ? operation.substr(1) : operation;
|
|
41
|
-
|
|
42
|
-
if (!field.match(/^([a-zA-Z]+)$/)) {
|
|
43
|
-
throw new Error(`Cannot filter by "${field}".`);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
field,
|
|
48
|
-
op
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
32
|
|
|
52
33
|
const createElasticsearchSortParams = args => {
|
|
53
34
|
const {
|
|
@@ -80,8 +61,7 @@ const createElasticsearchSortParams = args => {
|
|
|
80
61
|
return plugins;
|
|
81
62
|
}, {});
|
|
82
63
|
return (0, _sort.createSort)({
|
|
83
|
-
|
|
84
|
-
plugins: sortPlugins,
|
|
64
|
+
fieldPlugins: sortPlugins,
|
|
85
65
|
sort
|
|
86
66
|
});
|
|
87
67
|
};
|
|
@@ -238,8 +218,8 @@ const execElasticsearchBuildQueryPlugins = args => {
|
|
|
238
218
|
|
|
239
219
|
const {
|
|
240
220
|
field,
|
|
241
|
-
|
|
242
|
-
} = parseWhereKey(key);
|
|
221
|
+
operator
|
|
222
|
+
} = (0, _where.parseWhereKey)(key);
|
|
243
223
|
const modelField = modelFields[field];
|
|
244
224
|
|
|
245
225
|
if (!modelField) {
|
|
@@ -255,11 +235,11 @@ const execElasticsearchBuildQueryPlugins = args => {
|
|
|
255
235
|
throw new _error.default(`Field "${field}" is not searchable.`);
|
|
256
236
|
}
|
|
257
237
|
|
|
258
|
-
const plugin = operatorPlugins[
|
|
238
|
+
const plugin = operatorPlugins[operator];
|
|
259
239
|
|
|
260
240
|
if (!plugin) {
|
|
261
241
|
throw new _error.default("Operator plugin missing.", "PLUGIN_MISSING", {
|
|
262
|
-
operator
|
|
242
|
+
operator
|
|
263
243
|
});
|
|
264
244
|
}
|
|
265
245
|
|
|
@@ -281,7 +261,6 @@ const execElasticsearchBuildQueryPlugins = args => {
|
|
|
281
261
|
basePath: fieldPath,
|
|
282
262
|
path: keyword ? `${fieldPath}.keyword` : fieldPath,
|
|
283
263
|
value,
|
|
284
|
-
context,
|
|
285
264
|
keyword
|
|
286
265
|
});
|
|
287
266
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/helpers/createElasticsearchQueryBody.ts"],"names":["specialFields","noKeywordFields","parseWhereKeyRegExp","RegExp","parseWhereKey","key","match","Error","field","operation","op","substr","createElasticsearchSortParams","args","context","sort","modelFields","parentPath","searchPlugins","length","undefined","sortPlugins","Object","values","reduce","plugins","modelField","searchPlugin","type","fieldId","CmsEntryElasticsearchFieldPlugin","unmappedType","keyword","hasKeyword","sortable","isSortable","searchable","isSearchable","path","createFieldPath","createInitialQueryValue","where","query","must","must_not","should","filter","sharedIndex","process","env","ELASTICSEARCH_SHARED_INDEXES","tenant","tenancy","getCurrentTenant","push","term","id","published","TYPE_ENTRY_PUBLISHED","latest","TYPE_ENTRY_LATEST","WebinyError","createPath","isSystemField","includes","execElasticsearchBuildQueryPlugins","sf","keys","operatorPlugins","hasOwnProperty","cmsField","plugin","operator","fieldSearchPlugin","value","fieldPath","apply","basePath","createElasticsearchQueryBody","params","model","after","limit","queryPlugins","byType","pl","modify","bool","size","search_after","track_total_hits"],"mappings":";;;;;;;;;AAAA;;AACA;;AACA;;AACA;;AASA;;AASA;;AACA;;AACA;;AACA;;AA2BA,MAAMA,aAAa,GAAG,CAAC,WAAD,EAAc,QAAd,CAAtB;AACA,MAAMC,eAAe,GAAG,CAAC,MAAD,EAAS,QAAT,EAAmB,SAAnB,CAAxB;AAEA,MAAMC,mBAAmB,GAAG,IAAIC,MAAJ,CAAW,mCAAX,CAA5B;;AAEA,MAAMC,aAAa,GAAIC,GAAD,IAAiB;AACnC,QAAMC,KAAK,GAAGD,GAAG,CAACC,KAAJ,CAAUJ,mBAAV,CAAd;;AAEA,MAAI,CAACI,KAAL,EAAY;AACR,UAAM,IAAIC,KAAJ,CAAW,wCAAuCF,GAAI,GAAtD,CAAN;AACH;;AAED,QAAM,GAAGG,KAAH,EAAUC,SAAS,GAAG,IAAtB,IAA8BH,KAApC;AACA,QAAMI,EAAE,GAAGD,SAAS,CAACH,KAAV,CAAgB,IAAhB,IAAwBG,SAAS,CAACE,MAAV,CAAiB,CAAjB,CAAxB,GAA8CF,SAAzD;;AAEA,MAAI,CAACD,KAAK,CAACF,KAAN,CAAY,eAAZ,CAAL,EAAmC;AAC/B,UAAM,IAAIC,KAAJ,CAAW,qBAAoBC,KAAM,IAArC,CAAN;AACH;;AAED,SAAO;AAAEA,IAAAA,KAAF;AAASE,IAAAA;AAAT,GAAP;AACH,CAfD;;AAiBA,MAAME,6BAA6B,GAAIC,IAAD,IAAiD;AACnF,QAAM;AAAEC,IAAAA,OAAF;AAAWC,IAAAA,IAAX;AAAiBC,IAAAA,WAAjB;AAA8BC,IAAAA,U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;AAIhCC,MAAAA,UAJgC;AAKhC+D,MAAAA,KALgC;AAMhC9D,MAAAA;AANgC,KAAD,CAThC;AAiBHsE,IAAAA,IAAI,EAAEN,KAAK,GAAG,CAjBX;AAkBH;AACAO,IAAAA,YAAY,EAAE,2BAAaR,KAAb,CAnBX;AAoBH;AACAS,IAAAA,gBAAgB,EAAE;AArBf,GAAP;AAuBH,CA7CM","sourcesContent":["import WebinyError from \"@webiny/error\";\nimport { operatorPluginsList } from \"./operatorPluginsList\";\nimport { transformValueForSearch } from \"./transformValueForSearch\";\nimport { searchPluginsList } from \"./searchPluginsList\";\nimport {\n CmsContentEntryListArgs,\n CmsContentEntryListSort,\n CmsContentEntryListWhere,\n CmsContentModel,\n CmsContext\n} from \"@webiny/api-headless-cms/types\";\nimport { ElasticsearchQueryBuilderValueSearchPlugin, ElasticsearchQueryPlugin } from \"~/types\";\nimport {\n TYPE_ENTRY_LATEST,\n TYPE_ENTRY_PUBLISHED\n} from \"~/operations/entry/CmsContentEntryDynamoElastic\";\nimport {\n SearchBody as esSearchBody,\n Sort as esSort,\n ElasticsearchBoolQueryConfig\n} from \"@webiny/api-elasticsearch/types\";\nimport { decodeCursor } from \"@webiny/api-elasticsearch/cursors\";\nimport { createSort } from \"@webiny/api-elasticsearch/sort\";\nimport { createModelFields, ModelField, ModelFields } from \"./fields\";\nimport { CmsEntryElasticsearchFieldPlugin } from \"~/plugins/CmsEntryElasticsearchFieldPlugin\";\n\ninterface CreateElasticsearchParams {\n context: CmsContext;\n model: CmsContentModel;\n args: CmsContentEntryListArgs;\n parentPath?: string;\n}\n\ninterface CreateElasticsearchSortParams {\n context: CmsContext;\n sort?: CmsContentEntryListSort;\n modelFields: ModelFields;\n parentPath?: string;\n model: CmsContentModel;\n searchPlugins: Record<string, ElasticsearchQueryBuilderValueSearchPlugin>;\n}\n\ninterface CreateElasticsearchQueryArgs {\n model: CmsContentModel;\n context: CmsContext;\n where: CmsContentEntryListWhere;\n modelFields: ModelFields;\n parentPath?: string;\n searchPlugins: Record<string, ElasticsearchQueryBuilderValueSearchPlugin>;\n}\n\nconst specialFields = [\"published\", \"latest\"];\nconst noKeywordFields = [\"date\", \"number\", \"boolean\"];\n\nconst parseWhereKeyRegExp = new RegExp(/^([a-zA-Z0-9]+)(_[a-zA-Z0-9_]+)?$/);\n\nconst parseWhereKey = (key: string) => {\n const match = key.match(parseWhereKeyRegExp);\n\n if (!match) {\n throw new Error(`It is not possible to search by key \"${key}\"`);\n }\n\n const [, field, operation = \"eq\"] = match;\n const op = operation.match(/^_/) ? operation.substr(1) : operation;\n\n if (!field.match(/^([a-zA-Z]+)$/)) {\n throw new Error(`Cannot filter by \"${field}\".`);\n }\n\n return { field, op };\n};\n\nconst createElasticsearchSortParams = (args: CreateElasticsearchSortParams): esSort => {\n const { context, sort, modelFields, parentPath, searchPlugins } = args;\n\n if (!sort || sort.length === 0) {\n return undefined;\n }\n\n const sortPlugins = Object.values(modelFields).reduce((plugins, modelField) => {\n const searchPlugin = searchPlugins[modelField.type];\n\n plugins[modelField.field.fieldId] = new CmsEntryElasticsearchFieldPlugin({\n unmappedType: modelField.unmappedType,\n keyword: hasKeyword(modelField),\n sortable: modelField.isSortable,\n searchable: modelField.isSearchable,\n field: modelField.field.fieldId,\n path: createFieldPath({\n context,\n parentPath,\n modelField: modelField,\n searchPlugin\n })\n });\n return plugins;\n }, {});\n\n return createSort({\n context,\n plugins: sortPlugins,\n sort\n });\n};\n/**\n * Latest and published are specific in Elasticsearch to that extend that they are tagged in the __type property.\n * We allow either published or either latest.\n * Latest is used in the manage API and published in the read API.\n */\nconst createInitialQueryValue = (\n args: CreateElasticsearchQueryArgs\n): ElasticsearchBoolQueryConfig => {\n const { where, context } = args;\n\n const query: ElasticsearchBoolQueryConfig = {\n must: [],\n must_not: [],\n should: [],\n filter: []\n };\n\n // When ES index is shared between tenants, we need to filter records by tenant ID\n const sharedIndex = process.env.ELASTICSEARCH_SHARED_INDEXES === \"true\";\n if (sharedIndex) {\n const tenant = context.tenancy.getCurrentTenant();\n query.must.push({ term: { \"tenant.keyword\": tenant.id } });\n }\n /**\n * We must transform published and latest where args into something that is understandable by our Elasticsearch\n */\n if (where.published === true) {\n query.must.push({\n term: {\n \"__type.keyword\": TYPE_ENTRY_PUBLISHED\n }\n });\n } else if (where.latest === true) {\n query.must.push({\n term: {\n \"__type.keyword\": TYPE_ENTRY_LATEST\n }\n });\n }\n // we do not allow not published and not latest\n else if (where.published === false) {\n throw new WebinyError(\n `Cannot call Elasticsearch query with \"published\" set at false.`,\n \"ELASTICSEARCH_UNSUPPORTED_QUERY\",\n {\n where\n }\n );\n } else if (where.latest === false) {\n throw new WebinyError(\n `Cannot call Elasticsearch query with \"latest\" set at false.`,\n \"ELASTICSEARCH_UNSUPPORTED_QUERY\",\n {\n where\n }\n );\n }\n //\n return query;\n};\n\ninterface CreateFieldPathParams {\n modelField: ModelField;\n searchPlugin?: ElasticsearchQueryBuilderValueSearchPlugin;\n context: CmsContext;\n parentPath?: string;\n}\nconst createFieldPath = ({\n modelField,\n searchPlugin,\n context,\n parentPath\n}: CreateFieldPathParams): string => {\n let path;\n if (searchPlugin && typeof searchPlugin.createPath === \"function\") {\n path = searchPlugin.createPath({\n field: modelField.field,\n context\n });\n } else if (typeof modelField.path === \"function\") {\n path = modelField.path(modelField.field.fieldId);\n }\n if (!path) {\n path = modelField.path || modelField.field.fieldId || modelField.field.id;\n }\n return modelField.isSystemField || !parentPath || path.match(parentPath)\n ? path\n : `${parentPath}.${path}`;\n};\n\nconst hasKeyword = (modelField: ModelField): boolean => {\n /**\n * We defined some field types that MUST have no keyword added to the field path\n */\n if (noKeywordFields.includes(modelField.type)) {\n return false;\n } else if (modelField.unmappedType) {\n /**\n * If modelField has unmapped type defined, do not add keyword.\n */\n return false;\n } else if (modelField.keyword === false) {\n /**\n * And if specifically defined that modelField has no keyword, do not add it.\n */\n return false;\n }\n /**\n * All other fields have keyword added.\n */\n return true;\n};\n/*\n * Iterate through where keys and apply plugins where necessary\n */\nconst execElasticsearchBuildQueryPlugins = (\n args: CreateElasticsearchQueryArgs\n): ElasticsearchBoolQueryConfig => {\n const { where, modelFields, parentPath, context, searchPlugins } = args;\n const query = createInitialQueryValue(args);\n\n /**\n * Always remove special fields, as these do not exist in Elasticsearch.\n */\n for (const sf of specialFields) {\n delete where[sf];\n }\n\n if (!where || Object.keys(where).length === 0) {\n return query;\n }\n\n const operatorPlugins = operatorPluginsList(context);\n\n for (const key in where) {\n if (where.hasOwnProperty(key) === false) {\n continue;\n }\n /**\n * We do not need to go further if value is undefined.\n * There are few hardcoded possibilities when value is undefined, for example, ownedBy.\n */\n if (where[key] === undefined) {\n continue;\n }\n const { field, op } = parseWhereKey(key);\n const modelField = modelFields[field];\n\n if (!modelField) {\n throw new WebinyError(`There is no field \"${field}\".`);\n }\n const { isSearchable = false, field: cmsField } = modelField;\n if (!isSearchable) {\n throw new WebinyError(`Field \"${field}\" is not searchable.`);\n }\n const plugin = operatorPlugins[op];\n if (!plugin) {\n throw new WebinyError(\"Operator plugin missing.\", \"PLUGIN_MISSING\", {\n operator: op\n });\n }\n const fieldSearchPlugin = searchPlugins[modelField.type];\n const value = transformValueForSearch({\n plugins: searchPlugins,\n field: cmsField,\n value: where[key],\n context\n });\n\n const fieldPath = createFieldPath({\n context,\n searchPlugin: fieldSearchPlugin,\n modelField,\n parentPath: parentPath\n });\n const keyword = hasKeyword(modelField);\n plugin.apply(query, {\n basePath: fieldPath,\n path: keyword ? `${fieldPath}.keyword` : fieldPath,\n value,\n context,\n keyword\n });\n }\n\n return query;\n};\n\nexport const createElasticsearchQueryBody = (params: CreateElasticsearchParams): esSearchBody => {\n const { context, model, args, parentPath = null } = params;\n const { where, after, limit, sort } = args;\n\n const modelFields = createModelFields(context, model);\n const searchPlugins = searchPluginsList(context);\n\n const query = execElasticsearchBuildQueryPlugins({\n model,\n context,\n where,\n modelFields,\n parentPath,\n searchPlugins\n });\n\n const queryPlugins =\n context.plugins.byType<ElasticsearchQueryPlugin>(\"cms-elasticsearch-query\");\n for (const pl of queryPlugins) {\n pl.modify({ query, model, context });\n }\n\n return {\n query: {\n bool: {\n must: query.must.length > 0 ? query.must : undefined,\n must_not: query.must_not.length > 0 ? query.must_not : undefined,\n should: query.should.length > 0 ? query.should : undefined,\n filter: query.filter.length > 0 ? query.filter : undefined\n }\n },\n sort: createElasticsearchSortParams({\n context,\n sort,\n modelFields,\n parentPath,\n model,\n searchPlugins\n }),\n size: limit + 1,\n // eslint-disable-next-line\n search_after: decodeCursor(after) as any,\n // eslint-disable-next-line\n track_total_hits: true\n };\n};\n"],"file":"createElasticsearchQueryBody.js"}
|
|
1
|
+
{"version":3,"sources":["../../src/helpers/createElasticsearchQueryBody.ts"],"names":["specialFields","noKeywordFields","createElasticsearchSortParams","args","context","sort","modelFields","parentPath","searchPlugins","length","undefined","sortPlugins","Object","values","reduce","plugins","modelField","searchPlugin","type","field","fieldId","CmsEntryElasticsearchFieldPlugin","unmappedType","keyword","hasKeyword","sortable","isSortable","searchable","isSearchable","path","createFieldPath","fieldPlugins","createInitialQueryValue","where","query","must","must_not","should","filter","sharedIndex","process","env","ELASTICSEARCH_SHARED_INDEXES","tenant","tenancy","getCurrentTenant","push","term","id","published","TYPE_ENTRY_PUBLISHED","latest","TYPE_ENTRY_LATEST","WebinyError","createPath","isSystemField","match","includes","execElasticsearchBuildQueryPlugins","sf","keys","operatorPlugins","key","hasOwnProperty","operator","cmsField","plugin","fieldSearchPlugin","value","fieldPath","apply","basePath","createElasticsearchQueryBody","params","model","after","limit","queryPlugins","byType","pl","modify","bool","size","search_after","track_total_hits"],"mappings":";;;;;;;;;AAAA;;AACA;;AACA;;AACA;;AASA;;AASA;;AACA;;AACA;;AACA;;AACA;;AA2BA,MAAMA,aAAa,GAAG,CAAC,WAAD,EAAc,QAAd,CAAtB;AACA,MAAMC,eAAe,GAAG,CAAC,MAAD,EAAS,QAAT,EAAmB,SAAnB,CAAxB;;AAEA,MAAMC,6BAA6B,GAAIC,IAAD,IAAi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,MAAAA,WAHgC;AAIhCC,MAAAA,UAJgC;AAKhCmE,MAAAA,KALgC;AAMhClE,MAAAA;AANgC,KAAD,CAThC;AAiBH0E,IAAAA,IAAI,EAAEN,KAAK,GAAG,CAjBX;AAkBH;AACAO,IAAAA,YAAY,EAAE,2BAAaR,KAAb,CAnBX;AAoBH;AACAS,IAAAA,gBAAgB,EAAE;AArBf,GAAP;AAuBH,CA7CM","sourcesContent":["import WebinyError from \"@webiny/error\";\nimport { operatorPluginsList } from \"./operatorPluginsList\";\nimport { transformValueForSearch } from \"./transformValueForSearch\";\nimport { searchPluginsList } from \"./searchPluginsList\";\nimport {\n CmsContentEntryListArgs,\n CmsContentEntryListSort,\n CmsContentEntryListWhere,\n CmsContentModel,\n CmsContext\n} from \"@webiny/api-headless-cms/types\";\nimport { ElasticsearchQueryBuilderValueSearchPlugin, ElasticsearchQueryPlugin } from \"~/types\";\nimport {\n TYPE_ENTRY_LATEST,\n TYPE_ENTRY_PUBLISHED\n} from \"~/operations/entry/CmsContentEntryDynamoElastic\";\nimport {\n SearchBody as esSearchBody,\n Sort as esSort,\n ElasticsearchBoolQueryConfig\n} from \"@webiny/api-elasticsearch/types\";\nimport { decodeCursor } from \"@webiny/api-elasticsearch/cursors\";\nimport { createSort } from \"@webiny/api-elasticsearch/sort\";\nimport { createModelFields, ModelField, ModelFields } from \"./fields\";\nimport { CmsEntryElasticsearchFieldPlugin } from \"~/plugins/CmsEntryElasticsearchFieldPlugin\";\nimport { parseWhereKey } from \"@webiny/api-elasticsearch/where\";\n\ninterface CreateElasticsearchParams {\n context: CmsContext;\n model: CmsContentModel;\n args: CmsContentEntryListArgs;\n parentPath?: string;\n}\n\ninterface CreateElasticsearchSortParams {\n context: CmsContext;\n sort?: CmsContentEntryListSort;\n modelFields: ModelFields;\n parentPath?: string;\n model: CmsContentModel;\n searchPlugins: Record<string, ElasticsearchQueryBuilderValueSearchPlugin>;\n}\n\ninterface CreateElasticsearchQueryArgs {\n model: CmsContentModel;\n context: CmsContext;\n where: CmsContentEntryListWhere;\n modelFields: ModelFields;\n parentPath?: string;\n searchPlugins: Record<string, ElasticsearchQueryBuilderValueSearchPlugin>;\n}\n\nconst specialFields = [\"published\", \"latest\"];\nconst noKeywordFields = [\"date\", \"number\", \"boolean\"];\n\nconst createElasticsearchSortParams = (args: CreateElasticsearchSortParams): esSort => {\n const { context, sort, modelFields, parentPath, searchPlugins } = args;\n\n if (!sort || sort.length === 0) {\n return undefined;\n }\n\n const sortPlugins: Record<string, CmsEntryElasticsearchFieldPlugin> = Object.values(\n modelFields\n ).reduce((plugins, modelField) => {\n const searchPlugin = searchPlugins[modelField.type];\n\n plugins[modelField.field.fieldId] = new CmsEntryElasticsearchFieldPlugin({\n unmappedType: modelField.unmappedType,\n keyword: hasKeyword(modelField),\n sortable: modelField.isSortable,\n searchable: modelField.isSearchable,\n field: modelField.field.fieldId,\n path: createFieldPath({\n context,\n parentPath,\n modelField: modelField,\n searchPlugin\n })\n });\n return plugins;\n }, {});\n\n return createSort({\n fieldPlugins: sortPlugins,\n sort\n });\n};\n/**\n * Latest and published are specific in Elasticsearch to that extend that they are tagged in the __type property.\n * We allow either published or either latest.\n * Latest is used in the manage API and published in the read API.\n */\nconst createInitialQueryValue = (\n args: CreateElasticsearchQueryArgs\n): ElasticsearchBoolQueryConfig => {\n const { where, context } = args;\n\n const query: ElasticsearchBoolQueryConfig = {\n must: [],\n must_not: [],\n should: [],\n filter: []\n };\n\n // When ES index is shared between tenants, we need to filter records by tenant ID\n const sharedIndex = process.env.ELASTICSEARCH_SHARED_INDEXES === \"true\";\n if (sharedIndex) {\n const tenant = context.tenancy.getCurrentTenant();\n query.must.push({ term: { \"tenant.keyword\": tenant.id } });\n }\n /**\n * We must transform published and latest where args into something that is understandable by our Elasticsearch\n */\n if (where.published === true) {\n query.must.push({\n term: {\n \"__type.keyword\": TYPE_ENTRY_PUBLISHED\n }\n });\n } else if (where.latest === true) {\n query.must.push({\n term: {\n \"__type.keyword\": TYPE_ENTRY_LATEST\n }\n });\n }\n // we do not allow not published and not latest\n else if (where.published === false) {\n throw new WebinyError(\n `Cannot call Elasticsearch query with \"published\" set at false.`,\n \"ELASTICSEARCH_UNSUPPORTED_QUERY\",\n {\n where\n }\n );\n } else if (where.latest === false) {\n throw new WebinyError(\n `Cannot call Elasticsearch query with \"latest\" set at false.`,\n \"ELASTICSEARCH_UNSUPPORTED_QUERY\",\n {\n where\n }\n );\n }\n //\n return query;\n};\n\ninterface CreateFieldPathParams {\n modelField: ModelField;\n searchPlugin?: ElasticsearchQueryBuilderValueSearchPlugin;\n context: CmsContext;\n parentPath?: string;\n}\nconst createFieldPath = ({\n modelField,\n searchPlugin,\n context,\n parentPath\n}: CreateFieldPathParams): string => {\n let path;\n if (searchPlugin && typeof searchPlugin.createPath === \"function\") {\n path = searchPlugin.createPath({\n field: modelField.field,\n context\n });\n } else if (typeof modelField.path === \"function\") {\n path = modelField.path(modelField.field.fieldId);\n }\n if (!path) {\n path = modelField.path || modelField.field.fieldId || modelField.field.id;\n }\n return modelField.isSystemField || !parentPath || path.match(parentPath)\n ? path\n : `${parentPath}.${path}`;\n};\n\nconst hasKeyword = (modelField: ModelField): boolean => {\n /**\n * We defined some field types that MUST have no keyword added to the field path\n */\n if (noKeywordFields.includes(modelField.type)) {\n return false;\n } else if (modelField.unmappedType) {\n /**\n * If modelField has unmapped type defined, do not add keyword.\n */\n return false;\n } else if (modelField.keyword === false) {\n /**\n * And if specifically defined that modelField has no keyword, do not add it.\n */\n return false;\n }\n /**\n * All other fields have keyword added.\n */\n return true;\n};\n/*\n * Iterate through where keys and apply plugins where necessary\n */\nconst execElasticsearchBuildQueryPlugins = (\n args: CreateElasticsearchQueryArgs\n): ElasticsearchBoolQueryConfig => {\n const { where, modelFields, parentPath, context, searchPlugins } = args;\n const query = createInitialQueryValue(args);\n\n /**\n * Always remove special fields, as these do not exist in Elasticsearch.\n */\n for (const sf of specialFields) {\n delete where[sf];\n }\n\n if (!where || Object.keys(where).length === 0) {\n return query;\n }\n\n const operatorPlugins = operatorPluginsList(context);\n\n for (const key in where) {\n if (where.hasOwnProperty(key) === false) {\n continue;\n }\n /**\n * We do not need to go further if value is undefined.\n * There are few hardcoded possibilities when value is undefined, for example, ownedBy.\n */\n if (where[key] === undefined) {\n continue;\n }\n const { field, operator } = parseWhereKey(key);\n const modelField = modelFields[field];\n\n if (!modelField) {\n throw new WebinyError(`There is no field \"${field}\".`);\n }\n const { isSearchable = false, field: cmsField } = modelField;\n if (!isSearchable) {\n throw new WebinyError(`Field \"${field}\" is not searchable.`);\n }\n const plugin = operatorPlugins[operator];\n if (!plugin) {\n throw new WebinyError(\"Operator plugin missing.\", \"PLUGIN_MISSING\", {\n operator\n });\n }\n const fieldSearchPlugin = searchPlugins[modelField.type];\n const value = transformValueForSearch({\n plugins: searchPlugins,\n field: cmsField,\n value: where[key],\n context\n });\n\n const fieldPath = createFieldPath({\n context,\n searchPlugin: fieldSearchPlugin,\n modelField,\n parentPath: parentPath\n });\n const keyword = hasKeyword(modelField);\n plugin.apply(query, {\n basePath: fieldPath,\n path: keyword ? `${fieldPath}.keyword` : fieldPath,\n value,\n keyword\n });\n }\n\n return query;\n};\n\nexport const createElasticsearchQueryBody = (params: CreateElasticsearchParams): esSearchBody => {\n const { context, model, args, parentPath = null } = params;\n const { where, after, limit, sort } = args;\n\n const modelFields = createModelFields(context, model);\n const searchPlugins = searchPluginsList(context);\n\n const query = execElasticsearchBuildQueryPlugins({\n model,\n context,\n where,\n modelFields,\n parentPath,\n searchPlugins\n });\n\n const queryPlugins =\n context.plugins.byType<ElasticsearchQueryPlugin>(\"cms-elasticsearch-query\");\n for (const pl of queryPlugins) {\n pl.modify({ query, model, context });\n }\n\n return {\n query: {\n bool: {\n must: query.must.length > 0 ? query.must : undefined,\n must_not: query.must_not.length > 0 ? query.must_not : undefined,\n should: query.should.length > 0 ? query.should : undefined,\n filter: query.filter.length > 0 ? query.filter : undefined\n }\n },\n sort: createElasticsearchSortParams({\n context,\n sort,\n modelFields,\n parentPath,\n model,\n searchPlugins\n }),\n size: limit + 1,\n // eslint-disable-next-line\n search_after: decodeCursor(after) as any,\n // eslint-disable-next-line\n track_total_hits: true\n };\n};\n"],"file":"createElasticsearchQueryBody.js"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/api-headless-cms-ddb-es",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.16.0-beta.2",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"@webiny/api-headless-cms",
|
|
@@ -23,14 +23,14 @@
|
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"@babel/runtime": "7.15.3",
|
|
26
|
-
"@webiny/api-headless-cms": "5.
|
|
27
|
-
"@webiny/api-upgrade": "5.
|
|
28
|
-
"@webiny/db-dynamodb": "5.
|
|
29
|
-
"@webiny/error": "5.
|
|
30
|
-
"@webiny/handler": "5.
|
|
31
|
-
"@webiny/handler-aws": "5.
|
|
32
|
-
"@webiny/handler-db": "5.
|
|
33
|
-
"@webiny/plugins": "5.
|
|
26
|
+
"@webiny/api-headless-cms": "5.16.0-beta.2",
|
|
27
|
+
"@webiny/api-upgrade": "5.16.0-beta.2",
|
|
28
|
+
"@webiny/db-dynamodb": "5.16.0-beta.2",
|
|
29
|
+
"@webiny/error": "5.16.0-beta.2",
|
|
30
|
+
"@webiny/handler": "5.16.0-beta.2",
|
|
31
|
+
"@webiny/handler-aws": "5.16.0-beta.2",
|
|
32
|
+
"@webiny/handler-db": "5.16.0-beta.2",
|
|
33
|
+
"@webiny/plugins": "5.16.0-beta.2",
|
|
34
34
|
"dataloader": "2.0.0",
|
|
35
35
|
"dynamodb-toolbox": "0.3.4",
|
|
36
36
|
"jsonpack": "1.1.5",
|
|
@@ -46,10 +46,10 @@
|
|
|
46
46
|
"@elastic/elasticsearch": "7.12.0",
|
|
47
47
|
"@shelf/jest-elasticsearch": "^1.0.0",
|
|
48
48
|
"@types/jsonpack": "^1.1.0",
|
|
49
|
-
"@webiny/api-dynamodb-to-elasticsearch": "^5.
|
|
50
|
-
"@webiny/api-elasticsearch": "^5.
|
|
51
|
-
"@webiny/cli": "^5.
|
|
52
|
-
"@webiny/project-utils": "^5.
|
|
49
|
+
"@webiny/api-dynamodb-to-elasticsearch": "^5.16.0-beta.2",
|
|
50
|
+
"@webiny/api-elasticsearch": "^5.16.0-beta.2",
|
|
51
|
+
"@webiny/cli": "^5.16.0-beta.2",
|
|
52
|
+
"@webiny/project-utils": "^5.16.0-beta.2",
|
|
53
53
|
"jest": "^26.6.3",
|
|
54
54
|
"jest-dynalite": "^3.2.0",
|
|
55
55
|
"jest-environment-node": "^26.6.2",
|
|
@@ -68,5 +68,5 @@
|
|
|
68
68
|
"build": "yarn webiny run build",
|
|
69
69
|
"watch": "yarn webiny run watch"
|
|
70
70
|
},
|
|
71
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "f8ca0ffc49737da9d2f797382c4df9c701d6f037"
|
|
72
72
|
}
|