@ocap/indexdb 1.20.16 → 1.21.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.
Files changed (2) hide show
  1. package/lib/index.js +24 -1
  2. package/package.json +3 -3
package/lib/index.js CHANGED
@@ -1,15 +1,18 @@
1
1
  /* eslint-disable no-underscore-dangle */
2
+ const crypto = require('crypto');
2
3
  const Kareem = require('kareem');
3
4
  const omit = require('lodash/omit');
4
5
  const { Ready } = require('@ocap/util/lib/ready');
5
6
 
6
7
  class BaseIndex extends Ready {
7
- constructor(name) {
8
+ constructor(name, uniqIndex) {
8
9
  super();
9
10
 
10
11
  this.name = name;
11
12
  this.ready = false;
12
13
  this.readyCallbacks = [];
14
+ this.uniqIndex = uniqIndex;
15
+ this.primaryKey = typeof uniqIndex === 'string' ? uniqIndex : 'id';
13
16
 
14
17
  const hooks = new Kareem();
15
18
 
@@ -56,6 +59,26 @@ class BaseIndex extends Ready {
56
59
  _reset(context) {
57
60
  throw new Error('`_reset` must be implemented in sub indexdb');
58
61
  }
62
+
63
+ /**
64
+ * Generate an ID composed of multiple primary keys
65
+ */
66
+ generatePrimaryKey(doc) {
67
+ if (typeof doc === 'string') {
68
+ return doc;
69
+ }
70
+
71
+ // composite primary key
72
+ if (Array.isArray(this.uniqIndex)) {
73
+ const key = this.uniqIndex
74
+ .map((id) => doc[id])
75
+ .map((item) => (typeof item === 'object' ? JSON.stringify(item) : item))
76
+ .join('-');
77
+ return crypto.createHash('md5').update(key).digest('hex');
78
+ }
79
+
80
+ return doc[this.uniqIndex];
81
+ }
59
82
  }
60
83
 
61
84
  module.exports = BaseIndex;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.20.16",
6
+ "version": "1.21.0",
7
7
  "description": "Defines the basic interface for OCAP IndexDB",
8
8
  "main": "lib/main.js",
9
9
  "files": [
@@ -21,8 +21,8 @@
21
21
  "dependencies": {
22
22
  "kareem": "^2.4.1",
23
23
  "lodash": "^4.17.21",
24
- "@ocap/state": "1.20.16",
25
- "@ocap/util": "1.20.16"
24
+ "@ocap/util": "1.21.0",
25
+ "@ocap/state": "1.21.0"
26
26
  },
27
27
  "scripts": {
28
28
  "lint": "eslint tests lib",