@or-sdk/key-value-storage 0.23.4 → 0.23.5-beta.433.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.
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -35,10 +46,21 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
46
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
47
  }
37
48
  };
49
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
50
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
51
+ if (ar || !(i in from)) {
52
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
53
+ ar[i] = from[i];
54
+ }
55
+ }
56
+ return to.concat(ar || Array.prototype.slice.call(from));
57
+ };
38
58
  Object.defineProperty(exports, "__esModule", { value: true });
39
59
  exports.KeyValueStorage = void 0;
60
+ var base_1 = require("@or-sdk/base");
40
61
  var sdk_api_1 = require("@or-sdk/sdk-api");
41
62
  var constants_1 = require("./constants");
63
+ var utils_1 = require("./utils");
42
64
  var KeyValueStorage = (function () {
43
65
  function KeyValueStorage(params) {
44
66
  var token = params.token, discoveryUrl = params.discoveryUrl, accountId = params.accountId, sdkUrl = params.sdkUrl;
@@ -52,43 +74,124 @@ var KeyValueStorage = (function () {
52
74
  KeyValueStorage.prototype.composeRoute = function (route) {
53
75
  return this.sdkApi.isCrossAccount ? "accounts/".concat(this.sdkApi.currentAccountId, "/").concat(route) : route;
54
76
  };
55
- KeyValueStorage.prototype.listKeys = function (tableName, prefix, withValues) {
56
- if (withValues === void 0) { withValues = false; }
77
+ KeyValueStorage.prototype.listCollectionsPaginated = function (_a) {
78
+ var prefix = _a.prefix, maxKeys = _a.maxKeys, next = _a.next;
57
79
  return __awaiter(this, void 0, void 0, function () {
58
- var records;
59
- return __generator(this, function (_a) {
60
- switch (_a.label) {
61
- case 0: return [4, this.sdkApi.makeRequest({
62
- method: 'GET',
63
- route: this.composeRoute("".concat(constants_1.STORAGE_ROUTE, "/").concat(tableName)),
64
- params: {
65
- prefix: prefix ? prefix : undefined,
66
- withValues: withValues,
67
- },
80
+ var params, _b, tables, isTruncated, newNext;
81
+ return __generator(this, function (_c) {
82
+ switch (_c.label) {
83
+ case 0:
84
+ params = {
85
+ prefix: prefix ? prefix : undefined,
86
+ next: next ? next : undefined,
87
+ maxKeys: maxKeys ? maxKeys : undefined,
88
+ };
89
+ return [4, this.sdkApi.makeRequest({
90
+ method: 'GET',
91
+ route: this.composeRoute("".concat(constants_1.STORAGE_ROUTE)),
92
+ params: params,
93
+ })];
94
+ case 1:
95
+ _b = _c.sent(), tables = _b.tables, isTruncated = _b.isTruncated, newNext = _b.next;
96
+ return [2, __assign(__assign({}, (0, base_1.makeList)(tables)), { isTruncated: isTruncated, next: newNext })];
97
+ }
98
+ });
99
+ });
100
+ };
101
+ KeyValueStorage.prototype.listCollections = function (prefix) {
102
+ return __awaiter(this, void 0, void 0, function () {
103
+ var itemsTotal, prevNext, _a, items, next;
104
+ return __generator(this, function (_b) {
105
+ switch (_b.label) {
106
+ case 0:
107
+ itemsTotal = [];
108
+ _b.label = 1;
109
+ case 1: return [4, this.listCollectionsPaginated({
110
+ prefix: prefix,
111
+ next: prevNext,
68
112
  })];
113
+ case 2:
114
+ _a = _b.sent(), items = _a.items, next = _a.next;
115
+ prevNext = next;
116
+ itemsTotal = __spreadArray(__spreadArray([], itemsTotal, true), items, true);
117
+ _b.label = 3;
118
+ case 3:
119
+ if (prevNext) return [3, 1];
120
+ _b.label = 4;
121
+ case 4: return [2, (0, base_1.makeList)(itemsTotal)];
122
+ }
123
+ });
124
+ });
125
+ };
126
+ KeyValueStorage.prototype.listKeysPaginated = function (_a) {
127
+ var collectionName = _a.collectionName, prefix = _a.prefix, _b = _a.withValues, withValues = _b === void 0 ? false : _b, maxKeys = _a.maxKeys, next = _a.next;
128
+ return __awaiter(this, void 0, void 0, function () {
129
+ var params, _c, records, isTruncated, newNext;
130
+ return __generator(this, function (_d) {
131
+ switch (_d.label) {
132
+ case 0:
133
+ params = {
134
+ prefix: prefix ? prefix : undefined,
135
+ next: next ? next : undefined,
136
+ maxKeys: maxKeys ? maxKeys : undefined,
137
+ withValues: withValues,
138
+ };
139
+ return [4, this.sdkApi.makeRequest({
140
+ method: 'GET',
141
+ route: this.composeRoute("".concat(constants_1.STORAGE_ROUTE, "/").concat(collectionName)),
142
+ params: params,
143
+ })];
69
144
  case 1:
70
- records = (_a.sent()).value.records;
71
- return [2, records];
145
+ _c = (_d.sent()).value, records = _c.records, isTruncated = _c.isTruncated, newNext = _c.next;
146
+ return [2, __assign(__assign({}, (0, base_1.makeList)(records)), { next: newNext, isTruncated: isTruncated })];
72
147
  }
73
148
  });
74
149
  });
75
150
  };
76
- KeyValueStorage.prototype.getValueByKey = function (tableName, key) {
151
+ KeyValueStorage.prototype.listKeys = function (collectionName, prefix, withValues) {
152
+ if (withValues === void 0) { withValues = false; }
153
+ return __awaiter(this, void 0, void 0, function () {
154
+ var itemsTotal, prevNext, _a, items, next;
155
+ return __generator(this, function (_b) {
156
+ switch (_b.label) {
157
+ case 0:
158
+ itemsTotal = [];
159
+ _b.label = 1;
160
+ case 1: return [4, this.listKeysPaginated({
161
+ collectionName: collectionName,
162
+ prefix: prefix,
163
+ withValues: withValues,
164
+ next: prevNext,
165
+ })];
166
+ case 2:
167
+ _a = _b.sent(), items = _a.items, next = _a.next;
168
+ prevNext = next;
169
+ itemsTotal = __spreadArray(__spreadArray([], itemsTotal, true), items, true);
170
+ _b.label = 3;
171
+ case 3:
172
+ if (prevNext) return [3, 1];
173
+ _b.label = 4;
174
+ case 4: return [2, (0, base_1.makeList)(itemsTotal)];
175
+ }
176
+ });
177
+ });
178
+ };
179
+ KeyValueStorage.prototype.getValueByKey = function (collectionName, key) {
77
180
  return __awaiter(this, void 0, void 0, function () {
78
181
  return __generator(this, function (_a) {
79
182
  return [2, this.sdkApi.makeRequest({
80
183
  method: 'GET',
81
- route: this.composeRoute("".concat(constants_1.STORAGE_ROUTE, "/").concat(tableName, "/").concat(key)),
184
+ route: this.composeRoute("".concat(constants_1.STORAGE_ROUTE, "/").concat(collectionName, "/").concat(key)),
82
185
  })];
83
186
  });
84
187
  });
85
188
  };
86
- KeyValueStorage.prototype.setValueByKey = function (tableName, key, value, expire) {
189
+ KeyValueStorage.prototype.setValueByKey = function (collectionName, key, value, expire) {
87
190
  return __awaiter(this, void 0, void 0, function () {
88
191
  return __generator(this, function (_a) {
89
192
  return [2, this.sdkApi.makeRequest({
90
193
  method: 'POST',
91
- route: this.composeRoute("".concat(constants_1.STORAGE_ROUTE, "/").concat(tableName, "/").concat(key)),
194
+ route: this.composeRoute("".concat(constants_1.STORAGE_ROUTE, "/").concat(collectionName, "/").concat(key)),
92
195
  data: {
93
196
  expire: expire,
94
197
  value: value,
@@ -97,16 +200,83 @@ var KeyValueStorage = (function () {
97
200
  });
98
201
  });
99
202
  };
100
- KeyValueStorage.prototype.deleteKey = function (tableName, key) {
203
+ KeyValueStorage.prototype.deleteKey = function (collectionName, key) {
101
204
  return __awaiter(this, void 0, void 0, function () {
102
205
  return __generator(this, function (_a) {
103
206
  return [2, this.sdkApi.makeRequest({
104
207
  method: 'DELETE',
105
- route: this.composeRoute("".concat(constants_1.STORAGE_ROUTE, "/").concat(tableName, "/").concat(key)),
208
+ route: this.composeRoute("".concat(constants_1.STORAGE_ROUTE, "/").concat(collectionName, "/").concat(key)),
106
209
  })];
107
210
  });
108
211
  });
109
212
  };
213
+ KeyValueStorage.prototype.processMultipleRecords = function (records, action, collectionName) {
214
+ return __awaiter(this, void 0, void 0, function () {
215
+ return __generator(this, function (_a) {
216
+ return [2, this.sdkApi.makeRequest({
217
+ method: 'POST',
218
+ route: this.composeRoute("".concat(constants_1.STORAGE_MULTIPLE_ROUTE)),
219
+ data: {
220
+ keys: records,
221
+ method: action,
222
+ table: collectionName,
223
+ },
224
+ })];
225
+ });
226
+ });
227
+ };
228
+ KeyValueStorage.prototype.copyCollection = function (sourceCollectionName, targetCollectionName) {
229
+ return __awaiter(this, void 0, void 0, function () {
230
+ var items, recordGroups, _i, recordGroups_1, records;
231
+ return __generator(this, function (_a) {
232
+ switch (_a.label) {
233
+ case 0: return [4, this.listKeys(sourceCollectionName, '', true)];
234
+ case 1:
235
+ items = (_a.sent()).items;
236
+ recordGroups = (0, utils_1.splitIntoParts)(items);
237
+ _i = 0, recordGroups_1 = recordGroups;
238
+ _a.label = 2;
239
+ case 2:
240
+ if (!(_i < recordGroups_1.length)) return [3, 5];
241
+ records = recordGroups_1[_i];
242
+ return [4, this.processMultipleRecords(records, constants_1.Action.SET, targetCollectionName)];
243
+ case 3:
244
+ _a.sent();
245
+ _a.label = 4;
246
+ case 4:
247
+ _i++;
248
+ return [3, 2];
249
+ case 5: return [2];
250
+ }
251
+ });
252
+ });
253
+ };
254
+ KeyValueStorage.prototype.clearCollection = function (collectionName) {
255
+ return __awaiter(this, void 0, void 0, function () {
256
+ var items, recordGroups, _i, recordGroups_2, records;
257
+ return __generator(this, function (_a) {
258
+ switch (_a.label) {
259
+ case 0: return [4, this.listKeys(collectionName)];
260
+ case 1:
261
+ items = (_a.sent()).items;
262
+ recordGroups = (0, utils_1.splitIntoParts)(items);
263
+ _i = 0, recordGroups_2 = recordGroups;
264
+ _a.label = 2;
265
+ case 2:
266
+ if (!(_i < recordGroups_2.length)) return [3, 5];
267
+ records = recordGroups_2[_i];
268
+ return [4, this.processMultipleRecords(records, constants_1.Action.DELETE, collectionName)];
269
+ case 3:
270
+ _a.sent();
271
+ _a.label = 4;
272
+ case 4:
273
+ _i++;
274
+ return [3, 2];
275
+ case 5: return [2];
276
+ }
277
+ });
278
+ });
279
+ };
110
280
  return KeyValueStorage;
111
281
  }());
112
282
  exports.KeyValueStorage = KeyValueStorage;
@@ -1 +1 @@
1
- {"version":3,"file":"KeyValueStorage.js","sourceRoot":"","sources":["../../src/KeyValueStorage.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAAyC;AAOzC,yCAA4C;AAS5C;IASE,yBAAY,MAA6B;QAC/B,IAAA,KAAK,GAAsC,MAAM,MAA5C,EAAE,YAAY,GAAwB,MAAM,aAA9B,EAAE,SAAS,GAAa,MAAM,UAAnB,EAAE,MAAM,GAAK,MAAM,OAAX,CAAY;QAE1D,IAAI,CAAC,MAAM,GAAG,IAAI,gBAAM,CAAC;YACvB,KAAK,OAAA;YACL,YAAY,cAAA;YACZ,SAAS,WAAA;YACT,MAAM,QAAA;SACP,CAAC,CAAC;IACL,CAAC;IAED,sCAAY,GAAZ,UAAa,KAAa;QACxB,OAAO,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,mBAAY,IAAI,CAAC,MAAM,CAAC,gBAAgB,cAAI,KAAK,CAAE,CAAC,CAAC,CAAC,KAAK,CAAC;IAClG,CAAC;IAYY,kCAAQ,GAArB,UACE,SAAiB,EACjB,MAAe,EACf,UAAkB;QAAlB,2BAAA,EAAA,kBAAkB;;;;;4BAId,WAAM,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC;4BAChC,MAAM,EAAE,KAAK;4BACb,KAAK,EAAE,IAAI,CAAC,YAAY,CAAC,UAAG,yBAAa,cAAI,SAAS,CAAE,CAAC;4BACzD,MAAM,EAAE;gCACN,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,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KAGnC;IACH,sBAAC;AAAD,CAAC,AA5RD,IA4RC;AA5RY,0CAAe"}
@@ -1,5 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.STORAGE_ROUTE = void 0;
3
+ exports.Action = exports.STORAGE_MULTIPLE_ROUTE = exports.STORAGE_ROUTE = void 0;
4
4
  exports.STORAGE_ROUTE = '/storage';
5
+ exports.STORAGE_MULTIPLE_ROUTE = '/storage-multiple';
6
+ var Action;
7
+ (function (Action) {
8
+ Action["GET"] = "get";
9
+ Action["SET"] = "set";
10
+ Action["DELETE"] = "delete";
11
+ Action["EXISTS"] = "exists";
12
+ })(Action = exports.Action || (exports.Action = {}));
5
13
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,aAAa,GAAG,UAAU,CAAC"}
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,aAAa,GAAG,UAAU,CAAC;AAC3B,QAAA,sBAAsB,GAAG,mBAAmB,CAAC;AAE1D,IAAY,MAKX;AALD,WAAY,MAAM;IAChB,qBAAW,CAAA;IACX,qBAAW,CAAA;IACX,2BAAiB,CAAA;IACjB,2BAAiB,CAAA;AACnB,CAAC,EALW,MAAM,GAAN,cAAM,KAAN,cAAM,QAKjB"}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.splitIntoParts = void 0;
7
+ var splitIntoParts_1 = require("./splitIntoParts");
8
+ Object.defineProperty(exports, "splitIntoParts", { enumerable: true, get: function () { return __importDefault(splitIntoParts_1).default; } });
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":";;;;;;AAGA,mDAA6D;AAApD,iIAAA,OAAO,OAAkB"}
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function splitIntoParts(records) {
4
+ var partLength = 10;
5
+ return records
6
+ .map(function (record) {
7
+ return {
8
+ key: record.key,
9
+ value: record.value,
10
+ };
11
+ })
12
+ .reduce(function (resultArray, item, index) {
13
+ var partIndex = Math.floor(index / partLength);
14
+ if (!resultArray[partIndex]) {
15
+ resultArray[partIndex] = [];
16
+ }
17
+ resultArray[partIndex].push(item);
18
+ return resultArray;
19
+ }, []);
20
+ }
21
+ exports.default = splitIntoParts;
22
+ //# sourceMappingURL=splitIntoParts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"splitIntoParts.js","sourceRoot":"","sources":["../../../src/utils/splitIntoParts.ts"],"names":[],"mappings":";;AAEA,SAAS,cAAc,CAAC,OAAgD;IACtE,IAAM,UAAU,GAAG,EAAE,CAAC;IAEtB,OAAO,OAAO;SACX,GAAG,CAAC,UAAA,MAAM;QACT,OAAO;YACL,GAAG,EAAE,MAAM,CAAC,GAAG;YACf,KAAK,EAAE,MAAM,CAAC,KAAK;SACpB,CAAC;IACJ,CAAC,CAAC;SACD,MAAM,CAAC,UAAC,WAAW,EAAE,IAAI,EAAE,KAAK;QAC/B,IAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,UAAU,CAAC,CAAC;QAEjD,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,EAAE;YAC3B,WAAW,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC;SAC7B;QAED,WAAW,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAElC,OAAO,WAAW,CAAC;IACrB,CAAC,EAAE,EAA+B,CAAC,CAAC;AACxC,CAAC;AAED,kBAAe,cAAc,CAAC"}
@@ -7,8 +7,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ import { makeList } from '@or-sdk/base';
10
11
  import { SdkApi } from '@or-sdk/sdk-api';
11
- import { STORAGE_ROUTE } from './constants';
12
+ import { Action, STORAGE_MULTIPLE_ROUTE, STORAGE_ROUTE } from './constants';
13
+ import { splitIntoParts } from './utils';
12
14
  export class KeyValueStorage {
13
15
  constructor(params) {
14
16
  const { token, discoveryUrl, accountId, sdkUrl } = params;
@@ -22,32 +24,82 @@ export class KeyValueStorage {
22
24
  composeRoute(route) {
23
25
  return this.sdkApi.isCrossAccount ? `accounts/${this.sdkApi.currentAccountId}/${route}` : route;
24
26
  }
25
- listKeys(tableName, prefix, withValues = false) {
27
+ listCollectionsPaginated({ prefix, maxKeys, next, }) {
26
28
  return __awaiter(this, void 0, void 0, function* () {
27
- const { value: { records }, } = yield this.sdkApi.makeRequest({
29
+ const params = {
30
+ prefix: prefix ? prefix : undefined,
31
+ next: next ? next : undefined,
32
+ maxKeys: maxKeys ? maxKeys : undefined,
33
+ };
34
+ const { tables, isTruncated, next: newNext } = yield this.sdkApi.makeRequest({
28
35
  method: 'GET',
29
- route: this.composeRoute(`${STORAGE_ROUTE}/${tableName}`),
30
- params: {
31
- prefix: prefix ? prefix : undefined,
32
- withValues,
33
- },
36
+ route: this.composeRoute(`${STORAGE_ROUTE}`),
37
+ params,
38
+ });
39
+ return Object.assign(Object.assign({}, makeList(tables)), { isTruncated, next: newNext });
40
+ });
41
+ }
42
+ listCollections(prefix) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ let itemsTotal = [];
45
+ let prevNext;
46
+ do {
47
+ const { items, next } = yield this.listCollectionsPaginated({
48
+ prefix,
49
+ next: prevNext,
50
+ });
51
+ prevNext = next;
52
+ itemsTotal = [...itemsTotal, ...items];
53
+ } while (prevNext);
54
+ return makeList(itemsTotal);
55
+ });
56
+ }
57
+ listKeysPaginated({ collectionName, prefix, withValues = false, maxKeys, next, }) {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ const params = {
60
+ prefix: prefix ? prefix : undefined,
61
+ next: next ? next : undefined,
62
+ maxKeys: maxKeys ? maxKeys : undefined,
63
+ withValues,
64
+ };
65
+ const { value: { records, isTruncated, next: newNext } } = yield this.sdkApi.makeRequest({
66
+ method: 'GET',
67
+ route: this.composeRoute(`${STORAGE_ROUTE}/${collectionName}`),
68
+ params,
34
69
  });
35
- return records;
70
+ return Object.assign(Object.assign({}, makeList(records)), { next: newNext, isTruncated });
71
+ });
72
+ }
73
+ listKeys(collectionName, prefix, withValues = false) {
74
+ return __awaiter(this, void 0, void 0, function* () {
75
+ let itemsTotal = [];
76
+ let prevNext;
77
+ do {
78
+ const { items, next } = yield this.listKeysPaginated({
79
+ collectionName,
80
+ prefix,
81
+ withValues,
82
+ next: prevNext,
83
+ });
84
+ prevNext = next;
85
+ itemsTotal = [...itemsTotal, ...items];
86
+ } while (prevNext);
87
+ return makeList(itemsTotal);
36
88
  });
37
89
  }
38
- getValueByKey(tableName, key) {
90
+ getValueByKey(collectionName, key) {
39
91
  return __awaiter(this, void 0, void 0, function* () {
40
92
  return this.sdkApi.makeRequest({
41
93
  method: 'GET',
42
- route: this.composeRoute(`${STORAGE_ROUTE}/${tableName}/${key}`),
94
+ route: this.composeRoute(`${STORAGE_ROUTE}/${collectionName}/${key}`),
43
95
  });
44
96
  });
45
97
  }
46
- setValueByKey(tableName, key, value, expire) {
98
+ setValueByKey(collectionName, key, value, expire) {
47
99
  return __awaiter(this, void 0, void 0, function* () {
48
100
  return this.sdkApi.makeRequest({
49
101
  method: 'POST',
50
- route: this.composeRoute(`${STORAGE_ROUTE}/${tableName}/${key}`),
102
+ route: this.composeRoute(`${STORAGE_ROUTE}/${collectionName}/${key}`),
51
103
  data: {
52
104
  expire,
53
105
  value,
@@ -55,13 +107,44 @@ export class KeyValueStorage {
55
107
  });
56
108
  });
57
109
  }
58
- deleteKey(tableName, key) {
110
+ deleteKey(collectionName, key) {
59
111
  return __awaiter(this, void 0, void 0, function* () {
60
112
  return this.sdkApi.makeRequest({
61
113
  method: 'DELETE',
62
- route: this.composeRoute(`${STORAGE_ROUTE}/${tableName}/${key}`),
114
+ route: this.composeRoute(`${STORAGE_ROUTE}/${collectionName}/${key}`),
63
115
  });
64
116
  });
65
117
  }
118
+ processMultipleRecords(records, action, collectionName) {
119
+ return __awaiter(this, void 0, void 0, function* () {
120
+ return this.sdkApi.makeRequest({
121
+ method: 'POST',
122
+ route: this.composeRoute(`${STORAGE_MULTIPLE_ROUTE}`),
123
+ data: {
124
+ keys: records,
125
+ method: action,
126
+ table: collectionName,
127
+ },
128
+ });
129
+ });
130
+ }
131
+ copyCollection(sourceCollectionName, targetCollectionName) {
132
+ return __awaiter(this, void 0, void 0, function* () {
133
+ const { items } = yield this.listKeys(sourceCollectionName, '', true);
134
+ const recordGroups = splitIntoParts(items);
135
+ for (const records of recordGroups) {
136
+ yield this.processMultipleRecords(records, Action.SET, targetCollectionName);
137
+ }
138
+ });
139
+ }
140
+ clearCollection(collectionName) {
141
+ return __awaiter(this, void 0, void 0, function* () {
142
+ const { items } = yield this.listKeys(collectionName);
143
+ const recordGroups = splitIntoParts(items);
144
+ for (const records of recordGroups) {
145
+ yield this.processMultipleRecords(records, Action.DELETE, collectionName);
146
+ }
147
+ });
148
+ }
66
149
  }
67
150
  //# sourceMappingURL=KeyValueStorage.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"KeyValueStorage.js","sourceRoot":"","sources":["../../src/KeyValueStorage.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAOzC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAS5C,MAAM,OAAO,eAAe;IAS1B,YAAY,MAA6B;QACvC,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,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GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;YAEtD,MAAM,YAAY,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;YAE3C,KAAK,MAAM,OAAO,IAAI,YAAY,EAAE;gBAClC,MAAM,IAAI,CAAC,sBAAsB,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;aAC3E;QACH,CAAC;KAAA;CACF"}
@@ -1,2 +1,10 @@
1
1
  export const STORAGE_ROUTE = '/storage';
2
+ export const STORAGE_MULTIPLE_ROUTE = '/storage-multiple';
3
+ export var Action;
4
+ (function (Action) {
5
+ Action["GET"] = "get";
6
+ Action["SET"] = "set";
7
+ Action["DELETE"] = "delete";
8
+ Action["EXISTS"] = "exists";
9
+ })(Action || (Action = {}));
2
10
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,aAAa,GAAG,UAAU,CAAC"}
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,aAAa,GAAG,UAAU,CAAC;AACxC,MAAM,CAAC,MAAM,sBAAsB,GAAG,mBAAmB,CAAC;AAE1D,MAAM,CAAN,IAAY,MAKX;AALD,WAAY,MAAM;IAChB,qBAAW,CAAA;IACX,qBAAW,CAAA;IACX,2BAAiB,CAAA;IACjB,2BAAiB,CAAA;AACnB,CAAC,EALW,MAAM,KAAN,MAAM,QAKjB"}
@@ -0,0 +1,2 @@
1
+ export { default as splitIntoParts } from './splitIntoParts';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,kBAAkB,CAAC"}
@@ -0,0 +1,20 @@
1
+ function splitIntoParts(records) {
2
+ const partLength = 10;
3
+ return records
4
+ .map(record => {
5
+ return {
6
+ key: record.key,
7
+ value: record.value,
8
+ };
9
+ })
10
+ .reduce((resultArray, item, index) => {
11
+ const partIndex = Math.floor(index / partLength);
12
+ if (!resultArray[partIndex]) {
13
+ resultArray[partIndex] = [];
14
+ }
15
+ resultArray[partIndex].push(item);
16
+ return resultArray;
17
+ }, []);
18
+ }
19
+ export default splitIntoParts;
20
+ //# sourceMappingURL=splitIntoParts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"splitIntoParts.js","sourceRoot":"","sources":["../../../src/utils/splitIntoParts.ts"],"names":[],"mappings":"AAEA,SAAS,cAAc,CAAC,OAAgD;IACtE,MAAM,UAAU,GAAG,EAAE,CAAC;IAEtB,OAAO,OAAO;SACX,GAAG,CAAC,MAAM,CAAC,EAAE;QACZ,OAAO;YACL,GAAG,EAAE,MAAM,CAAC,GAAG;YACf,KAAK,EAAE,MAAM,CAAC,KAAK;SACpB,CAAC;IACJ,CAAC,CAAC;SACD,MAAM,CAAC,CAAC,WAAW,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE;QACnC,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,UAAU,CAAC,CAAC;QAEjD,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,EAAE;YAC3B,WAAW,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC;SAC7B;QAED,WAAW,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAElC,OAAO,WAAW,CAAC;IACrB,CAAC,EAAE,EAA+B,CAAC,CAAC;AACxC,CAAC;AAED,eAAe,cAAc,CAAC"}
@@ -1,10 +1,28 @@
1
- import { KeyValueStorageConfig, KeyValueStorageRecord, KeyValueStorageRecordWithLastModified, DeletedKeyValueStorageRecord } from './types';
1
+ import { List } from '@or-sdk/base';
2
+ import { KeyValueStorageConfig, KeyValueStorageRecord, KeyValueStorageRecordWithLastModified, DeletedKeyValueStorageRecord, PaginatedList, MultipleRecordsResult } from './types';
3
+ import { Action } from './constants';
2
4
  export declare class KeyValueStorage {
3
5
  private readonly sdkApi;
4
6
  constructor(params: KeyValueStorageConfig);
5
7
  composeRoute(route: string): string;
6
- listKeys(tableName: string, prefix?: string, withValues?: boolean): Promise<KeyValueStorageRecordWithLastModified[]>;
7
- getValueByKey(tableName: string, key: string): Promise<KeyValueStorageRecord>;
8
- setValueByKey(tableName: string, key: string, value: Record<string, unknown> | string, expire?: number): Promise<KeyValueStorageRecord>;
9
- deleteKey(tableName: string, key: string): Promise<DeletedKeyValueStorageRecord>;
8
+ listCollectionsPaginated({ prefix, maxKeys, next, }: {
9
+ prefix?: string;
10
+ maxKeys?: number;
11
+ next?: string;
12
+ }): Promise<PaginatedList<string>>;
13
+ listCollections(prefix?: string): Promise<List<string>>;
14
+ listKeysPaginated({ collectionName, prefix, withValues, maxKeys, next, }: {
15
+ collectionName: string;
16
+ prefix?: string;
17
+ withValues: boolean;
18
+ maxKeys?: string;
19
+ next?: string;
20
+ }): Promise<PaginatedList<KeyValueStorageRecordWithLastModified>>;
21
+ listKeys(collectionName: string, prefix?: string, withValues?: boolean): Promise<List<KeyValueStorageRecordWithLastModified>>;
22
+ getValueByKey(collectionName: string, key: string): Promise<KeyValueStorageRecord>;
23
+ setValueByKey(collectionName: string, key: string, value: Record<string, unknown> | string, expire?: number): Promise<KeyValueStorageRecord>;
24
+ deleteKey(collectionName: string, key: string): Promise<DeletedKeyValueStorageRecord>;
25
+ processMultipleRecords(records: KeyValueStorageRecord[], action: Action, collectionName: string): Promise<MultipleRecordsResult>;
26
+ copyCollection(sourceCollectionName: string, targetCollectionName: string): Promise<void>;
27
+ clearCollection(collectionName: string): Promise<void>;
10
28
  }
@@ -1 +1,8 @@
1
1
  export declare const STORAGE_ROUTE = "/storage";
2
+ export declare const STORAGE_MULTIPLE_ROUTE = "/storage-multiple";
3
+ export declare enum Action {
4
+ GET = "get",
5
+ SET = "set",
6
+ DELETE = "delete",
7
+ EXISTS = "exists"
8
+ }
@@ -1,4 +1,4 @@
1
- import { Token } from '@or-sdk/base';
1
+ import { List, Token } from '@or-sdk/base';
2
2
  export declare type KeyValueStorageConfig = {
3
3
  token: Token;
4
4
  discoveryUrl?: string;
@@ -12,4 +12,26 @@ export declare type KeyValueStorageRecord = {
12
12
  export declare type KeyValueStorageRecordWithLastModified = KeyValueStorageRecord & {
13
13
  lastModified: string;
14
14
  };
15
+ export declare type ListCollectionsResponse = {
16
+ tables: string[];
17
+ isTruncated: boolean;
18
+ count: number;
19
+ next?: string;
20
+ };
21
+ export declare type ListKeysResponse = {
22
+ value: {
23
+ records: KeyValueStorageRecordWithLastModified[];
24
+ isTruncated: boolean;
25
+ count: number;
26
+ next?: string;
27
+ };
28
+ };
15
29
  export declare type DeletedKeyValueStorageRecord = Required<Pick<KeyValueStorageRecord, 'value'>>;
30
+ export declare type PaginatedList<T> = List<T> & {
31
+ isTruncated: boolean;
32
+ next?: string;
33
+ };
34
+ export declare type KeyValueStorageRecordExists = KeyValueStorageRecord & {
35
+ exists: boolean;
36
+ };
37
+ export declare type MultipleRecordsResult = KeyValueStorageRecord[] | KeyValueStorageRecordExists[] | boolean;
@@ -0,0 +1 @@
1
+ export { default as splitIntoParts } from './splitIntoParts';
@@ -0,0 +1,3 @@
1
+ import { KeyValueStorageRecord, KeyValueStorageRecordWithLastModified } from '../types';
2
+ declare function splitIntoParts(records: KeyValueStorageRecordWithLastModified[]): KeyValueStorageRecord[][];
3
+ export default splitIntoParts;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.23.4",
2
+ "version": "0.23.5-beta.433.0",
3
3
  "name": "@or-sdk/key-value-storage",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
@@ -26,6 +26,5 @@
26
26
  "dependencies": {
27
27
  "@or-sdk/base": "^0.24.2",
28
28
  "@or-sdk/sdk-api": "^0.23.4"
29
- },
30
- "gitHead": "7467e3ef0fcf3df42472266f512506432b5f8395"
29
+ }
31
30
  }
@@ -1,11 +1,13 @@
1
+ import { List, makeList } from '@or-sdk/base';
1
2
  import { SdkApi } from '@or-sdk/sdk-api';
2
3
  import {
3
4
  KeyValueStorageConfig,
4
5
  KeyValueStorageRecord,
5
6
  KeyValueStorageRecordWithLastModified,
6
- DeletedKeyValueStorageRecord,
7
+ DeletedKeyValueStorageRecord, ListKeysResponse, ListCollectionsResponse, PaginatedList, MultipleRecordsResult,
7
8
  } from './types';
8
- import { STORAGE_ROUTE } from './constants';
9
+ import { Action, STORAGE_MULTIPLE_ROUTE, STORAGE_ROUTE } from './constants';
10
+ import { splitIntoParts } from './utils';
9
11
 
10
12
  /**
11
13
  * OneReach KeyValueStorage service client
@@ -39,73 +41,181 @@ export class KeyValueStorage {
39
41
  }
40
42
 
41
43
  /**
42
- * List keys in a table starting with given substring
44
+ * List collections with pagination
43
45
  *
44
46
  * ```typescript
45
- * const tableName = 'my-table';
47
+ * const prefix = '__some_prefix';
48
+ * const maxKeys = 30;
49
+ * const next = 'next-token'; // next page token from previous listCollectionsPaginated call
50
+ * const list = await keyValueStorage.listCollectionsPaginated({prefix, withValues, next});
51
+ * ```
52
+ */
53
+ public async listCollectionsPaginated({
54
+ prefix,
55
+ maxKeys,
56
+ next,
57
+ }: { prefix?: string; maxKeys?: number; next?: string; }): Promise<PaginatedList<string>> {
58
+ const params = {
59
+ prefix: prefix ? prefix : undefined,
60
+ next: next ? next : undefined,
61
+ maxKeys: maxKeys ? maxKeys : undefined,
62
+ };
63
+
64
+ const { tables, isTruncated, next: newNext } = await this.sdkApi.makeRequest<ListCollectionsResponse>({
65
+ method: 'GET',
66
+ route: this.composeRoute(`${STORAGE_ROUTE}`),
67
+ params,
68
+ });
69
+
70
+ return {
71
+ ...makeList<string>(tables),
72
+ isTruncated,
73
+ next: newNext,
74
+ };
75
+ }
76
+
77
+ /**
78
+ * List collections
79
+ *
80
+ * ```typescript
81
+ * const prefix = '__some_prefix';
82
+ * const list = await keyValueStorage.listCollections(prefix, withValues);
83
+ * ```
84
+ */
85
+ public async listCollections(prefix?: string): Promise<List<string>> {
86
+ let itemsTotal: string[] = [];
87
+ let prevNext;
88
+
89
+ do {
90
+ const { items, next }: PaginatedList<string> = await this.listCollectionsPaginated({
91
+ prefix,
92
+ next: prevNext,
93
+ });
94
+
95
+ prevNext = next;
96
+ itemsTotal = [...itemsTotal, ...items];
97
+ } while (prevNext);
98
+
99
+ return makeList<string>(itemsTotal);
100
+ }
101
+
102
+ /**
103
+ * List keys in a collection starting with given substring with pagination
104
+ *
105
+ * ```typescript
106
+ * const collectionName = 'my-collection';
107
+ * const prefix = '__some_prefix';
108
+ * const withValues = false; // set to true to receive record values alongside key names
109
+ * const maxKeys = 30;
110
+ * const next = 'next-token'; // next page token from previous listKeysPaginated call
111
+ * const list = await keyValueStorage.listKeysPaginated({collectionName, prefix, withValues, maxKeys, next});
112
+ * ```
113
+ */
114
+ public async listKeysPaginated({
115
+ collectionName,
116
+ prefix,
117
+ withValues = false,
118
+ maxKeys,
119
+ next,
120
+ }: {
121
+ collectionName: string;
122
+ prefix?: string;
123
+ withValues: boolean;
124
+ maxKeys?: string;
125
+ next?: string;
126
+ }
127
+ ): Promise<PaginatedList<KeyValueStorageRecordWithLastModified>> {
128
+ const params = {
129
+ prefix: prefix ? prefix : undefined,
130
+ next: next ? next : undefined,
131
+ maxKeys: maxKeys ? maxKeys : undefined,
132
+ withValues,
133
+ };
134
+ const { value: { records, isTruncated, next: newNext } } = await this.sdkApi.makeRequest<ListKeysResponse>({
135
+ method: 'GET',
136
+ route: this.composeRoute(`${STORAGE_ROUTE}/${collectionName}`),
137
+ params,
138
+ });
139
+
140
+ return {
141
+ ...makeList<KeyValueStorageRecordWithLastModified>(records),
142
+ next: newNext,
143
+ isTruncated,
144
+ };
145
+ }
146
+
147
+ /**
148
+ * List keys in a collection starting with given substring
149
+ *
150
+ * ```typescript
151
+ * const collectionName = 'my-collection';
46
152
  * const prefix = '__some_prefix';
47
153
  * const withValues = false; // set to true to receive record values alongside key names
48
- * const list = await keyValueStorage.listKeys(tableName, prefix, withValues);
154
+ * const list = await keyValueStorage.listKeys(collectionName, prefix, withValues);
49
155
  * ```
50
156
  */
51
157
  public async listKeys(
52
- tableName: string,
158
+ collectionName: string,
53
159
  prefix?: string,
54
- withValues = false
55
- ): Promise<KeyValueStorageRecordWithLastModified[]> {
56
- const {
57
- value: { records },
58
- } = await this.sdkApi.makeRequest({
59
- method: 'GET',
60
- route: this.composeRoute(`${STORAGE_ROUTE}/${tableName}`),
61
- params: {
62
- prefix: prefix ? prefix : undefined,
160
+ withValues = false,
161
+ ): Promise<List<KeyValueStorageRecordWithLastModified>> {
162
+ let itemsTotal: KeyValueStorageRecordWithLastModified[] = [];
163
+ let prevNext;
164
+
165
+ do {
166
+ const { items, next }: PaginatedList<KeyValueStorageRecordWithLastModified> = await this.listKeysPaginated({
167
+ collectionName,
168
+ prefix,
63
169
  withValues,
64
- },
65
- });
170
+ next: prevNext,
171
+ });
66
172
 
67
- return records;
173
+ prevNext = next!;
174
+ itemsTotal = [...itemsTotal, ...items];
175
+ } while (prevNext);
176
+
177
+ return makeList<KeyValueStorageRecordWithLastModified>(itemsTotal);
68
178
  }
69
179
 
70
180
  /**
71
- * Get value from table by key
181
+ * Get value from collection by key
72
182
  *
73
183
  * ```typescript
74
- * const tableName = 'my-table';
184
+ * const collectionName = 'my-collection';
75
185
  * const key = 'my-key';
76
- * const record = await keyValueStorage.getValueByKey(tableName, key);
186
+ * const record = await keyValueStorage.getValueByKey(collectionName, key);
77
187
  * ```
78
188
  */
79
189
  public async getValueByKey(
80
- tableName: string,
190
+ collectionName: string,
81
191
  key: string
82
192
  ): Promise<KeyValueStorageRecord> {
83
193
  return this.sdkApi.makeRequest({
84
194
  method: 'GET',
85
- route: this.composeRoute(`${STORAGE_ROUTE}/${tableName}/${key}`),
195
+ route: this.composeRoute(`${STORAGE_ROUTE}/${collectionName}/${key}`),
86
196
  });
87
197
  }
88
198
 
89
199
  /**
90
- * Set value to table by key
200
+ * Set value to collection by key
91
201
  *
92
202
  * ```typescript
93
- * const tableName = 'my-table';
203
+ * const collectionName = 'my-collection';
94
204
  * const key = 'my-key';
95
205
  * const value = { 'prop': 'value' };
96
206
  * const expire = 300000; // time before record deletion in milliseconds
97
- * await keyValueStorage.setValueByKey(tableName, key, value, expire);
207
+ * await keyValueStorage.setValueByKey(collectionName, key, value, expire);
98
208
  * ```
99
209
  */
100
210
  public async setValueByKey(
101
- tableName: string,
211
+ collectionName: string,
102
212
  key: string,
103
213
  value: Record<string, unknown> | string,
104
214
  expire?: number
105
215
  ): Promise<KeyValueStorageRecord> {
106
216
  return this.sdkApi.makeRequest({
107
217
  method: 'POST',
108
- route: this.composeRoute(`${STORAGE_ROUTE}/${tableName}/${key}`),
218
+ route: this.composeRoute(`${STORAGE_ROUTE}/${collectionName}/${key}`),
109
219
  data: {
110
220
  expire,
111
221
  value,
@@ -114,21 +224,80 @@ export class KeyValueStorage {
114
224
  }
115
225
 
116
226
  /**
117
- * Delete a key from a table
227
+ * Delete a key from a collection
118
228
  *
119
229
  * ```typescript
120
- * const tableName = 'my-table';
230
+ * const collectionName = 'my-collection';
121
231
  * const key = 'my-key';
122
- * const record = await keyValueStorage.deleteKey(tableName, key);
232
+ * const record = await keyValueStorage.deleteKey(collectionName, key);
123
233
  * ```
124
234
  */
125
235
  public async deleteKey(
126
- tableName: string,
236
+ collectionName: string,
127
237
  key: string
128
238
  ): Promise<DeletedKeyValueStorageRecord> {
129
239
  return this.sdkApi.makeRequest({
130
240
  method: 'DELETE',
131
- route: this.composeRoute(`${STORAGE_ROUTE}/${tableName}/${key}`),
241
+ route: this.composeRoute(`${STORAGE_ROUTE}/${collectionName}/${key}`),
242
+ });
243
+ }
244
+
245
+ /**
246
+ * Process multiple records
247
+ *
248
+ * ```typescript
249
+ * const collectionName = 'my-collection';
250
+ * const records = [{ key: 'new-key', value: 'new-value' }]; // array of records to process, max 10
251
+ * const action = 'set'; // [ get, set, delete, exists ]
252
+ * const result = await keyValueStorage.processMultipleRecords(records, action, collectionName);
253
+ * ```
254
+ */
255
+ public async processMultipleRecords(records: KeyValueStorageRecord[], action: Action, collectionName: string): Promise<MultipleRecordsResult> {
256
+ return this.sdkApi.makeRequest({
257
+ method: 'POST',
258
+ route: this.composeRoute(`${STORAGE_MULTIPLE_ROUTE}`),
259
+ data: {
260
+ keys: records,
261
+ method: action,
262
+ table: collectionName,
263
+ },
132
264
  });
133
265
  }
266
+
267
+ /**
268
+ * Copy records from one collection to another
269
+ *
270
+ * Existing records in the target collection will be preserved unless they have matching keys with records from the source collection
271
+ * ```typescript
272
+ * const sourceCollectionName = 'old-collection';
273
+ * const targetCollectionName = 'new-collection';
274
+ * const await keyValueStorage.copyCollection(sourceCollectionName, targetCollectionName);
275
+ * ```
276
+ */
277
+ public async copyCollection(sourceCollectionName: string, targetCollectionName: string): Promise<void> {
278
+ const { items } = await this.listKeys(sourceCollectionName, '', true);
279
+
280
+ const recordGroups = splitIntoParts(items);
281
+
282
+ for (const records of recordGroups) {
283
+ await this.processMultipleRecords(records, Action.SET, targetCollectionName);
284
+ }
285
+ }
286
+
287
+ /**
288
+ * Delete all records from collection
289
+ * ```typescript
290
+ * const collectionName = 'collection';
291
+ * const await keyValueStorage.clearCollection(collectionName);
292
+ * ```
293
+ */
294
+ public async clearCollection(collectionName: string): Promise<void> {
295
+ const { items } = await this.listKeys(collectionName);
296
+
297
+ const recordGroups = splitIntoParts(items);
298
+
299
+ for (const records of recordGroups) {
300
+ await this.processMultipleRecords(records, Action.DELETE, collectionName);
301
+ }
302
+ }
134
303
  }
package/src/constants.ts CHANGED
@@ -1 +1,9 @@
1
1
  export const STORAGE_ROUTE = '/storage';
2
+ export const STORAGE_MULTIPLE_ROUTE = '/storage-multiple';
3
+
4
+ export enum Action {
5
+ GET = 'get',
6
+ SET = 'set',
7
+ DELETE = 'delete',
8
+ EXISTS = 'exists',
9
+ }
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Token } from '@or-sdk/base';
1
+ import { List, Token } from '@or-sdk/base';
2
2
 
3
3
  export type KeyValueStorageConfig = {
4
4
  /**
@@ -22,7 +22,6 @@ export type KeyValueStorageConfig = {
22
22
  sdkUrl?: string;
23
23
  };
24
24
 
25
-
26
25
  export type KeyValueStorageRecord = {
27
26
  /**
28
27
  * Record key
@@ -36,4 +35,31 @@ export type KeyValueStorageRecordWithLastModified = KeyValueStorageRecord & {
36
35
  lastModified: string;
37
36
  };
38
37
 
38
+ export type ListCollectionsResponse = {
39
+ tables: string[];
40
+ isTruncated: boolean;
41
+ count: number;
42
+ next?: string;
43
+ };
44
+
45
+ export type ListKeysResponse = {
46
+ value: {
47
+ records: KeyValueStorageRecordWithLastModified[];
48
+ isTruncated: boolean;
49
+ count: number;
50
+ next?: string;
51
+ };
52
+ };
53
+
39
54
  export type DeletedKeyValueStorageRecord = Required<Pick<KeyValueStorageRecord, 'value'>>;
55
+
56
+ export type PaginatedList<T> = List<T> & {
57
+ isTruncated: boolean;
58
+ next?: string;
59
+ };
60
+
61
+ export type KeyValueStorageRecordExists = KeyValueStorageRecord & {
62
+ exists: boolean;
63
+ };
64
+
65
+ export type MultipleRecordsResult = KeyValueStorageRecord[] | KeyValueStorageRecordExists[] | boolean;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @internal
3
+ */
4
+ export { default as splitIntoParts } from './splitIntoParts';
@@ -0,0 +1,26 @@
1
+ import { KeyValueStorageRecord, KeyValueStorageRecordWithLastModified } from '../types';
2
+
3
+ function splitIntoParts(records: KeyValueStorageRecordWithLastModified[]): KeyValueStorageRecord[][] {
4
+ const partLength = 10;
5
+
6
+ return records
7
+ .map(record => {
8
+ return {
9
+ key: record.key,
10
+ value: record.value,
11
+ };
12
+ })
13
+ .reduce((resultArray, item, index) => {
14
+ const partIndex = Math.floor(index / partLength);
15
+
16
+ if (!resultArray[partIndex]) {
17
+ resultArray[partIndex] = [];
18
+ }
19
+
20
+ resultArray[partIndex].push(item);
21
+
22
+ return resultArray;
23
+ }, [] as KeyValueStorageRecord[][]);
24
+ }
25
+
26
+ export default splitIntoParts;