@abtnode/connect-storage 1.16.19-beta-340de95d → 1.16.19-beta-e6aac665
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/lib/index.js +11 -5
- package/lib/state.js +11 -4
- package/package.json +3 -3
package/lib/index.js
CHANGED
|
@@ -8,19 +8,25 @@ class SequelizeStorage extends EventEmitter {
|
|
|
8
8
|
if (!options.dbPath) {
|
|
9
9
|
throw new Error('SequelizeStorage requires dbPath to be set');
|
|
10
10
|
}
|
|
11
|
+
if (!options.model) {
|
|
12
|
+
throw new Error('SequelizeStorage requires model to be set');
|
|
13
|
+
}
|
|
14
|
+
if (!options.primaryKey) {
|
|
15
|
+
throw new Error('SequelizeStorage requires primaryKey to be set');
|
|
16
|
+
}
|
|
11
17
|
|
|
12
18
|
super(options);
|
|
13
19
|
|
|
14
20
|
this.sequelize = createSequelize(options.dbPath);
|
|
15
21
|
this.models = getConnectModels();
|
|
22
|
+
|
|
16
23
|
setupModels(this.models, this.sequelize);
|
|
17
24
|
|
|
18
|
-
this.state = new ConnectionState(this.models.
|
|
25
|
+
this.state = new ConnectionState(this.models[options.model], options);
|
|
26
|
+
this.primaryKey = options.primaryKey;
|
|
19
27
|
|
|
20
28
|
doSchemaMigration(options.dbPath, 'connect')
|
|
21
29
|
.then(() => {
|
|
22
|
-
// eslint-disable-next-line no-console
|
|
23
|
-
console.info(`Connection storage schema migration succeed: ${options.dbPath}`);
|
|
24
30
|
if (typeof options.onload === 'function') {
|
|
25
31
|
options.onload();
|
|
26
32
|
}
|
|
@@ -51,13 +57,13 @@ class SequelizeStorage extends EventEmitter {
|
|
|
51
57
|
}
|
|
52
58
|
|
|
53
59
|
async delete(token) {
|
|
54
|
-
const num = await this.state.remove({ token });
|
|
60
|
+
const num = await this.state.remove({ [this.primaryKey]: token });
|
|
55
61
|
this.emit('destroy', token);
|
|
56
62
|
return num;
|
|
57
63
|
}
|
|
58
64
|
|
|
59
65
|
async exist(token, did) {
|
|
60
|
-
const data = await this.state.count({ token, did });
|
|
66
|
+
const data = await this.state.count({ [this.primaryKey]: token, did });
|
|
61
67
|
return data;
|
|
62
68
|
}
|
|
63
69
|
|
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
|
|
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(
|
|
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-
|
|
6
|
+
"version": "1.16.19-beta-e6aac665",
|
|
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-
|
|
22
|
+
"@abtnode/models": "1.16.19-beta-e6aac665",
|
|
23
23
|
"lodash": "^4.17.21"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"jest": "^27.5.1"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "09b0172831521ef73f7879940b977c2696b5b247"
|
|
29
29
|
}
|