@webiny/api-file-manager-ddb 0.0.0-mt-2 → 0.0.0-unstable.1145e7667f

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.
Files changed (56) hide show
  1. package/definitions/filesEntity.d.ts +3 -2
  2. package/definitions/filesEntity.js +3 -7
  3. package/definitions/filesEntity.js.map +1 -0
  4. package/definitions/settingsEntity.d.ts +3 -4
  5. package/definitions/settingsEntity.js +9 -27
  6. package/definitions/settingsEntity.js.map +1 -0
  7. package/definitions/systemEntity.d.ts +3 -4
  8. package/definitions/systemEntity.js +5 -17
  9. package/definitions/systemEntity.js.map +1 -0
  10. package/definitions/table.d.ts +6 -5
  11. package/definitions/table.js +14 -17
  12. package/definitions/table.js.map +1 -0
  13. package/index.d.ts +9 -5
  14. package/index.js +52 -11
  15. package/index.js.map +1 -0
  16. package/operations/files/FilesStorageOperations.d.ts +12 -8
  17. package/operations/files/FilesStorageOperations.js +182 -127
  18. package/operations/files/FilesStorageOperations.js.map +1 -0
  19. package/operations/files/fields.d.ts +1 -2
  20. package/operations/files/fields.js +3 -6
  21. package/operations/files/fields.js.map +1 -0
  22. package/operations/files/index.d.ts +2 -2
  23. package/operations/files/index.js +1 -1
  24. package/operations/files/index.js.map +1 -0
  25. package/operations/settings/SettingsStorageOperations.d.ts +7 -8
  26. package/operations/settings/SettingsStorageOperations.js +37 -64
  27. package/operations/settings/SettingsStorageOperations.js.map +1 -0
  28. package/operations/settings/index.d.ts +2 -4
  29. package/operations/settings/index.js +1 -1
  30. package/operations/settings/index.js.map +1 -0
  31. package/operations/system/SystemStorageOperations.d.ts +6 -7
  32. package/operations/system/SystemStorageOperations.js +16 -49
  33. package/operations/system/SystemStorageOperations.js.map +1 -0
  34. package/operations/system/index.d.ts +2 -4
  35. package/operations/system/index.js +1 -1
  36. package/operations/system/index.js.map +1 -0
  37. package/operations/utils.js +1 -11
  38. package/operations/utils.js.map +1 -0
  39. package/package.json +19 -18
  40. package/plugins/FileAttributePlugin.d.ts +2 -2
  41. package/plugins/FileAttributePlugin.js +4 -14
  42. package/plugins/FileAttributePlugin.js.map +1 -0
  43. package/plugins/FileDynamoDbFieldPlugin.js +1 -6
  44. package/plugins/FileDynamoDbFieldPlugin.js.map +1 -0
  45. package/plugins/SettingsAttributePlugin.d.ts +2 -2
  46. package/plugins/SettingsAttributePlugin.js +4 -14
  47. package/plugins/SettingsAttributePlugin.js.map +1 -0
  48. package/plugins/SystemAttributePlugin.d.ts +2 -2
  49. package/plugins/SystemAttributePlugin.js +3 -13
  50. package/plugins/SystemAttributePlugin.js.map +1 -0
  51. package/plugins/index.d.ts +4 -0
  52. package/plugins/index.js +49 -0
  53. package/plugins/index.js.map +1 -0
  54. package/types.js.map +1 -0
  55. package/operations/configurations.d.ts +0 -14
  56. package/operations/configurations.js +0 -22
@@ -1,76 +1,52 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.FilesStorageOperations = void 0;
9
-
8
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
10
9
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
12
10
  var _error = _interopRequireDefault(require("@webiny/error"));
13
-
14
- var _table = _interopRequireDefault(require("../../definitions/table"));
15
-
16
- var _filesEntity = _interopRequireDefault(require("../../definitions/filesEntity"));
17
-
11
+ var _table = require("../../definitions/table");
18
12
  var _query = require("@webiny/db-dynamodb/utils/query");
19
-
20
13
  var _cursor = require("@webiny/db-dynamodb/utils/cursor");
21
-
22
14
  var _filter = require("@webiny/db-dynamodb/utils/filter");
23
-
24
15
  var _sort = require("@webiny/db-dynamodb/utils/sort");
25
-
26
16
  var _FileDynamoDbFieldPlugin = require("../../plugins/FileDynamoDbFieldPlugin");
27
-
28
17
  var _batchWrite = require("@webiny/db-dynamodb/utils/batchWrite");
29
-
30
18
  var _get = require("@webiny/db-dynamodb/utils/get");
31
-
32
- var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
33
-
34
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
35
-
36
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
37
-
19
+ var _dbDynamodb = require("@webiny/db-dynamodb");
38
20
  class FilesStorageOperations {
39
- get context() {
40
- return this._context;
41
- }
42
-
43
21
  constructor({
44
- context
22
+ documentClient,
23
+ plugins
45
24
  }) {
46
- (0, _defineProperty2.default)(this, "_context", void 0);
25
+ (0, _defineProperty2.default)(this, "plugins", void 0);
47
26
  (0, _defineProperty2.default)(this, "table", void 0);
48
- (0, _defineProperty2.default)(this, "entity", void 0);
49
- this._context = context;
50
- this.table = (0, _table.default)({
51
- context
52
- });
53
- this.entity = (0, _filesEntity.default)({
54
- context,
55
- table: this.table
27
+ (0, _defineProperty2.default)(this, "fileEntity", void 0);
28
+ (0, _defineProperty2.default)(this, "aliasEntity", void 0);
29
+ this.plugins = plugins;
30
+ this.table = (0, _table.createTable)({
31
+ documentClient
56
32
  });
33
+ this.fileEntity = (0, _dbDynamodb.createStandardEntity)(this.table, "FM.File");
34
+ this.aliasEntity = (0, _dbDynamodb.createStandardEntity)(this.table, "FM.FileAlias");
57
35
  }
58
-
59
36
  async get(params) {
60
37
  const {
61
38
  where
62
39
  } = params;
63
40
  const keys = {
64
41
  PK: this.createPartitionKey(where),
65
- SK: this.createSortKey(where)
42
+ SK: "A"
66
43
  };
67
-
68
44
  try {
69
45
  const file = await (0, _get.get)({
70
- entity: this.entity,
46
+ entity: this.fileEntity,
71
47
  keys
72
48
  });
73
- return (0, _cleanup.cleanupItem)(this.entity, file);
49
+ return file ? file.data : null;
74
50
  } catch (ex) {
75
51
  throw new _error.default(ex.message || "Could not fetch requested file.", ex.code || "GET_FILE_ERROR", {
76
52
  error: ex,
@@ -78,92 +54,150 @@ class FilesStorageOperations {
78
54
  });
79
55
  }
80
56
  }
81
-
82
57
  async create(params) {
83
58
  const {
84
59
  file
85
60
  } = params;
86
- const keys = {
61
+ const items = [];
62
+ const item = {
87
63
  PK: this.createPartitionKey(file),
88
- SK: this.createSortKey(file)
64
+ SK: "A",
65
+ GSI1_PK: this.createGSI1PartitionKey(file),
66
+ GSI1_SK: file.id,
67
+ TYPE: "fm.file",
68
+ data: file
89
69
  };
90
-
91
- const item = _objectSpread(_objectSpread(_objectSpread({}, file), keys), {}, {
92
- TYPE: "fm.file"
70
+ items.push(this.fileEntity.putBatch(item));
71
+ this.createNewAliasesRecords(file).forEach(alias => {
72
+ items.push(this.aliasEntity.putBatch(alias));
93
73
  });
94
-
95
74
  try {
96
- await this.entity.put(item);
75
+ await (0, _batchWrite.batchWriteAll)({
76
+ table: this.table,
77
+ items
78
+ });
97
79
  } catch (ex) {
98
80
  throw new _error.default(ex.message || "Could not create a new file in the DynamoDB.", ex.code || "CREATE_FILE_ERROR", {
99
81
  error: ex,
100
- item
82
+ items
101
83
  });
102
84
  }
103
-
104
85
  return file;
105
86
  }
106
-
107
87
  async update(params) {
108
88
  const {
109
89
  file
110
90
  } = params;
111
- const keys = {
91
+ const items = [];
92
+ const item = {
112
93
  PK: this.createPartitionKey(file),
113
- SK: this.createSortKey(file)
94
+ SK: "A",
95
+ GSI1_PK: this.createGSI1PartitionKey(file),
96
+ GSI1_SK: file.id,
97
+ TYPE: "fm.file",
98
+ data: file
114
99
  };
115
-
116
- const item = _objectSpread(_objectSpread(_objectSpread({}, file), keys), {}, {
117
- TYPE: "fm.file"
100
+ items.push(this.fileEntity.putBatch(item));
101
+ const existingAliases = await (0, _query.queryAll)({
102
+ entity: this.aliasEntity,
103
+ partitionKey: `T#${file.tenant}#L#${file.locale}#FM#FILE#${file.id}`,
104
+ options: {
105
+ beginsWith: `ALIAS#`
106
+ }
107
+ });
108
+ const newAliases = this.createNewAliasesRecords(file, existingAliases.map(alias => alias.data));
109
+ newAliases.forEach(alias => {
110
+ items.push(this.aliasEntity.putBatch(alias));
118
111
  });
119
112
 
113
+ // Delete aliases that are in the DB but are NOT in the file.
114
+ for (const {
115
+ data
116
+ } of existingAliases) {
117
+ if (!file.aliases.some(alias => data.alias === alias)) {
118
+ items.push(this.aliasEntity.deleteBatch({
119
+ PK: this.createPartitionKey(file),
120
+ SK: `ALIAS#${data.alias}`
121
+ }));
122
+ }
123
+ }
120
124
  try {
121
- await this.entity.put(item);
125
+ await (0, _batchWrite.batchWriteAll)({
126
+ table: this.table,
127
+ items
128
+ });
122
129
  } catch (ex) {
123
130
  throw new _error.default(ex.message || "Could not update a file in the DynamoDB.", ex.code || "UPDATE_FILE_ERROR", {
124
- error: ex,
125
- item
131
+ items
126
132
  });
127
133
  }
128
-
129
- return file;
134
+ return params.file;
130
135
  }
131
-
132
136
  async delete(params) {
133
137
  const {
134
138
  file
135
139
  } = params;
136
140
  const keys = {
137
141
  PK: this.createPartitionKey(file),
138
- SK: this.createSortKey(file)
142
+ SK: "A"
139
143
  };
144
+ const aliasItems = await (0, _query.queryAll)({
145
+ entity: this.aliasEntity,
146
+ partitionKey: keys.PK,
147
+ options: {
148
+ beginsWith: "ALIAS#"
149
+ }
150
+ });
140
151
 
152
+ // All items to delete in batch
153
+ const items = [];
141
154
  try {
142
- await this.entity.delete(keys);
155
+ // Delete the main file item
156
+ items.push(this.fileEntity.deleteBatch(keys));
157
+
158
+ // Delete file alias items
159
+ aliasItems.forEach(item => {
160
+ items.push(this.aliasEntity.deleteBatch({
161
+ PK: item.PK,
162
+ SK: item.SK
163
+ }));
164
+ });
165
+ await (0, _batchWrite.batchWriteAll)({
166
+ table: this.table,
167
+ items
168
+ });
143
169
  } catch (ex) {
144
170
  throw new _error.default(ex.message || "Could not delete file from the DynamoDB.", ex.code || "DELETE_FILE_ERROR", {
145
171
  error: ex,
146
172
  file,
147
- keys
173
+ keys,
174
+ aliasItems
148
175
  });
149
176
  }
150
177
  }
151
-
152
178
  async createBatch(params) {
153
179
  const {
154
180
  files
155
181
  } = params;
156
- const items = files.map(file => {
157
- return this.entity.putBatch(_objectSpread(_objectSpread({}, file), {}, {
182
+
183
+ // Items to be written in batch
184
+ const items = [];
185
+ files.forEach(file => {
186
+ items.push(this.fileEntity.putBatch({
158
187
  PK: this.createPartitionKey(file),
159
- SK: this.createSortKey(file),
160
- TYPE: "fm.file"
188
+ SK: "A",
189
+ GSI1_PK: this.createGSI1PartitionKey(file),
190
+ GSI1_SK: file.id,
191
+ TYPE: "fm.file",
192
+ data: file
161
193
  }));
194
+ this.createNewAliasesRecords(file).forEach(alias => {
195
+ items.push(this.aliasEntity.putBatch(alias));
196
+ });
162
197
  });
163
-
164
198
  try {
165
199
  await (0, _batchWrite.batchWriteAll)({
166
- table: this.entity.table,
200
+ table: this.table,
167
201
  items
168
202
  });
169
203
  } catch (ex) {
@@ -172,10 +206,8 @@ class FilesStorageOperations {
172
206
  files
173
207
  });
174
208
  }
175
-
176
209
  return files;
177
210
  }
178
-
179
211
  async list(params) {
180
212
  const {
181
213
  where: initialWhere,
@@ -187,14 +219,14 @@ class FilesStorageOperations {
187
219
  where: initialWhere
188
220
  });
189
221
  const queryAllParams = {
190
- entity: this.entity,
191
- partitionKey: this.createPartitionKey(initialWhere),
222
+ entity: this.fileEntity,
223
+ partitionKey: this.createGSI1PartitionKey(initialWhere),
192
224
  options
193
225
  };
194
226
  let items = [];
195
-
196
227
  try {
197
- items = await (0, _query.queryAll)(queryAllParams);
228
+ const dbItems = await (0, _query.queryAll)(queryAllParams);
229
+ items = dbItems.map(item => item.data);
198
230
  } catch (ex) {
199
231
  throw new _error.default(ex.message || "Could not query for the files.", ex.code || "FILE_LIST_ERROR", {
200
232
  error: ex,
@@ -210,27 +242,23 @@ class FilesStorageOperations {
210
242
  }
211
243
  });
212
244
  }
213
-
214
- const where = _objectSpread({}, initialWhere);
215
-
245
+ const where = (0, _objectSpread2.default)({}, initialWhere);
216
246
  if (where.search) {
217
247
  where.contains = {
218
248
  fields: ["name", "tags"],
219
249
  value: where.search
220
250
  };
221
251
  }
222
-
223
252
  delete where["tenant"];
224
253
  delete where["locale"];
225
254
  delete where["search"];
226
- const fields = this.context.plugins.byType(_FileDynamoDbFieldPlugin.FileDynamoDbFieldPlugin.type);
255
+ const fields = this.plugins.byType(_FileDynamoDbFieldPlugin.FileDynamoDbFieldPlugin.type);
227
256
  /**
228
257
  * Filter the read items via the code.
229
258
  * It will build the filters out of the where input and transform the values it is using.
230
259
  */
231
-
232
260
  const filteredFiles = (0, _filter.filterItems)({
233
- plugins: this.context.plugins,
261
+ plugins: this.plugins,
234
262
  items,
235
263
  where,
236
264
  fields
@@ -240,13 +268,12 @@ class FilesStorageOperations {
240
268
  * Sorting is also done via the code.
241
269
  * It takes the sort input and sorts by it via the lodash sortBy method.
242
270
  */
243
-
244
271
  const sortedFiles = (0, _sort.sortItems)({
245
272
  items: filteredFiles,
246
273
  sort,
247
274
  fields
248
275
  });
249
- const start = (0, _cursor.decodeCursor)(after) || 0;
276
+ const start = parseInt((0, _cursor.decodeCursor)(after) || "0") || 0;
250
277
  const hasMoreItems = totalCount > start + limit;
251
278
  const end = limit > totalCount + start + limit ? undefined : start + limit;
252
279
  const files = sortedFiles.slice(start, end);
@@ -254,7 +281,6 @@ class FilesStorageOperations {
254
281
  * Although we do not need a cursor here, we will use it as such to keep it standardized.
255
282
  * Number is simply encoded.
256
283
  */
257
-
258
284
  const cursor = files.length > 0 ? (0, _cursor.encodeCursor)(start + limit) : null;
259
285
  const meta = {
260
286
  hasMoreItems,
@@ -263,83 +289,112 @@ class FilesStorageOperations {
263
289
  };
264
290
  return [files, meta];
265
291
  }
266
-
267
292
  async tags(params) {
268
293
  const {
269
- where
294
+ where: initialWhere
270
295
  } = params;
271
296
  const queryAllParams = {
272
- entity: this.entity,
273
- partitionKey: this.createPartitionKey(where),
297
+ entity: this.fileEntity,
298
+ partitionKey: this.createGSI1PartitionKey(initialWhere),
274
299
  options: {
300
+ index: "GSI1",
275
301
  gte: " ",
276
302
  reverse: false
277
303
  }
278
304
  };
279
305
  let results = [];
280
-
281
306
  try {
282
- results = await (0, _query.queryAll)(queryAllParams);
307
+ const dbItems = await (0, _query.queryAll)(queryAllParams);
308
+ results = dbItems.map(item => item.data);
283
309
  } catch (ex) {
284
310
  throw new _error.default(ex.message || "Error in the DynamoDB query.", ex.code || "DYNAMODB_ERROR", {
285
311
  error: ex,
286
312
  query: queryAllParams
287
313
  });
288
314
  }
315
+ const fields = this.plugins.byType(_FileDynamoDbFieldPlugin.FileDynamoDbFieldPlugin.type);
316
+ const where = (0, _objectSpread2.default)({}, initialWhere);
317
+ delete where["tenant"];
318
+ delete where["locale"];
319
+
289
320
  /**
290
- * Aggregate all the tags from all the items in the database.
321
+ * Filter the read items via the code.
322
+ * It will build the filters out of the where input and transform the values it is using.
291
323
  */
292
-
293
-
294
- const tagsObject = results.reduce((collection, item) => {
295
- for (const tag of item.tags) {
296
- if (!collection[tag]) {
297
- collection[tag] = [];
298
- }
299
-
300
- collection[tag].push(item.id);
324
+ const filteredItems = (0, _filter.filterItems)({
325
+ plugins: this.plugins,
326
+ items: results,
327
+ where,
328
+ fields
329
+ });
330
+ const tags = filteredItems.reduce((collection, item) => {
331
+ const tags = Array.isArray(item.tags) ? item.tags : [];
332
+ for (const tag of tags) {
333
+ var _collection$tag;
334
+ collection[tag] = {
335
+ tag,
336
+ count: (((_collection$tag = collection[tag]) === null || _collection$tag === void 0 ? void 0 : _collection$tag.count) || 0) + 1
337
+ };
301
338
  }
302
-
303
339
  return collection;
304
340
  }, {});
305
- const tags = Object.keys(tagsObject);
306
- const hasMoreItems = false;
307
- const totalCount = tags.length;
308
- const meta = {
309
- hasMoreItems,
310
- totalCount,
311
- cursor: null
312
- };
313
- return [tags, meta];
341
+ return Object.values(tags).sort((a, b) => {
342
+ return a.tag < b.tag ? -1 : 1;
343
+ }).sort((a, b) => {
344
+ return a.count > b.count ? -1 : 1;
345
+ });
314
346
  }
315
-
316
347
  createQueryAllOptions({
317
348
  where
318
349
  }) {
319
- const options = {};
320
-
350
+ const options = {
351
+ index: "GSI1"
352
+ };
321
353
  if (where.id) {
322
354
  options.eq = where.id;
355
+ } else {
356
+ options.gt = " ";
323
357
  }
324
-
325
358
  return options;
326
359
  }
327
-
328
360
  createPartitionKey(params) {
329
361
  const {
330
362
  tenant,
331
- locale
363
+ locale,
364
+ id
332
365
  } = params;
333
- return `T#${tenant}#L#${locale}#FM#F`;
366
+ return `T#${tenant}#L#${locale}#FM#FILE#${id}`;
334
367
  }
335
-
336
- createSortKey(params) {
368
+ createGSI1PartitionKey(params) {
337
369
  const {
338
- id
370
+ tenant,
371
+ locale
339
372
  } = params;
340
- return id;
373
+ return `T#${tenant}#L#${locale}#FM#FILES`;
341
374
  }
375
+ createNewAliasesRecords(file, existingAliases = []) {
376
+ return (file.aliases || []).map(alias => {
377
+ // If alias is already in the DB, skip it.
378
+ if (existingAliases.find(item => item.alias === alias)) {
379
+ return null;
380
+ }
342
381
 
382
+ // Add a new alias.
383
+ return {
384
+ PK: this.createPartitionKey(file),
385
+ SK: `ALIAS#${alias}`,
386
+ GSI1_PK: `T#${file.tenant}#FM#FILE_ALIASES`,
387
+ GSI1_SK: alias,
388
+ TYPE: "fm.fileAlias",
389
+ data: {
390
+ alias,
391
+ tenant: file.tenant,
392
+ locale: file.locale,
393
+ fileId: file.id,
394
+ key: file.key
395
+ }
396
+ };
397
+ }).filter(Boolean);
398
+ }
343
399
  }
344
-
345
400
  exports.FilesStorageOperations = FilesStorageOperations;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["FilesStorageOperations","constructor","documentClient","plugins","table","createTable","fileEntity","createStandardEntity","aliasEntity","get","params","where","keys","PK","createPartitionKey","SK","file","getEntityItem","entity","data","ex","WebinyError","message","code","error","create","items","item","GSI1_PK","createGSI1PartitionKey","GSI1_SK","id","TYPE","push","putBatch","createNewAliasesRecords","forEach","alias","batchWriteAll","update","existingAliases","queryAll","partitionKey","tenant","locale","options","beginsWith","newAliases","map","aliases","some","deleteBatch","delete","aliasItems","createBatch","files","list","initialWhere","limit","after","sort","createQueryAllOptions","queryAllParams","dbItems","queryParams","name","search","contains","fields","value","byType","FileDynamoDbFieldPlugin","type","filteredFiles","filterItems","totalCount","length","sortedFiles","sortItems","start","parseInt","decodeCursor","hasMoreItems","end","undefined","slice","cursor","encodeCursor","meta","tags","index","gte","reverse","results","query","filteredItems","reduce","collection","Array","isArray","tag","count","Object","values","a","b","eq","gt","find","fileId","key","filter","Boolean"],"sources":["FilesStorageOperations.ts"],"sourcesContent":["import {\n File,\n FileAlias,\n FileManagerFilesStorageOperations,\n FileManagerFilesStorageOperationsCreateBatchParams,\n FileManagerFilesStorageOperationsCreateParams,\n FileManagerFilesStorageOperationsDeleteParams,\n FileManagerFilesStorageOperationsGetParams,\n FileManagerFilesStorageOperationsListParams,\n FileManagerFilesStorageOperationsListParamsWhere,\n FileManagerFilesStorageOperationsListResponse,\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 { createTable } from \"~/definitions/table\";\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 { createStandardEntity, DbItem } from \"@webiny/db-dynamodb\";\nimport { DocumentClient } from \"aws-sdk/clients/dynamodb\";\nimport { PluginsContainer } from \"@webiny/plugins\";\n\ntype FileItem = DbItem<File>;\ntype FileAliasItem = DbItem<FileAlias>;\n\ninterface ConstructorParams {\n documentClient: DocumentClient;\n plugins: PluginsContainer;\n}\n\ninterface QueryAllOptionsParams {\n where: FileManagerFilesStorageOperationsListParamsWhere;\n}\n\ninterface CreatePartitionKeyParams {\n locale: string;\n tenant: string;\n id: string;\n}\n\ntype CreateGSI1PartitionKeyParams = Pick<CreatePartitionKeyParams, \"tenant\" | \"locale\">;\n\nexport class FilesStorageOperations implements FileManagerFilesStorageOperations {\n private readonly plugins: PluginsContainer;\n private readonly table: Table;\n private readonly fileEntity: Entity<any>;\n private readonly aliasEntity: Entity<any>;\n\n public constructor({ documentClient, plugins }: ConstructorParams) {\n this.plugins = plugins;\n this.table = createTable({ documentClient });\n this.fileEntity = createStandardEntity(this.table, \"FM.File\");\n this.aliasEntity = createStandardEntity(this.table, \"FM.FileAlias\");\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: \"A\"\n };\n try {\n const file = await getEntityItem<{ data: File }>({\n entity: this.fileEntity,\n keys\n });\n\n return file ? file.data : null;\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 items: any[] = [];\n\n const item: FileItem = {\n PK: this.createPartitionKey(file),\n SK: \"A\",\n GSI1_PK: this.createGSI1PartitionKey(file),\n GSI1_SK: file.id,\n TYPE: \"fm.file\",\n data: file\n };\n\n items.push(this.fileEntity.putBatch(item));\n\n this.createNewAliasesRecords(file).forEach(alias => {\n items.push(this.aliasEntity.putBatch(alias));\n });\n\n try {\n await batchWriteAll({\n table: this.table,\n items\n });\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 items\n }\n );\n }\n\n return file;\n }\n\n public async update(params: FileManagerFilesStorageOperationsUpdateParams): Promise<File> {\n const { file } = params;\n\n const items: any[] = [];\n\n const item: FileItem = {\n PK: this.createPartitionKey(file),\n SK: \"A\",\n GSI1_PK: this.createGSI1PartitionKey(file),\n GSI1_SK: file.id,\n TYPE: \"fm.file\",\n data: file\n };\n\n items.push(this.fileEntity.putBatch(item));\n\n const existingAliases = await queryAll<FileAliasItem>({\n entity: this.aliasEntity,\n partitionKey: `T#${file.tenant}#L#${file.locale}#FM#FILE#${file.id}`,\n options: {\n beginsWith: `ALIAS#`\n }\n });\n\n const newAliases = this.createNewAliasesRecords(\n file,\n existingAliases.map(alias => alias.data)\n );\n\n newAliases.forEach(alias => {\n items.push(this.aliasEntity.putBatch(alias));\n });\n\n // Delete aliases that are in the DB but are NOT in the file.\n for (const { data } of existingAliases) {\n if (!file.aliases.some(alias => data.alias === alias)) {\n items.push(\n this.aliasEntity.deleteBatch({\n PK: this.createPartitionKey(file),\n SK: `ALIAS#${data.alias}`\n })\n );\n }\n }\n\n try {\n await batchWriteAll({\n table: this.table,\n items\n });\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 items\n }\n );\n }\n return params.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: \"A\"\n };\n\n const aliasItems = await queryAll({\n entity: this.aliasEntity,\n partitionKey: keys.PK,\n options: {\n beginsWith: \"ALIAS#\"\n }\n });\n\n // All items to delete in batch\n const items: any[] = [];\n\n try {\n // Delete the main file item\n items.push(this.fileEntity.deleteBatch(keys));\n\n // Delete file alias items\n aliasItems.forEach(item => {\n items.push(\n this.aliasEntity.deleteBatch({\n PK: item.PK,\n SK: item.SK\n })\n );\n });\n\n await batchWriteAll({ table: this.table, items });\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 aliasItems\n }\n );\n }\n }\n\n public async createBatch(\n params: FileManagerFilesStorageOperationsCreateBatchParams\n ): Promise<File[]> {\n const { files } = params;\n\n // Items to be written in batch\n const items: any[] = [];\n\n files.forEach(file => {\n items.push(\n this.fileEntity.putBatch({\n PK: this.createPartitionKey(file),\n SK: \"A\",\n GSI1_PK: this.createGSI1PartitionKey(file),\n GSI1_SK: file.id,\n TYPE: \"fm.file\",\n data: file\n })\n );\n\n this.createNewAliasesRecords(file).forEach(alias => {\n items.push(this.aliasEntity.putBatch(alias));\n });\n });\n\n try {\n await batchWriteAll({\n table: this.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.fileEntity,\n partitionKey: this.createGSI1PartitionKey(initialWhere),\n options\n };\n let items = [];\n try {\n const dbItems = await queryAll<{ data: File }>(queryAllParams);\n items = dbItems.map(item => item.data);\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.plugins.byType<FileDynamoDbFieldPlugin>(FileDynamoDbFieldPlugin.type);\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.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.fileEntity,\n partitionKey: this.createGSI1PartitionKey(initialWhere),\n options: {\n index: \"GSI1\",\n gte: \" \",\n reverse: false\n }\n };\n let results = [];\n try {\n const dbItems = await queryAll<{ data: File }>(queryAllParams);\n results = dbItems.map(item => item.data);\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.plugins.byType<FileDynamoDbFieldPlugin>(FileDynamoDbFieldPlugin.type);\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.plugins,\n items: results,\n where,\n fields\n });\n\n const tags = filteredItems.reduce<\n Record<string, FileManagerFilesStorageOperationsTagsResponse>\n >((collection, item) => {\n const tags = Array.isArray(item.tags) ? item.tags : [];\n\n for (const tag of tags) {\n collection[tag] = {\n tag,\n count: (collection[tag]?.count || 0) + 1\n };\n }\n\n return collection;\n }, {});\n\n return Object.values(tags)\n .sort((a, b) => {\n return a.tag < b.tag ? -1 : 1;\n })\n .sort((a, b) => {\n return a.count > b.count ? -1 : 1;\n });\n }\n\n private createQueryAllOptions({ where }: QueryAllOptionsParams): DynamoDBToolboxQueryOptions {\n const options: DynamoDBToolboxQueryOptions = { index: \"GSI1\" };\n if (where.id) {\n options.eq = where.id;\n } else {\n options.gt = \" \";\n }\n return options;\n }\n\n private createPartitionKey(params: CreatePartitionKeyParams): string {\n const { tenant, locale, id } = params;\n return `T#${tenant}#L#${locale}#FM#FILE#${id}`;\n }\n private createGSI1PartitionKey(params: CreateGSI1PartitionKeyParams): string {\n const { tenant, locale } = params;\n return `T#${tenant}#L#${locale}#FM#FILES`;\n }\n\n private createNewAliasesRecords(\n file: File,\n existingAliases: FileAlias[] = []\n ): FileAliasItem[] {\n return (file.aliases || [])\n .map(alias => {\n // If alias is already in the DB, skip it.\n if (existingAliases.find(item => item.alias === alias)) {\n return null;\n }\n\n // Add a new alias.\n return {\n PK: this.createPartitionKey(file),\n SK: `ALIAS#${alias}`,\n GSI1_PK: `T#${file.tenant}#FM#FILE_ALIASES`,\n GSI1_SK: alias,\n TYPE: \"fm.fileAlias\",\n data: {\n alias,\n tenant: file.tenant,\n locale: file.locale,\n fileId: file.id,\n key: file.key\n }\n };\n })\n .filter(Boolean) as FileAliasItem[];\n }\n}\n"],"mappings":";;;;;;;;;AAiBA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAwBO,MAAMA,sBAAsB,CAA8C;EAMtEC,WAAW,CAAC;IAAEC,cAAc;IAAEC;EAA2B,CAAC,EAAE;IAAA;IAAA;IAAA;IAAA;IAC/D,IAAI,CAACA,OAAO,GAAGA,OAAO;IACtB,IAAI,CAACC,KAAK,GAAG,IAAAC,kBAAW,EAAC;MAAEH;IAAe,CAAC,CAAC;IAC5C,IAAI,CAACI,UAAU,GAAG,IAAAC,gCAAoB,EAAC,IAAI,CAACH,KAAK,EAAE,SAAS,CAAC;IAC7D,IAAI,CAACI,WAAW,GAAG,IAAAD,gCAAoB,EAAC,IAAI,CAACH,KAAK,EAAE,cAAc,CAAC;EACvE;EAEA,MAAaK,GAAG,CAACC,MAAkD,EAAwB;IACvF,MAAM;MAAEC;IAAM,CAAC,GAAGD,MAAM;IACxB,MAAME,IAAI,GAAG;MACTC,EAAE,EAAE,IAAI,CAACC,kBAAkB,CAACH,KAAK,CAAC;MAClCI,EAAE,EAAE;IACR,CAAC;IACD,IAAI;MACA,MAAMC,IAAI,GAAG,MAAM,IAAAC,QAAa,EAAiB;QAC7CC,MAAM,EAAE,IAAI,CAACZ,UAAU;QACvBM;MACJ,CAAC,CAAC;MAEF,OAAOI,IAAI,GAAGA,IAAI,CAACG,IAAI,GAAG,IAAI;IAClC,CAAC,CAAC,OAAOC,EAAE,EAAE;MACT,MAAM,IA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C,GAAGrB,MAAM;IACrC,OAAQ,KAAIiC,MAAO,MAAKC,MAAO,YAAWb,EAAG,EAAC;EAClD;EACQF,sBAAsB,CAACnB,MAAoC,EAAU;IACzE,MAAM;MAAEiC,MAAM;MAAEC;IAAO,CAAC,GAAGlC,MAAM;IACjC,OAAQ,KAAIiC,MAAO,MAAKC,MAAO,WAAU;EAC7C;EAEQT,uBAAuB,CAC3BnB,IAAU,EACVwB,eAA4B,GAAG,EAAE,EAClB;IACf,OAAO,CAACxB,IAAI,CAACiC,OAAO,IAAI,EAAE,EACrBD,GAAG,CAACX,KAAK,IAAI;MACV;MACA,IAAIG,eAAe,CAACoE,IAAI,CAACjF,IAAI,IAAIA,IAAI,CAACU,KAAK,KAAKA,KAAK,CAAC,EAAE;QACpD,OAAO,IAAI;MACf;;MAEA;MACA,OAAO;QACHxB,EAAE,EAAE,IAAI,CAACC,kBAAkB,CAACE,IAAI,CAAC;QACjCD,EAAE,EAAG,SAAQsB,KAAM,EAAC;QACpBT,OAAO,EAAG,KAAIZ,IAAI,CAAC2B,MAAO,kBAAiB;QAC3Cb,OAAO,EAAEO,KAAK;QACdL,IAAI,EAAE,cAAc;QACpBb,IAAI,EAAE;UACFkB,KAAK;UACLM,MAAM,EAAE3B,IAAI,CAAC2B,MAAM;UACnBC,MAAM,EAAE5B,IAAI,CAAC4B,MAAM;UACnBiE,MAAM,EAAE7F,IAAI,CAACe,EAAE;UACf+E,GAAG,EAAE9F,IAAI,CAAC8F;QACd;MACJ,CAAC;IACL,CAAC,CAAC,CACDC,MAAM,CAACC,OAAO,CAAC;EACxB;AACJ;AAAC"}
@@ -1,3 +1,2 @@
1
1
  import { FileDynamoDbFieldPlugin } from "../../plugins/FileDynamoDbFieldPlugin";
2
- declare const _default: () => FileDynamoDbFieldPlugin[];
3
- export default _default;
2
+ export declare const createFileFieldsPlugins: () => FileDynamoDbFieldPlugin[];
@@ -3,11 +3,9 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
7
-
6
+ exports.createFileFieldsPlugins = void 0;
8
7
  var _FileDynamoDbFieldPlugin = require("../../plugins/FileDynamoDbFieldPlugin");
9
-
10
- var _default = () => [new _FileDynamoDbFieldPlugin.FileDynamoDbFieldPlugin({
8
+ const createFileFieldsPlugins = () => [new _FileDynamoDbFieldPlugin.FileDynamoDbFieldPlugin({
11
9
  field: "id"
12
10
  }),
13
11
  /**
@@ -38,5 +36,4 @@ new _FileDynamoDbFieldPlugin.FileDynamoDbFieldPlugin({
38
36
  field: "createdOn",
39
37
  type: "date"
40
38
  })];
41
-
42
- exports.default = _default;
39
+ exports.createFileFieldsPlugins = createFileFieldsPlugins;
@@ -0,0 +1 @@
1
+ {"version":3,"names":["createFileFieldsPlugins","FileDynamoDbFieldPlugin","field","path","type"],"sources":["fields.ts"],"sourcesContent":["import { FileDynamoDbFieldPlugin } from \"~/plugins/FileDynamoDbFieldPlugin\";\n\nexport const createFileFieldsPlugins = () => [\n new FileDynamoDbFieldPlugin({\n field: \"id\"\n }),\n /**\n * Path plugin for the field that are ddb map type and the value filtered by is the id property in the object.\n */\n new FileDynamoDbFieldPlugin({\n field: \"createdBy\",\n path: \"createdBy.id\"\n }),\n /**\n * Path plugin for meta field properties.\n */\n new FileDynamoDbFieldPlugin({\n field: \"private\",\n path: \"meta.private\"\n }),\n /**\n * Path plugin for tag field.\n */\n new FileDynamoDbFieldPlugin({\n field: \"tag\",\n path: \"tags\"\n }),\n /**\n * Value transformation for the dateTime field.\n */\n new FileDynamoDbFieldPlugin({\n field: \"createdOn\",\n type: \"date\"\n })\n];\n"],"mappings":";;;;;;AAAA;AAEO,MAAMA,uBAAuB,GAAG,MAAM,CACzC,IAAIC,gDAAuB,CAAC;EACxBC,KAAK,EAAE;AACX,CAAC,CAAC;AACF;AACJ;AACA;AACI,IAAID,gDAAuB,CAAC;EACxBC,KAAK,EAAE,WAAW;EAClBC,IAAI,EAAE;AACV,CAAC,CAAC;AACF;AACJ;AACA;AACI,IAAIF,gDAAuB,CAAC;EACxBC,KAAK,EAAE,SAAS;EAChBC,IAAI,EAAE;AACV,CAAC,CAAC;AACF;AACJ;AACA;AACI,IAAIF,gDAAuB,CAAC;EACxBC,KAAK,EAAE,KAAK;EACZC,IAAI,EAAE;AACV,CAAC,CAAC;AACF;AACJ;AACA;AACI,IAAIF,gDAAuB,CAAC;EACxBC,KAAK,EAAE,WAAW;EAClBE,IAAI,EAAE;AACV,CAAC,CAAC,CACL;AAAC"}
@@ -1,6 +1,6 @@
1
- import { FilesStorageOperationsProviderPlugin, Params } from "@webiny/api-file-manager/plugins/definitions/FilesStorageOperationsProviderPlugin";
1
+ import { FilesStorageOperationsProviderPlugin, FilesStorageOperationsProviderPluginParams } from "@webiny/api-file-manager/plugins/definitions/FilesStorageOperationsProviderPlugin";
2
2
  import { FilesStorageOperations } from "./FilesStorageOperations";
3
3
  export declare class FilesStorageOperationsProviderDdb extends FilesStorageOperationsProviderPlugin {
4
4
  name: string;
5
- provide({ context }: Params): Promise<FilesStorageOperations>;
5
+ provide({ context }: FilesStorageOperationsProviderPluginParams): Promise<FilesStorageOperations>;
6
6
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
4
 
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
@@ -0,0 +1 @@
1
+ {"version":3,"names":["FilesStorageOperationsProviderDdb","FilesStorageOperationsProviderPlugin","provide","context","plugins","register","fields","FilesStorageOperations"],"sources":["index.ts"],"sourcesContent":["import {\n FilesStorageOperationsProviderPlugin,\n FilesStorageOperationsProviderPluginParams\n} from \"@webiny/api-file-manager/plugins/definitions/FilesStorageOperationsProviderPlugin\";\nimport { FilesStorageOperations } from \"./FilesStorageOperations\";\nimport fields from \"./fields\";\n\nexport class FilesStorageOperationsProviderDdb extends FilesStorageOperationsProviderPlugin {\n public override name = \"fm.storageOperationsProvider.files.ddb\";\n async provide({ context }: FilesStorageOperationsProviderPluginParams) {\n context.plugins.register(fields());\n return new FilesStorageOperations({\n context\n });\n }\n}\n"],"mappings":";;;;;;;;;;;AAAA;;AAIA;;AACA;;AAEO,MAAMA,iCAAN,SAAgDC,0EAAhD,CAAqF;EAAA;IAAA;IAAA,4CACjE,wCADiE;EAAA;;EAE3E,MAAPC,OAAO,CAAC;IAAEC;EAAF,CAAD,EAA0D;IACnEA,OAAO,CAACC,OAAR,CAAgBC,QAAhB,CAAyB,IAAAC,eAAA,GAAzB;IACA,OAAO,IAAIC,8CAAJ,CAA2B;MAC9BJ;IAD8B,CAA3B,CAAP;EAGH;;AAPuF"}
@@ -1,15 +1,14 @@
1
- import { FileManagerContext, FileManagerSettings, FileManagerSettingsStorageOperations, FileManagerSettingsStorageOperationsCreateParams, FileManagerSettingsStorageOperationsUpdateParams } from "@webiny/api-file-manager/types";
2
- interface ConstructorParams {
3
- context: FileManagerContext;
1
+ import { FileManagerSettings, FileManagerSettingsStorageOperations, FileManagerSettingsStorageOperationsCreateParams, FileManagerSettingsStorageOperationsUpdateParams, FileManagerStorageOperationsDeleteSettings, FileManagerStorageOperationsGetSettingsParams } from "@webiny/api-file-manager/types";
2
+ import { DocumentClient } from "aws-sdk/clients/dynamodb";
3
+ interface SettingsStorageOperationsConfig {
4
+ documentClient: DocumentClient;
4
5
  }
5
6
  export declare class SettingsStorageOperations implements FileManagerSettingsStorageOperations {
6
- private readonly _context;
7
7
  private readonly _entity;
8
- private get partitionKey();
9
- constructor({ context }: ConstructorParams);
10
- get(): Promise<FileManagerSettings>;
8
+ constructor({ documentClient }: SettingsStorageOperationsConfig);
9
+ get({ tenant }: FileManagerStorageOperationsGetSettingsParams): Promise<FileManagerSettings | null>;
11
10
  create({ data }: FileManagerSettingsStorageOperationsCreateParams): Promise<FileManagerSettings>;
12
11
  update({ data }: FileManagerSettingsStorageOperationsUpdateParams): Promise<FileManagerSettings>;
13
- delete(): Promise<void>;
12
+ delete({ tenant }: FileManagerStorageOperationsDeleteSettings): Promise<void>;
14
13
  }
15
14
  export {};