@webiny/api-file-manager-ddb 5.25.0 → 5.26.0-beta.0
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/definitions/table.js +1 -5
- package/definitions/table.js.map +1 -1
- package/operations/files/FilesStorageOperations.js +21 -4
- package/operations/files/FilesStorageOperations.js.map +1 -1
- package/package.json +9 -9
- package/operations/configurations.d.ts +0 -14
- package/operations/configurations.js +0 -22
- package/operations/configurations.js.map +0 -1
package/definitions/table.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
3
|
Object.defineProperty(exports, "__esModule", {
|
|
6
4
|
value: true
|
|
7
5
|
});
|
|
@@ -9,8 +7,6 @@ exports.default = void 0;
|
|
|
9
7
|
|
|
10
8
|
var _dynamodbToolbox = require("dynamodb-toolbox");
|
|
11
9
|
|
|
12
|
-
var _configurations = _interopRequireDefault(require("../operations/configurations"));
|
|
13
|
-
|
|
14
10
|
var _utils = require("../operations/utils");
|
|
15
11
|
|
|
16
12
|
var _default = params => {
|
|
@@ -18,7 +14,7 @@ var _default = params => {
|
|
|
18
14
|
context
|
|
19
15
|
} = params;
|
|
20
16
|
return new _dynamodbToolbox.Table({
|
|
21
|
-
name:
|
|
17
|
+
name: process.env.DB_TABLE_FILE_MANGER || process.env.DB_TABLE || (0, _utils.getTable)(context),
|
|
22
18
|
partitionKey: "PK",
|
|
23
19
|
sortKey: "SK",
|
|
24
20
|
DocumentClient: (0, _utils.getDocumentClient)(context)
|
package/definitions/table.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["table.ts"],"names":["params","context","Table","name","
|
|
1
|
+
{"version":3,"sources":["table.ts"],"names":["params","context","Table","name","process","env","DB_TABLE_FILE_MANGER","DB_TABLE","partitionKey","sortKey","DocumentClient"],"mappings":";;;;;;;AAAA;;AACA;;eAMgBA,MAAD,IAAgC;AAC3C,QAAM;AAAEC,IAAAA;AAAF,MAAcD,MAApB;AACA,SAAO,IAAIE,sBAAJ,CAAU;AACbC,IAAAA,IAAI,EAAEC,OAAO,CAACC,GAAR,CAAYC,oBAAZ,IAAoCF,OAAO,CAACC,GAAR,CAAYE,QAAhD,IAA4D,qBAASN,OAAT,CADrD;AAEbO,IAAAA,YAAY,EAAE,IAFD;AAGbC,IAAAA,OAAO,EAAE,IAHI;AAIbC,IAAAA,cAAc,EAAE,8BAAkBT,OAAlB;AAJH,GAAV,CAAP;AAMH,C","sourcesContent":["import { Table } from \"dynamodb-toolbox\";\nimport { getDocumentClient, getTable } from \"~/operations/utils\";\nimport { FileManagerContext } from \"@webiny/api-file-manager/types\";\n\nexport interface TableParams {\n context: FileManagerContext;\n}\nexport default (params: TableParams): Table => {\n const { context } = params;\n return new Table({\n name: process.env.DB_TABLE_FILE_MANGER || process.env.DB_TABLE || getTable(context),\n partitionKey: \"PK\",\n sortKey: \"SK\",\n DocumentClient: getDocumentClient(context)\n });\n};\n"]}
|
|
@@ -266,11 +266,11 @@ class FilesStorageOperations {
|
|
|
266
266
|
|
|
267
267
|
async tags(params) {
|
|
268
268
|
const {
|
|
269
|
-
where
|
|
269
|
+
where: initialWhere
|
|
270
270
|
} = params;
|
|
271
271
|
const queryAllParams = {
|
|
272
272
|
entity: this.entity,
|
|
273
|
-
partitionKey: this.createPartitionKey(
|
|
273
|
+
partitionKey: this.createPartitionKey(initialWhere),
|
|
274
274
|
options: {
|
|
275
275
|
gte: " ",
|
|
276
276
|
reverse: false
|
|
@@ -286,12 +286,29 @@ class FilesStorageOperations {
|
|
|
286
286
|
query: queryAllParams
|
|
287
287
|
});
|
|
288
288
|
}
|
|
289
|
+
|
|
290
|
+
const fields = this.context.plugins.byType(_FileDynamoDbFieldPlugin.FileDynamoDbFieldPlugin.type);
|
|
291
|
+
|
|
292
|
+
const where = _objectSpread({}, initialWhere);
|
|
293
|
+
|
|
294
|
+
delete where["tenant"];
|
|
295
|
+
delete where["locale"];
|
|
289
296
|
/**
|
|
290
|
-
*
|
|
297
|
+
* Filter the read items via the code.
|
|
298
|
+
* It will build the filters out of the where input and transform the values it is using.
|
|
291
299
|
*/
|
|
292
300
|
|
|
301
|
+
const filteredItems = (0, _filter.filterItems)({
|
|
302
|
+
plugins: this.context.plugins,
|
|
303
|
+
items: results,
|
|
304
|
+
where,
|
|
305
|
+
fields
|
|
306
|
+
});
|
|
307
|
+
/**
|
|
308
|
+
* Aggregate all the tags from all the filtered items.
|
|
309
|
+
*/
|
|
293
310
|
|
|
294
|
-
const tagsObject =
|
|
311
|
+
const tagsObject = filteredItems.reduce((collection, item) => {
|
|
295
312
|
const tags = Array.isArray(item.tags) ? item.tags : [];
|
|
296
313
|
|
|
297
314
|
for (const tag of tags) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["FilesStorageOperations.ts"],"names":["FilesStorageOperations","context","_context","constructor","table","entity","get","params","where","keys","PK","createPartitionKey","SK","createSortKey","file","ex","WebinyError","message","code","error","create","item","TYPE","put","update","delete","createBatch","files","items","map","putBatch","list","initialWhere","limit","after","sort","options","createQueryAllOptions","queryAllParams","partitionKey","queryParams","name","search","contains","fields","value","plugins","byType","FileDynamoDbFieldPlugin","type","filteredFiles","totalCount","length","sortedFiles","start","parseInt","hasMoreItems","end","undefined","slice","cursor","meta","tags","gte","reverse","results","query","tagsObject","reduce","collection","Array","isArray","tag","push","id","Object","eq","tenant","locale"],"mappings":";;;;;;;;;;;AAiBA;;AACA;;AACA;;AAEA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;;;AAyBO,MAAMA,sBAAN,CAA0E;AAK1D,MAAPC,OAAO,GAAuB;AACtC,WAAO,KAAKC,QAAZ;AACH;;AAEMC,EAAAA,WAAW,CAAC;AAAEF,IAAAA;AAAF,GAAD,EAAiC;AAAA;AAAA;AAAA;AAC/C,SAAKC,QAAL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KAAhE;AAMA,WAAO,CAACE,IAAD,EAAOD,IAAP,CAAP;AACH;;AAEOxB,EAAAA,qBAAqB,CAAC;AAAE7B,IAAAA;AAAF,GAAD,EAAgE;AACzF,UAAM4B,OAAoC,GAAG,EAA7C;;AACA,QAAI5B,KAAK,CAACkE,EAAV,EAAc;AACVtC,MAAAA,OAAO,CAACwC,EAAR,GAAapE,KAAK,CAACkE,EAAnB;AACH;;AACD,WAAOtC,OAAP;AACH;;AAEOzB,EAAAA,kBAAkB,CAACJ,MAAD,EAA2C;AACjE,UAAM;AAAEsE,MAAAA,MAAF;AAAUC,MAAAA;AAAV,QAAqBvE,MAA3B;AACA,WAAQ,KAAIsE,MAAO,MAAKC,MAAO,OAA/B;AACH;;AAEOjE,EAAAA,aAAa,CAACN,MAAD,EAA8B;AAC/C,UAAM;AAAEmE,MAAAA;AAAF,QAASnE,MAAf;AAEA,WAAOmE,EAAP;AACH;;AA/T4E","sourcesContent":["import {\n File,\n FileManagerContext,\n FileManagerFilesStorageOperations,\n FileManagerFilesStorageOperationsCreateBatchParams,\n FileManagerFilesStorageOperationsCreateParams,\n FileManagerFilesStorageOperationsDeleteParams,\n FileManagerFilesStorageOperationsGetParams,\n FileManagerFilesStorageOperationsListParams,\n FileManagerFilesStorageOperationsListParamsWhere,\n FileManagerFilesStorageOperationsListResponse,\n FileManagerFilesStorageOperationsListResponseMeta,\n FileManagerFilesStorageOperationsTagsParams,\n FileManagerFilesStorageOperationsTagsResponse,\n FileManagerFilesStorageOperationsUpdateParams\n} from \"@webiny/api-file-manager/types\";\nimport { Entity, Table } from \"dynamodb-toolbox\";\nimport WebinyError from \"@webiny/error\";\nimport defineTable from \"~/definitions/table\";\nimport defineFilesEntity from \"~/definitions/filesEntity\";\nimport { queryOptions as DynamoDBToolboxQueryOptions } from \"dynamodb-toolbox/dist/classes/Table\";\nimport { queryAll } from \"@webiny/db-dynamodb/utils/query\";\nimport { decodeCursor, encodeCursor } from \"@webiny/db-dynamodb/utils/cursor\";\nimport { filterItems } from \"@webiny/db-dynamodb/utils/filter\";\nimport { sortItems } from \"@webiny/db-dynamodb/utils/sort\";\nimport { FileDynamoDbFieldPlugin } from \"~/plugins/FileDynamoDbFieldPlugin\";\nimport { batchWriteAll } from \"@webiny/db-dynamodb/utils/batchWrite\";\nimport { get as getEntityItem } from \"@webiny/db-dynamodb/utils/get\";\nimport { cleanupItem } from \"@webiny/db-dynamodb/utils/cleanup\";\n\ninterface FileItem extends File {\n PK: string;\n SK: string;\n TYPE: string;\n}\n\ninterface ConstructorParams {\n context: FileManagerContext;\n}\n\ninterface QueryAllOptionsParams {\n where: FileManagerFilesStorageOperationsListParamsWhere;\n}\n\ninterface CreatePartitionKeyParams {\n locale: string;\n tenant: string;\n}\n\ninterface CreateSortKeyParams {\n id: string;\n}\n\nexport class FilesStorageOperations implements FileManagerFilesStorageOperations {\n private readonly _context: FileManagerContext;\n private readonly table: Table;\n private readonly entity: Entity<any>;\n\n private get context(): FileManagerContext {\n return this._context;\n }\n\n public constructor({ context }: ConstructorParams) {\n this._context = context;\n this.table = defineTable({\n context\n });\n\n this.entity = defineFilesEntity({\n context,\n table: this.table\n });\n }\n\n public async get(params: FileManagerFilesStorageOperationsGetParams): Promise<File | null> {\n const { where } = params;\n const keys = {\n PK: this.createPartitionKey(where),\n SK: this.createSortKey(where)\n };\n try {\n const file = await getEntityItem<File>({\n entity: this.entity,\n keys\n });\n return cleanupItem<File>(this.entity, file);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not fetch requested file.\",\n ex.code || \"GET_FILE_ERROR\",\n {\n error: ex,\n where\n }\n );\n }\n }\n\n public async create(params: FileManagerFilesStorageOperationsCreateParams): Promise<File> {\n const { file } = params;\n\n const keys = {\n PK: this.createPartitionKey(file),\n SK: this.createSortKey(file)\n };\n const item: FileItem = {\n ...file,\n ...keys,\n TYPE: \"fm.file\"\n };\n try {\n await this.entity.put(item);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not create a new file in the DynamoDB.\",\n ex.code || \"CREATE_FILE_ERROR\",\n {\n error: ex,\n item\n }\n );\n }\n\n return file;\n }\n\n public async update(params: FileManagerFilesStorageOperationsUpdateParams): Promise<File> {\n const { file } = params;\n const keys = {\n PK: this.createPartitionKey(file),\n SK: this.createSortKey(file)\n };\n\n const item: FileItem = {\n ...file,\n ...keys,\n TYPE: \"fm.file\"\n };\n try {\n await this.entity.put(item);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not update a file in the DynamoDB.\",\n ex.code || \"UPDATE_FILE_ERROR\",\n {\n error: ex,\n item\n }\n );\n }\n return file;\n }\n\n public async delete(params: FileManagerFilesStorageOperationsDeleteParams): Promise<void> {\n const { file } = params;\n const keys = {\n PK: this.createPartitionKey(file),\n SK: this.createSortKey(file)\n };\n\n try {\n await this.entity.delete(keys);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not delete file from the DynamoDB.\",\n ex.code || \"DELETE_FILE_ERROR\",\n {\n error: ex,\n file,\n keys\n }\n );\n }\n }\n\n public async createBatch(\n params: FileManagerFilesStorageOperationsCreateBatchParams\n ): Promise<File[]> {\n const { files } = params;\n\n const items = files.map(file => {\n return this.entity.putBatch({\n ...file,\n PK: this.createPartitionKey(file),\n SK: this.createSortKey(file),\n TYPE: \"fm.file\"\n });\n });\n\n try {\n await batchWriteAll({\n table: this.entity.table,\n items\n });\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not batch insert a list of files.\",\n ex.code || \"BATCH_CREATE_FILES_ERROR\",\n {\n error: ex,\n files\n }\n );\n }\n return files;\n }\n\n public async list(\n params: FileManagerFilesStorageOperationsListParams\n ): Promise<FileManagerFilesStorageOperationsListResponse> {\n const { where: initialWhere, limit, after, sort } = params;\n\n const options = this.createQueryAllOptions({\n where: initialWhere\n });\n const queryAllParams = {\n entity: this.entity,\n partitionKey: this.createPartitionKey(initialWhere),\n options\n };\n let items = [];\n try {\n items = await queryAll<File>(queryAllParams);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not query for the files.\",\n ex.code || \"FILE_LIST_ERROR\",\n {\n error: ex,\n where: initialWhere,\n limit,\n after,\n sort,\n queryParams: {\n options,\n partitionKey: queryAllParams.partitionKey,\n entity: queryAllParams.entity.name,\n table: queryAllParams.entity.table.name\n }\n }\n );\n }\n\n const where: Partial<FileManagerFilesStorageOperationsListParamsWhere> & {\n contains?: { fields: string[]; value: string };\n } = {\n ...initialWhere\n };\n if (where.search) {\n where.contains = {\n fields: [\"name\", \"tags\"],\n value: where.search\n };\n }\n delete where[\"tenant\"];\n delete where[\"locale\"];\n delete where[\"search\"];\n\n const fields = this.context.plugins.byType<FileDynamoDbFieldPlugin>(\n FileDynamoDbFieldPlugin.type\n );\n /**\n * Filter the read items via the code.\n * It will build the filters out of the where input and transform the values it is using.\n */\n const filteredFiles = filterItems({\n plugins: this.context.plugins,\n items,\n where,\n fields\n });\n\n const totalCount = filteredFiles.length;\n /**\n * Sorting is also done via the code.\n * It takes the sort input and sorts by it via the lodash sortBy method.\n */\n const sortedFiles = sortItems({\n items: filteredFiles,\n sort,\n fields\n });\n\n const start = parseInt(decodeCursor(after) || \"0\") || 0;\n const hasMoreItems = totalCount > start + limit;\n const end = limit > totalCount + start + limit ? undefined : start + limit;\n const files = sortedFiles.slice(start, end);\n /**\n * Although we do not need a cursor here, we will use it as such to keep it standardized.\n * Number is simply encoded.\n */\n const cursor = files.length > 0 ? encodeCursor(start + limit) : null;\n\n const meta = {\n hasMoreItems,\n totalCount: totalCount,\n cursor\n };\n\n return [files, meta];\n }\n public async tags(\n params: FileManagerFilesStorageOperationsTagsParams\n ): Promise<FileManagerFilesStorageOperationsTagsResponse> {\n const { where } = params;\n\n const queryAllParams = {\n entity: this.entity,\n partitionKey: this.createPartitionKey(where),\n options: {\n gte: \" \",\n reverse: false\n }\n };\n let results = [];\n try {\n results = await queryAll<File>(queryAllParams);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Error in the DynamoDB query.\",\n ex.code || \"DYNAMODB_ERROR\",\n {\n error: ex,\n query: queryAllParams\n }\n );\n }\n /**\n * Aggregate all the tags from all the items in the database.\n */\n const tagsObject = results.reduce((collection, item) => {\n const tags = Array.isArray(item.tags) ? item.tags : [];\n for (const tag of tags) {\n if (!collection[tag]) {\n collection[tag] = [];\n }\n collection[tag].push(item.id);\n }\n return collection;\n }, {} as Record<string, string[]>);\n\n const tags: string[] = Object.keys(tagsObject);\n\n const hasMoreItems = false;\n const totalCount = tags.length;\n\n const meta: FileManagerFilesStorageOperationsListResponseMeta = {\n hasMoreItems,\n totalCount,\n cursor: null\n };\n\n return [tags, meta];\n }\n\n private createQueryAllOptions({ where }: QueryAllOptionsParams): DynamoDBToolboxQueryOptions {\n const options: DynamoDBToolboxQueryOptions = {};\n if (where.id) {\n options.eq = where.id;\n }\n return options;\n }\n\n private createPartitionKey(params: CreatePartitionKeyParams): string {\n const { tenant, locale } = params;\n return `T#${tenant}#L#${locale}#FM#F`;\n }\n\n private createSortKey(params: CreateSortKeyParams) {\n const { id } = params;\n\n return id;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["FilesStorageOperations.ts"],"names":["FilesStorageOperations","context","_context","constructor","table","entity","get","params","where","keys","PK","createPartitionKey","SK","createSortKey","file","ex","WebinyError","message","code","error","create","item","TYPE","put","update","delete","createBatch","files","items","map","putBatch","list","initialWhere","limit","after","sort","options","createQueryAllOptions","queryAllParams","partitionKey","queryParams","name","search","contains","fields","value","plugins","byType","FileDynamoDbFieldPlugin","type","filteredFiles","totalCount","length","sortedFiles","start","parseInt","hasMoreItems","end","undefined","slice","cursor","meta","tags","gte","reverse","results","query","filteredItems","tagsObject","reduce","collection","Array","isArray","tag","push","id","Object","eq","tenant","locale"],"mappings":";;;;;;;;;;;AAkBA;;AACA;;AACA;;AAEA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;;;AAyBO,MAAMA,sBAAN,CAA0E;AAK1D,MAAPC,OAAO,GAAuB;AACtC,WAAO,KAAK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AACyC,IAAhC,GAAuC,EAApD;;AACA,WAAK,MAAMW,GAAX,IAAkBX,IAAlB,EAAwB;AACpB,YAAI,CAACQ,UAAU,CAACG,GAAD,CAAf,EAAsB;AAClBH,UAAAA,UAAU,CAACG,GAAD,CAAV,GAAkB,EAAlB;AACH;;AACDH,QAAAA,UAAU,CAACG,GAAD,CAAV,CAAgBC,IAAhB,CAAqBrD,IAAI,CAACsD,EAA1B;AACH;;AACD,aAAOL,UAAP;AACH,KATkB,EAShB,EATgB,CAAnB;AAWA,UAAMR,IAAc,GAAGc,MAAM,CAACnE,IAAP,CAAY2D,UAAZ,CAAvB;AAEA,UAAMZ,YAAY,GAAG,KAArB;AACA,UAAML,UAAU,GAAGW,IAAI,CAACV,MAAxB;AAEA,UAAMS,IAAuD,GAAG;AAC5DL,MAAAA,YAD4D;AAE5DL,MAAAA,UAF4D;AAG5DS,MAAAA,MAAM,EAAE;AAHoD,KAAhE;AAMA,WAAO,CAACE,IAAD,EAAOD,IAAP,CAAP;AACH;;AAEOxB,EAAAA,qBAAqB,CAAC;AAAE7B,IAAAA;AAAF,GAAD,EAAgE;AACzF,UAAM4B,OAAoC,GAAG,EAA7C;;AACA,QAAI5B,KAAK,CAACmE,EAAV,EAAc;AACVvC,MAAAA,OAAO,CAACyC,EAAR,GAAarE,KAAK,CAACmE,EAAnB;AACH;;AACD,WAAOvC,OAAP;AACH;;AAEOzB,EAAAA,kBAAkB,CAACJ,MAAD,EAA2C;AACjE,UAAM;AAAEuE,MAAAA,MAAF;AAAUC,MAAAA;AAAV,QAAqBxE,MAA3B;AACA,WAAQ,KAAIuE,MAAO,MAAKC,MAAO,OAA/B;AACH;;AAEOlE,EAAAA,aAAa,CAACN,MAAD,EAA8B;AAC/C,UAAM;AAAEoE,MAAAA;AAAF,QAASpE,MAAf;AAEA,WAAOoE,EAAP;AACH;;AAvV4E","sourcesContent":["import {\n File,\n FileManagerContext,\n FileManagerFilesStorageOperations,\n FileManagerFilesStorageOperationsCreateBatchParams,\n FileManagerFilesStorageOperationsCreateParams,\n FileManagerFilesStorageOperationsDeleteParams,\n FileManagerFilesStorageOperationsGetParams,\n FileManagerFilesStorageOperationsListParams,\n FileManagerFilesStorageOperationsListParamsWhere,\n FileManagerFilesStorageOperationsListResponse,\n FileManagerFilesStorageOperationsListResponseMeta,\n FileManagerFilesStorageOperationsTagsParams,\n FileManagerFilesStorageOperationsTagsParamsWhere,\n FileManagerFilesStorageOperationsTagsResponse,\n FileManagerFilesStorageOperationsUpdateParams\n} from \"@webiny/api-file-manager/types\";\nimport { Entity, Table } from \"dynamodb-toolbox\";\nimport WebinyError from \"@webiny/error\";\nimport defineTable from \"~/definitions/table\";\nimport defineFilesEntity from \"~/definitions/filesEntity\";\nimport { queryOptions as DynamoDBToolboxQueryOptions } from \"dynamodb-toolbox/dist/classes/Table\";\nimport { queryAll } from \"@webiny/db-dynamodb/utils/query\";\nimport { decodeCursor, encodeCursor } from \"@webiny/db-dynamodb/utils/cursor\";\nimport { filterItems } from \"@webiny/db-dynamodb/utils/filter\";\nimport { sortItems } from \"@webiny/db-dynamodb/utils/sort\";\nimport { FileDynamoDbFieldPlugin } from \"~/plugins/FileDynamoDbFieldPlugin\";\nimport { batchWriteAll } from \"@webiny/db-dynamodb/utils/batchWrite\";\nimport { get as getEntityItem } from \"@webiny/db-dynamodb/utils/get\";\nimport { cleanupItem } from \"@webiny/db-dynamodb/utils/cleanup\";\n\ninterface FileItem extends File {\n PK: string;\n SK: string;\n TYPE: string;\n}\n\ninterface ConstructorParams {\n context: FileManagerContext;\n}\n\ninterface QueryAllOptionsParams {\n where: FileManagerFilesStorageOperationsListParamsWhere;\n}\n\ninterface CreatePartitionKeyParams {\n locale: string;\n tenant: string;\n}\n\ninterface CreateSortKeyParams {\n id: string;\n}\n\nexport class FilesStorageOperations implements FileManagerFilesStorageOperations {\n private readonly _context: FileManagerContext;\n private readonly table: Table;\n private readonly entity: Entity<any>;\n\n private get context(): FileManagerContext {\n return this._context;\n }\n\n public constructor({ context }: ConstructorParams) {\n this._context = context;\n this.table = defineTable({\n context\n });\n\n this.entity = defineFilesEntity({\n context,\n table: this.table\n });\n }\n\n public async get(params: FileManagerFilesStorageOperationsGetParams): Promise<File | null> {\n const { where } = params;\n const keys = {\n PK: this.createPartitionKey(where),\n SK: this.createSortKey(where)\n };\n try {\n const file = await getEntityItem<File>({\n entity: this.entity,\n keys\n });\n return cleanupItem<File>(this.entity, file);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not fetch requested file.\",\n ex.code || \"GET_FILE_ERROR\",\n {\n error: ex,\n where\n }\n );\n }\n }\n\n public async create(params: FileManagerFilesStorageOperationsCreateParams): Promise<File> {\n const { file } = params;\n\n const keys = {\n PK: this.createPartitionKey(file),\n SK: this.createSortKey(file)\n };\n const item: FileItem = {\n ...file,\n ...keys,\n TYPE: \"fm.file\"\n };\n try {\n await this.entity.put(item);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not create a new file in the DynamoDB.\",\n ex.code || \"CREATE_FILE_ERROR\",\n {\n error: ex,\n item\n }\n );\n }\n\n return file;\n }\n\n public async update(params: FileManagerFilesStorageOperationsUpdateParams): Promise<File> {\n const { file } = params;\n const keys = {\n PK: this.createPartitionKey(file),\n SK: this.createSortKey(file)\n };\n\n const item: FileItem = {\n ...file,\n ...keys,\n TYPE: \"fm.file\"\n };\n try {\n await this.entity.put(item);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not update a file in the DynamoDB.\",\n ex.code || \"UPDATE_FILE_ERROR\",\n {\n error: ex,\n item\n }\n );\n }\n return file;\n }\n\n public async delete(params: FileManagerFilesStorageOperationsDeleteParams): Promise<void> {\n const { file } = params;\n const keys = {\n PK: this.createPartitionKey(file),\n SK: this.createSortKey(file)\n };\n\n try {\n await this.entity.delete(keys);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not delete file from the DynamoDB.\",\n ex.code || \"DELETE_FILE_ERROR\",\n {\n error: ex,\n file,\n keys\n }\n );\n }\n }\n\n public async createBatch(\n params: FileManagerFilesStorageOperationsCreateBatchParams\n ): Promise<File[]> {\n const { files } = params;\n\n const items = files.map(file => {\n return this.entity.putBatch({\n ...file,\n PK: this.createPartitionKey(file),\n SK: this.createSortKey(file),\n TYPE: \"fm.file\"\n });\n });\n\n try {\n await batchWriteAll({\n table: this.entity.table,\n items\n });\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not batch insert a list of files.\",\n ex.code || \"BATCH_CREATE_FILES_ERROR\",\n {\n error: ex,\n files\n }\n );\n }\n return files;\n }\n\n public async list(\n params: FileManagerFilesStorageOperationsListParams\n ): Promise<FileManagerFilesStorageOperationsListResponse> {\n const { where: initialWhere, limit, after, sort } = params;\n\n const options = this.createQueryAllOptions({\n where: initialWhere\n });\n const queryAllParams = {\n entity: this.entity,\n partitionKey: this.createPartitionKey(initialWhere),\n options\n };\n let items = [];\n try {\n items = await queryAll<File>(queryAllParams);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not query for the files.\",\n ex.code || \"FILE_LIST_ERROR\",\n {\n error: ex,\n where: initialWhere,\n limit,\n after,\n sort,\n queryParams: {\n options,\n partitionKey: queryAllParams.partitionKey,\n entity: queryAllParams.entity.name,\n table: queryAllParams.entity.table.name\n }\n }\n );\n }\n\n const where: Partial<FileManagerFilesStorageOperationsListParamsWhere> & {\n contains?: { fields: string[]; value: string };\n } = {\n ...initialWhere\n };\n if (where.search) {\n where.contains = {\n fields: [\"name\", \"tags\"],\n value: where.search\n };\n }\n delete where[\"tenant\"];\n delete where[\"locale\"];\n delete where[\"search\"];\n\n const fields = this.context.plugins.byType<FileDynamoDbFieldPlugin>(\n FileDynamoDbFieldPlugin.type\n );\n /**\n * Filter the read items via the code.\n * It will build the filters out of the where input and transform the values it is using.\n */\n const filteredFiles = filterItems({\n plugins: this.context.plugins,\n items,\n where,\n fields\n });\n\n const totalCount = filteredFiles.length;\n /**\n * Sorting is also done via the code.\n * It takes the sort input and sorts by it via the lodash sortBy method.\n */\n const sortedFiles = sortItems({\n items: filteredFiles,\n sort,\n fields\n });\n\n const start = parseInt(decodeCursor(after) || \"0\") || 0;\n const hasMoreItems = totalCount > start + limit;\n const end = limit > totalCount + start + limit ? undefined : start + limit;\n const files = sortedFiles.slice(start, end);\n /**\n * Although we do not need a cursor here, we will use it as such to keep it standardized.\n * Number is simply encoded.\n */\n const cursor = files.length > 0 ? encodeCursor(start + limit) : null;\n\n const meta = {\n hasMoreItems,\n totalCount: totalCount,\n cursor\n };\n\n return [files, meta];\n }\n\n public async tags(\n params: FileManagerFilesStorageOperationsTagsParams\n ): Promise<FileManagerFilesStorageOperationsTagsResponse> {\n const { where: initialWhere } = params;\n\n const queryAllParams = {\n entity: this.entity,\n partitionKey: this.createPartitionKey(initialWhere),\n options: {\n gte: \" \",\n reverse: false\n }\n };\n let results = [];\n try {\n results = await queryAll<File>(queryAllParams);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Error in the DynamoDB query.\",\n ex.code || \"DYNAMODB_ERROR\",\n {\n error: ex,\n query: queryAllParams\n }\n );\n }\n\n const fields = this.context.plugins.byType<FileDynamoDbFieldPlugin>(\n FileDynamoDbFieldPlugin.type\n );\n\n const where: Partial<FileManagerFilesStorageOperationsTagsParamsWhere> = {\n ...initialWhere\n };\n\n delete where[\"tenant\"];\n delete where[\"locale\"];\n\n /**\n * Filter the read items via the code.\n * It will build the filters out of the where input and transform the values it is using.\n */\n const filteredItems = filterItems({\n plugins: this.context.plugins,\n items: results,\n where,\n fields\n });\n\n /**\n * Aggregate all the tags from all the filtered items.\n */\n const tagsObject = filteredItems.reduce((collection, item) => {\n const tags = Array.isArray(item.tags) ? item.tags : [];\n for (const tag of tags) {\n if (!collection[tag]) {\n collection[tag] = [];\n }\n collection[tag].push(item.id);\n }\n return collection;\n }, {} as Record<string, string[]>);\n\n const tags: string[] = Object.keys(tagsObject);\n\n const hasMoreItems = false;\n const totalCount = tags.length;\n\n const meta: FileManagerFilesStorageOperationsListResponseMeta = {\n hasMoreItems,\n totalCount,\n cursor: null\n };\n\n return [tags, meta];\n }\n\n private createQueryAllOptions({ where }: QueryAllOptionsParams): DynamoDBToolboxQueryOptions {\n const options: DynamoDBToolboxQueryOptions = {};\n if (where.id) {\n options.eq = where.id;\n }\n return options;\n }\n\n private createPartitionKey(params: CreatePartitionKeyParams): string {\n const { tenant, locale } = params;\n return `T#${tenant}#L#${locale}#FM#F`;\n }\n\n private createSortKey(params: CreateSortKeyParams) {\n const { id } = params;\n\n return id;\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/api-file-manager-ddb",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.26.0-beta.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"@webiny/api-file-manager",
|
|
@@ -21,12 +21,12 @@
|
|
|
21
21
|
],
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@babel/runtime": "7.
|
|
25
|
-
"@webiny/api-file-manager": "5.
|
|
26
|
-
"@webiny/db-dynamodb": "5.
|
|
27
|
-
"@webiny/error": "5.
|
|
28
|
-
"@webiny/project-utils": "5.
|
|
29
|
-
"aws-sdk": "2.
|
|
24
|
+
"@babel/runtime": "7.16.7",
|
|
25
|
+
"@webiny/api-file-manager": "5.26.0-beta.0",
|
|
26
|
+
"@webiny/db-dynamodb": "5.26.0-beta.0",
|
|
27
|
+
"@webiny/error": "5.26.0-beta.0",
|
|
28
|
+
"@webiny/project-utils": "5.26.0-beta.0",
|
|
29
|
+
"aws-sdk": "2.1066.0",
|
|
30
30
|
"dynamodb-toolbox": "0.3.5"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"@babel/plugin-transform-runtime": "^7.16.4",
|
|
37
37
|
"@babel/preset-env": "^7.16.4",
|
|
38
38
|
"@babel/preset-typescript": "^7.16.0",
|
|
39
|
-
"@webiny/cli": "^5.
|
|
39
|
+
"@webiny/cli": "^5.26.0-beta.0",
|
|
40
40
|
"jest": "^26.6.3",
|
|
41
41
|
"jest-dynalite": "^3.2.0",
|
|
42
42
|
"jsonpack": "^1.1.5",
|
|
@@ -63,5 +63,5 @@
|
|
|
63
63
|
]
|
|
64
64
|
}
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "4ce1afe0630101262047a016a4b00e4e01f47095"
|
|
67
67
|
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
var _default = {
|
|
8
|
-
db: () => ({
|
|
9
|
-
table: process.env.DB_TABLE_FILE_MANGER || process.env.DB_TABLE,
|
|
10
|
-
keys: [{
|
|
11
|
-
primary: true,
|
|
12
|
-
unique: true,
|
|
13
|
-
name: "primary",
|
|
14
|
-
fields: [{
|
|
15
|
-
name: "PK"
|
|
16
|
-
}, {
|
|
17
|
-
name: "SK"
|
|
18
|
-
}]
|
|
19
|
-
}]
|
|
20
|
-
})
|
|
21
|
-
};
|
|
22
|
-
exports.default = _default;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["configurations.ts"],"names":["db","table","process","env","DB_TABLE_FILE_MANGER","DB_TABLE","keys","primary","unique","name","fields"],"mappings":";;;;;;eAAe;AACXA,EAAAA,EAAE,EAAE,OAAO;AACPC,IAAAA,KAAK,EAAEC,OAAO,CAACC,GAAR,CAAYC,oBAAZ,IAAoCF,OAAO,CAACC,GAAR,CAAYE,QADhD;AAEPC,IAAAA,IAAI,EAAE,CACF;AACIC,MAAAA,OAAO,EAAE,IADb;AAEIC,MAAAA,MAAM,EAAE,IAFZ;AAGIC,MAAAA,IAAI,EAAE,SAHV;AAIIC,MAAAA,MAAM,EAAE,CAAC;AAAED,QAAAA,IAAI,EAAE;AAAR,OAAD,EAAiB;AAAEA,QAAAA,IAAI,EAAE;AAAR,OAAjB;AAJZ,KADE;AAFC,GAAP;AADO,C","sourcesContent":["export default {\n db: () => ({\n table: process.env.DB_TABLE_FILE_MANGER || process.env.DB_TABLE,\n keys: [\n {\n primary: true,\n unique: true,\n name: \"primary\",\n fields: [{ name: \"PK\" }, { name: \"SK\" }]\n }\n ]\n })\n};\n"]}
|