@webiny/api-file-manager-ddb 0.0.0-mt-1
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/LICENSE +21 -0
- package/README.md +30 -0
- package/definitions/filesEntity.d.ts +7 -0
- package/definitions/filesEntity.js +80 -0
- package/definitions/settingsEntity.d.ts +7 -0
- package/definitions/settingsEntity.js +56 -0
- package/definitions/systemEntity.d.ts +7 -0
- package/definitions/systemEntity.js +44 -0
- package/definitions/table.d.ts +6 -0
- package/definitions/table.js +28 -0
- package/index.d.ts +5 -0
- package/index.js +16 -0
- package/operations/configurations.d.ts +14 -0
- package/operations/configurations.js +22 -0
- package/operations/files/FilesStorageOperations.d.ts +22 -0
- package/operations/files/FilesStorageOperations.js +345 -0
- package/operations/files/fields.d.ts +3 -0
- package/operations/files/fields.js +42 -0
- package/operations/files/index.d.ts +6 -0
- package/operations/files/index.js +35 -0
- package/operations/settings/SettingsStorageOperations.d.ts +15 -0
- package/operations/settings/SettingsStorageOperations.js +120 -0
- package/operations/settings/index.d.ts +8 -0
- package/operations/settings/index.js +32 -0
- package/operations/system/SystemStorageOperations.d.ts +14 -0
- package/operations/system/SystemStorageOperations.js +108 -0
- package/operations/system/index.d.ts +8 -0
- package/operations/system/index.js +32 -0
- package/operations/utils.d.ts +4 -0
- package/operations/utils.js +36 -0
- package/package.json +67 -0
- package/plugins/FileAttributePlugin.d.ts +4 -0
- package/plugins/FileAttributePlugin.js +27 -0
- package/plugins/FileDynamoDbFieldPlugin.d.ts +4 -0
- package/plugins/FileDynamoDbFieldPlugin.js +17 -0
- package/plugins/SettingsAttributePlugin.d.ts +4 -0
- package/plugins/SettingsAttributePlugin.js +27 -0
- package/plugins/SystemAttributePlugin.d.ts +4 -0
- package/plugins/SystemAttributePlugin.js +27 -0
- package/types.d.ts +0 -0
- package/types.js +1 -0
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.FilesStorageOperations = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
|
+
|
|
14
|
+
var _table = _interopRequireDefault(require("../../definitions/table"));
|
|
15
|
+
|
|
16
|
+
var _filesEntity = _interopRequireDefault(require("../../definitions/filesEntity"));
|
|
17
|
+
|
|
18
|
+
var _query = require("@webiny/db-dynamodb/utils/query");
|
|
19
|
+
|
|
20
|
+
var _cursor = require("@webiny/db-dynamodb/utils/cursor");
|
|
21
|
+
|
|
22
|
+
var _filter = require("@webiny/db-dynamodb/utils/filter");
|
|
23
|
+
|
|
24
|
+
var _sort = require("@webiny/db-dynamodb/utils/sort");
|
|
25
|
+
|
|
26
|
+
var _FileDynamoDbFieldPlugin = require("../../plugins/FileDynamoDbFieldPlugin");
|
|
27
|
+
|
|
28
|
+
var _batchWrite = require("@webiny/db-dynamodb/utils/batchWrite");
|
|
29
|
+
|
|
30
|
+
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
|
+
|
|
38
|
+
class FilesStorageOperations {
|
|
39
|
+
get context() {
|
|
40
|
+
return this._context;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
constructor({
|
|
44
|
+
context
|
|
45
|
+
}) {
|
|
46
|
+
(0, _defineProperty2.default)(this, "_context", void 0);
|
|
47
|
+
(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
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async get(params) {
|
|
60
|
+
const {
|
|
61
|
+
where
|
|
62
|
+
} = params;
|
|
63
|
+
const keys = {
|
|
64
|
+
PK: this.createPartitionKey(where),
|
|
65
|
+
SK: this.createSortKey(where)
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
const file = await (0, _get.get)({
|
|
70
|
+
entity: this.entity,
|
|
71
|
+
keys
|
|
72
|
+
});
|
|
73
|
+
return (0, _cleanup.cleanupItem)(this.entity, file);
|
|
74
|
+
} catch (ex) {
|
|
75
|
+
throw new _error.default(ex.message || "Could not fetch requested file.", ex.code || "GET_FILE_ERROR", {
|
|
76
|
+
error: ex,
|
|
77
|
+
where
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async create(params) {
|
|
83
|
+
const {
|
|
84
|
+
file
|
|
85
|
+
} = params;
|
|
86
|
+
const keys = {
|
|
87
|
+
PK: this.createPartitionKey(file),
|
|
88
|
+
SK: this.createSortKey(file)
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const item = _objectSpread(_objectSpread(_objectSpread({}, file), keys), {}, {
|
|
92
|
+
TYPE: "fm.file"
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
try {
|
|
96
|
+
await this.entity.put(item);
|
|
97
|
+
} catch (ex) {
|
|
98
|
+
throw new _error.default(ex.message || "Could not create a new file in the DynamoDB.", ex.code || "CREATE_FILE_ERROR", {
|
|
99
|
+
error: ex,
|
|
100
|
+
item
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return file;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async update(params) {
|
|
108
|
+
const {
|
|
109
|
+
file
|
|
110
|
+
} = params;
|
|
111
|
+
const keys = {
|
|
112
|
+
PK: this.createPartitionKey(file),
|
|
113
|
+
SK: this.createSortKey(file)
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const item = _objectSpread(_objectSpread(_objectSpread({}, file), keys), {}, {
|
|
117
|
+
TYPE: "fm.file"
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
await this.entity.put(item);
|
|
122
|
+
} catch (ex) {
|
|
123
|
+
throw new _error.default(ex.message || "Could not update a file in the DynamoDB.", ex.code || "UPDATE_FILE_ERROR", {
|
|
124
|
+
error: ex,
|
|
125
|
+
item
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return file;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async delete(params) {
|
|
133
|
+
const {
|
|
134
|
+
file
|
|
135
|
+
} = params;
|
|
136
|
+
const keys = {
|
|
137
|
+
PK: this.createPartitionKey(file),
|
|
138
|
+
SK: this.createSortKey(file)
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
try {
|
|
142
|
+
await this.entity.delete(keys);
|
|
143
|
+
} catch (ex) {
|
|
144
|
+
throw new _error.default(ex.message || "Could not delete file from the DynamoDB.", ex.code || "DELETE_FILE_ERROR", {
|
|
145
|
+
error: ex,
|
|
146
|
+
file,
|
|
147
|
+
keys
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async createBatch(params) {
|
|
153
|
+
const {
|
|
154
|
+
files
|
|
155
|
+
} = params;
|
|
156
|
+
const items = files.map(file => {
|
|
157
|
+
return this.entity.putBatch(_objectSpread(_objectSpread({}, file), {}, {
|
|
158
|
+
PK: this.createPartitionKey(file),
|
|
159
|
+
SK: this.createSortKey(file),
|
|
160
|
+
TYPE: "fm.file"
|
|
161
|
+
}));
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
try {
|
|
165
|
+
await (0, _batchWrite.batchWriteAll)({
|
|
166
|
+
table: this.entity.table,
|
|
167
|
+
items
|
|
168
|
+
});
|
|
169
|
+
} catch (ex) {
|
|
170
|
+
throw new _error.default(ex.message || "Could not batch insert a list of files.", ex.code || "BATCH_CREATE_FILES_ERROR", {
|
|
171
|
+
error: ex,
|
|
172
|
+
files
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return files;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async list(params) {
|
|
180
|
+
const {
|
|
181
|
+
where: initialWhere,
|
|
182
|
+
limit,
|
|
183
|
+
after,
|
|
184
|
+
sort
|
|
185
|
+
} = params;
|
|
186
|
+
const options = this.createQueryAllOptions({
|
|
187
|
+
where: initialWhere
|
|
188
|
+
});
|
|
189
|
+
const queryAllParams = {
|
|
190
|
+
entity: this.entity,
|
|
191
|
+
partitionKey: this.createPartitionKey(initialWhere),
|
|
192
|
+
options
|
|
193
|
+
};
|
|
194
|
+
let items = [];
|
|
195
|
+
|
|
196
|
+
try {
|
|
197
|
+
items = await (0, _query.queryAll)(queryAllParams);
|
|
198
|
+
} catch (ex) {
|
|
199
|
+
throw new _error.default(ex.message || "Could not query for the files.", ex.code || "FILE_LIST_ERROR", {
|
|
200
|
+
error: ex,
|
|
201
|
+
where: initialWhere,
|
|
202
|
+
limit,
|
|
203
|
+
after,
|
|
204
|
+
sort,
|
|
205
|
+
queryParams: {
|
|
206
|
+
options,
|
|
207
|
+
partitionKey: queryAllParams.partitionKey,
|
|
208
|
+
entity: queryAllParams.entity.name,
|
|
209
|
+
table: queryAllParams.entity.table.name
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const where = _objectSpread({}, initialWhere);
|
|
215
|
+
|
|
216
|
+
if (where.search) {
|
|
217
|
+
where.contains = {
|
|
218
|
+
fields: ["name", "tags"],
|
|
219
|
+
value: where.search
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
delete where["tenant"];
|
|
224
|
+
delete where["locale"];
|
|
225
|
+
delete where["search"];
|
|
226
|
+
const fields = this.context.plugins.byType(_FileDynamoDbFieldPlugin.FileDynamoDbFieldPlugin.type);
|
|
227
|
+
/**
|
|
228
|
+
* Filter the read items via the code.
|
|
229
|
+
* It will build the filters out of the where input and transform the values it is using.
|
|
230
|
+
*/
|
|
231
|
+
|
|
232
|
+
const filteredFiles = (0, _filter.filterItems)({
|
|
233
|
+
plugins: this.context.plugins,
|
|
234
|
+
items,
|
|
235
|
+
where,
|
|
236
|
+
fields
|
|
237
|
+
});
|
|
238
|
+
const totalCount = filteredFiles.length;
|
|
239
|
+
/**
|
|
240
|
+
* Sorting is also done via the code.
|
|
241
|
+
* It takes the sort input and sorts by it via the lodash sortBy method.
|
|
242
|
+
*/
|
|
243
|
+
|
|
244
|
+
const sortedFiles = (0, _sort.sortItems)({
|
|
245
|
+
items: filteredFiles,
|
|
246
|
+
sort,
|
|
247
|
+
fields
|
|
248
|
+
});
|
|
249
|
+
const start = (0, _cursor.decodeCursor)(after) || 0;
|
|
250
|
+
const hasMoreItems = totalCount > start + limit;
|
|
251
|
+
const end = limit > totalCount + start + limit ? undefined : start + limit;
|
|
252
|
+
const files = sortedFiles.slice(start, end);
|
|
253
|
+
/**
|
|
254
|
+
* Although we do not need a cursor here, we will use it as such to keep it standardized.
|
|
255
|
+
* Number is simply encoded.
|
|
256
|
+
*/
|
|
257
|
+
|
|
258
|
+
const cursor = files.length > 0 ? (0, _cursor.encodeCursor)(start + limit) : null;
|
|
259
|
+
const meta = {
|
|
260
|
+
hasMoreItems,
|
|
261
|
+
totalCount: totalCount,
|
|
262
|
+
cursor
|
|
263
|
+
};
|
|
264
|
+
return [files, meta];
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
async tags(params) {
|
|
268
|
+
const {
|
|
269
|
+
where
|
|
270
|
+
} = params;
|
|
271
|
+
const queryAllParams = {
|
|
272
|
+
entity: this.entity,
|
|
273
|
+
partitionKey: this.createPartitionKey(where),
|
|
274
|
+
options: {
|
|
275
|
+
gte: " ",
|
|
276
|
+
reverse: false
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
let results = [];
|
|
280
|
+
|
|
281
|
+
try {
|
|
282
|
+
results = await (0, _query.queryAll)(queryAllParams);
|
|
283
|
+
} catch (ex) {
|
|
284
|
+
throw new _error.default(ex.message || "Error in the DynamoDB query.", ex.code || "DYNAMODB_ERROR", {
|
|
285
|
+
error: ex,
|
|
286
|
+
query: queryAllParams
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Aggregate all the tags from all the items in the database.
|
|
291
|
+
*/
|
|
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);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return collection;
|
|
304
|
+
}, {});
|
|
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];
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
createQueryAllOptions({
|
|
317
|
+
where
|
|
318
|
+
}) {
|
|
319
|
+
const options = {};
|
|
320
|
+
|
|
321
|
+
if (where.id) {
|
|
322
|
+
options.eq = where.id;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return options;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
createPartitionKey(params) {
|
|
329
|
+
const {
|
|
330
|
+
tenant,
|
|
331
|
+
locale
|
|
332
|
+
} = params;
|
|
333
|
+
return `T#${tenant}#L#${locale}#FM#F`;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
createSortKey(params) {
|
|
337
|
+
const {
|
|
338
|
+
id
|
|
339
|
+
} = params;
|
|
340
|
+
return id;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
exports.FilesStorageOperations = FilesStorageOperations;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _FileDynamoDbFieldPlugin = require("../../plugins/FileDynamoDbFieldPlugin");
|
|
9
|
+
|
|
10
|
+
var _default = () => [new _FileDynamoDbFieldPlugin.FileDynamoDbFieldPlugin({
|
|
11
|
+
field: "id"
|
|
12
|
+
}),
|
|
13
|
+
/**
|
|
14
|
+
* Path plugin for the field that are ddb map type and the value filtered by is the id property in the object.
|
|
15
|
+
*/
|
|
16
|
+
new _FileDynamoDbFieldPlugin.FileDynamoDbFieldPlugin({
|
|
17
|
+
field: "createdBy",
|
|
18
|
+
path: "createdBy.id"
|
|
19
|
+
}),
|
|
20
|
+
/**
|
|
21
|
+
* Path plugin for meta field properties.
|
|
22
|
+
*/
|
|
23
|
+
new _FileDynamoDbFieldPlugin.FileDynamoDbFieldPlugin({
|
|
24
|
+
field: "private",
|
|
25
|
+
path: "meta.private"
|
|
26
|
+
}),
|
|
27
|
+
/**
|
|
28
|
+
* Path plugin for tag field.
|
|
29
|
+
*/
|
|
30
|
+
new _FileDynamoDbFieldPlugin.FileDynamoDbFieldPlugin({
|
|
31
|
+
field: "tag",
|
|
32
|
+
path: "tags"
|
|
33
|
+
}),
|
|
34
|
+
/**
|
|
35
|
+
* Value transformation for the dateTime field.
|
|
36
|
+
*/
|
|
37
|
+
new _FileDynamoDbFieldPlugin.FileDynamoDbFieldPlugin({
|
|
38
|
+
field: "createdOn",
|
|
39
|
+
type: "date"
|
|
40
|
+
})];
|
|
41
|
+
|
|
42
|
+
exports.default = _default;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { FilesStorageOperationsProviderPlugin, Params } from "@webiny/api-file-manager/plugins/definitions/FilesStorageOperationsProviderPlugin";
|
|
2
|
+
import { FilesStorageOperations } from "./FilesStorageOperations";
|
|
3
|
+
export declare class FilesStorageOperationsProviderDdb extends FilesStorageOperationsProviderPlugin {
|
|
4
|
+
name: string;
|
|
5
|
+
provide({ context }: Params): Promise<FilesStorageOperations>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.FilesStorageOperationsProviderDdb = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
var _FilesStorageOperationsProviderPlugin = require("@webiny/api-file-manager/plugins/definitions/FilesStorageOperationsProviderPlugin");
|
|
13
|
+
|
|
14
|
+
var _FilesStorageOperations = require("./FilesStorageOperations");
|
|
15
|
+
|
|
16
|
+
var _fields = _interopRequireDefault(require("./fields"));
|
|
17
|
+
|
|
18
|
+
class FilesStorageOperationsProviderDdb extends _FilesStorageOperationsProviderPlugin.FilesStorageOperationsProviderPlugin {
|
|
19
|
+
constructor(...args) {
|
|
20
|
+
super(...args);
|
|
21
|
+
(0, _defineProperty2.default)(this, "name", "fm.storageOperationsProvider.files.ddb");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async provide({
|
|
25
|
+
context
|
|
26
|
+
}) {
|
|
27
|
+
context.plugins.register((0, _fields.default)());
|
|
28
|
+
return new _FilesStorageOperations.FilesStorageOperations({
|
|
29
|
+
context
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
exports.FilesStorageOperationsProviderDdb = FilesStorageOperationsProviderDdb;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { FileManagerContext, FileManagerSettings, FileManagerSettingsStorageOperations, FileManagerSettingsStorageOperationsCreateParams, FileManagerSettingsStorageOperationsUpdateParams } from "@webiny/api-file-manager/types";
|
|
2
|
+
interface ConstructorParams {
|
|
3
|
+
context: FileManagerContext;
|
|
4
|
+
}
|
|
5
|
+
export declare class SettingsStorageOperations implements FileManagerSettingsStorageOperations {
|
|
6
|
+
private readonly _context;
|
|
7
|
+
private readonly _entity;
|
|
8
|
+
private get partitionKey();
|
|
9
|
+
constructor({ context }: ConstructorParams);
|
|
10
|
+
get(): Promise<FileManagerSettings>;
|
|
11
|
+
create({ data }: FileManagerSettingsStorageOperationsCreateParams): Promise<FileManagerSettings>;
|
|
12
|
+
update({ data }: FileManagerSettingsStorageOperationsUpdateParams): Promise<FileManagerSettings>;
|
|
13
|
+
delete(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.SettingsStorageOperations = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
|
+
|
|
14
|
+
var _table = _interopRequireDefault(require("../../definitions/table"));
|
|
15
|
+
|
|
16
|
+
var _settingsEntity = _interopRequireDefault(require("../../definitions/settingsEntity"));
|
|
17
|
+
|
|
18
|
+
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; }
|
|
19
|
+
|
|
20
|
+
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; }
|
|
21
|
+
|
|
22
|
+
const SORT_KEY = "default";
|
|
23
|
+
|
|
24
|
+
class SettingsStorageOperations {
|
|
25
|
+
get partitionKey() {
|
|
26
|
+
const tenant = this._context.tenancy.getCurrentTenant();
|
|
27
|
+
|
|
28
|
+
if (!tenant) {
|
|
29
|
+
throw new _error.default("Tenant missing.", "TENANT_NOT_FOUND");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return `T#${tenant.id}#FM#SETTINGS`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
constructor({
|
|
36
|
+
context
|
|
37
|
+
}) {
|
|
38
|
+
(0, _defineProperty2.default)(this, "_context", void 0);
|
|
39
|
+
(0, _defineProperty2.default)(this, "_entity", void 0);
|
|
40
|
+
this._context = context;
|
|
41
|
+
const table = (0, _table.default)({
|
|
42
|
+
context
|
|
43
|
+
});
|
|
44
|
+
this._entity = (0, _settingsEntity.default)({
|
|
45
|
+
context,
|
|
46
|
+
table
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async get() {
|
|
51
|
+
try {
|
|
52
|
+
const settings = await this._entity.get({
|
|
53
|
+
PK: this.partitionKey,
|
|
54
|
+
SK: SORT_KEY
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (!settings || !settings.Item) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return settings.Item;
|
|
62
|
+
} catch (ex) {
|
|
63
|
+
throw new _error.default(ex.message || "Could not fetch the FileManager settings.", ex.code || "GET_SETTINGS_ERROR");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async create({
|
|
68
|
+
data
|
|
69
|
+
}) {
|
|
70
|
+
const original = await this.get();
|
|
71
|
+
/**
|
|
72
|
+
* TODO: check if need to throw an error on existing settings
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
if (original) {
|
|
76
|
+
return await this.update({
|
|
77
|
+
original,
|
|
78
|
+
data
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
await this._entity.put(_objectSpread({
|
|
84
|
+
PK: this.partitionKey,
|
|
85
|
+
SK: SORT_KEY
|
|
86
|
+
}, data));
|
|
87
|
+
return data;
|
|
88
|
+
} catch (ex) {
|
|
89
|
+
throw new _error.default(ex.message || "Cannot create FileManager settings.", ex.code || "CREATE_FM_SETTINGS_ERROR", {
|
|
90
|
+
data
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async update({
|
|
96
|
+
data
|
|
97
|
+
}) {
|
|
98
|
+
try {
|
|
99
|
+
await this._entity.update(_objectSpread({
|
|
100
|
+
PK: this.partitionKey,
|
|
101
|
+
SK: SORT_KEY
|
|
102
|
+
}, data));
|
|
103
|
+
return data;
|
|
104
|
+
} catch (ex) {
|
|
105
|
+
throw new _error.default(ex.message || "Cannot update FileManager settings.", ex.code || "UPDATE_FM_SETTINGS_ERROR", {
|
|
106
|
+
data
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async delete() {
|
|
112
|
+
return this._entity.delete({
|
|
113
|
+
PK: this.partitionKey,
|
|
114
|
+
SK: SORT_KEY
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
exports.SettingsStorageOperations = SettingsStorageOperations;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { SettingsStorageOperations } from "./SettingsStorageOperations";
|
|
2
|
+
import { SettingsStorageOperationsProviderPlugin } from "@webiny/api-file-manager/plugins/definitions/SettingsStorageOperationsProviderPlugin";
|
|
3
|
+
export declare class SettingsStorageOperationsProviderDdbPlugin extends SettingsStorageOperationsProviderPlugin {
|
|
4
|
+
name: string;
|
|
5
|
+
provide({ context }: {
|
|
6
|
+
context: any;
|
|
7
|
+
}): Promise<SettingsStorageOperations>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.SettingsStorageOperationsProviderDdbPlugin = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
var _SettingsStorageOperations = require("./SettingsStorageOperations");
|
|
13
|
+
|
|
14
|
+
var _SettingsStorageOperationsProviderPlugin = require("@webiny/api-file-manager/plugins/definitions/SettingsStorageOperationsProviderPlugin");
|
|
15
|
+
|
|
16
|
+
class SettingsStorageOperationsProviderDdbPlugin extends _SettingsStorageOperationsProviderPlugin.SettingsStorageOperationsProviderPlugin {
|
|
17
|
+
constructor(...args) {
|
|
18
|
+
super(...args);
|
|
19
|
+
(0, _defineProperty2.default)(this, "name", "fm.storageOperationsProvider.settings.ddb");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async provide({
|
|
23
|
+
context
|
|
24
|
+
}) {
|
|
25
|
+
return new _SettingsStorageOperations.SettingsStorageOperations({
|
|
26
|
+
context
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
exports.SettingsStorageOperationsProviderDdbPlugin = SettingsStorageOperationsProviderDdbPlugin;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { FileManagerContext, FileManagerSystem, FileManagerSystemStorageOperations, FileManagerSystemStorageOperationsCreateParams, FileManagerSystemStorageOperationsUpdateParams } from "@webiny/api-file-manager/types";
|
|
2
|
+
interface ConstructorParams {
|
|
3
|
+
context: FileManagerContext;
|
|
4
|
+
}
|
|
5
|
+
export declare class SystemStorageOperations implements FileManagerSystemStorageOperations {
|
|
6
|
+
private readonly _context;
|
|
7
|
+
private readonly _entity;
|
|
8
|
+
private get partitionKey();
|
|
9
|
+
constructor({ context }: ConstructorParams);
|
|
10
|
+
get(): Promise<FileManagerSystem | null>;
|
|
11
|
+
create(params: FileManagerSystemStorageOperationsCreateParams): Promise<FileManagerSystem>;
|
|
12
|
+
update(params: FileManagerSystemStorageOperationsUpdateParams): Promise<FileManagerSystem>;
|
|
13
|
+
}
|
|
14
|
+
export {};
|