@abtnode/connect-storage 1.16.8-next-d1e52353
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 +14 -0
- package/lib/index.js +67 -0
- package/lib/state.js +37 -0
- package/package.json +29 -0
package/README.md
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const { EventEmitter } = require('events');
|
|
2
|
+
const { createSequelize, getConnectModels, setupModels, doSchemaMigration } = require('@abtnode/models');
|
|
3
|
+
|
|
4
|
+
const ConnectionState = require('./state');
|
|
5
|
+
|
|
6
|
+
class SequelizeStorage extends EventEmitter {
|
|
7
|
+
constructor(options = {}) {
|
|
8
|
+
if (!options.dbPath) {
|
|
9
|
+
throw new Error('SequelizeStorage requires dbPath to be set');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
super(options);
|
|
13
|
+
|
|
14
|
+
this.sequelize = createSequelize(options.dbPath);
|
|
15
|
+
this.models = getConnectModels();
|
|
16
|
+
setupModels(this.models, this.sequelize);
|
|
17
|
+
|
|
18
|
+
this.state = new ConnectionState(this.models.Connection);
|
|
19
|
+
|
|
20
|
+
doSchemaMigration(options.dbPath, 'connect')
|
|
21
|
+
.then(() => {
|
|
22
|
+
// eslint-disable-next-line no-console
|
|
23
|
+
console.info(`Connection storage schema migration succeed: ${options.dbPath}`);
|
|
24
|
+
if (typeof options.onload === 'function') {
|
|
25
|
+
options.onload();
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
.catch((err) => {
|
|
29
|
+
console.error(`Connection storage schema migration failed: ${options.dbPath}`, err);
|
|
30
|
+
if (typeof options.onload === 'function') {
|
|
31
|
+
options.onload();
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async read(token) {
|
|
37
|
+
return this.state.read(token);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async create(token, status = 'created') {
|
|
41
|
+
const doc = await this.state.start(token, status);
|
|
42
|
+
this.emit('create', doc);
|
|
43
|
+
return doc;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async update(token, updates) {
|
|
47
|
+
const doc = await this.state.update(token, updates);
|
|
48
|
+
this.emit('update', doc);
|
|
49
|
+
return doc;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async delete(token) {
|
|
53
|
+
const num = await this.state.remove({ token });
|
|
54
|
+
this.emit('destroy', token);
|
|
55
|
+
return num;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async exist(token, did) {
|
|
59
|
+
return this.state.count({ token, did });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async clear() {
|
|
63
|
+
return this.state.reset();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
module.exports = SequelizeStorage;
|
package/lib/state.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const omit = require('lodash/omit');
|
|
2
|
+
const pick = require('lodash/pick');
|
|
3
|
+
const { BaseState } = require('@abtnode/models');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @extends BaseState<import('@abtnode/models').ConnectionState>
|
|
7
|
+
*/
|
|
8
|
+
class Connection extends BaseState {
|
|
9
|
+
async start(token, status = 'created') {
|
|
10
|
+
const doc = await this.insert({ token, status });
|
|
11
|
+
return this._format(doc);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async update(token, updates) {
|
|
15
|
+
const doc = await this.findOne({ token });
|
|
16
|
+
if (!doc) {
|
|
17
|
+
throw new Error(`Connect session does not exist: ${token}`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const knownKeys = Object.keys(this.model.getAttributes());
|
|
21
|
+
const extra = omit(updates, knownKeys);
|
|
22
|
+
|
|
23
|
+
await super.update({ token }, { ...pick(updates, knownKeys), __extra: { ...doc.__extra, ...extra } });
|
|
24
|
+
return this._format(doc, updates);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async read(token) {
|
|
28
|
+
const doc = await this.findOne({ token });
|
|
29
|
+
return this._format(doc);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
_format(doc, extra = {}) {
|
|
33
|
+
return doc ? omit({ ...doc, ...doc.__extra, ...extra }, ['__extra']) : doc;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = Connection;
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@abtnode/connect-storage",
|
|
3
|
+
"publishConfig": {
|
|
4
|
+
"access": "public"
|
|
5
|
+
},
|
|
6
|
+
"version": "1.16.8-next-d1e52353",
|
|
7
|
+
"description": "Sequelize storage for @arcblock/did-auth",
|
|
8
|
+
"main": "lib/index.js",
|
|
9
|
+
"files": [
|
|
10
|
+
"lib"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"lint": "eslint tests lib",
|
|
14
|
+
"lint:fix": "eslint --fix tests lib",
|
|
15
|
+
"test": "node tools/jest.js",
|
|
16
|
+
"coverage": "npm run test -- --coverage"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [],
|
|
19
|
+
"author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@abtnode/models": "1.16.8-next-d1e52353",
|
|
23
|
+
"lodash": "^4.17.21"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"jest": "^27.5.1"
|
|
27
|
+
},
|
|
28
|
+
"gitHead": "d357376aa3df9ef789befc7f548629deecb04d96"
|
|
29
|
+
}
|