@abtnode/models 1.16.8-next-199ae4cb → 1.16.8-next-e7fab584

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/migrate.js CHANGED
@@ -11,7 +11,12 @@ const fs_1 = require("fs");
11
11
  const path_1 = require("path");
12
12
  const core_1 = require("@nedb/core");
13
13
  const umzug_1 = require("umzug");
14
+ const logger_1 = __importDefault(require("@abtnode/logger"));
15
+ // @ts-ignore
16
+ const user_1 = require("@abtnode/util/lib/user"); // eslint-disable-line
14
17
  const models_1 = require("./models");
18
+ const util_1 = require("./util");
19
+ const logger = (0, logger_1.default)('@abtnode/models:migration');
15
20
  const MIGRATION_SCRIPT_EXTENSION = process.env.MIGRATION_SCRIPT_EXTENSION || 'js';
16
21
  async function doSchemaMigration(dbFile, group) {
17
22
  const sequelize = (0, models_1.createSequelize)(dbFile);
@@ -22,14 +27,18 @@ async function doSchemaMigration(dbFile, group) {
22
27
  migrations: { glob: [`migrations/${group}/*.${MIGRATION_SCRIPT_EXTENSION}`, { cwd: __dirname }] },
23
28
  context: sequelize.getQueryInterface(),
24
29
  storage: new umzug_1.SequelizeStorage({ sequelize }),
25
- logger: process.env.NODE_ENV === 'test' ? undefined : console,
30
+ logger,
26
31
  });
27
32
  return umzug.up();
28
33
  }
29
34
  exports.doSchemaMigration = doSchemaMigration;
30
- async function doDataMigration({ srcDir, dbFile, mapping, models, printInfo = console.info, // eslint-disable-line no-console
31
- printError = console.error, printSuccess = console.info, // eslint-disable-line no-console
35
+ async function doDataMigration({ srcDir, dbFile, mapping, models, printInfo = logger.info, // eslint-disable-line no-console
36
+ printError = logger.error, printSuccess = logger.info, // eslint-disable-line no-console
32
37
  }) {
38
+ if ((0, fs_1.existsSync)(srcDir) === false) {
39
+ printError(`Can not scan srcDir ${srcDir}`);
40
+ return;
41
+ }
33
42
  const sequelize = (0, models_1.createSequelize)(dbFile);
34
43
  Object.values(mapping).forEach((x) => {
35
44
  if (models[x]) {
@@ -43,6 +52,7 @@ printError = console.error, printSuccess = console.info, // eslint-disable-line
43
52
  }
44
53
  });
45
54
  const files = (0, fs_1.readdirSync)(srcDir).filter((file) => file.endsWith('.db'));
55
+ const dataDir = (0, util_1.getAvatarDir)(srcDir);
46
56
  for (const file of files) {
47
57
  const tableName = file.slice(0, -3);
48
58
  const modelName = mapping[tableName];
@@ -74,6 +84,11 @@ printError = console.error, printSuccess = console.info, // eslint-disable-line
74
84
  if (modelName === 'User') {
75
85
  // rename fields
76
86
  newData.sourceProvider = newData.sourceProvider || (0, lodash_get_1.default)(newData, 'extraConfigs.sourceProvider') || 'wallet';
87
+ // extra avatar
88
+ if (doc.avatar && doc.avatar.startsWith('data:image')) {
89
+ newData.avatar = await (0, user_1.extractUserAvatar)(doc.avatar, { dataDir });
90
+ printInfo('Convert avatar for user', doc.did, newData.avatar);
91
+ }
77
92
  // split fields
78
93
  const passports = (newData.passports || []).filter((p) => p.id);
79
94
  const connectedAccounts = (0, lodash_get_1.default)(newData, 'extraConfigs.connectedAccounts') ||
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.down = exports.up = void 0;
4
+ const up = async ({ context }) => {
5
+ await context.addIndex('users', ['approved']);
6
+ await context.addIndex('users', ['fullName']);
7
+ await context.addIndex('users', ['email']);
8
+ await context.addIndex('connected_accounts', ['userDid']);
9
+ await context.addIndex('passports', ['userDid']);
10
+ await context.addIndex('passports', ['name', 'status', 'userDid']);
11
+ await context.addIndex('permissions', ['name']);
12
+ await context.addIndex('permissions', ['type']);
13
+ await context.addIndex('sessions', ['type']);
14
+ await context.addIndex('sessions', ['key']);
15
+ };
16
+ exports.up = up;
17
+ // eslint-disable-next-line
18
+ const down = async ({ context }) => { };
19
+ exports.down = down;
@@ -6,6 +6,7 @@ const models = (0, models_1.getServerModels)();
6
6
  const up = async ({ context }) => {
7
7
  await context.createTable('access_keys', models.AccessKey.GENESIS_ATTRIBUTES);
8
8
  await context.createTable('audit_logs', models.AuditLog.GENESIS_ATTRIBUTES);
9
+ await context.createTable('backups', models.Backup.GENESIS_ATTRIBUTES);
9
10
  await context.createTable('blocklet_extras', models.BlockletExtra.GENESIS_ATTRIBUTES);
10
11
  await context.createTable('blocklets', models.Blocklet.GENESIS_ATTRIBUTES);
11
12
  await context.createTable('caches', models.Cache.GENESIS_ATTRIBUTES);
@@ -20,7 +21,6 @@ const up = async ({ context }) => {
20
21
  await context.createTable('passports', models.Passport.GENESIS_ATTRIBUTES);
21
22
  await context.createTable('connected_accounts', models.ConnectedAccount.GENESIS_ATTRIBUTES);
22
23
  await context.createTable('webhooks', models.WebHook.GENESIS_ATTRIBUTES);
23
- await context.createTable('backups', models.Backup.GENESIS_ATTRIBUTES);
24
24
  };
25
25
  exports.up = up;
26
26
  const down = async ({ context }) => {
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.down = exports.up = void 0;
4
+ const up = async ({ context }) => {
5
+ await context.addIndex('audit_logs', ['scope', 'category']);
6
+ await context.addIndex('audit_logs', ['scope', 'actor']);
7
+ await context.addIndex('audit_logs', ['category']);
8
+ await context.addIndex('backups', ['appPid']);
9
+ await context.addIndex('blocklets', ['appDid']);
10
+ await context.addIndex('blocklets', ['appPid']);
11
+ await context.addIndex('jobs', ['queue']);
12
+ await context.addIndex('notifications', ['sender']);
13
+ await context.addIndex('notifications', ['receiver']);
14
+ await context.addIndex('users', ['approved']);
15
+ await context.addIndex('users', ['fullName']);
16
+ await context.addIndex('users', ['email']);
17
+ await context.addIndex('connected_accounts', ['userDid']);
18
+ await context.addIndex('passports', ['userDid']);
19
+ await context.addIndex('passports', ['name', 'status', 'userDid']);
20
+ await context.addIndex('permissions', ['name']);
21
+ await context.addIndex('permissions', ['type']);
22
+ await context.addIndex('sessions', ['type']);
23
+ await context.addIndex('sessions', ['key']);
24
+ };
25
+ exports.up = up;
26
+ // eslint-disable-next-line
27
+ const down = async ({ context }) => { };
28
+ exports.down = down;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.down = exports.up = void 0;
4
+ const up = async ({ context }) => {
5
+ await context.addIndex('messages', ['did']);
6
+ };
7
+ exports.up = up;
8
+ // eslint-disable-next-line
9
+ const down = async ({ context }) => { };
10
+ exports.down = down;
@@ -13,7 +13,7 @@ function createAuditLogModel() {
13
13
  sequelize,
14
14
  modelName: 'AuditLog',
15
15
  tableName: 'audit_logs',
16
- indexes: [{ fields: ['scope', 'category', 'id'] }, { fields: ['scope', 'actor', 'id'] }],
16
+ indexes: [{ fields: ['scope', 'category'] }, { fields: ['scope', 'actor'] }, { fields: ['category'] }],
17
17
  timestamps: true,
18
18
  });
19
19
  }
@@ -13,7 +13,7 @@ function createBackupModel() {
13
13
  sequelize,
14
14
  modelName: 'Backup',
15
15
  tableName: 'backups',
16
- indexes: [{ fields: ['appPid', 'id'] }],
16
+ indexes: [{ fields: ['appPid'] }],
17
17
  timestamps: true,
18
18
  });
19
19
  }
@@ -11,7 +11,7 @@ function createBlockletModel() {
11
11
  sequelize,
12
12
  modelName: 'Blocklet',
13
13
  tableName: 'blocklets',
14
- indexes: [{ fields: ['appDid', 'id'] }, { fields: ['appPid', 'id'] }],
14
+ indexes: [{ fields: ['appDid'] }, { fields: ['appPid'] }],
15
15
  timestamps: true,
16
16
  });
17
17
  }
@@ -12,7 +12,7 @@ function createConnectedAccountModel() {
12
12
  sequelize,
13
13
  modelName: 'ConnectedAccount',
14
14
  tableName: 'connected_accounts',
15
- indexes: [{ fields: ['userDid', 'did'] }],
15
+ indexes: [{ fields: ['userDid'] }],
16
16
  timestamps: false,
17
17
  });
18
18
  }
@@ -11,7 +11,6 @@ function createHttpChallengeModel() {
11
11
  ...this.GENESIS_ATTRIBUTES,
12
12
  }, {
13
13
  sequelize,
14
- indexes: [{ fields: ['key', 'id'] }],
15
14
  modelName: 'HttpChallenge',
16
15
  tableName: 'http_challenges',
17
16
  timestamps: true,
package/lib/models/job.js CHANGED
@@ -12,7 +12,7 @@ function createJobModel() {
12
12
  ...this.GENESIS_ATTRIBUTES,
13
13
  }, {
14
14
  sequelize,
15
- indexes: [{ fields: ['queue', 'id'] }],
15
+ indexes: [{ fields: ['queue'] }],
16
16
  modelName: 'Job',
17
17
  tableName: 'jobs',
18
18
  timestamps: true,
@@ -13,7 +13,7 @@ function createMessageModel() {
13
13
  }, {
14
14
  sequelize,
15
15
  modelName: 'Message',
16
- indexes: [{ fields: ['did', 'id'] }],
16
+ indexes: [{ fields: ['did'] }],
17
17
  tableName: 'messages',
18
18
  timestamps: true,
19
19
  });
@@ -12,7 +12,7 @@ function createNotificationModel() {
12
12
  }, {
13
13
  sequelize,
14
14
  modelName: 'Notification',
15
- indexes: [{ fields: ['sender', 'id'] }, { fields: ['receiver', 'id'] }],
15
+ indexes: [{ fields: ['sender'] }, { fields: ['receiver'] }],
16
16
  tableName: 'notifications',
17
17
  timestamps: true,
18
18
  });
@@ -12,7 +12,7 @@ function createPassportModel() {
12
12
  sequelize,
13
13
  modelName: 'Passport',
14
14
  tableName: 'passports',
15
- indexes: [{ fields: ['name', 'status', 'userDid'] }],
15
+ indexes: [{ fields: ['name', 'status', 'userDid'] }, { fields: ['userDid'] }],
16
16
  timestamps: false,
17
17
  });
18
18
  }
@@ -11,7 +11,7 @@ function createRbacModel() {
11
11
  ...this.GENESIS_ATTRIBUTES,
12
12
  }, {
13
13
  sequelize,
14
- indexes: [{ fields: ['name', 'id'] }],
14
+ indexes: [{ fields: ['name'] }],
15
15
  modelName: 'Rbac',
16
16
  tableName: 'permissions',
17
17
  timestamps: true,
@@ -12,7 +12,7 @@ function createSessionModel() {
12
12
  ...this.GENESIS_ATTRIBUTES,
13
13
  }, {
14
14
  sequelize,
15
- indexes: [{ fields: ['type', 'id'] }, { fields: ['key', 'id'] }],
15
+ indexes: [{ fields: ['type'] }, { fields: ['key'] }],
16
16
  modelName: 'Session',
17
17
  tableName: 'sessions',
18
18
  timestamps: true,
@@ -13,7 +13,7 @@ function createUserModel() {
13
13
  modelName: 'User',
14
14
  tableName: 'users',
15
15
  timestamps: true,
16
- indexes: [{ fields: ['approved', 'did'] }, { fields: ['email', 'did'] }, { fields: ['fullName', 'did'] }],
16
+ indexes: [{ fields: ['approved'] }, { fields: ['email'] }, { fields: ['fullName'] }],
17
17
  });
18
18
  }
19
19
  static associate(models) {
@@ -46,8 +46,7 @@ function createUserModel() {
46
46
  type: sequelize_1.DataTypes.STRING(128),
47
47
  },
48
48
  avatar: {
49
- // FIXME: make this a shorter string?
50
- type: sequelize_1.DataTypes.TEXT,
49
+ type: sequelize_1.DataTypes.STRING(128),
51
50
  },
52
51
  role: {
53
52
  type: sequelize_1.DataTypes.STRING(64),
package/lib/types.d.ts CHANGED
@@ -5,10 +5,6 @@ export type DynamicModel<T> = ModelDefined<T, T> & {
5
5
  [key: string]: any;
6
6
  };
7
7
  };
8
- export type QueryInterface = {
9
- createTable: Function;
10
- dropTable: Function;
11
- };
12
8
  export type UpdateOptions = {
13
9
  returnUpdatedDocs?: boolean;
14
10
  };
package/lib/util.d.ts CHANGED
@@ -4,3 +4,4 @@ export declare function formatConditions(conditions: Record<string, any>): Inclu
4
4
  export declare function formatOrder(sort: Record<string, any>): Order;
5
5
  export declare function formatSelection(selection: Record<string, any>): string[];
6
6
  export declare function formatParams(params: FindOptions): FindOptions;
7
+ export declare function getAvatarDir(srcDir: any): any;
package/lib/util.js CHANGED
@@ -3,8 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.formatParams = exports.formatSelection = exports.formatOrder = exports.formatConditions = exports.generateId = void 0;
6
+ exports.getAvatarDir = exports.formatParams = exports.formatSelection = exports.formatOrder = exports.formatConditions = exports.generateId = void 0;
7
7
  const uuid_1 = require("uuid");
8
+ const path_1 = require("path");
8
9
  const sequelize_1 = require("sequelize");
9
10
  const lodash_isempty_1 = __importDefault(require("lodash.isempty"));
10
11
  function generateId() {
@@ -106,3 +107,7 @@ function formatParams(params) {
106
107
  return params;
107
108
  }
108
109
  exports.formatParams = formatParams;
110
+ function getAvatarDir(srcDir) {
111
+ return srcDir.endsWith('/core') ? (0, path_1.join)((0, path_1.dirname)(srcDir), 'data/_abtnode') : srcDir;
112
+ }
113
+ exports.getAvatarDir = getAvatarDir;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abtnode/models",
3
- "version": "1.16.8-next-199ae4cb",
3
+ "version": "1.16.8-next-e7fab584",
4
4
  "description": "Sequelize models for blocklet server and blocklet service",
5
5
  "homepage": "https://github.com/ArcBlock/blocklet-server#readme",
6
6
  "publishConfig": {
@@ -32,7 +32,8 @@
32
32
  "url": "https://github.com/ArcBlock/blocklet-server/issues"
33
33
  },
34
34
  "dependencies": {
35
- "@abtnode/types": "1.16.8-next-199ae4cb",
35
+ "@abtnode/logger": "1.16.8-next-e7fab584",
36
+ "@abtnode/types": "1.16.8-next-e7fab584",
36
37
  "@nedb/core": "^2.1.5",
37
38
  "lodash.clonedeep": "^4.5.0",
38
39
  "lodash.get": "^4.4.2",
@@ -54,5 +55,5 @@
54
55
  "typescript": "^4.8.4"
55
56
  },
56
57
  "resolutions": {},
57
- "gitHead": "a3c52c2929d25254f33a08eca0261fbf4dbc7b4c"
58
+ "gitHead": "65d5a71c762d73765befe957e3361a2570dc9882"
58
59
  }