@webiny/db-dynamodb 0.0.0-unstable.40876133bb → 0.0.0-unstable.496cf268ac
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/BatchProcess.js +3 -37
- package/BatchProcess.js.map +1 -1
- package/DynamoDbDriver.js +3 -38
- package/DynamoDbDriver.js.map +1 -1
- package/QueryGenerator.js +4 -18
- package/QueryGenerator.js.map +1 -1
- package/index.d.ts +2 -0
- package/index.js +24 -3
- package/index.js.map +1 -1
- package/operators/comparison/beginsWith.js.map +1 -1
- package/operators/comparison/between.js.map +1 -1
- package/operators/comparison/eq.js +0 -2
- package/operators/comparison/eq.js.map +1 -1
- package/operators/comparison/gt.js.map +1 -1
- package/operators/comparison/gte.js.map +1 -1
- package/operators/comparison/lt.js.map +1 -1
- package/operators/comparison/lte.js.map +1 -1
- package/operators/index.js +0 -11
- package/operators/index.js.map +1 -1
- package/operators/logical/and.js +0 -5
- package/operators/logical/and.js.map +1 -1
- package/operators/logical/or.js +0 -5
- package/operators/logical/or.js.map +1 -1
- package/package.json +9 -9
- package/plugins/definitions/AttributePlugin.js +0 -13
- package/plugins/definitions/AttributePlugin.js.map +1 -1
- package/plugins/definitions/DateTimeTransformPlugin.js +0 -14
- package/plugins/definitions/DateTimeTransformPlugin.js.map +1 -1
- package/plugins/definitions/FieldPathPlugin.js +0 -12
- package/plugins/definitions/FieldPathPlugin.js.map +1 -1
- package/plugins/definitions/FieldPlugin.js +0 -16
- package/plugins/definitions/FieldPlugin.js.map +1 -1
- package/plugins/definitions/NumberTransformPlugin.js +0 -11
- package/plugins/definitions/NumberTransformPlugin.js.map +1 -1
- package/plugins/definitions/TimeTransformPlugin.js +0 -14
- package/plugins/definitions/TimeTransformPlugin.js.map +1 -1
- package/plugins/definitions/ValueFilterPlugin.js +0 -12
- package/plugins/definitions/ValueFilterPlugin.js.map +1 -1
- package/plugins/definitions/ValueTransformPlugin.js +0 -13
- package/plugins/definitions/ValueTransformPlugin.js.map +1 -1
- package/plugins/definitions/assignFields.js +0 -9
- package/plugins/definitions/assignFields.js.map +1 -1
- package/plugins/filters/andIn.d.ts +2 -2
- package/plugins/filters/andIn.js +3 -8
- package/plugins/filters/andIn.js.map +1 -1
- package/plugins/filters/between.d.ts +2 -2
- package/plugins/filters/between.js +3 -8
- package/plugins/filters/between.js.map +1 -1
- package/plugins/filters/contains.d.ts +2 -2
- package/plugins/filters/contains.js +3 -6
- package/plugins/filters/contains.js.map +1 -1
- package/plugins/filters/eq.d.ts +2 -2
- package/plugins/filters/eq.js +3 -5
- package/plugins/filters/eq.js.map +1 -1
- package/plugins/filters/fuzzy.d.ts +2 -2
- package/plugins/filters/fuzzy.js +3 -7
- package/plugins/filters/fuzzy.js.map +1 -1
- package/plugins/filters/gt.d.ts +2 -2
- package/plugins/filters/gt.js +3 -4
- package/plugins/filters/gt.js.map +1 -1
- package/plugins/filters/gte.d.ts +2 -2
- package/plugins/filters/gte.js +3 -4
- package/plugins/filters/gte.js.map +1 -1
- package/plugins/filters/in.d.ts +2 -2
- package/plugins/filters/in.js +3 -8
- package/plugins/filters/in.js.map +1 -1
- package/plugins/filters/index.js +0 -14
- package/plugins/filters/index.js.map +1 -1
- package/plugins/filters/lt.d.ts +2 -2
- package/plugins/filters/lt.js +3 -4
- package/plugins/filters/lt.js.map +1 -1
- package/plugins/filters/lte.d.ts +2 -2
- package/plugins/filters/lte.js +3 -4
- package/plugins/filters/lte.js.map +1 -1
- package/plugins/filters/startsWith.d.ts +2 -2
- package/plugins/filters/startsWith.js +3 -7
- package/plugins/filters/startsWith.js.map +1 -1
- package/plugins/index.js +0 -4
- package/plugins/index.js.map +1 -1
- package/statements/createKeyConditionExpressionArgs.js +0 -7
- package/statements/createKeyConditionExpressionArgs.js.map +1 -1
- package/statements/processStatement.js +0 -7
- package/statements/processStatement.js.map +1 -1
- package/types.d.ts +8 -0
- package/types.js.map +1 -1
- package/utils/attributes.js +0 -7
- package/utils/attributes.js.map +1 -1
- package/utils/batchRead.js +0 -22
- package/utils/batchRead.js.map +1 -1
- package/utils/batchWrite.js +0 -6
- package/utils/batchWrite.js.map +1 -1
- package/utils/cleanup.js +0 -10
- package/utils/cleanup.js.map +1 -1
- package/utils/createStandardEntity.d.ts +2 -0
- package/utils/createStandardEntity.js +34 -0
- package/utils/createStandardEntity.js.map +1 -0
- package/utils/cursor.js +0 -6
- package/utils/cursor.js.map +1 -1
- package/utils/filter.js +0 -37
- package/utils/filter.js.map +1 -1
- package/utils/get.js +0 -4
- package/utils/get.js.map +1 -1
- package/utils/listResponse.js +0 -4
- package/utils/listResponse.js.map +1 -1
- package/utils/query.d.ts +6 -1
- package/utils/query.js +21 -21
- package/utils/query.js.map +1 -1
- package/utils/sort.js +0 -17
- package/utils/sort.js.map +1 -1
- package/utils/table.js +0 -5
- package/utils/table.js.map +1 -1
package/utils/cleanup.js
CHANGED
|
@@ -1,45 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.cleanupItem = cleanupItem;
|
|
9
8
|
exports.cleanupItems = cleanupItems;
|
|
10
|
-
|
|
11
9
|
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
12
|
-
|
|
13
10
|
/**
|
|
14
11
|
* The attributes defined by us or the dynamodb-toolbox library.
|
|
15
12
|
* Add more attributes if necessary.
|
|
16
13
|
*/
|
|
17
14
|
const attributesToRemove = ["PK", "SK", "created", "_ct", "modified", "_mt", "entity", "_et", "GSI1_PK", "GSI1_SK", "TYPE"];
|
|
18
|
-
|
|
19
15
|
function cleanupItem(entity, item, removeAttributes = []) {
|
|
20
16
|
if (!item) {
|
|
21
17
|
return null;
|
|
22
18
|
}
|
|
23
|
-
|
|
24
19
|
const newItem = (0, _objectSpread2.default)({}, item);
|
|
25
20
|
const targets = attributesToRemove.concat(removeAttributes);
|
|
26
21
|
const attributes = entity.schema.attributes;
|
|
27
|
-
|
|
28
22
|
for (const key in item) {
|
|
29
23
|
if (item.hasOwnProperty(key) === false) {
|
|
30
24
|
continue;
|
|
31
25
|
}
|
|
32
|
-
|
|
33
26
|
if (attributes[key] && targets.includes(key) === false) {
|
|
34
27
|
continue;
|
|
35
28
|
}
|
|
36
|
-
|
|
37
29
|
delete newItem[key];
|
|
38
30
|
}
|
|
39
|
-
|
|
40
31
|
return newItem;
|
|
41
32
|
}
|
|
42
|
-
|
|
43
33
|
function cleanupItems(entity, items, removeAttributes = []) {
|
|
44
34
|
return items.map(item => cleanupItem(entity, item, removeAttributes));
|
|
45
35
|
}
|
package/utils/cleanup.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["attributesToRemove","cleanupItem","entity","item","removeAttributes","newItem","targets","concat","attributes","schema","key","hasOwnProperty","includes","cleanupItems","items","map"],"sources":["cleanup.ts"],"sourcesContent":["import { Entity } from \"dynamodb-toolbox\";\n\n/**\n * The attributes defined by us or the dynamodb-toolbox library.\n * Add more attributes if necessary.\n */\nconst attributesToRemove = [\n \"PK\",\n \"SK\",\n \"created\",\n \"_ct\",\n \"modified\",\n \"_mt\",\n \"entity\",\n \"_et\",\n \"GSI1_PK\",\n \"GSI1_SK\",\n \"TYPE\"\n];\n\nexport function cleanupItem<T>(\n entity: Entity<any>,\n item?: (T & Record<string, any>) | null,\n removeAttributes: string[] = []\n): T | null {\n if (!item) {\n return null;\n }\n const newItem = {\n ...item\n };\n const targets = attributesToRemove.concat(removeAttributes);\n const attributes = entity.schema.attributes;\n for (const key in item) {\n if (item.hasOwnProperty(key) === false) {\n continue;\n }\n if (attributes[key] && targets.includes(key) === false) {\n continue;\n }\n delete newItem[key];\n }\n return newItem;\n}\n\nexport function cleanupItems<T>(\n entity: Entity<any>,\n items: (T & Record<string, any>)[],\n removeAttributes: string[] = []\n): T[] {\n return items.map(item => cleanupItem<T>(entity, item, removeAttributes) as T);\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"names":["attributesToRemove","cleanupItem","entity","item","removeAttributes","newItem","targets","concat","attributes","schema","key","hasOwnProperty","includes","cleanupItems","items","map"],"sources":["cleanup.ts"],"sourcesContent":["import { Entity } from \"dynamodb-toolbox\";\n\n/**\n * The attributes defined by us or the dynamodb-toolbox library.\n * Add more attributes if necessary.\n */\nconst attributesToRemove = [\n \"PK\",\n \"SK\",\n \"created\",\n \"_ct\",\n \"modified\",\n \"_mt\",\n \"entity\",\n \"_et\",\n \"GSI1_PK\",\n \"GSI1_SK\",\n \"TYPE\"\n];\n\nexport function cleanupItem<T>(\n entity: Entity<any>,\n item?: (T & Record<string, any>) | null,\n removeAttributes: string[] = []\n): T | null {\n if (!item) {\n return null;\n }\n const newItem = {\n ...item\n };\n const targets = attributesToRemove.concat(removeAttributes);\n const attributes = entity.schema.attributes;\n for (const key in item) {\n if (item.hasOwnProperty(key) === false) {\n continue;\n }\n if (attributes[key] && targets.includes(key) === false) {\n continue;\n }\n delete newItem[key];\n }\n return newItem;\n}\n\nexport function cleanupItems<T>(\n entity: Entity<any>,\n items: (T & Record<string, any>)[],\n removeAttributes: string[] = []\n): T[] {\n return items.map(item => cleanupItem<T>(entity, item, removeAttributes) as T);\n}\n"],"mappings":";;;;;;;;;AAEA;AACA;AACA;AACA;AACA,MAAMA,kBAAkB,GAAG,CACvB,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,KAAK,EACL,UAAU,EACV,KAAK,EACL,QAAQ,EACR,KAAK,EACL,SAAS,EACT,SAAS,EACT,MAAM,CACT;AAEM,SAASC,WAAW,CACvBC,MAAmB,EACnBC,IAAuC,EACvCC,gBAA0B,GAAG,EAAE,EACvB;EACR,IAAI,CAACD,IAAI,EAAE;IACP,OAAO,IAAI;EACf;EACA,MAAME,OAAO,mCACNF,IAAI,CACV;EACD,MAAMG,OAAO,GAAGN,kBAAkB,CAACO,MAAM,CAACH,gBAAgB,CAAC;EAC3D,MAAMI,UAAU,GAAGN,MAAM,CAACO,MAAM,CAACD,UAAU;EAC3C,KAAK,MAAME,GAAG,IAAIP,IAAI,EAAE;IACpB,IAAIA,IAAI,CAACQ,cAAc,CAACD,GAAG,CAAC,KAAK,KAAK,EAAE;MACpC;IACJ;IACA,IAAIF,UAAU,CAACE,GAAG,CAAC,IAAIJ,OAAO,CAACM,QAAQ,CAACF,GAAG,CAAC,KAAK,KAAK,EAAE;MACpD;IACJ;IACA,OAAOL,OAAO,CAACK,GAAG,CAAC;EACvB;EACA,OAAOL,OAAO;AAClB;AAEO,SAASQ,YAAY,CACxBX,MAAmB,EACnBY,KAAkC,EAClCV,gBAA0B,GAAG,EAAE,EAC5B;EACH,OAAOU,KAAK,CAACC,GAAG,CAACZ,IAAI,IAAIF,WAAW,CAAIC,MAAM,EAAEC,IAAI,EAAEC,gBAAgB,CAAM,CAAC;AACjF"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createStandardEntity = void 0;
|
|
7
|
+
var _dynamodbToolbox = require("dynamodb-toolbox");
|
|
8
|
+
const createStandardEntity = (table, name) => {
|
|
9
|
+
return new _dynamodbToolbox.Entity({
|
|
10
|
+
name,
|
|
11
|
+
table,
|
|
12
|
+
attributes: {
|
|
13
|
+
PK: {
|
|
14
|
+
partitionKey: true
|
|
15
|
+
},
|
|
16
|
+
SK: {
|
|
17
|
+
sortKey: true
|
|
18
|
+
},
|
|
19
|
+
GSI1_PK: {
|
|
20
|
+
type: "string"
|
|
21
|
+
},
|
|
22
|
+
GSI1_SK: {
|
|
23
|
+
type: "string"
|
|
24
|
+
},
|
|
25
|
+
TYPE: {
|
|
26
|
+
type: "string"
|
|
27
|
+
},
|
|
28
|
+
data: {
|
|
29
|
+
type: "map"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
exports.createStandardEntity = createStandardEntity;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["createStandardEntity","table","name","Entity","attributes","PK","partitionKey","SK","sortKey","GSI1_PK","type","GSI1_SK","TYPE","data"],"sources":["createStandardEntity.ts"],"sourcesContent":["import { Entity, Table } from \"dynamodb-toolbox\";\n\nexport const createStandardEntity = (table: Table, name: string): Entity<any> => {\n return new Entity({\n name,\n table,\n attributes: {\n PK: {\n partitionKey: true\n },\n SK: {\n sortKey: true\n },\n GSI1_PK: {\n type: \"string\"\n },\n GSI1_SK: {\n type: \"string\"\n },\n TYPE: {\n type: \"string\"\n },\n data: {\n type: \"map\"\n }\n }\n });\n};\n"],"mappings":";;;;;;AAAA;AAEO,MAAMA,oBAAoB,GAAG,CAACC,KAAY,EAAEC,IAAY,KAAkB;EAC7E,OAAO,IAAIC,uBAAM,CAAC;IACdD,IAAI;IACJD,KAAK;IACLG,UAAU,EAAE;MACRC,EAAE,EAAE;QACAC,YAAY,EAAE;MAClB,CAAC;MACDC,EAAE,EAAE;QACAC,OAAO,EAAE;MACb,CAAC;MACDC,OAAO,EAAE;QACLC,IAAI,EAAE;MACV,CAAC;MACDC,OAAO,EAAE;QACLD,IAAI,EAAE;MACV,CAAC;MACDE,IAAI,EAAE;QACFF,IAAI,EAAE;MACV,CAAC;MACDG,IAAI,EAAE;QACFH,IAAI,EAAE;MACV;IACJ;EACJ,CAAC,CAAC;AACN,CAAC;AAAC"}
|
package/utils/cursor.js
CHANGED
|
@@ -4,23 +4,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.encodeCursor = exports.decodeCursor = void 0;
|
|
7
|
-
|
|
8
7
|
const encodeCursor = cursor => {
|
|
9
8
|
if (!cursor) {
|
|
10
9
|
return null;
|
|
11
10
|
}
|
|
12
|
-
|
|
13
11
|
return Buffer.from(JSON.stringify(cursor)).toString("base64");
|
|
14
12
|
};
|
|
15
|
-
|
|
16
13
|
exports.encodeCursor = encodeCursor;
|
|
17
|
-
|
|
18
14
|
const decodeCursor = cursor => {
|
|
19
15
|
if (!cursor) {
|
|
20
16
|
return null;
|
|
21
17
|
}
|
|
22
|
-
|
|
23
18
|
return JSON.parse(Buffer.from(cursor, "base64").toString("ascii"));
|
|
24
19
|
};
|
|
25
|
-
|
|
26
20
|
exports.decodeCursor = decodeCursor;
|
package/utils/cursor.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["encodeCursor","cursor","Buffer","from","JSON","stringify","toString","decodeCursor","parse"],"sources":["cursor.ts"],"sourcesContent":["export const encodeCursor = (cursor?: any): string | null => {\n if (!cursor) {\n return null;\n }\n\n return Buffer.from(JSON.stringify(cursor)).toString(\"base64\");\n};\n\nexport const decodeCursor = (cursor?: string | null): string | null => {\n if (!cursor) {\n return null;\n }\n\n return JSON.parse(Buffer.from(cursor, \"base64\").toString(\"ascii\"));\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"names":["encodeCursor","cursor","Buffer","from","JSON","stringify","toString","decodeCursor","parse"],"sources":["cursor.ts"],"sourcesContent":["export const encodeCursor = (cursor?: any): string | null => {\n if (!cursor) {\n return null;\n }\n\n return Buffer.from(JSON.stringify(cursor)).toString(\"base64\");\n};\n\nexport const decodeCursor = (cursor?: string | null): string | null => {\n if (!cursor) {\n return null;\n }\n\n return JSON.parse(Buffer.from(cursor, \"base64\").toString(\"ascii\"));\n};\n"],"mappings":";;;;;;AAAO,MAAMA,YAAY,GAAIC,MAAY,IAAoB;EACzD,IAAI,CAACA,MAAM,EAAE;IACT,OAAO,IAAI;EACf;EAEA,OAAOC,MAAM,CAACC,IAAI,CAACC,IAAI,CAACC,SAAS,CAACJ,MAAM,CAAC,CAAC,CAACK,QAAQ,CAAC,QAAQ,CAAC;AACjE,CAAC;AAAC;AAEK,MAAMC,YAAY,GAAIN,MAAsB,IAAoB;EACnE,IAAI,CAACA,MAAM,EAAE;IACT,OAAO,IAAI;EACf;EAEA,OAAOG,IAAI,CAACI,KAAK,CAACN,MAAM,CAACC,IAAI,CAACF,MAAM,EAAE,QAAQ,CAAC,CAACK,QAAQ,CAAC,OAAO,CAAC,CAAC;AACtE,CAAC;AAAC"}
|
package/utils/filter.js
CHANGED
|
@@ -1,18 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.filterItems = filterItems;
|
|
9
|
-
|
|
10
8
|
var _dotProp = _interopRequireDefault(require("dot-prop"));
|
|
11
|
-
|
|
12
9
|
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
|
-
|
|
14
10
|
var _ValueFilterPlugin = require("../plugins/definitions/ValueFilterPlugin");
|
|
15
|
-
|
|
16
11
|
const getMappedPlugins = params => {
|
|
17
12
|
return params.plugins.byType(params.type).reduce((plugins, plugin) => {
|
|
18
13
|
/**
|
|
@@ -23,7 +18,6 @@ const getMappedPlugins = params => {
|
|
|
23
18
|
return plugins;
|
|
24
19
|
}, {});
|
|
25
20
|
};
|
|
26
|
-
|
|
27
21
|
const extractWhereArgs = key => {
|
|
28
22
|
const result = key.split("_");
|
|
29
23
|
const field = result.shift();
|
|
@@ -31,7 +25,6 @@ const extractWhereArgs = key => {
|
|
|
31
25
|
/**
|
|
32
26
|
* When rawOp is not, it means it is equal negated so just return that.
|
|
33
27
|
*/
|
|
34
|
-
|
|
35
28
|
if (rawOp === "not") {
|
|
36
29
|
return {
|
|
37
30
|
field,
|
|
@@ -39,7 +32,6 @@ const extractWhereArgs = key => {
|
|
|
39
32
|
negate: true
|
|
40
33
|
};
|
|
41
34
|
}
|
|
42
|
-
|
|
43
35
|
const negate = rawOp.match("not_") !== null;
|
|
44
36
|
const operation = rawOp.replace("not_", "");
|
|
45
37
|
return {
|
|
@@ -48,19 +40,15 @@ const extractWhereArgs = key => {
|
|
|
48
40
|
negate
|
|
49
41
|
};
|
|
50
42
|
};
|
|
51
|
-
|
|
52
43
|
const findFilterPlugin = (plugins, operation) => {
|
|
53
44
|
if (plugins[operation]) {
|
|
54
45
|
return plugins[operation];
|
|
55
46
|
}
|
|
56
|
-
|
|
57
47
|
throw new _error.default(`Missing filter plugin definition.`, "FILTER_PLUGIN_ERROR", {
|
|
58
48
|
operation
|
|
59
49
|
});
|
|
60
50
|
};
|
|
61
|
-
|
|
62
51
|
const multiSearchFieldOperations = ["contains", "fuzzy"];
|
|
63
|
-
|
|
64
52
|
const createFilters = params => {
|
|
65
53
|
const {
|
|
66
54
|
plugins,
|
|
@@ -71,11 +59,9 @@ const createFilters = params => {
|
|
|
71
59
|
/**
|
|
72
60
|
* Skip everything if there are no conditions to be applied.
|
|
73
61
|
*/
|
|
74
|
-
|
|
75
62
|
if (keys.length === 0) {
|
|
76
63
|
return [];
|
|
77
64
|
}
|
|
78
|
-
|
|
79
65
|
const filterPlugins = getMappedPlugins({
|
|
80
66
|
plugins,
|
|
81
67
|
type: _ValueFilterPlugin.ValueFilterPlugin.type,
|
|
@@ -83,29 +69,23 @@ const createFilters = params => {
|
|
|
83
69
|
});
|
|
84
70
|
return keys.reduce((filters, key) => {
|
|
85
71
|
const compareValue = where[key];
|
|
86
|
-
|
|
87
72
|
if (compareValue === undefined) {
|
|
88
73
|
return filters;
|
|
89
74
|
}
|
|
90
75
|
/**
|
|
91
76
|
* @see DynamoDbContainsFilter
|
|
92
77
|
*/
|
|
93
|
-
|
|
94
|
-
|
|
95
78
|
if (multiSearchFieldOperations.includes(key) === true) {
|
|
96
79
|
const data = compareValue;
|
|
97
80
|
let transformValue = undefined;
|
|
98
81
|
const paths = data.fields.map(field => {
|
|
99
82
|
const fieldPlugin = fields.find(plugin => plugin.getField() === field);
|
|
100
|
-
|
|
101
83
|
if (fieldPlugin) {
|
|
102
84
|
transformValue = value => {
|
|
103
85
|
return fieldPlugin.transformValue(value);
|
|
104
86
|
};
|
|
105
|
-
|
|
106
87
|
return fieldPlugin.getPath();
|
|
107
88
|
}
|
|
108
|
-
|
|
109
89
|
return field;
|
|
110
90
|
});
|
|
111
91
|
filters.push({
|
|
@@ -117,7 +97,6 @@ const createFilters = params => {
|
|
|
117
97
|
});
|
|
118
98
|
return filters;
|
|
119
99
|
}
|
|
120
|
-
|
|
121
100
|
const {
|
|
122
101
|
field,
|
|
123
102
|
operation,
|
|
@@ -127,15 +106,12 @@ const createFilters = params => {
|
|
|
127
106
|
const fieldPlugin = fields.find(plugin => plugin.getField() === field);
|
|
128
107
|
let path = field;
|
|
129
108
|
let transformValue = undefined;
|
|
130
|
-
|
|
131
109
|
if (fieldPlugin) {
|
|
132
110
|
transformValue = value => {
|
|
133
111
|
return fieldPlugin.transformValue(value);
|
|
134
112
|
};
|
|
135
|
-
|
|
136
113
|
path = fieldPlugin.getPath();
|
|
137
114
|
}
|
|
138
|
-
|
|
139
115
|
filters.push({
|
|
140
116
|
compareValue,
|
|
141
117
|
filterPlugin,
|
|
@@ -149,35 +125,27 @@ const createFilters = params => {
|
|
|
149
125
|
/**
|
|
150
126
|
* Transforms the value with given transformer callable.
|
|
151
127
|
*/
|
|
152
|
-
|
|
153
|
-
|
|
154
128
|
const transform = (value, transformValue) => {
|
|
155
129
|
if (!transformValue) {
|
|
156
130
|
return value;
|
|
157
131
|
}
|
|
158
|
-
|
|
159
132
|
if (Array.isArray(value)) {
|
|
160
133
|
return value.map(v => transformValue(v));
|
|
161
134
|
}
|
|
162
|
-
|
|
163
135
|
return transformValue(value);
|
|
164
136
|
};
|
|
165
137
|
/**
|
|
166
138
|
* Creates a filter callable that we can send to the .filter() method of the array.
|
|
167
139
|
*/
|
|
168
|
-
|
|
169
|
-
|
|
170
140
|
const createFilterCallable = params => {
|
|
171
141
|
const filters = createFilters(params);
|
|
172
142
|
/**
|
|
173
143
|
* Just return null so there are no filters to be applied.
|
|
174
144
|
* Later in the code we check for null so we do not loop through the items.
|
|
175
145
|
*/
|
|
176
|
-
|
|
177
146
|
if (filters.length === 0) {
|
|
178
147
|
return null;
|
|
179
148
|
}
|
|
180
|
-
|
|
181
149
|
return item => {
|
|
182
150
|
for (const filter of filters) {
|
|
183
151
|
const result = filter.paths.some(path => {
|
|
@@ -189,25 +157,20 @@ const createFilterCallable = params => {
|
|
|
189
157
|
});
|
|
190
158
|
return filter.negate ? !matched : matched;
|
|
191
159
|
});
|
|
192
|
-
|
|
193
160
|
if (result === false) {
|
|
194
161
|
return false;
|
|
195
162
|
}
|
|
196
163
|
}
|
|
197
|
-
|
|
198
164
|
return true;
|
|
199
165
|
};
|
|
200
166
|
};
|
|
201
|
-
|
|
202
167
|
function filterItems(params) {
|
|
203
168
|
const filter = createFilterCallable(params);
|
|
204
169
|
/**
|
|
205
170
|
* No point in going through all the items when there are no filters to be applied.
|
|
206
171
|
*/
|
|
207
|
-
|
|
208
172
|
if (!filter) {
|
|
209
173
|
return params.items;
|
|
210
174
|
}
|
|
211
|
-
|
|
212
175
|
return params.items.filter(filter);
|
|
213
176
|
}
|
package/utils/filter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["getMappedPlugins","params","plugins","byType","type","reduce","plugin","op","property","extractWhereArgs","key","result","split","field","shift","rawOp","length","join","operation","negate","match","replace","findFilterPlugin","WebinyError","multiSearchFieldOperations","createFilters","where","fields","keys","Object","filterPlugins","ValueFilterPlugin","filters","compareValue","undefined","includes","data","transformValue","paths","map","fieldPlugin","find","getField","value","getPath","push","filterPlugin","path","transform","Array","isArray","v","createFilterCallable","item","filter","some","dotProp","get","matched","matches","filterItems","items"],"sources":["filter.ts"],"sourcesContent":["import dotProp from \"dot-prop\";\nimport WebinyError from \"@webiny/error\";\nimport { Plugin, PluginsContainer } from \"@webiny/plugins\";\nimport { ValueFilterPlugin } from \"~/plugins/definitions/ValueFilterPlugin\";\nimport { FieldPlugin } from \"~/plugins/definitions/FieldPlugin\";\nimport { DynamoDbContainsFilter } from \"~/types\";\n\ntype TransformValue = (value: any) => any;\n\nexport interface Params<T = any> {\n plugins: PluginsContainer;\n items: T[];\n where: Record<string, any>;\n /**\n * An array of fields that require some special operation.\n */\n fields: FieldPlugin[];\n}\n\ninterface MappedPluginParams<T extends Plugin = Plugin> {\n plugins: PluginsContainer;\n type: string;\n property: keyof T;\n}\n\ninterface Filter {\n compareValue: any;\n filterPlugin: ValueFilterPlugin;\n transformValue?: TransformValue;\n paths: string[];\n negate: boolean;\n}\n\nconst getMappedPlugins = <T extends Plugin>(params: MappedPluginParams<T>): Record<string, T> => {\n return params.plugins.byType<T>(params.type).reduce((plugins, plugin) => {\n /**\n * We expect op to be a string, that is why we cast.\n */\n const op = plugin[params.property] as unknown as string;\n plugins[op] = plugin;\n return plugins;\n }, {} as Record<string, T>);\n};\n\ninterface ExtractWhereArgsResult {\n field: string;\n operation: string;\n negate: boolean;\n}\nconst extractWhereArgs = (key: string): ExtractWhereArgsResult => {\n const result = key.split(\"_\");\n const field = result.shift() as string;\n const rawOp = result.length === 0 ? \"eq\" : result.join(\"_\");\n /**\n * When rawOp is not, it means it is equal negated so just return that.\n */\n if (rawOp === \"not\") {\n return {\n field,\n operation: \"eq\",\n negate: true\n };\n }\n const negate = rawOp.match(\"not_\") !== null;\n const operation = rawOp.replace(\"not_\", \"\");\n return {\n field,\n operation,\n negate\n };\n};\n\nconst findFilterPlugin = (\n plugins: Record<string, ValueFilterPlugin>,\n operation: string\n): ValueFilterPlugin => {\n if (plugins[operation]) {\n return plugins[operation];\n }\n throw new WebinyError(`Missing filter plugin definition.`, \"FILTER_PLUGIN_ERROR\", {\n operation\n });\n};\n\nconst multiSearchFieldOperations = [\"contains\", \"fuzzy\"];\n\nconst createFilters = (params: Omit<Params, \"items\">): Filter[] => {\n const { plugins, where, fields } = params;\n\n const keys = Object.keys(where);\n /**\n * Skip everything if there are no conditions to be applied.\n */\n if (keys.length === 0) {\n return [];\n }\n const filterPlugins = getMappedPlugins<ValueFilterPlugin>({\n plugins,\n type: ValueFilterPlugin.type,\n property: \"operation\"\n });\n\n return keys.reduce((filters, key) => {\n const compareValue = where[key];\n if (compareValue === undefined) {\n return filters;\n }\n /**\n * @see DynamoDbContainsFilter\n */\n if (multiSearchFieldOperations.includes(key) === true) {\n const data: DynamoDbContainsFilter = compareValue;\n let transformValue: TransformValue | undefined = undefined;\n const paths = data.fields.map(field => {\n const fieldPlugin = fields.find(plugin => plugin.getField() === field);\n if (fieldPlugin) {\n transformValue = (value: any) => {\n return fieldPlugin.transformValue(value);\n };\n return fieldPlugin.getPath();\n }\n return field;\n });\n filters.push({\n compareValue: data.value,\n filterPlugin: findFilterPlugin(filterPlugins, key),\n transformValue,\n paths,\n negate: false\n });\n return filters;\n }\n\n const { field, operation, negate } = extractWhereArgs(key);\n\n const filterPlugin = findFilterPlugin(filterPlugins, operation);\n\n const fieldPlugin = fields.find(plugin => plugin.getField() === field);\n let path: string = field;\n let transformValue: TransformValue | undefined = undefined;\n if (fieldPlugin) {\n transformValue = (value: any) => {\n return fieldPlugin.transformValue(value);\n };\n path = fieldPlugin.getPath();\n }\n\n filters.push({\n compareValue,\n filterPlugin,\n transformValue,\n paths: [path],\n negate\n });\n\n return filters;\n }, [] as Filter[]);\n};\n/**\n * Transforms the value with given transformer callable.\n */\nconst transform = (value: any, transformValue?: TransformValue): any => {\n if (!transformValue) {\n return value;\n }\n if (Array.isArray(value)) {\n return value.map(v => transformValue(v));\n }\n return transformValue(value);\n};\n/**\n * Creates a filter callable that we can send to the .filter() method of the array.\n */\nconst createFilterCallable = (params: Omit<Params, \"items\">): ((item: any) => boolean) | null => {\n const filters = createFilters(params);\n /**\n * Just return null so there are no filters to be applied.\n * Later in the code we check for null so we do not loop through the items.\n */\n if (filters.length === 0) {\n return null;\n }\n\n return (item: any) => {\n for (const filter of filters) {\n const result = filter.paths.some(path => {\n const value = transform(dotProp.get(item, path), filter.transformValue);\n const compareValue = transform(filter.compareValue, filter.transformValue);\n const matched = filter.filterPlugin.matches({\n value,\n compareValue\n });\n\n return filter.negate ? !matched : matched;\n });\n if (result === false) {\n return false;\n }\n }\n return true;\n };\n};\n\nexport function filterItems<T = any>(params: Params<T>): T[] {\n const filter = createFilterCallable(params);\n /**\n * No point in going through all the items when there are no filters to be applied.\n */\n if (!filter) {\n return params.items;\n }\n return params.items.filter(filter);\n}\n"],"mappings":";;;;;;;;;AAAA;;AACA;;AAEA;;AA8BA,MAAMA,gBAAgB,GAAsBC,MAAnB,IAAwE;EAC7F,OAAOA,MAAM,CAACC,OAAP,CAAeC,MAAf,CAAyBF,MAAM,CAACG,IAAhC,EAAsCC,MAAtC,CAA6C,CAACH,OAAD,EAAUI,MAAV,KAAqB;IACrE;AACR;AACA;IACQ,MAAMC,EAAE,GAAGD,MAAM,CAACL,MAAM,CAACO,QAAR,CAAjB;IACAN,OAAO,CAACK,EAAD,CAAP,GAAcD,MAAd;IACA,OAAOJ,OAAP;EACH,CAPM,EAOJ,EAPI,CAAP;AAQH,CATD;;AAgBA,MAAMO,gBAAgB,GAAIC,GAAD,IAAyC;EAC9D,MAAMC,MAAM,GAAGD,GAAG,CAACE,KAAJ,CAAU,GAAV,CAAf;EACA,MAAMC,KAAK,GAAGF,MAAM,CAACG,KAAP,EAAd;EACA,MAAMC,KAAK,GAAGJ,MAAM,CAACK,MAAP,KAAkB,CAAlB,GAAsB,IAAtB,GAA6BL,MAAM,CAACM,IAAP,CAAY,GAAZ,CAA3C;EACA;AACJ;AACA;;EACI,IAAIF,KAAK,KAAK,KAAd,EAAqB;IACjB,OAAO;MACHF,KADG;MAEHK,SAAS,EAAE,IAFR;MAGHC,MAAM,EAAE;IAHL,CAAP;EAKH;;EACD,MAAMA,MAAM,GAAGJ,KAAK,CAACK,KAAN,CAAY,MAAZ,MAAwB,IAAvC;EACA,MAAMF,SAAS,GAAGH,KAAK,CAAC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OAAO,KAAP;MACH;IACJ;;IACD,OAAO,IAAP;EACH,CAjBD;AAkBH,CA5BD;;AA8BO,SAASiD,WAAT,CAA8B3D,MAA9B,EAAsD;EACzD,MAAMqD,MAAM,GAAGF,oBAAoB,CAACnD,MAAD,CAAnC;EACA;AACJ;AACA;;EACI,IAAI,CAACqD,MAAL,EAAa;IACT,OAAOrD,MAAM,CAAC4D,KAAd;EACH;;EACD,OAAO5D,MAAM,CAAC4D,KAAP,CAAaP,MAAb,CAAoBA,MAApB,CAAP;AACH"}
|
|
1
|
+
{"version":3,"names":["getMappedPlugins","params","plugins","byType","type","reduce","plugin","op","property","extractWhereArgs","key","result","split","field","shift","rawOp","length","join","operation","negate","match","replace","findFilterPlugin","WebinyError","multiSearchFieldOperations","createFilters","where","fields","keys","Object","filterPlugins","ValueFilterPlugin","filters","compareValue","undefined","includes","data","transformValue","paths","map","fieldPlugin","find","getField","value","getPath","push","filterPlugin","path","transform","Array","isArray","v","createFilterCallable","item","filter","some","dotProp","get","matched","matches","filterItems","items"],"sources":["filter.ts"],"sourcesContent":["import dotProp from \"dot-prop\";\nimport WebinyError from \"@webiny/error\";\nimport { Plugin, PluginsContainer } from \"@webiny/plugins\";\nimport { ValueFilterPlugin } from \"~/plugins/definitions/ValueFilterPlugin\";\nimport { FieldPlugin } from \"~/plugins/definitions/FieldPlugin\";\nimport { DynamoDbContainsFilter } from \"~/types\";\n\ntype TransformValue = (value: any) => any;\n\nexport interface Params<T = any> {\n plugins: PluginsContainer;\n items: T[];\n where: Record<string, any>;\n /**\n * An array of fields that require some special operation.\n */\n fields: FieldPlugin[];\n}\n\ninterface MappedPluginParams<T extends Plugin = Plugin> {\n plugins: PluginsContainer;\n type: string;\n property: keyof T;\n}\n\ninterface Filter {\n compareValue: any;\n filterPlugin: ValueFilterPlugin;\n transformValue?: TransformValue;\n paths: string[];\n negate: boolean;\n}\n\nconst getMappedPlugins = <T extends Plugin>(params: MappedPluginParams<T>): Record<string, T> => {\n return params.plugins.byType<T>(params.type).reduce((plugins, plugin) => {\n /**\n * We expect op to be a string, that is why we cast.\n */\n const op = plugin[params.property] as unknown as string;\n plugins[op] = plugin;\n return plugins;\n }, {} as Record<string, T>);\n};\n\ninterface ExtractWhereArgsResult {\n field: string;\n operation: string;\n negate: boolean;\n}\nconst extractWhereArgs = (key: string): ExtractWhereArgsResult => {\n const result = key.split(\"_\");\n const field = result.shift() as string;\n const rawOp = result.length === 0 ? \"eq\" : result.join(\"_\");\n /**\n * When rawOp is not, it means it is equal negated so just return that.\n */\n if (rawOp === \"not\") {\n return {\n field,\n operation: \"eq\",\n negate: true\n };\n }\n const negate = rawOp.match(\"not_\") !== null;\n const operation = rawOp.replace(\"not_\", \"\");\n return {\n field,\n operation,\n negate\n };\n};\n\nconst findFilterPlugin = (\n plugins: Record<string, ValueFilterPlugin>,\n operation: string\n): ValueFilterPlugin => {\n if (plugins[operation]) {\n return plugins[operation];\n }\n throw new WebinyError(`Missing filter plugin definition.`, \"FILTER_PLUGIN_ERROR\", {\n operation\n });\n};\n\nconst multiSearchFieldOperations = [\"contains\", \"fuzzy\"];\n\nconst createFilters = (params: Omit<Params, \"items\">): Filter[] => {\n const { plugins, where, fields } = params;\n\n const keys = Object.keys(where);\n /**\n * Skip everything if there are no conditions to be applied.\n */\n if (keys.length === 0) {\n return [];\n }\n const filterPlugins = getMappedPlugins<ValueFilterPlugin>({\n plugins,\n type: ValueFilterPlugin.type,\n property: \"operation\"\n });\n\n return keys.reduce((filters, key) => {\n const compareValue = where[key];\n if (compareValue === undefined) {\n return filters;\n }\n /**\n * @see DynamoDbContainsFilter\n */\n if (multiSearchFieldOperations.includes(key) === true) {\n const data: DynamoDbContainsFilter = compareValue;\n let transformValue: TransformValue | undefined = undefined;\n const paths = data.fields.map(field => {\n const fieldPlugin = fields.find(plugin => plugin.getField() === field);\n if (fieldPlugin) {\n transformValue = (value: any) => {\n return fieldPlugin.transformValue(value);\n };\n return fieldPlugin.getPath();\n }\n return field;\n });\n filters.push({\n compareValue: data.value,\n filterPlugin: findFilterPlugin(filterPlugins, key),\n transformValue,\n paths,\n negate: false\n });\n return filters;\n }\n\n const { field, operation, negate } = extractWhereArgs(key);\n\n const filterPlugin = findFilterPlugin(filterPlugins, operation);\n\n const fieldPlugin = fields.find(plugin => plugin.getField() === field);\n let path: string = field;\n let transformValue: TransformValue | undefined = undefined;\n if (fieldPlugin) {\n transformValue = (value: any) => {\n return fieldPlugin.transformValue(value);\n };\n path = fieldPlugin.getPath();\n }\n\n filters.push({\n compareValue,\n filterPlugin,\n transformValue,\n paths: [path],\n negate\n });\n\n return filters;\n }, [] as Filter[]);\n};\n/**\n * Transforms the value with given transformer callable.\n */\nconst transform = (value: any, transformValue?: TransformValue): any => {\n if (!transformValue) {\n return value;\n }\n if (Array.isArray(value)) {\n return value.map(v => transformValue(v));\n }\n return transformValue(value);\n};\n/**\n * Creates a filter callable that we can send to the .filter() method of the array.\n */\nconst createFilterCallable = (params: Omit<Params, \"items\">): ((item: any) => boolean) | null => {\n const filters = createFilters(params);\n /**\n * Just return null so there are no filters to be applied.\n * Later in the code we check for null so we do not loop through the items.\n */\n if (filters.length === 0) {\n return null;\n }\n\n return (item: any) => {\n for (const filter of filters) {\n const result = filter.paths.some(path => {\n const value = transform(dotProp.get(item, path), filter.transformValue);\n const compareValue = transform(filter.compareValue, filter.transformValue);\n const matched = filter.filterPlugin.matches({\n value,\n compareValue\n });\n\n return filter.negate ? !matched : matched;\n });\n if (result === false) {\n return false;\n }\n }\n return true;\n };\n};\n\nexport function filterItems<T = any>(params: Params<T>): T[] {\n const filter = createFilterCallable(params);\n /**\n * No point in going through all the items when there are no filters to be applied.\n */\n if (!filter) {\n return params.items;\n }\n return params.items.filter(filter);\n}\n"],"mappings":";;;;;;;AAAA;AACA;AAEA;AA8BA,MAAMA,gBAAgB,GAAsBC,MAA6B,IAAwB;EAC7F,OAAOA,MAAM,CAACC,OAAO,CAACC,MAAM,CAAIF,MAAM,CAACG,IAAI,CAAC,CAACC,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ACA;AACA;EACI,IAAI+B,OAAO,CAAChB,MAAM,KAAK,CAAC,EAAE;IACtB,OAAO,IAAI;EACf;EAEA,OAAQqC,IAAS,IAAK;IAClB,KAAK,MAAMC,MAAM,IAAItB,OAAO,EAAE;MAC1B,MAAMrB,MAAM,GAAG2C,MAAM,CAAChB,KAAK,CAACiB,IAAI,CAACR,IAAI,IAAI;QACrC,MAAMJ,KAAK,GAAGK,SAAS,CAACQ,gBAAO,CAACC,GAAG,CAACJ,IAAI,EAAEN,IAAI,CAAC,EAAEO,MAAM,CAACjB,cAAc,CAAC;QACvE,MAAMJ,YAAY,GAAGe,SAAS,CAACM,MAAM,CAACrB,YAAY,EAAEqB,MAAM,CAACjB,cAAc,CAAC;QAC1E,MAAMqB,OAAO,GAAGJ,MAAM,CAACR,YAAY,CAACa,OAAO,CAAC;UACxChB,KAAK;UACLV;QACJ,CAAC,CAAC;QAEF,OAAOqB,MAAM,CAACnC,MAAM,GAAG,CAACuC,OAAO,GAAGA,OAAO;MAC7C,CAAC,CAAC;MACF,IAAI/C,MAAM,KAAK,KAAK,EAAE;QAClB,OAAO,KAAK;MAChB;IACJ;IACA,OAAO,IAAI;EACf,CAAC;AACL,CAAC;AAEM,SAASiD,WAAW,CAAU3D,MAAiB,EAAO;EACzD,MAAMqD,MAAM,GAAGF,oBAAoB,CAACnD,MAAM,CAAC;EAC3C;AACJ;AACA;EACI,IAAI,CAACqD,MAAM,EAAE;IACT,OAAOrD,MAAM,CAAC4D,KAAK;EACvB;EACA,OAAO5D,MAAM,CAAC4D,KAAK,CAACP,MAAM,CAACA,MAAM,CAAC;AACtC"}
|
package/utils/get.js
CHANGED
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.get = void 0;
|
|
7
|
-
|
|
8
7
|
/**
|
|
9
8
|
* Gets a single record from the DynamoDB table.
|
|
10
9
|
* Returns either record or null.
|
|
@@ -19,12 +18,9 @@ const get = async params => {
|
|
|
19
18
|
keys
|
|
20
19
|
} = params;
|
|
21
20
|
const result = await entity.get(keys);
|
|
22
|
-
|
|
23
21
|
if (!result || !result.Item) {
|
|
24
22
|
return null;
|
|
25
23
|
}
|
|
26
|
-
|
|
27
24
|
return result.Item;
|
|
28
25
|
};
|
|
29
|
-
|
|
30
26
|
exports.get = get;
|
package/utils/get.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["get","params","entity","keys","result","Item"],"sources":["get.ts"],"sourcesContent":["import { Entity } from \"dynamodb-toolbox\";\n\nexport interface GetRecordParams {\n entity: Entity<any>;\n keys: {\n PK: string;\n SK: string;\n };\n}\n\n/**\n * Gets a single record from the DynamoDB table.\n * Returns either record or null.\n *\n * Be aware to wrap in try/catch to avoid the error killing your app.\n *\n * @throws\n */\nexport const get = async <T>(params: GetRecordParams): Promise<T | null> => {\n const { entity, keys } = params;\n\n const result = await entity.get(keys);\n\n if (!result || !result.Item) {\n return null;\n }\n return result.Item;\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"names":["get","params","entity","keys","result","Item"],"sources":["get.ts"],"sourcesContent":["import { Entity } from \"dynamodb-toolbox\";\n\nexport interface GetRecordParams {\n entity: Entity<any>;\n keys: {\n PK: string;\n SK: string;\n };\n}\n\n/**\n * Gets a single record from the DynamoDB table.\n * Returns either record or null.\n *\n * Be aware to wrap in try/catch to avoid the error killing your app.\n *\n * @throws\n */\nexport const get = async <T>(params: GetRecordParams): Promise<T | null> => {\n const { entity, keys } = params;\n\n const result = await entity.get(keys);\n\n if (!result || !result.Item) {\n return null;\n }\n return result.Item;\n};\n"],"mappings":";;;;;;AAUA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMA,GAAG,GAAG,MAAUC,MAAuB,IAAwB;EACxE,MAAM;IAAEC,MAAM;IAAEC;EAAK,CAAC,GAAGF,MAAM;EAE/B,MAAMG,MAAM,GAAG,MAAMF,MAAM,CAACF,GAAG,CAACG,IAAI,CAAC;EAErC,IAAI,CAACC,MAAM,IAAI,CAACA,MAAM,CAACC,IAAI,EAAE;IACzB,OAAO,IAAI;EACf;EACA,OAAOD,MAAM,CAACC,IAAI;AACtB,CAAC;AAAC"}
|
package/utils/listResponse.js
CHANGED
|
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.createListResponse = createListResponse;
|
|
7
|
-
|
|
8
7
|
var _cursor = require("./cursor");
|
|
9
|
-
|
|
10
8
|
function createListResponse(params) {
|
|
11
9
|
const {
|
|
12
10
|
items: initialItems,
|
|
@@ -15,11 +13,9 @@ function createListResponse(params) {
|
|
|
15
13
|
limit
|
|
16
14
|
} = params;
|
|
17
15
|
let start = Number((0, _cursor.decodeCursor)(after));
|
|
18
|
-
|
|
19
16
|
if (isNaN(start) === true) {
|
|
20
17
|
start = 0;
|
|
21
18
|
}
|
|
22
|
-
|
|
23
19
|
const hasMoreItems = totalCount > start + limit;
|
|
24
20
|
const end = limit > totalCount + start + limit ? undefined : start + limit;
|
|
25
21
|
const items = end ? initialItems.slice(start, end) : initialItems;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["createListResponse","params","items","initialItems","after","totalCount","limit","start","Number","decodeCursor","isNaN","hasMoreItems","end","undefined","slice","cursor","length","encodeCursor","meta"],"sources":["listResponse.ts"],"sourcesContent":["import { decodeCursor, encodeCursor } from \"~/utils/cursor\";\n\ninterface MetaResponse {\n cursor: string | null;\n totalCount: number;\n hasMoreItems: boolean;\n}\n\ninterface CreateListResponseParams<T> {\n items: T[];\n after?: string | null;\n totalCount: number;\n limit: number;\n}\n\nexport function createListResponse<T>(params: CreateListResponseParams<T>): [T[], MetaResponse] {\n const { items: initialItems, after, totalCount, limit } = params;\n let start = Number(decodeCursor(after));\n if (isNaN(start) === true) {\n start = 0;\n }\n const hasMoreItems = totalCount > start + limit;\n const end = limit > totalCount + start + limit ? undefined : start + limit;\n const items = end ? initialItems.slice(start, end) : initialItems;\n\n const cursor = items.length > 0 ? encodeCursor(start + limit) : null;\n\n const meta = {\n hasMoreItems,\n totalCount,\n cursor\n };\n\n return [items, meta];\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"names":["createListResponse","params","items","initialItems","after","totalCount","limit","start","Number","decodeCursor","isNaN","hasMoreItems","end","undefined","slice","cursor","length","encodeCursor","meta"],"sources":["listResponse.ts"],"sourcesContent":["import { decodeCursor, encodeCursor } from \"~/utils/cursor\";\n\ninterface MetaResponse {\n cursor: string | null;\n totalCount: number;\n hasMoreItems: boolean;\n}\n\ninterface CreateListResponseParams<T> {\n items: T[];\n after?: string | null;\n totalCount: number;\n limit: number;\n}\n\nexport function createListResponse<T>(params: CreateListResponseParams<T>): [T[], MetaResponse] {\n const { items: initialItems, after, totalCount, limit } = params;\n let start = Number(decodeCursor(after));\n if (isNaN(start) === true) {\n start = 0;\n }\n const hasMoreItems = totalCount > start + limit;\n const end = limit > totalCount + start + limit ? undefined : start + limit;\n const items = end ? initialItems.slice(start, end) : initialItems;\n\n const cursor = items.length > 0 ? encodeCursor(start + limit) : null;\n\n const meta = {\n hasMoreItems,\n totalCount,\n cursor\n };\n\n return [items, meta];\n}\n"],"mappings":";;;;;;AAAA;AAeO,SAASA,kBAAkB,CAAIC,MAAmC,EAAuB;EAC5F,MAAM;IAAEC,KAAK,EAAEC,YAAY;IAAEC,KAAK;IAAEC,UAAU;IAAEC;EAAM,CAAC,GAAGL,MAAM;EAChE,IAAIM,KAAK,GAAGC,MAAM,CAAC,IAAAC,oBAAY,EAACL,KAAK,CAAC,CAAC;EACvC,IAAIM,KAAK,CAACH,KAAK,CAAC,KAAK,IAAI,EAAE;IACvBA,KAAK,GAAG,CAAC;EACb;EACA,MAAMI,YAAY,GAAGN,UAAU,GAAGE,KAAK,GAAGD,KAAK;EAC/C,MAAMM,GAAG,GAAGN,KAAK,GAAGD,UAAU,GAAGE,KAAK,GAAGD,KAAK,GAAGO,SAAS,GAAGN,KAAK,GAAGD,KAAK;EAC1E,MAAMJ,KAAK,GAAGU,GAAG,GAAGT,YAAY,CAACW,KAAK,CAACP,KAAK,EAAEK,GAAG,CAAC,GAAGT,YAAY;EAEjE,MAAMY,MAAM,GAAGb,KAAK,CAACc,MAAM,GAAG,CAAC,GAAG,IAAAC,oBAAY,EAACV,KAAK,GAAGD,KAAK,CAAC,GAAG,IAAI;EAEpE,MAAMY,IAAI,GAAG;IACTP,YAAY;IACZN,UAAU;IACVU;EACJ,CAAC;EAED,OAAO,CAACb,KAAK,EAAEgB,IAAI,CAAC;AACxB"}
|
package/utils/query.d.ts
CHANGED
|
@@ -27,6 +27,11 @@ export declare type DbItem<T> = T & {
|
|
|
27
27
|
*/
|
|
28
28
|
export declare const queryOne: <T>(params: QueryOneParams) => Promise<DbItem<T> | null>;
|
|
29
29
|
/**
|
|
30
|
-
* Will run the query to fetch the results no matter how
|
|
30
|
+
* Will run the query to fetch the results no matter how many iterations it needs to go through.
|
|
31
31
|
*/
|
|
32
32
|
export declare const queryAll: <T>(params: QueryAllParams) => Promise<DbItem<T>[]>;
|
|
33
|
+
/**
|
|
34
|
+
* Will run the query to fetch the results no matter how many iterations it needs to go through.
|
|
35
|
+
* Results of each iteration will be passed to the provided callback
|
|
36
|
+
*/
|
|
37
|
+
export declare const queryAllWithCallback: <T>(params: QueryAllParams, callback: (items: DbItem<T>[]) => Promise<void>) => Promise<void>;
|
package/utils/query.js
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
|
-
exports.queryOne = exports.queryAll = void 0;
|
|
9
|
-
|
|
7
|
+
exports.queryOne = exports.queryAllWithCallback = exports.queryAll = void 0;
|
|
10
8
|
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
|
-
|
|
12
9
|
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
|
-
|
|
14
10
|
/**
|
|
15
11
|
* Will run query only once. Pass the previous to run the query again to fetch new data.
|
|
16
12
|
* It returns the result and the items it found.
|
|
@@ -28,7 +24,6 @@ const query = async params => {
|
|
|
28
24
|
* In case there is no previous result we must make a new query.
|
|
29
25
|
* This is the first query on the given partition key.
|
|
30
26
|
*/
|
|
31
|
-
|
|
32
27
|
if (!previous) {
|
|
33
28
|
result = await entity.query(partitionKey, options);
|
|
34
29
|
} else if (typeof previous.next === "function") {
|
|
@@ -38,7 +33,6 @@ const query = async params => {
|
|
|
38
33
|
* and we return a null to keep the query from repeating.
|
|
39
34
|
*/
|
|
40
35
|
result = await previous.next();
|
|
41
|
-
|
|
42
36
|
if (result === false) {
|
|
43
37
|
return {
|
|
44
38
|
result: null,
|
|
@@ -59,15 +53,12 @@ const query = async params => {
|
|
|
59
53
|
/**
|
|
60
54
|
* We expect the result to contain an Items array and if not, something went wrong, very wrong.
|
|
61
55
|
*/
|
|
62
|
-
|
|
63
|
-
|
|
64
56
|
if (!result || !result.Items || !Array.isArray(result.Items)) {
|
|
65
57
|
throw new _error.default("Error when querying for content entries - no result.", "QUERY_ERROR", {
|
|
66
58
|
partitionKey,
|
|
67
59
|
options
|
|
68
60
|
});
|
|
69
61
|
}
|
|
70
|
-
|
|
71
62
|
return {
|
|
72
63
|
result,
|
|
73
64
|
items: result.Items
|
|
@@ -76,8 +67,6 @@ const query = async params => {
|
|
|
76
67
|
/**
|
|
77
68
|
* Will run the query to fetch the first possible item from the database.
|
|
78
69
|
*/
|
|
79
|
-
|
|
80
|
-
|
|
81
70
|
const queryOne = async params => {
|
|
82
71
|
const {
|
|
83
72
|
items
|
|
@@ -90,30 +79,41 @@ const queryOne = async params => {
|
|
|
90
79
|
return item ? item : null;
|
|
91
80
|
};
|
|
92
81
|
/**
|
|
93
|
-
* Will run the query to fetch the results no matter how
|
|
82
|
+
* Will run the query to fetch the results no matter how many iterations it needs to go through.
|
|
94
83
|
*/
|
|
95
|
-
|
|
96
|
-
|
|
97
84
|
exports.queryOne = queryOne;
|
|
98
|
-
|
|
99
85
|
const queryAll = async params => {
|
|
100
86
|
const items = [];
|
|
101
87
|
let results;
|
|
102
88
|
let previousResult = undefined;
|
|
103
|
-
|
|
104
89
|
while (results = await query((0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
|
|
105
90
|
previous: previousResult
|
|
106
91
|
}))) {
|
|
107
92
|
items.push(...results.items);
|
|
108
|
-
|
|
109
93
|
if (!results.result) {
|
|
110
94
|
return items;
|
|
111
95
|
}
|
|
112
|
-
|
|
113
96
|
previousResult = results.result;
|
|
114
97
|
}
|
|
115
|
-
|
|
116
98
|
return items;
|
|
117
99
|
};
|
|
118
100
|
|
|
119
|
-
|
|
101
|
+
/**
|
|
102
|
+
* Will run the query to fetch the results no matter how many iterations it needs to go through.
|
|
103
|
+
* Results of each iteration will be passed to the provided callback
|
|
104
|
+
*/
|
|
105
|
+
exports.queryAll = queryAll;
|
|
106
|
+
const queryAllWithCallback = async (params, callback) => {
|
|
107
|
+
let results;
|
|
108
|
+
let previousResult = undefined;
|
|
109
|
+
while (results = await query((0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
|
|
110
|
+
previous: previousResult
|
|
111
|
+
}))) {
|
|
112
|
+
if (!results.result) {
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
await callback(results.items);
|
|
116
|
+
previousResult = results.result;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
exports.queryAllWithCallback = queryAllWithCallback;
|
package/utils/query.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["query","params","entity","previous","partitionKey","options","result","next","items","Items","Array","isArray","WebinyError","queryOne","limit","item","shift","queryAll","results","previousResult","undefined","push"],"sources":["query.ts"],"sourcesContent":["import { queryOptions as DynamoDBToolboxQueryOptions } from \"dynamodb-toolbox/dist/classes/Table\";\nimport WebinyError from \"@webiny/error\";\nimport { Entity } from \"dynamodb-toolbox\";\n\nexport interface QueryAllParams {\n entity: Entity<any>;\n partitionKey: string;\n options?: DynamoDBToolboxQueryOptions;\n}\n\nexport interface QueryOneParams extends QueryAllParams {\n options?: Omit<DynamoDBToolboxQueryOptions, \"limit\">;\n}\n\nexport interface QueryParams extends QueryAllParams {\n previous?: any;\n}\n\nexport interface QueryResult<T> {\n result: any | null;\n items: T[];\n}\n\nexport type DbItem<T> = T & {\n PK: string;\n SK: string;\n TYPE: string;\n GSI1_PK?: string;\n GSI1_SK?: string;\n};\n\n/**\n * Will run query only once. Pass the previous to run the query again to fetch new data.\n * It returns the result and the items it found.\n * Result is required to fetch the items that were not fetched in the previous run.\n */\nconst query = async <T>(params: QueryParams): Promise<QueryResult<T>> => {\n const { entity, previous, partitionKey, options } = params;\n let result;\n /**\n * In case there is no previous result we must make a new query.\n * This is the first query on the given partition key.\n */\n if (!previous) {\n result = await entity.query(partitionKey, options);\n } else if (typeof previous.next === \"function\") {\n /**\n * In case we have a previous result and it has a next method, we run it.\n * In case result of the next method is false, it means it has nothing else to read\n * and we return a null to keep the query from repeating.\n */\n result = await previous.next();\n if (result === false) {\n return {\n result: null,\n items: []\n };\n }\n } else {\n /**\n * This could probably never happen but keep it here just in case to break the query loop.\n * Basically, either previous does not exist or it exists and it does not have the next method\n * and at that point a result returned will be null and loop should not start again.\n */\n return {\n result: null,\n items: []\n };\n }\n /**\n * We expect the result to contain an Items array and if not, something went wrong, very wrong.\n */\n if (!result || !result.Items || !Array.isArray(result.Items)) {\n throw new WebinyError(\n \"Error when querying for content entries - no result.\",\n \"QUERY_ERROR\",\n {\n partitionKey,\n options\n }\n );\n }\n return {\n result,\n items: result.Items\n };\n};\n/**\n * Will run the query to fetch the first possible item from the database.\n */\nexport const queryOne = async <T>(params: QueryOneParams): Promise<DbItem<T> | null> => {\n const { items } = await query<DbItem<T>>({\n ...params,\n options: {\n ...(params.options || {}),\n limit: 1\n }\n });\n const item = items.shift();\n return item ? item : null;\n};\n/**\n * Will run the query to fetch the results no matter how
|
|
1
|
+
{"version":3,"names":["query","params","entity","previous","partitionKey","options","result","next","items","Items","Array","isArray","WebinyError","queryOne","limit","item","shift","queryAll","results","previousResult","undefined","push","queryAllWithCallback","callback"],"sources":["query.ts"],"sourcesContent":["import { queryOptions as DynamoDBToolboxQueryOptions } from \"dynamodb-toolbox/dist/classes/Table\";\nimport WebinyError from \"@webiny/error\";\nimport { Entity } from \"dynamodb-toolbox\";\n\nexport interface QueryAllParams {\n entity: Entity<any>;\n partitionKey: string;\n options?: DynamoDBToolboxQueryOptions;\n}\n\nexport interface QueryOneParams extends QueryAllParams {\n options?: Omit<DynamoDBToolboxQueryOptions, \"limit\">;\n}\n\nexport interface QueryParams extends QueryAllParams {\n previous?: any;\n}\n\nexport interface QueryResult<T> {\n result: any | null;\n items: T[];\n}\n\nexport type DbItem<T> = T & {\n PK: string;\n SK: string;\n TYPE: string;\n GSI1_PK?: string;\n GSI1_SK?: string;\n};\n\n/**\n * Will run query only once. Pass the previous to run the query again to fetch new data.\n * It returns the result and the items it found.\n * Result is required to fetch the items that were not fetched in the previous run.\n */\nconst query = async <T>(params: QueryParams): Promise<QueryResult<T>> => {\n const { entity, previous, partitionKey, options } = params;\n let result;\n /**\n * In case there is no previous result we must make a new query.\n * This is the first query on the given partition key.\n */\n if (!previous) {\n result = await entity.query(partitionKey, options);\n } else if (typeof previous.next === \"function\") {\n /**\n * In case we have a previous result and it has a next method, we run it.\n * In case result of the next method is false, it means it has nothing else to read\n * and we return a null to keep the query from repeating.\n */\n result = await previous.next();\n if (result === false) {\n return {\n result: null,\n items: []\n };\n }\n } else {\n /**\n * This could probably never happen but keep it here just in case to break the query loop.\n * Basically, either previous does not exist or it exists and it does not have the next method\n * and at that point a result returned will be null and loop should not start again.\n */\n return {\n result: null,\n items: []\n };\n }\n /**\n * We expect the result to contain an Items array and if not, something went wrong, very wrong.\n */\n if (!result || !result.Items || !Array.isArray(result.Items)) {\n throw new WebinyError(\n \"Error when querying for content entries - no result.\",\n \"QUERY_ERROR\",\n {\n partitionKey,\n options\n }\n );\n }\n return {\n result,\n items: result.Items\n };\n};\n/**\n * Will run the query to fetch the first possible item from the database.\n */\nexport const queryOne = async <T>(params: QueryOneParams): Promise<DbItem<T> | null> => {\n const { items } = await query<DbItem<T>>({\n ...params,\n options: {\n ...(params.options || {}),\n limit: 1\n }\n });\n const item = items.shift();\n return item ? item : null;\n};\n/**\n * Will run the query to fetch the results no matter how many iterations it needs to go through.\n */\nexport const queryAll = async <T>(params: QueryAllParams): Promise<DbItem<T>[]> => {\n const items: DbItem<T>[] = [];\n let results: QueryResult<DbItem<T>>;\n let previousResult: any = undefined;\n while ((results = await query({ ...params, previous: previousResult }))) {\n items.push(...results.items);\n if (!results.result) {\n return items;\n }\n previousResult = results.result;\n }\n return items;\n};\n\n/**\n * Will run the query to fetch the results no matter how many iterations it needs to go through.\n * Results of each iteration will be passed to the provided callback\n */\nexport const queryAllWithCallback = async <T>(\n params: QueryAllParams,\n callback: (items: DbItem<T>[]) => Promise<void>\n): Promise<void> => {\n let results: QueryResult<DbItem<T>>;\n let previousResult: any = undefined;\n while ((results = await query({ ...params, previous: previousResult }))) {\n if (!results.result) {\n break;\n }\n await callback(results.items);\n previousResult = results.result;\n }\n};\n"],"mappings":";;;;;;;;AACA;AA8BA;AACA;AACA;AACA;AACA;AACA,MAAMA,KAAK,GAAG,MAAUC,MAAmB,IAA8B;EACrE,MAAM;IAAEC,MAAM;IAAEC,QAAQ;IAAEC,YAAY;IAAEC;EAAQ,CAAC,GAAGJ,MAAM;EAC1D,IAAIK,MAAM;EACV;AACJ;AACA;AACA;EACI,IAAI,CAACH,QAAQ,EAAE;IACXG,MAAM,GAAG,MAAMJ,MAAM,CAACF,KAAK,CAACI,YAAY,EAAEC,OAAO,CAAC;EACtD,CAAC,MAAM,IAAI,OAA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