@abtnode/connect-storage 1.16.19-beta-340de95d → 1.16.19-beta-7b2db880

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Connect Storage
2
2
 
3
- Simple storage implemented on `@abtnode/models`, should be used with `WalletHandler` from `@blocklet/sdk`
3
+ Simple did connect storage that can be used cross multiple processes.
4
4
 
5
5
  ## Usage
6
6
 
@@ -11,4 +11,20 @@ yarn add @abtnode/connect-storage
11
11
  Then:
12
12
 
13
13
  ```javascript
14
+ import { join } from 'path';
15
+
16
+ import AuthStorage from '@abtnode/connect-storage';
17
+ import WalletAuthenticator from '@blocklet/sdk/lib/wallet-authenticator';
18
+ import WalletHandler from '@blocklet/sdk/lib/wallet-handler';
19
+ import Config from '@blocklet/sdk/lib/config';
20
+
21
+ export const authenticator = new WalletAuthenticator();
22
+ export const handlers = new WalletHandler({
23
+ authenticator,
24
+ tokenStorage: new AuthStorage({
25
+ // FIXME: update this
26
+ dbPath: join(Config.env.dataDir, 'path/to/component.db'),
27
+ }),
28
+ });
29
+
14
30
  ```
package/lib/index.js CHANGED
@@ -9,18 +9,21 @@ class SequelizeStorage extends EventEmitter {
9
9
  throw new Error('SequelizeStorage requires dbPath to be set');
10
10
  }
11
11
 
12
+ const model = options.v2 ? 'ConnectionV2' : 'Connection';
13
+ const primaryKey = options.v2 ? 'sessionId' : 'token';
14
+
12
15
  super(options);
13
16
 
14
- this.sequelize = createSequelize(options.dbPath);
15
- this.models = getConnectModels();
16
- setupModels(this.models, this.sequelize);
17
+ const sequelize = createSequelize(options.dbPath);
18
+ const models = getConnectModels();
19
+
20
+ setupModels(models, sequelize);
17
21
 
18
- this.state = new ConnectionState(this.models.Connection);
22
+ this.state = new ConnectionState(models[model], { ...options, primaryKey });
23
+ this.primaryKey = primaryKey;
19
24
 
20
25
  doSchemaMigration(options.dbPath, 'connect')
21
26
  .then(() => {
22
- // eslint-disable-next-line no-console
23
- console.info(`Connection storage schema migration succeed: ${options.dbPath}`);
24
27
  if (typeof options.onload === 'function') {
25
28
  options.onload();
26
29
  }
@@ -51,13 +54,13 @@ class SequelizeStorage extends EventEmitter {
51
54
  }
52
55
 
53
56
  async delete(token) {
54
- const num = await this.state.remove({ token });
57
+ const num = await this.state.remove({ [this.primaryKey]: token });
55
58
  this.emit('destroy', token);
56
59
  return num;
57
60
  }
58
61
 
59
62
  async exist(token, did) {
60
- const data = await this.state.count({ token, did });
63
+ const data = await this.state.count({ [this.primaryKey]: token, did });
61
64
  return data;
62
65
  }
63
66
 
package/lib/state.js CHANGED
@@ -7,12 +7,16 @@ const { BaseState } = require('@abtnode/models');
7
7
  */
8
8
  class Connection extends BaseState {
9
9
  async start(token, status = 'created') {
10
- const doc = await this.insert({ token, status });
10
+ const attrs =
11
+ status && typeof status === 'object'
12
+ ? { [this.config.primaryKey]: token, ...status }
13
+ : { [this.config.primaryKey]: token, status };
14
+ const doc = await this.insert(attrs);
11
15
  return this._format(doc);
12
16
  }
13
17
 
14
18
  async update(token, updates) {
15
- const doc = await this.findOne({ token });
19
+ const doc = await this.findOne({ [this.config.primaryKey]: token });
16
20
  if (!doc) {
17
21
  throw new Error(`Connect session does not exist: ${token}`);
18
22
  }
@@ -20,12 +24,15 @@ class Connection extends BaseState {
20
24
  const knownKeys = Object.keys(this.model.getAttributes());
21
25
  const extra = omit(updates, knownKeys);
22
26
 
23
- await super.update({ token }, { ...pick(updates, knownKeys), __extra: { ...doc.__extra, ...extra } });
27
+ await super.update(
28
+ { [this.config.primaryKey]: token },
29
+ { ...pick(updates, knownKeys), __extra: { ...doc.__extra, ...extra } }
30
+ );
24
31
  return this._format(doc, updates);
25
32
  }
26
33
 
27
34
  async read(token) {
28
- const doc = await this.findOne({ token });
35
+ const doc = await this.findOne({ [this.config.primaryKey]: token });
29
36
  return this._format(doc);
30
37
  }
31
38
 
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.19-beta-340de95d",
6
+ "version": "1.16.19-beta-7b2db880",
7
7
  "description": "Sequelize storage for @arcblock/did-auth",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -19,11 +19,11 @@
19
19
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
20
20
  "license": "Apache-2.0",
21
21
  "dependencies": {
22
- "@abtnode/models": "1.16.19-beta-340de95d",
22
+ "@abtnode/models": "1.16.19-beta-7b2db880",
23
23
  "lodash": "^4.17.21"
24
24
  },
25
25
  "devDependencies": {
26
26
  "jest": "^27.5.1"
27
27
  },
28
- "gitHead": "cc7afa5fe28ef4b0304f97b7e0b9ad8194427ae7"
28
+ "gitHead": "598b576b97dd7accbfa1bb509b75e423ad27e5e2"
29
29
  }