@webiny/db-dynamodb 0.0.0-unstable.78f581c1d2 → 0.0.0-unstable.7be00a75a9
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/DynamoDbDriver.d.ts +18 -32
- package/DynamoDbDriver.js +215 -261
- package/DynamoDbDriver.js.map +1 -1
- package/README.md +6 -62
- package/exports/api/db.d.ts +2 -0
- package/exports/api/db.js +2 -0
- package/feature/FilterUtil/FilterUtil.d.ts +11 -0
- package/feature/FilterUtil/FilterUtil.js +26 -0
- package/feature/FilterUtil/FilterUtil.js.map +1 -0
- package/feature/FilterUtil/abstractions/FilterUtil.d.ts +17 -0
- package/feature/FilterUtil/abstractions/FilterUtil.js +5 -0
- package/feature/FilterUtil/abstractions/FilterUtil.js.map +1 -0
- package/feature/FilterUtil/createFilters.d.ts +22 -0
- package/feature/FilterUtil/createFilters.js +92 -0
- package/feature/FilterUtil/createFilters.js.map +1 -0
- package/feature/FilterUtil/extractWhereArgs.d.ts +7 -0
- package/feature/FilterUtil/extractWhereArgs.js +20 -0
- package/feature/FilterUtil/extractWhereArgs.js.map +1 -0
- package/feature/FilterUtil/feature.d.ts +4 -0
- package/feature/FilterUtil/feature.js +11 -0
- package/feature/FilterUtil/feature.js.map +1 -0
- package/feature/FilterUtil/index.d.ts +2 -0
- package/feature/FilterUtil/index.js +2 -0
- package/feature/ValueFilter/ValueFilterRegistry.d.ts +12 -0
- package/feature/ValueFilter/ValueFilterRegistry.js +27 -0
- package/feature/ValueFilter/ValueFilterRegistry.js.map +1 -0
- package/feature/ValueFilter/abstractions/ValueFilter.d.ts +22 -0
- package/feature/ValueFilter/abstractions/ValueFilter.js +5 -0
- package/feature/ValueFilter/abstractions/ValueFilter.js.map +1 -0
- package/feature/ValueFilter/abstractions/ValueFilterRegistry.d.ts +10 -0
- package/feature/ValueFilter/abstractions/ValueFilterRegistry.js +5 -0
- package/feature/ValueFilter/abstractions/ValueFilterRegistry.js.map +1 -0
- package/feature/ValueFilter/feature.d.ts +4 -0
- package/feature/ValueFilter/feature.js +33 -0
- package/feature/ValueFilter/feature.js.map +1 -0
- package/feature/ValueFilter/filters/AndInFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/AndInFilter.js +28 -0
- package/feature/ValueFilter/filters/AndInFilter.js.map +1 -0
- package/feature/ValueFilter/filters/BetweenFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/BetweenFilter.js +31 -0
- package/feature/ValueFilter/filters/BetweenFilter.js.map +1 -0
- package/feature/ValueFilter/filters/ContainsFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/ContainsFilter.js +42 -0
- package/feature/ValueFilter/filters/ContainsFilter.js.map +1 -0
- package/feature/ValueFilter/filters/EqFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/EqFilter.js +24 -0
- package/feature/ValueFilter/filters/EqFilter.js.map +1 -0
- package/feature/ValueFilter/filters/FuzzyFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/FuzzyFilter.js +37 -0
- package/feature/ValueFilter/filters/FuzzyFilter.js.map +1 -0
- package/feature/ValueFilter/filters/GtFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/GtFilter.js +22 -0
- package/feature/ValueFilter/filters/GtFilter.js.map +1 -0
- package/feature/ValueFilter/filters/GteFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/GteFilter.js +22 -0
- package/feature/ValueFilter/filters/GteFilter.js.map +1 -0
- package/feature/ValueFilter/filters/InFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/InFilter.js +28 -0
- package/feature/ValueFilter/filters/InFilter.js.map +1 -0
- package/feature/ValueFilter/filters/LtFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/LtFilter.js +22 -0
- package/feature/ValueFilter/filters/LtFilter.js.map +1 -0
- package/feature/ValueFilter/filters/LteFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/LteFilter.js +22 -0
- package/feature/ValueFilter/filters/LteFilter.js.map +1 -0
- package/feature/ValueFilter/filters/StartsWithFilter.d.ts +11 -0
- package/feature/ValueFilter/filters/StartsWithFilter.js +29 -0
- package/feature/ValueFilter/filters/StartsWithFilter.js.map +1 -0
- package/feature/ValueFilter/index.d.ts +3 -0
- package/feature/ValueFilter/index.js +3 -0
- package/index.d.ts +5 -1
- package/index.js +12 -14
- package/index.js.map +1 -1
- package/package.json +32 -30
- package/plugins/definitions/DateTimeTransformPlugin.d.ts +2 -1
- package/plugins/definitions/DateTimeTransformPlugin.js +21 -50
- package/plugins/definitions/DateTimeTransformPlugin.js.map +1 -1
- package/plugins/definitions/FieldPlugin.d.ts +2 -2
- package/plugins/definitions/FieldPlugin.js +36 -68
- package/plugins/definitions/FieldPlugin.js.map +1 -1
- package/plugins/definitions/TimeTransformPlugin.d.ts +2 -1
- package/plugins/definitions/TimeTransformPlugin.js +29 -56
- package/plugins/definitions/TimeTransformPlugin.js.map +1 -1
- package/plugins/definitions/ValueTransformPlugin.d.ts +2 -7
- package/plugins/definitions/ValueTransformPlugin.js +20 -46
- package/plugins/definitions/ValueTransformPlugin.js.map +1 -1
- package/plugins/definitions/assignFields.js +15 -34
- package/plugins/definitions/assignFields.js.map +1 -1
- package/plugins/index.d.ts +5 -2
- package/plugins/index.js +5 -19
- package/store/entity.d.ts +6 -0
- package/store/entity.js +8 -0
- package/store/entity.js.map +1 -0
- package/store/keys.d.ts +6 -0
- package/store/keys.js +9 -0
- package/store/keys.js.map +1 -0
- package/store/types.d.ts +6 -0
- package/store/types.js +0 -0
- package/toolbox.d.ts +60 -0
- package/toolbox.js +1 -0
- package/types.d.ts +10 -23
- package/types.js +0 -5
- package/utils/batch/batchRead.d.ts +15 -0
- package/utils/batch/batchRead.js +45 -0
- package/utils/batch/batchRead.js.map +1 -0
- package/utils/batch/batchWrite.d.ts +13 -0
- package/utils/batch/batchWrite.js +39 -0
- package/utils/batch/batchWrite.js.map +1 -0
- package/utils/batch/index.d.ts +3 -0
- package/utils/batch/index.js +3 -0
- package/utils/batch/types.d.ts +30 -0
- package/utils/batch/types.js +0 -0
- package/utils/cleanup.d.ts +3 -3
- package/utils/cleanup.js +36 -41
- package/utils/cleanup.js.map +1 -1
- package/utils/count.d.ts +2 -0
- package/utils/count.js +11 -0
- package/utils/count.js.map +1 -0
- package/utils/createEntity.d.ts +36 -0
- package/utils/createEntity.js +82 -0
- package/utils/createEntity.js.map +1 -0
- package/utils/createTable.d.ts +11 -0
- package/utils/createTable.js +29 -0
- package/utils/createTable.js.map +1 -0
- package/utils/cursor.js +8 -23
- package/utils/cursor.js.map +1 -1
- package/utils/delete.d.ts +10 -0
- package/utils/delete.js +9 -0
- package/utils/delete.js.map +1 -0
- package/utils/entity/Entity.d.ts +28 -0
- package/utils/entity/Entity.js +95 -0
- package/utils/entity/Entity.js.map +1 -0
- package/utils/entity/EntityReadBatch.d.ts +18 -0
- package/utils/entity/EntityReadBatch.js +31 -0
- package/utils/entity/EntityReadBatch.js.map +1 -0
- package/utils/entity/EntityReadBatchBuilder.d.ts +10 -0
- package/utils/entity/EntityReadBatchBuilder.js +18 -0
- package/utils/entity/EntityReadBatchBuilder.js.map +1 -0
- package/utils/entity/EntityWriteBatch.d.ts +23 -0
- package/utils/entity/EntityWriteBatch.js +44 -0
- package/utils/entity/EntityWriteBatch.js.map +1 -0
- package/utils/entity/EntityWriteBatchBuilder.d.ts +11 -0
- package/utils/entity/EntityWriteBatchBuilder.js +19 -0
- package/utils/entity/EntityWriteBatchBuilder.js.map +1 -0
- package/utils/entity/getEntity.d.ts +4 -0
- package/utils/entity/getEntity.js +11 -0
- package/utils/entity/getEntity.js.map +1 -0
- package/utils/entity/index.d.ts +7 -0
- package/utils/entity/index.js +7 -0
- package/utils/entity/types.d.ts +75 -0
- package/utils/entity/types.js +0 -0
- package/utils/get.d.ts +8 -6
- package/utils/get.js +15 -28
- package/utils/get.js.map +1 -1
- package/utils/index.d.ts +13 -0
- package/utils/index.js +13 -0
- package/utils/put.d.ts +12 -0
- package/utils/put.js +10 -0
- package/utils/put.js.map +1 -0
- package/utils/query.d.ts +20 -14
- package/utils/query.js +84 -108
- package/utils/query.js.map +1 -1
- package/utils/scan.d.ts +38 -0
- package/utils/scan.js +56 -0
- package/utils/scan.js.map +1 -0
- package/utils/sort.d.ts +1 -1
- package/utils/sort.js +38 -84
- package/utils/sort.js.map +1 -1
- package/utils/table/Table.d.ts +10 -0
- package/utils/table/Table.js +28 -0
- package/utils/table/Table.js.map +1 -0
- package/utils/table/TableReadBatch.d.ts +19 -0
- package/utils/table/TableReadBatch.js +46 -0
- package/utils/table/TableReadBatch.js.map +1 -0
- package/utils/table/TableWriteBatch.d.ts +21 -0
- package/utils/table/TableWriteBatch.js +52 -0
- package/utils/table/TableWriteBatch.js.map +1 -0
- package/utils/table/index.d.ts +4 -0
- package/utils/table/index.js +4 -0
- package/utils/table/types.d.ts +39 -0
- package/utils/table/types.js +0 -0
- package/BatchProcess.d.ts +0 -47
- package/BatchProcess.js +0 -198
- package/BatchProcess.js.map +0 -1
- package/QueryGenerator.d.ts +0 -21
- package/QueryGenerator.js +0 -78
- package/QueryGenerator.js.map +0 -1
- package/operators/comparison/beginsWith.d.ts +0 -3
- package/operators/comparison/beginsWith.js +0 -24
- package/operators/comparison/beginsWith.js.map +0 -1
- package/operators/comparison/between.d.ts +0 -3
- package/operators/comparison/between.js +0 -30
- package/operators/comparison/between.js.map +0 -1
- package/operators/comparison/eq.d.ts +0 -3
- package/operators/comparison/eq.js +0 -34
- package/operators/comparison/eq.js.map +0 -1
- package/operators/comparison/gt.d.ts +0 -3
- package/operators/comparison/gt.js +0 -24
- package/operators/comparison/gt.js.map +0 -1
- package/operators/comparison/gte.d.ts +0 -3
- package/operators/comparison/gte.js +0 -24
- package/operators/comparison/gte.js.map +0 -1
- package/operators/comparison/lt.d.ts +0 -3
- package/operators/comparison/lt.js +0 -24
- package/operators/comparison/lt.js.map +0 -1
- package/operators/comparison/lte.d.ts +0 -3
- package/operators/comparison/lte.js +0 -24
- package/operators/comparison/lte.js.map +0 -1
- package/operators/index.d.ts +0 -12
- package/operators/index.js +0 -39
- package/operators/index.js.map +0 -1
- package/operators/logical/and.d.ts +0 -3
- package/operators/logical/and.js +0 -63
- package/operators/logical/and.js.map +0 -1
- package/operators/logical/or.d.ts +0 -3
- package/operators/logical/or.js +0 -63
- package/operators/logical/or.js.map +0 -1
- package/plugins/definitions/AttributePlugin.d.ts +0 -20
- package/plugins/definitions/AttributePlugin.js +0 -61
- package/plugins/definitions/AttributePlugin.js.map +0 -1
- package/plugins/definitions/FieldPathPlugin.d.ts +0 -22
- package/plugins/definitions/FieldPathPlugin.js +0 -46
- package/plugins/definitions/FieldPathPlugin.js.map +0 -1
- package/plugins/definitions/NumberTransformPlugin.d.ts +0 -4
- package/plugins/definitions/NumberTransformPlugin.js +0 -45
- package/plugins/definitions/NumberTransformPlugin.js.map +0 -1
- package/plugins/definitions/ValueFilterPlugin.d.ts +0 -20
- package/plugins/definitions/ValueFilterPlugin.js +0 -49
- package/plugins/definitions/ValueFilterPlugin.js.map +0 -1
- package/plugins/filters/andIn.d.ts +0 -3
- package/plugins/filters/andIn.js +0 -35
- package/plugins/filters/andIn.js.map +0 -1
- package/plugins/filters/between.d.ts +0 -3
- package/plugins/filters/between.js +0 -36
- package/plugins/filters/between.js.map +0 -1
- package/plugins/filters/contains.d.ts +0 -3
- package/plugins/filters/contains.js +0 -32
- package/plugins/filters/contains.js.map +0 -1
- package/plugins/filters/eq.d.ts +0 -3
- package/plugins/filters/eq.js +0 -33
- package/plugins/filters/eq.js.map +0 -1
- package/plugins/filters/fuzzy.d.ts +0 -3
- package/plugins/filters/fuzzy.js +0 -30
- package/plugins/filters/fuzzy.js.map +0 -1
- package/plugins/filters/gt.d.ts +0 -3
- package/plugins/filters/gt.js +0 -20
- package/plugins/filters/gt.js.map +0 -1
- package/plugins/filters/gte.d.ts +0 -3
- package/plugins/filters/gte.js +0 -20
- package/plugins/filters/gte.js.map +0 -1
- package/plugins/filters/in.d.ts +0 -3
- package/plugins/filters/in.js +0 -35
- package/plugins/filters/in.js.map +0 -1
- package/plugins/filters/index.d.ts +0 -2
- package/plugins/filters/index.js +0 -34
- package/plugins/filters/index.js.map +0 -1
- package/plugins/filters/lt.d.ts +0 -3
- package/plugins/filters/lt.js +0 -20
- package/plugins/filters/lt.js.map +0 -1
- package/plugins/filters/lte.d.ts +0 -3
- package/plugins/filters/lte.js +0 -20
- package/plugins/filters/lte.js.map +0 -1
- package/plugins/filters/startsWith.d.ts +0 -3
- package/plugins/filters/startsWith.js +0 -33
- package/plugins/filters/startsWith.js.map +0 -1
- package/plugins/index.js.map +0 -1
- package/statements/createKeyConditionExpressionArgs.d.ts +0 -15
- package/statements/createKeyConditionExpressionArgs.js +0 -52
- package/statements/createKeyConditionExpressionArgs.js.map +0 -1
- package/statements/processStatement.d.ts +0 -3
- package/statements/processStatement.js +0 -42
- package/statements/processStatement.js.map +0 -1
- package/types.js.map +0 -1
- package/utils/attributes.d.ts +0 -10
- package/utils/attributes.js +0 -31
- package/utils/attributes.js.map +0 -1
- package/utils/batchRead.d.ts +0 -15
- package/utils/batchRead.js +0 -89
- package/utils/batchRead.js.map +0 -1
- package/utils/batchWrite.d.ts +0 -17
- package/utils/batchWrite.js +0 -30
- package/utils/batchWrite.js.map +0 -1
- package/utils/filter.d.ts +0 -12
- package/utils/filter.js +0 -213
- package/utils/filter.js.map +0 -1
- package/utils/listResponse.d.ts +0 -13
- package/utils/listResponse.js +0 -33
- package/utils/listResponse.js.map +0 -1
- package/utils/table.d.ts +0 -7
- package/utils/table.js +0 -27
- package/utils/table.js.map +0 -1
package/utils/put.js
ADDED
package/utils/put.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils/put.js","sources":["../../src/utils/put.ts"],"sourcesContent":["import type { Entity } from \"~/toolbox.js\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\n\nexport type IPutParamsItem<T extends GenericRecord = GenericRecord> = {\n PK: string;\n SK: string;\n [key: string]: any;\n} & T;\n\nexport interface IPutParams<T extends GenericRecord = GenericRecord> {\n entity: Entity;\n item: IPutParamsItem<T>;\n}\n\nexport const put = async <T extends GenericRecord = GenericRecord>(params: IPutParams<T>) => {\n const { entity, item } = params;\n\n return await entity.put(item, {\n execute: true,\n strictSchemaCheck: false\n });\n};\n"],"names":["put","params","entity","item"],"mappings":"AAcO,MAAMA,MAAM,OAAgDC;IAC/D,MAAM,EAAEC,MAAM,EAAEC,IAAI,EAAE,GAAGF;IAEzB,OAAO,MAAMC,OAAO,GAAG,CAACC,MAAM;QAC1B,SAAS;QACT,mBAAmB;IACvB;AACJ"}
|
package/utils/query.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import type { Entity, EntityQueryOptions } from "../toolbox.js";
|
|
2
|
+
import type { GenericRecord } from "@webiny/api/types.js";
|
|
3
3
|
export interface QueryAllParams {
|
|
4
4
|
entity: Entity<any>;
|
|
5
5
|
partitionKey: string;
|
|
6
|
-
options?:
|
|
6
|
+
options?: EntityQueryOptions;
|
|
7
7
|
}
|
|
8
8
|
export interface QueryOneParams extends QueryAllParams {
|
|
9
|
-
options?: Omit<
|
|
9
|
+
options?: Omit<EntityQueryOptions, "limit">;
|
|
10
10
|
}
|
|
11
11
|
export interface QueryParams extends QueryAllParams {
|
|
12
12
|
previous?: any;
|
|
@@ -15,18 +15,24 @@ export interface QueryResult<T> {
|
|
|
15
15
|
result: any | null;
|
|
16
16
|
items: T[];
|
|
17
17
|
}
|
|
18
|
-
export declare type DbItem<T> = T & {
|
|
19
|
-
PK: string;
|
|
20
|
-
SK: string;
|
|
21
|
-
TYPE: string;
|
|
22
|
-
GSI1_PK?: string;
|
|
23
|
-
GSI1_SK?: string;
|
|
24
|
-
};
|
|
25
18
|
/**
|
|
26
19
|
* Will run the query to fetch the first possible item from the database.
|
|
27
20
|
*/
|
|
28
|
-
export declare const queryOne: <T>(params: QueryOneParams) => Promise<
|
|
21
|
+
export declare const queryOne: <T>(params: QueryOneParams) => Promise<T | null>;
|
|
22
|
+
export declare const queryOneClean: <T>(params: QueryOneParams) => Promise<T | null>;
|
|
29
23
|
/**
|
|
30
|
-
* Will run the query to fetch the results no matter how
|
|
24
|
+
* Will run the query to fetch the results no matter how many iterations it needs to go through.
|
|
31
25
|
*/
|
|
32
|
-
export declare const queryAll: <T>(params: QueryAllParams) => Promise<
|
|
26
|
+
export declare const queryAll: <T>(params: QueryAllParams) => Promise<T[]>;
|
|
27
|
+
export declare const queryAllClean: <T>(params: QueryAllParams) => Promise<T[]>;
|
|
28
|
+
export interface IQueryPageResponse<T> {
|
|
29
|
+
items: T[];
|
|
30
|
+
lastEvaluatedKey: GenericRecord;
|
|
31
|
+
}
|
|
32
|
+
export declare const queryPerPage: <T>(params: QueryAllParams) => Promise<IQueryPageResponse<T>>;
|
|
33
|
+
export declare const queryPerPageClean: <T>(params: QueryAllParams) => Promise<IQueryPageResponse<T>>;
|
|
34
|
+
/**
|
|
35
|
+
* Will run the query to fetch the results no matter how many iterations it needs to go through.
|
|
36
|
+
* Results of each iteration will be passed to the provided callback
|
|
37
|
+
*/
|
|
38
|
+
export declare const queryAllWithCallback: <T>(params: QueryAllParams, callback: (items: T[]) => Promise<void>) => Promise<void>;
|
package/utils/query.js
CHANGED
|
@@ -1,119 +1,95 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
});
|
|
8
|
-
exports.queryOne = exports.queryAll = void 0;
|
|
9
|
-
|
|
10
|
-
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
|
-
|
|
12
|
-
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Will run query only once. Pass the previous to run the query again to fetch new data.
|
|
16
|
-
* It returns the result and the items it found.
|
|
17
|
-
* Result is required to fetch the items that were not fetched in the previous run.
|
|
18
|
-
*/
|
|
19
|
-
const query = async params => {
|
|
20
|
-
const {
|
|
21
|
-
entity,
|
|
22
|
-
previous,
|
|
23
|
-
partitionKey,
|
|
24
|
-
options
|
|
25
|
-
} = params;
|
|
26
|
-
let result;
|
|
27
|
-
/**
|
|
28
|
-
* In case there is no previous result we must make a new query.
|
|
29
|
-
* This is the first query on the given partition key.
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
if (!previous) {
|
|
33
|
-
result = await entity.query(partitionKey, options);
|
|
34
|
-
} else if (typeof previous.next === "function") {
|
|
35
|
-
/**
|
|
36
|
-
* In case we have a previous result and it has a next method, we run it.
|
|
37
|
-
* In case result of the next method is false, it means it has nothing else to read
|
|
38
|
-
* and we return a null to keep the query from repeating.
|
|
39
|
-
*/
|
|
40
|
-
result = await previous.next();
|
|
41
|
-
|
|
42
|
-
if (result === false) {
|
|
43
|
-
return {
|
|
1
|
+
import error from "@webiny/error";
|
|
2
|
+
import { cleanupItem, cleanupItems } from "./cleanup.js";
|
|
3
|
+
const query = async (params)=>{
|
|
4
|
+
const { entity, previous, partitionKey, options } = params;
|
|
5
|
+
let result;
|
|
6
|
+
if (previous) if ("function" != typeof previous.next) return {
|
|
44
7
|
result: null,
|
|
45
8
|
items: []
|
|
46
|
-
|
|
9
|
+
};
|
|
10
|
+
else {
|
|
11
|
+
result = await previous.next();
|
|
12
|
+
if (false === result) return {
|
|
13
|
+
result: null,
|
|
14
|
+
items: []
|
|
15
|
+
};
|
|
47
16
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
*/
|
|
17
|
+
else result = await entity.query(partitionKey, options);
|
|
18
|
+
if (!result || !result.Items || !Array.isArray(result.Items)) throw new error("Error when querying for content entries - no result.", "QUERY_ERROR", {
|
|
19
|
+
partitionKey,
|
|
20
|
+
options
|
|
21
|
+
});
|
|
54
22
|
return {
|
|
55
|
-
|
|
56
|
-
|
|
23
|
+
result,
|
|
24
|
+
items: result.Items
|
|
57
25
|
};
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
partitionKey,
|
|
67
|
-
options
|
|
26
|
+
};
|
|
27
|
+
const queryOne = async (params)=>{
|
|
28
|
+
const { items } = await query({
|
|
29
|
+
...params,
|
|
30
|
+
options: {
|
|
31
|
+
...params.options || {},
|
|
32
|
+
limit: 1
|
|
33
|
+
}
|
|
68
34
|
});
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return {
|
|
72
|
-
result,
|
|
73
|
-
items: result.Items
|
|
74
|
-
};
|
|
35
|
+
const item = items.shift();
|
|
36
|
+
return item ? item : null;
|
|
75
37
|
};
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const queryOne = async params => {
|
|
82
|
-
const {
|
|
83
|
-
items
|
|
84
|
-
} = await query((0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
|
|
85
|
-
options: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, params.options || {}), {}, {
|
|
86
|
-
limit: 1
|
|
87
|
-
})
|
|
88
|
-
}));
|
|
89
|
-
const item = items.shift();
|
|
90
|
-
return item ? item : null;
|
|
38
|
+
const queryOneClean = async (params)=>{
|
|
39
|
+
const result = await queryOne(params);
|
|
40
|
+
if (!result) return null;
|
|
41
|
+
return cleanupItem(params.entity, result);
|
|
91
42
|
};
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
43
|
+
const queryAll = async (params)=>{
|
|
44
|
+
const items = [];
|
|
45
|
+
let results;
|
|
46
|
+
let previousResult;
|
|
47
|
+
while(results = await query({
|
|
48
|
+
...params,
|
|
49
|
+
previous: previousResult
|
|
50
|
+
})){
|
|
51
|
+
items.push(...results.items);
|
|
52
|
+
if (!results.result) break;
|
|
53
|
+
previousResult = results.result;
|
|
54
|
+
}
|
|
55
|
+
return items;
|
|
56
|
+
};
|
|
57
|
+
const queryAllClean = async (params)=>{
|
|
58
|
+
const results = await queryAll(params);
|
|
59
|
+
return cleanupItems(params.entity, results);
|
|
60
|
+
};
|
|
61
|
+
const queryPerPage = async (params)=>{
|
|
62
|
+
const result = await query({
|
|
63
|
+
...params,
|
|
64
|
+
options: {
|
|
65
|
+
...params.options,
|
|
66
|
+
limit: params.options?.limit || 50
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
return {
|
|
70
|
+
items: result.items,
|
|
71
|
+
lastEvaluatedKey: result.result?.LastEvaluatedKey
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
const queryPerPageClean = async (params)=>{
|
|
75
|
+
const result = await queryPerPage(params);
|
|
76
|
+
return {
|
|
77
|
+
items: cleanupItems(params.entity, result.items),
|
|
78
|
+
lastEvaluatedKey: result.lastEvaluatedKey
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
const queryAllWithCallback = async (params, callback)=>{
|
|
82
|
+
let results;
|
|
83
|
+
let previousResult;
|
|
84
|
+
while(results = await query({
|
|
85
|
+
...params,
|
|
86
|
+
previous: previousResult
|
|
87
|
+
})){
|
|
88
|
+
if (!results.result) break;
|
|
89
|
+
await callback(results.items);
|
|
90
|
+
previousResult = results.result;
|
|
111
91
|
}
|
|
112
|
-
|
|
113
|
-
previousResult = results.result;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return items;
|
|
117
92
|
};
|
|
93
|
+
export { queryAll, queryAllClean, queryAllWithCallback, queryOne, queryOneClean, queryPerPage, queryPerPageClean };
|
|
118
94
|
|
|
119
|
-
|
|
95
|
+
//# sourceMappingURL=query.js.map
|
package/utils/query.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"utils/query.js","sources":["../../src/utils/query.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport type { Entity, EntityQueryOptions } from \"~/toolbox.js\";\nimport { cleanupItem, cleanupItems } from \"~/utils/cleanup.js\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\n\nexport interface QueryAllParams {\n entity: Entity<any>;\n partitionKey: string;\n options?: EntityQueryOptions;\n}\n\nexport interface QueryOneParams extends QueryAllParams {\n options?: Omit<EntityQueryOptions, \"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\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 // @ts-expect-error\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<T | null> => {\n const { items } = await query<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\nexport const queryOneClean = async <T>(params: QueryOneParams): Promise<T | null> => {\n const result = await queryOne<T>(params);\n if (!result) {\n return null;\n }\n return cleanupItem(params.entity, result);\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<T[]> => {\n const items: T[] = [];\n let results: QueryResult<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\nexport const queryAllClean = async <T>(params: QueryAllParams): Promise<T[]> => {\n const results = await queryAll<T>(params);\n return cleanupItems(params.entity, results);\n};\n\nexport interface IQueryPageResponse<T> {\n items: T[];\n lastEvaluatedKey: GenericRecord;\n}\n\nexport const queryPerPage = async <T>(params: QueryAllParams): Promise<IQueryPageResponse<T>> => {\n const result = await query<T>({\n ...params,\n options: {\n ...params.options,\n limit: params.options?.limit || 50\n }\n });\n\n return {\n items: result.items,\n lastEvaluatedKey: result.result?.LastEvaluatedKey\n };\n};\n\nexport const queryPerPageClean = async <T>(\n params: QueryAllParams\n): Promise<IQueryPageResponse<T>> => {\n const result = await queryPerPage<T>(params);\n return {\n items: cleanupItems<T>(params.entity, result.items),\n lastEvaluatedKey: result.lastEvaluatedKey\n };\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: T[]) => Promise<void>\n): Promise<void> => {\n let results: QueryResult<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"],"names":["query","params","entity","previous","partitionKey","options","result","Array","WebinyError","queryOne","items","item","queryOneClean","cleanupItem","queryAll","results","previousResult","queryAllClean","cleanupItems","queryPerPage","queryPerPageClean","queryAllWithCallback","callback"],"mappings":";;AA6BA,MAAMA,QAAQ,OAAUC;IACpB,MAAM,EAAEC,MAAM,EAAEC,QAAQ,EAAEC,YAAY,EAAEC,OAAO,EAAE,GAAGJ;IACpD,IAAIK;IAKJ,IAAKH,UAGE,IAAI,AAAyB,cAAzB,OAAOA,SAAS,IAAI,EAmB3B,OAAO;QACH,QAAQ;QACR,OAAO,EAAE;IACb;SAtB4C;QAM5CG,SAAS,MAAMH,SAAS,IAAI;QAC5B,IAAIG,AAAW,UAAXA,QACA,OAAO;YACH,QAAQ;YACR,OAAO,EAAE;QACb;IAER;SAdIA,SAAS,MAAMJ,OAAO,KAAK,CAACE,cAAcC;IA4B9C,IAAI,CAACC,UAAU,CAACA,OAAO,KAAK,IAAI,CAACC,MAAM,OAAO,CAACD,OAAO,KAAK,GACvD,MAAM,IAAIE,MACN,wDACA,eACA;QACIJ;QACAC;IACJ;IAGR,OAAO;QACHC;QACA,OAAOA,OAAO,KAAK;IACvB;AACJ;AAIO,MAAMG,WAAW,OAAUR;IAC9B,MAAM,EAAES,KAAK,EAAE,GAAG,MAAMV,MAAS;QAC7B,GAAGC,MAAM;QACT,SAAS;YACL,GAAIA,OAAO,OAAO,IAAI,CAAC,CAAC;YACxB,OAAO;QACX;IACJ;IACA,MAAMU,OAAOD,MAAM,KAAK;IACxB,OAAOC,OAAOA,OAAO;AACzB;AAEO,MAAMC,gBAAgB,OAAUX;IACnC,MAAMK,SAAS,MAAMG,SAAYR;IACjC,IAAI,CAACK,QACD,OAAO;IAEX,OAAOO,YAAYZ,OAAO,MAAM,EAAEK;AACtC;AAIO,MAAMQ,WAAW,OAAUb;IAC9B,MAAMS,QAAa,EAAE;IACrB,IAAIK;IACJ,IAAIC;IACJ,MAAQD,UAAU,MAAMf,MAAM;QAAE,GAAGC,MAAM;QAAE,UAAUe;IAAe,GAAK;QACrEN,MAAM,IAAI,IAAIK,QAAQ,KAAK;QAC3B,IAAI,CAACA,QAAQ,MAAM,EACf;QAEJC,iBAAiBD,QAAQ,MAAM;IACnC;IACA,OAAOL;AACX;AAEO,MAAMO,gBAAgB,OAAUhB;IACnC,MAAMc,UAAU,MAAMD,SAAYb;IAClC,OAAOiB,aAAajB,OAAO,MAAM,EAAEc;AACvC;AAOO,MAAMI,eAAe,OAAUlB;IAClC,MAAMK,SAAS,MAAMN,MAAS;QAC1B,GAAGC,MAAM;QACT,SAAS;YACL,GAAGA,OAAO,OAAO;YACjB,OAAOA,OAAO,OAAO,EAAE,SAAS;QACpC;IACJ;IAEA,OAAO;QACH,OAAOK,OAAO,KAAK;QACnB,kBAAkBA,OAAO,MAAM,EAAE;IACrC;AACJ;AAEO,MAAMc,oBAAoB,OAC7BnB;IAEA,MAAMK,SAAS,MAAMa,aAAgBlB;IACrC,OAAO;QACH,OAAOiB,aAAgBjB,OAAO,MAAM,EAAEK,OAAO,KAAK;QAClD,kBAAkBA,OAAO,gBAAgB;IAC7C;AACJ;AAMO,MAAMe,uBAAuB,OAChCpB,QACAqB;IAEA,IAAIP;IACJ,IAAIC;IACJ,MAAQD,UAAU,MAAMf,MAAM;QAAE,GAAGC,MAAM;QAAE,UAAUe;IAAe,GAAK;QACrE,IAAI,CAACD,QAAQ,MAAM,EACf;QAEJ,MAAMO,SAASP,QAAQ,KAAK;QAC5BC,iBAAiBD,QAAQ,MAAM;IACnC;AACJ"}
|
package/utils/scan.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ScanInput, ScanOutput } from "@webiny/aws-sdk/client-dynamodb/index.js";
|
|
2
|
+
import type { Entity, ScanOptions, TableDef } from "../toolbox.js";
|
|
3
|
+
import type { ExecuteWithRetryOptions } from "@webiny/utils";
|
|
4
|
+
export type { ScanOptions, ScanInput, ScanOutput };
|
|
5
|
+
export interface BaseScanParams {
|
|
6
|
+
options?: ScanOptions;
|
|
7
|
+
params?: Partial<ScanInput>;
|
|
8
|
+
}
|
|
9
|
+
export interface ScanWithTable extends BaseScanParams {
|
|
10
|
+
table: TableDef;
|
|
11
|
+
entity?: never;
|
|
12
|
+
}
|
|
13
|
+
export interface ScanWithEntity extends BaseScanParams {
|
|
14
|
+
entity: Entity;
|
|
15
|
+
table?: never;
|
|
16
|
+
}
|
|
17
|
+
export type ScanParams = ScanWithTable | ScanWithEntity;
|
|
18
|
+
export interface ScanResponse<T = any> {
|
|
19
|
+
items: T[];
|
|
20
|
+
count?: number;
|
|
21
|
+
scannedCount?: number;
|
|
22
|
+
lastEvaluatedKey?: ScanOutput["LastEvaluatedKey"];
|
|
23
|
+
next?: () => Promise<ScanResponse<T>>;
|
|
24
|
+
requestId: string;
|
|
25
|
+
error: any;
|
|
26
|
+
}
|
|
27
|
+
export type ScanDbItem<T> = T & {
|
|
28
|
+
PK: string;
|
|
29
|
+
SK: string;
|
|
30
|
+
GSI1_PK: string;
|
|
31
|
+
GSI1_SK: string;
|
|
32
|
+
TYPE: string;
|
|
33
|
+
};
|
|
34
|
+
export declare const scan: <T>(params: ScanParams) => Promise<ScanResponse<T>>;
|
|
35
|
+
interface ScanWithCallbackOptions {
|
|
36
|
+
retry?: true | ExecuteWithRetryOptions;
|
|
37
|
+
}
|
|
38
|
+
export declare const scanWithCallback: <T>(params: ScanParams, callback: (result: ScanResponse<ScanDbItem<T>>) => Promise<void | boolean>, options?: ScanWithCallbackOptions) => Promise<void>;
|
package/utils/scan.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { executeWithRetry } from "@webiny/utils";
|
|
2
|
+
const createNext = (result)=>{
|
|
3
|
+
if (!result?.LastEvaluatedKey || !result.next) return;
|
|
4
|
+
return async ()=>{
|
|
5
|
+
const response = await result.next();
|
|
6
|
+
return convertResult(response);
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
const convertResult = (result)=>({
|
|
10
|
+
items: result.Items || [],
|
|
11
|
+
count: result.Count,
|
|
12
|
+
scannedCount: result.ScannedCount,
|
|
13
|
+
lastEvaluatedKey: result.LastEvaluatedKey || void 0,
|
|
14
|
+
next: createNext(result),
|
|
15
|
+
error: result.error,
|
|
16
|
+
requestId: result.$response?.requestId || ""
|
|
17
|
+
});
|
|
18
|
+
const scan = async (params)=>{
|
|
19
|
+
const { options } = params;
|
|
20
|
+
const table = params.table ? params.table : params.entity.table;
|
|
21
|
+
if (!table) throw new Error(`Missing table for scan: ${JSON.stringify(options)}`);
|
|
22
|
+
const result = await table.scan({
|
|
23
|
+
...options,
|
|
24
|
+
execute: true
|
|
25
|
+
}, params.params);
|
|
26
|
+
return convertResult(result);
|
|
27
|
+
};
|
|
28
|
+
const scanWithCallback = async (params, callback, options)=>{
|
|
29
|
+
const usingRetry = Boolean(options?.retry);
|
|
30
|
+
const retryOptions = options?.retry === true ? {} : options?.retry;
|
|
31
|
+
const executeScan = ()=>scan(params);
|
|
32
|
+
const getInitialResult = ()=>{
|
|
33
|
+
if (usingRetry) return executeWithRetry(executeScan, retryOptions);
|
|
34
|
+
return executeScan();
|
|
35
|
+
};
|
|
36
|
+
let result = await getInitialResult();
|
|
37
|
+
if (!result.items?.length && !result.lastEvaluatedKey) return;
|
|
38
|
+
const callbackResult = await callback(result);
|
|
39
|
+
const mustBreak = false === callbackResult;
|
|
40
|
+
if (mustBreak) return;
|
|
41
|
+
while(result.next){
|
|
42
|
+
const executeNext = ()=>result.next();
|
|
43
|
+
const getNextResult = ()=>{
|
|
44
|
+
if (usingRetry) return executeWithRetry(executeNext, retryOptions);
|
|
45
|
+
return executeNext();
|
|
46
|
+
};
|
|
47
|
+
result = await getNextResult();
|
|
48
|
+
const callbackResult = await callback(result);
|
|
49
|
+
const mustBreak = false === callbackResult;
|
|
50
|
+
if (mustBreak) break;
|
|
51
|
+
if (!result.next) return;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
export { scan, scanWithCallback };
|
|
55
|
+
|
|
56
|
+
//# sourceMappingURL=scan.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils/scan.js","sources":["../../src/utils/scan.ts"],"sourcesContent":["import type { ScanInput, ScanOutput } from \"@webiny/aws-sdk/client-dynamodb/index.js\";\nimport type { Entity, ScanOptions, TableDef } from \"~/toolbox.js\";\nimport type { ExecuteWithRetryOptions } from \"@webiny/utils\";\nimport { executeWithRetry } from \"@webiny/utils\";\n\nexport type { ScanOptions, ScanInput, ScanOutput };\n\nexport interface BaseScanParams {\n options?: ScanOptions;\n params?: Partial<ScanInput>;\n}\n\nexport interface ScanWithTable extends BaseScanParams {\n table: TableDef;\n entity?: never;\n}\n\nexport interface ScanWithEntity extends BaseScanParams {\n entity: Entity;\n table?: never;\n}\n\nexport type ScanParams = ScanWithTable | ScanWithEntity;\n\nexport interface ScanResponse<T = any> {\n items: T[];\n count?: number;\n scannedCount?: number;\n lastEvaluatedKey?: ScanOutput[\"LastEvaluatedKey\"];\n next?: () => Promise<ScanResponse<T>>;\n requestId: string;\n error: any;\n}\n\ninterface DdbScanResult<T> {\n Items?: T[];\n Count?: number;\n ScannedCount?: number;\n LastEvaluatedKey?: ScanOutput[\"LastEvaluatedKey\"];\n next?: () => Promise<DdbScanResult<T>>;\n error?: any;\n $response?: {\n requestId: string;\n };\n}\n\ntype NextCb<T> = () => Promise<ScanResponse<T>>;\n\nconst createNext = <T>(result: DdbScanResult<T>): NextCb<T> | undefined => {\n if (!result?.LastEvaluatedKey || !result.next) {\n return undefined;\n }\n return async () => {\n const response = await result!.next!();\n return convertResult(response);\n };\n};\n\nconst convertResult = <T>(result: DdbScanResult<T>): ScanResponse<T> => {\n return {\n items: result.Items || [],\n count: result.Count,\n scannedCount: result.ScannedCount,\n lastEvaluatedKey: result.LastEvaluatedKey || undefined,\n next: createNext<T>(result),\n error: result.error,\n requestId: result.$response?.requestId || \"\"\n };\n};\n\nexport type ScanDbItem<T> = T & {\n PK: string;\n SK: string;\n GSI1_PK: string;\n GSI1_SK: string;\n TYPE: string;\n};\n\nexport const scan = async <T>(params: ScanParams): Promise<ScanResponse<T>> => {\n const { options } = params;\n\n const table = params.table ? params.table : params.entity.table;\n if (!table) {\n throw new Error(`Missing table for scan: ${JSON.stringify(options)}`);\n }\n\n const result = await table.scan(\n {\n ...options,\n execute: true\n },\n params.params\n );\n\n return convertResult(result) as ScanResponse<T>;\n};\n\ninterface ScanWithCallbackOptions {\n retry?: true | ExecuteWithRetryOptions;\n}\n\nexport const scanWithCallback = async <T>(\n params: ScanParams,\n callback: (result: ScanResponse<ScanDbItem<T>>) => Promise<void | boolean>,\n options?: ScanWithCallbackOptions\n): Promise<void> => {\n // For backwards compatibility, we still allow for executing the scan without retries.\n const usingRetry = Boolean(options?.retry);\n const retryOptions = options?.retry === true ? {} : options?.retry;\n\n const executeScan = () => scan<ScanDbItem<T>>(params);\n const getInitialResult = () => {\n if (usingRetry) {\n return executeWithRetry(executeScan, retryOptions);\n }\n return executeScan();\n };\n\n let result = await getInitialResult();\n\n if (!result.items?.length && !result.lastEvaluatedKey) {\n return;\n }\n\n // If the result of the callback was `false`, that means the\n // user's intention was to stop further table scanning.\n const callbackResult = await callback(result);\n const mustBreak = callbackResult === false;\n if (mustBreak) {\n return;\n }\n\n while (result.next) {\n const executeNext = () => result.next!();\n const getNextResult = () => {\n if (usingRetry) {\n return executeWithRetry(executeNext, retryOptions);\n }\n return executeNext();\n };\n\n result = await getNextResult();\n\n // If the result of the callback was `false`, that means the\n // user's intention was to stop further table scanning.\n const callbackResult = await callback(result);\n const mustBreak = callbackResult === false;\n if (mustBreak) {\n break;\n }\n\n if (!result.next) {\n return;\n }\n }\n};\n"],"names":["createNext","result","response","convertResult","undefined","scan","params","options","table","Error","JSON","scanWithCallback","callback","usingRetry","Boolean","retryOptions","executeScan","getInitialResult","executeWithRetry","callbackResult","mustBreak","executeNext","getNextResult"],"mappings":";AAgDA,MAAMA,aAAa,CAAIC;IACnB,IAAI,CAACA,QAAQ,oBAAoB,CAACA,OAAO,IAAI,EACzC;IAEJ,OAAO;QACH,MAAMC,WAAW,MAAMD,OAAQ,IAAI;QACnC,OAAOE,cAAcD;IACzB;AACJ;AAEA,MAAMC,gBAAgB,CAAIF,SACf;QACH,OAAOA,OAAO,KAAK,IAAI,EAAE;QACzB,OAAOA,OAAO,KAAK;QACnB,cAAcA,OAAO,YAAY;QACjC,kBAAkBA,OAAO,gBAAgB,IAAIG;QAC7C,MAAMJ,WAAcC;QACpB,OAAOA,OAAO,KAAK;QACnB,WAAWA,OAAO,SAAS,EAAE,aAAa;IAC9C;AAWG,MAAMI,OAAO,OAAUC;IAC1B,MAAM,EAAEC,OAAO,EAAE,GAAGD;IAEpB,MAAME,QAAQF,OAAO,KAAK,GAAGA,OAAO,KAAK,GAAGA,OAAO,MAAM,CAAC,KAAK;IAC/D,IAAI,CAACE,OACD,MAAM,IAAIC,MAAM,CAAC,wBAAwB,EAAEC,KAAK,SAAS,CAACH,UAAU;IAGxE,MAAMN,SAAS,MAAMO,MAAM,IAAI,CAC3B;QACI,GAAGD,OAAO;QACV,SAAS;IACb,GACAD,OAAO,MAAM;IAGjB,OAAOH,cAAcF;AACzB;AAMO,MAAMU,mBAAmB,OAC5BL,QACAM,UACAL;IAGA,MAAMM,aAAaC,QAAQP,SAAS;IACpC,MAAMQ,eAAeR,SAAS,UAAU,OAAO,CAAC,IAAIA,SAAS;IAE7D,MAAMS,cAAc,IAAMX,KAAoBC;IAC9C,MAAMW,mBAAmB;QACrB,IAAIJ,YACA,OAAOK,iBAAiBF,aAAaD;QAEzC,OAAOC;IACX;IAEA,IAAIf,SAAS,MAAMgB;IAEnB,IAAI,CAAChB,OAAO,KAAK,EAAE,UAAU,CAACA,OAAO,gBAAgB,EACjD;IAKJ,MAAMkB,iBAAiB,MAAMP,SAASX;IACtC,MAAMmB,YAAYD,AAAmB,UAAnBA;IAClB,IAAIC,WACA;IAGJ,MAAOnB,OAAO,IAAI,CAAE;QAChB,MAAMoB,cAAc,IAAMpB,OAAO,IAAI;QACrC,MAAMqB,gBAAgB;YAClB,IAAIT,YACA,OAAOK,iBAAiBG,aAAaN;YAEzC,OAAOM;QACX;QAEApB,SAAS,MAAMqB;QAIf,MAAMH,iBAAiB,MAAMP,SAASX;QACtC,MAAMmB,YAAYD,AAAmB,UAAnBA;QAClB,IAAIC,WACA;QAGJ,IAAI,CAACnB,OAAO,IAAI,EACZ;IAER;AACJ"}
|
package/utils/sort.d.ts
CHANGED
package/utils/sort.js
CHANGED
|
@@ -1,91 +1,45 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
});
|
|
8
|
-
exports.sortItems = sortItems;
|
|
9
|
-
|
|
10
|
-
var _orderBy = _interopRequireDefault(require("lodash/orderBy"));
|
|
11
|
-
|
|
12
|
-
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
|
-
|
|
14
|
-
const extractSort = (sortBy, fields) => {
|
|
15
|
-
const result = sortBy.split("_");
|
|
16
|
-
|
|
17
|
-
if (result.length !== 2) {
|
|
18
|
-
throw new _error.default("Problem in determining the sorting for the entry items.", "SORT_ERROR", {
|
|
19
|
-
sortBy
|
|
1
|
+
import orderBy from "lodash/orderBy.js";
|
|
2
|
+
import error from "@webiny/error";
|
|
3
|
+
const extractSort = (sortBy, fields)=>{
|
|
4
|
+
const result = sortBy.split("_");
|
|
5
|
+
if (2 !== result.length) throw new error("Problem in determining the sorting for the entry items.", "SORT_ERROR", {
|
|
6
|
+
sortBy
|
|
20
7
|
});
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
throw new _error.default("Sorting field does not exist.", "SORTING_FIELD_ERROR", {
|
|
27
|
-
field,
|
|
28
|
-
order,
|
|
29
|
-
fields
|
|
8
|
+
const [field, order] = result;
|
|
9
|
+
if (!field) throw new error("Sorting field does not exist.", "SORTING_FIELD_ERROR", {
|
|
10
|
+
field,
|
|
11
|
+
order,
|
|
12
|
+
fields
|
|
30
13
|
});
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (isSortable === false) {
|
|
37
|
-
throw new _error.default(`Cannot sort by given field: "${field}".`, "UNSUPPORTED_SORT_ERROR", {
|
|
38
|
-
fields,
|
|
39
|
-
field
|
|
14
|
+
const fieldPlugin = fields.find((f)=>f.getField() === field);
|
|
15
|
+
const isSortable = fieldPlugin ? fieldPlugin.isSortable() : true;
|
|
16
|
+
if (false === isSortable) throw new error(`Cannot sort by given field: "${field}".`, "UNSUPPORTED_SORT_ERROR", {
|
|
17
|
+
fields,
|
|
18
|
+
field
|
|
40
19
|
});
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
reverse: order.toUpperCase() === "DESC"
|
|
46
|
-
};
|
|
20
|
+
return {
|
|
21
|
+
field,
|
|
22
|
+
reverse: "DESC" === order.toUpperCase()
|
|
23
|
+
};
|
|
47
24
|
};
|
|
48
|
-
|
|
49
25
|
function sortItems(params) {
|
|
50
|
-
|
|
51
|
-
items
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const info = {
|
|
64
|
-
sorters: [],
|
|
65
|
-
orders: []
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
for (const sort of initialSort) {
|
|
69
|
-
/**
|
|
70
|
-
* Possibly empty array item was passed.
|
|
71
|
-
*/
|
|
72
|
-
if (!sort) {
|
|
73
|
-
continue;
|
|
26
|
+
const { items, sort: initialSort = [], fields = [] } = params;
|
|
27
|
+
if (items.length <= 1 || false === Array.isArray(initialSort) || 0 === initialSort.length) return items;
|
|
28
|
+
const info = {
|
|
29
|
+
sorters: [],
|
|
30
|
+
orders: []
|
|
31
|
+
};
|
|
32
|
+
for (const sort of initialSort){
|
|
33
|
+
if (!sort) continue;
|
|
34
|
+
const { field, reverse } = extractSort(sort, fields);
|
|
35
|
+
const fieldPlugin = fields.find((f)=>f.getField() === field);
|
|
36
|
+
const path = fieldPlugin ? fieldPlugin.getPath() : field;
|
|
37
|
+
info.sorters.push(path);
|
|
38
|
+
info.orders.push(true === reverse ? "desc" : "asc");
|
|
74
39
|
}
|
|
40
|
+
if (0 === info.sorters.length) return items;
|
|
41
|
+
return orderBy(items, info.sorters, info.orders);
|
|
42
|
+
}
|
|
43
|
+
export { sortItems };
|
|
75
44
|
|
|
76
|
-
|
|
77
|
-
field,
|
|
78
|
-
reverse
|
|
79
|
-
} = extractSort(sort, fields);
|
|
80
|
-
const fieldPlugin = fields.find(f => f.getField() === field);
|
|
81
|
-
const path = fieldPlugin ? fieldPlugin.getPath() : field;
|
|
82
|
-
info.sorters.push(path);
|
|
83
|
-
info.orders.push(reverse === true ? "desc" : "asc");
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (info.sorters.length === 0) {
|
|
87
|
-
return items;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return (0, _orderBy.default)(items, info.sorters, info.orders);
|
|
91
|
-
}
|
|
45
|
+
//# sourceMappingURL=sort.js.map
|
package/utils/sort.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"utils/sort.js","sources":["../../src/utils/sort.ts"],"sourcesContent":["import lodashOrderBy from \"lodash/orderBy.js\";\nimport WebinyError from \"@webiny/error\";\nimport type { FieldPlugin } from \"~/plugins/definitions/FieldPlugin.js\";\n\ninterface Info {\n sorters: string[];\n orders: (boolean | \"asc\" | \"desc\")[];\n}\n\ninterface Response {\n reverse: boolean;\n field: string;\n}\n\nconst extractSort = (sortBy: string, fields: FieldPlugin[]): Response => {\n const result = sortBy.split(\"_\");\n if (result.length !== 2) {\n throw new WebinyError(\n \"Problem in determining the sorting for the entry items.\",\n \"SORT_ERROR\",\n {\n sortBy\n }\n );\n }\n const [field, order] = result;\n\n if (!field) {\n throw new WebinyError(\"Sorting field does not exist.\", \"SORTING_FIELD_ERROR\", {\n field,\n order,\n fields\n });\n }\n const fieldPlugin = fields.find(f => f.getField() === field);\n const isSortable = fieldPlugin ? fieldPlugin.isSortable() : true;\n if (isSortable === false) {\n throw new WebinyError(`Cannot sort by given field: \"${field}\".`, \"UNSUPPORTED_SORT_ERROR\", {\n fields,\n field\n });\n }\n\n return {\n field,\n reverse: order.toUpperCase() === \"DESC\"\n };\n};\n\ninterface Params<T> {\n /**\n * The items we are sorting.\n */\n items: T[];\n /**\n * Sort options. For example: [\"id_ASC\"]\n */\n sort?: string[];\n /**\n * Fields we can sort by.\n */\n fields?: FieldPlugin[];\n}\n\nexport function sortItems<T = any>(params: Params<T>): T[] {\n const { items, sort: initialSort = [], fields = [] } = params;\n /**\n * Skip sorting if nothing was passed to sort by or nothing to sort.\n */\n if (items.length <= 1 || Array.isArray(initialSort) === false || initialSort.length === 0) {\n return items;\n }\n\n const info: Info = {\n sorters: [],\n orders: []\n };\n\n for (const sort of initialSort) {\n /**\n * Possibly empty array item was passed.\n */\n if (!sort) {\n continue;\n }\n const { field, reverse } = extractSort(sort, fields);\n const fieldPlugin = fields.find(f => f.getField() === field);\n const path = fieldPlugin ? fieldPlugin.getPath() : field;\n\n info.sorters.push(path);\n info.orders.push(reverse === true ? \"desc\" : \"asc\");\n }\n\n if (info.sorters.length === 0) {\n return items;\n }\n\n return lodashOrderBy(items, info.sorters, info.orders);\n}\n"],"names":["extractSort","sortBy","fields","result","WebinyError","field","order","fieldPlugin","f","isSortable","sortItems","params","items","initialSort","Array","info","sort","reverse","path","lodashOrderBy"],"mappings":";;AAcA,MAAMA,cAAc,CAACC,QAAgBC;IACjC,MAAMC,SAASF,OAAO,KAAK,CAAC;IAC5B,IAAIE,AAAkB,MAAlBA,OAAO,MAAM,EACb,MAAM,IAAIC,MACN,2DACA,cACA;QACIH;IACJ;IAGR,MAAM,CAACI,OAAOC,MAAM,GAAGH;IAEvB,IAAI,CAACE,OACD,MAAM,IAAID,MAAY,iCAAiC,uBAAuB;QAC1EC;QACAC;QACAJ;IACJ;IAEJ,MAAMK,cAAcL,OAAO,IAAI,CAACM,CAAAA,IAAKA,EAAE,QAAQ,OAAOH;IACtD,MAAMI,aAAaF,cAAcA,YAAY,UAAU,KAAK;IAC5D,IAAIE,AAAe,UAAfA,YACA,MAAM,IAAIL,MAAY,CAAC,6BAA6B,EAAEC,MAAM,EAAE,CAAC,EAAE,0BAA0B;QACvFH;QACAG;IACJ;IAGJ,OAAO;QACHA;QACA,SAASC,AAAwB,WAAxBA,MAAM,WAAW;IAC9B;AACJ;AAiBO,SAASI,UAAmBC,MAAiB;IAChD,MAAM,EAAEC,KAAK,EAAE,MAAMC,cAAc,EAAE,EAAEX,SAAS,EAAE,EAAE,GAAGS;IAIvD,IAAIC,MAAM,MAAM,IAAI,KAAKE,AAA+B,UAA/BA,MAAM,OAAO,CAACD,gBAA0BA,AAAuB,MAAvBA,YAAY,MAAM,EAC/E,OAAOD;IAGX,MAAMG,OAAa;QACf,SAAS,EAAE;QACX,QAAQ,EAAE;IACd;IAEA,KAAK,MAAMC,QAAQH,YAAa;QAI5B,IAAI,CAACG,MACD;QAEJ,MAAM,EAAEX,KAAK,EAAEY,OAAO,EAAE,GAAGjB,YAAYgB,MAAMd;QAC7C,MAAMK,cAAcL,OAAO,IAAI,CAACM,CAAAA,IAAKA,EAAE,QAAQ,OAAOH;QACtD,MAAMa,OAAOX,cAAcA,YAAY,OAAO,KAAKF;QAEnDU,KAAK,OAAO,CAAC,IAAI,CAACG;QAClBH,KAAK,MAAM,CAAC,IAAI,CAACE,AAAY,SAAZA,UAAmB,SAAS;IACjD;IAEA,IAAIF,AAAwB,MAAxBA,KAAK,OAAO,CAAC,MAAM,EACnB,OAAOH;IAGX,OAAOO,QAAcP,OAAOG,KAAK,OAAO,EAAEA,KAAK,MAAM;AACzD"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { TableConstructor } from "../../toolbox.js";
|
|
2
|
+
import { Table as BaseTable } from "../../toolbox.js";
|
|
3
|
+
import type { ITable, ITableReadBatch, ITableScanParams, ITableScanResponse, ITableWriteBatch } from "./types.js";
|
|
4
|
+
export declare class Table<Name extends string = string, PartitionKey extends string = string, SortKey extends string = string> implements ITable<Name, PartitionKey, SortKey> {
|
|
5
|
+
readonly table: BaseTable<Name, PartitionKey, SortKey>;
|
|
6
|
+
constructor(params: TableConstructor<Name, PartitionKey, SortKey>);
|
|
7
|
+
createWriter(): ITableWriteBatch;
|
|
8
|
+
createReader(): ITableReadBatch;
|
|
9
|
+
scan<T>(params: ITableScanParams): Promise<ITableScanResponse<T>>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Table } from "../../toolbox.js";
|
|
2
|
+
import { createTableWriteBatch } from "./TableWriteBatch.js";
|
|
3
|
+
import { createTableReadBatch } from "./TableReadBatch.js";
|
|
4
|
+
import { scan } from "../scan.js";
|
|
5
|
+
class Table_Table {
|
|
6
|
+
constructor(params){
|
|
7
|
+
this.table = new Table(params);
|
|
8
|
+
}
|
|
9
|
+
createWriter() {
|
|
10
|
+
return createTableWriteBatch({
|
|
11
|
+
table: this.table
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
createReader() {
|
|
15
|
+
return createTableReadBatch({
|
|
16
|
+
table: this.table
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
async scan(params) {
|
|
20
|
+
return scan({
|
|
21
|
+
...params,
|
|
22
|
+
table: this.table
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export { Table_Table as Table };
|
|
27
|
+
|
|
28
|
+
//# sourceMappingURL=Table.js.map
|