electrodb 2.10.2 → 2.10.3

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/entity.js +21 -43
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "electrodb",
3
- "version": "2.10.2",
3
+ "version": "2.10.3",
4
4
  "description": "A library to more easily create and interact with multiple entities and heretical relationships in dynamodb",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/entity.js CHANGED
@@ -184,36 +184,6 @@ class Entity {
184
184
  return this.model.version;
185
185
  }
186
186
 
187
- // ownsItem(item) {
188
- // return (
189
- // item &&
190
- // this.getName() === item[this.identifiers.entity] &&
191
- // this.getVersion() === item[this.identifiers.version] &&
192
- // validations.isStringHasLength(item[this.identifiers.entity]) &&
193
- // validations.isStringHasLength(item[this.identifiers.version])
194
- // ) || !!this.ownsKeys(item)
195
- // }
196
-
197
- // ownsKeys({keys = {}}) {
198
- // let {pk, sk} = this.model.prefixes[TableIndex];
199
- // let hasSK = this.model.lookup.indexHasSortKeys[TableIndex];
200
- // let pkMatch = typeof keys[pk.field] === "string" && keys[pk.field].startsWith(pk.prefix);
201
- // let skMatch = pkMatch && !hasSK;
202
- // if (pkMatch && hasSK) {
203
- // skMatch = typeof keys[sk.field] === "string" && keys[sk.field].startsWith(sk.prefix);
204
- // }
205
- //
206
- // return (pkMatch && skMatch &&
207
- // this._formatKeysToItem(TableIndex, key) !== null);
208
- // }
209
-
210
- // ownsCursor({ cursor }) {
211
- // if (typeof cursor === 'string') {
212
- // cursor = u.cursorFormatter.deserialize(cursor);
213
- // }
214
- // return this.ownsKeys({ keys: cursor });
215
- // }
216
-
217
187
  ownsItem(item) {
218
188
  return (
219
189
  item &&
@@ -246,13 +216,18 @@ class Entity {
246
216
  ownsKeys(key = {}) {
247
217
  let { pk, sk } = this.model.prefixes[TableIndex];
248
218
  let hasSK = this.model.lookup.indexHasSortKeys[TableIndex];
249
- let pkMatch =
250
- typeof key[pk.field] === "string" && key[pk.field].startsWith(pk.prefix);
219
+ const typeofPkProvided = typeof key[pk.field];
220
+ const pkPrefixMatch =
221
+ typeofPkProvided === "string" && key[pk.field].startsWith(pk.prefix);
222
+ const isNumericPk = typeofPkProvided === "number" && pk.cast === "number";
223
+ let pkMatch = pkPrefixMatch || isNumericPk;
251
224
  let skMatch = pkMatch && !hasSK;
252
225
  if (pkMatch && hasSK) {
253
- skMatch =
254
- typeof key[sk.field] === "string" &&
255
- key[sk.field].startsWith(sk.prefix);
226
+ const typeofSkProvided = typeof key[sk.field];
227
+ const skPrefixMatch =
228
+ typeofSkProvided === "string" && key[sk.field].startsWith(sk.prefix);
229
+ const isNumericSk = typeofSkProvided === "number" && sk.cast === "number";
230
+ skMatch = skPrefixMatch || isNumericSk;
256
231
  }
257
232
 
258
233
  return (
@@ -1481,7 +1456,7 @@ class Entity {
1481
1456
  }
1482
1457
 
1483
1458
  _createKeyDeconstructor(prefixes = {}, labels = [], attributes = {}) {
1484
- let { prefix, isCustom, postfix } = prefixes;
1459
+ let { prefix, isCustom, postfix, cast } = prefixes;
1485
1460
  let names = [];
1486
1461
  let types = [];
1487
1462
  let pattern = `^${this._regexpEscape(prefix || "")}`;
@@ -1512,16 +1487,19 @@ class Entity {
1512
1487
  let regex = new RegExp(pattern, "i");
1513
1488
 
1514
1489
  return ({ key } = {}) => {
1515
- if (!["string", "number"].includes(typeof key)) {
1490
+ const typeofKey = typeof key;
1491
+ if (!["string", "number"].includes(typeofKey)) {
1516
1492
  return null;
1517
1493
  }
1518
1494
  key = `${key}`;
1495
+ const isNumeric =
1496
+ cast === CastKeyOptions.number && typeofKey === "number";
1519
1497
  let match = key.match(regex);
1520
1498
  let results = {};
1521
- if (match) {
1499
+ if (match || isNumeric) {
1522
1500
  for (let i = 0; i < names.length; i++) {
1523
- let key = names[i];
1524
- let value = match[i + 1];
1501
+ let keyName = names[i];
1502
+ let value = isNumeric ? key : match[i + 1];
1525
1503
  let type = types[i];
1526
1504
  switch (type) {
1527
1505
  case "number": {
@@ -1533,8 +1511,8 @@ class Entity {
1533
1511
  break;
1534
1512
  }
1535
1513
  }
1536
- if (key && value !== undefined) {
1537
- results[key] = value;
1514
+ if (keyName && value !== undefined) {
1515
+ results[keyName] = value;
1538
1516
  }
1539
1517
  }
1540
1518
  } else {
@@ -1565,7 +1543,7 @@ class Entity {
1565
1543
  let skComposites = {};
1566
1544
  if (indexHasSortKey) {
1567
1545
  const sk = keys[skName];
1568
- if (!sk) {
1546
+ if (sk === undefined) {
1569
1547
  return null;
1570
1548
  }
1571
1549
  skComposites = deconstructors.sk({ key: sk });