@abtnode/models 1.16.19-beta-e6aac665 → 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/lib/migrate.d.ts +0 -12
- package/lib/migrate.js +2 -120
- package/lib/types.d.ts +5 -5
- package/lib/util.d.ts +0 -1
- package/lib/util.js +1 -7
- package/package.json +6 -9
package/lib/migrate.d.ts
CHANGED
|
@@ -1,13 +1 @@
|
|
|
1
1
|
export declare function doSchemaMigration(dbFile: string, group: string): Promise<import("umzug").MigrationMeta[]>;
|
|
2
|
-
type DataMigrationArgs = {
|
|
3
|
-
srcDir: string;
|
|
4
|
-
dbFile: string;
|
|
5
|
-
mapping: Record<string, string>;
|
|
6
|
-
models: Record<string, any>;
|
|
7
|
-
printInfo?: Function;
|
|
8
|
-
printError?: Function;
|
|
9
|
-
printSuccess?: Function;
|
|
10
|
-
};
|
|
11
|
-
export declare function doDataMigration({ srcDir, dbFile, mapping, models, printInfo, // eslint-disable-line no-console
|
|
12
|
-
printError, printSuccess, }: DataMigrationArgs): Promise<void>;
|
|
13
|
-
export {};
|
package/lib/migrate.js
CHANGED
|
@@ -3,20 +3,11 @@ 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.
|
|
7
|
-
/* eslint-disable
|
|
8
|
-
/* eslint-disable no-continue */
|
|
9
|
-
const lodash_get_1 = __importDefault(require("lodash.get"));
|
|
10
|
-
const fs_1 = require("fs");
|
|
11
|
-
const path_1 = require("path");
|
|
12
|
-
const core_1 = require("@nedb/core");
|
|
6
|
+
exports.doSchemaMigration = void 0;
|
|
7
|
+
/* eslint-disable import/prefer-default-export */
|
|
13
8
|
const umzug_1 = require("umzug");
|
|
14
9
|
const logger_1 = __importDefault(require("@abtnode/logger"));
|
|
15
|
-
const constant_1 = require("@blocklet/constant");
|
|
16
|
-
// @ts-ignore
|
|
17
|
-
const user_1 = require("@abtnode/util/lib/user"); // eslint-disable-line
|
|
18
10
|
const models_1 = require("./models");
|
|
19
|
-
const util_1 = require("./util");
|
|
20
11
|
const logger = (0, logger_1.default)('@abtnode/models:migration');
|
|
21
12
|
const MIGRATION_SCRIPT_EXTENSION = process.env.MIGRATION_SCRIPT_EXTENSION || 'js';
|
|
22
13
|
// eslint-disable-next-line require-await
|
|
@@ -35,112 +26,3 @@ async function doSchemaMigration(dbFile, group) {
|
|
|
35
26
|
return umzug.up();
|
|
36
27
|
}
|
|
37
28
|
exports.doSchemaMigration = doSchemaMigration;
|
|
38
|
-
async function doDataMigration({ srcDir, dbFile, mapping, models, printInfo = logger.info, // eslint-disable-line no-console
|
|
39
|
-
printError = logger.error, printSuccess = logger.info, // eslint-disable-line no-console
|
|
40
|
-
}) {
|
|
41
|
-
if ((0, fs_1.existsSync)(srcDir) === false) {
|
|
42
|
-
printError(`Can not scan srcDir ${srcDir}`);
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
const sequelize = (0, models_1.createSequelize)(dbFile);
|
|
46
|
-
Object.values(mapping).forEach((x) => {
|
|
47
|
-
if (models[x]) {
|
|
48
|
-
models[x].initialize(sequelize);
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
// Init hidden models
|
|
52
|
-
['Passport', 'ConnectedAccount'].forEach((x) => {
|
|
53
|
-
if (models[x]) {
|
|
54
|
-
models[x].initialize(sequelize);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
const files = (0, fs_1.readdirSync)(srcDir).filter((file) => file.endsWith('.db'));
|
|
58
|
-
const dataDir = (0, util_1.getAvatarDir)(srcDir);
|
|
59
|
-
for (const file of files) {
|
|
60
|
-
const tableName = file.slice(0, -3);
|
|
61
|
-
const modelName = mapping[tableName];
|
|
62
|
-
if (!modelName) {
|
|
63
|
-
printInfo(`No mapping found for NeDB file: ${file}. Skipping.`);
|
|
64
|
-
continue;
|
|
65
|
-
}
|
|
66
|
-
const model = models[modelName];
|
|
67
|
-
if (!model) {
|
|
68
|
-
printInfo(`No Sequelize model found for NeDB file: ${file}. Skipping.`);
|
|
69
|
-
continue;
|
|
70
|
-
}
|
|
71
|
-
const filename = (0, path_1.join)(srcDir, file);
|
|
72
|
-
try {
|
|
73
|
-
const db = new core_1.DataStore({ filename, autoload: true });
|
|
74
|
-
const docs = await db.find({});
|
|
75
|
-
// Insert data into Sequelize database
|
|
76
|
-
for (const doc of docs) {
|
|
77
|
-
const newData = {
|
|
78
|
-
...doc,
|
|
79
|
-
id: doc._id,
|
|
80
|
-
createdAt: new Date(doc.createdAt || doc.created),
|
|
81
|
-
updatedAt: new Date(doc.updatedAt || doc.updated),
|
|
82
|
-
};
|
|
83
|
-
if (!newData.id) {
|
|
84
|
-
delete newData.id;
|
|
85
|
-
}
|
|
86
|
-
// Hack: 1-time-use data split to multiple tables
|
|
87
|
-
if (modelName === 'User') {
|
|
88
|
-
// rename fields
|
|
89
|
-
newData.sourceProvider =
|
|
90
|
-
newData.sourceProvider || (0, lodash_get_1.default)(newData, 'extraConfigs.sourceProvider') || constant_1.LOGIN_PROVIDER.WALLET;
|
|
91
|
-
// extra avatar
|
|
92
|
-
if (doc.avatar && doc.avatar.startsWith('data:image')) {
|
|
93
|
-
newData.avatar = await (0, user_1.extractUserAvatar)(doc.avatar, { dataDir });
|
|
94
|
-
printInfo('Convert avatar for user', doc.did, newData.avatar);
|
|
95
|
-
}
|
|
96
|
-
// split fields
|
|
97
|
-
const passports = (newData.passports || []).filter((p) => p.id);
|
|
98
|
-
const connectedAccounts = (0, lodash_get_1.default)(newData, 'extraConfigs.connectedAccounts') ||
|
|
99
|
-
[
|
|
100
|
-
{
|
|
101
|
-
provider: constant_1.LOGIN_PROVIDER.WALLET,
|
|
102
|
-
did: newData.did,
|
|
103
|
-
pk: newData.pk,
|
|
104
|
-
lastLoginAt: newData.lastLoginAt,
|
|
105
|
-
firstLoginAt: newData.firstLoginAt,
|
|
106
|
-
},
|
|
107
|
-
].filter((a) => a.provider && a.did);
|
|
108
|
-
try {
|
|
109
|
-
await model.create(newData);
|
|
110
|
-
await Promise.all(passports.map((p) => models.Passport.create({ ...p, userDid: newData.did })));
|
|
111
|
-
await Promise.all(connectedAccounts.map((a) => models.ConnectedAccount.create({ ...a, userDid: newData.did })));
|
|
112
|
-
printInfo(`Import user into sqlite: ${newData.did}`);
|
|
113
|
-
}
|
|
114
|
-
catch (err) {
|
|
115
|
-
printError('Error importing user into sqlite:', err, doc);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
// Hack: 1-time-use data transformation
|
|
120
|
-
if (modelName === 'Site') {
|
|
121
|
-
if (Array.isArray(doc.domainAliases)) {
|
|
122
|
-
newData.domainAliases = doc.domainAliases.map((x) => {
|
|
123
|
-
if (typeof x === 'string') {
|
|
124
|
-
return { value: x, isProtected: false };
|
|
125
|
-
}
|
|
126
|
-
return x;
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
try {
|
|
131
|
-
await model.create(newData);
|
|
132
|
-
printInfo(`Import data into sqlite ${modelName}:`, doc._id);
|
|
133
|
-
}
|
|
134
|
-
catch (err) {
|
|
135
|
-
printError('Error importing data into sqlite:', err);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
printSuccess(`Migrated data from nedb ${filename} to sqlite ${modelName}.`);
|
|
140
|
-
}
|
|
141
|
-
catch (err) {
|
|
142
|
-
printError(`Error reading data from NeDB file ${filename}: ${err.message}`);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
exports.doDataMigration = doDataMigration;
|
package/lib/types.d.ts
CHANGED
|
@@ -21,11 +21,6 @@ export type PagingOptions = {
|
|
|
21
21
|
pageSize?: number;
|
|
22
22
|
page?: number;
|
|
23
23
|
};
|
|
24
|
-
export type ApplyBasicQueryCasting<T> = T | T[] | (T extends (infer U)[] ? U : any) | any;
|
|
25
|
-
export type Condition<T> = ApplyBasicQueryCasting<T> | QuerySelector<ApplyBasicQueryCasting<T>>;
|
|
26
|
-
export type ConditionOptions<T> = {
|
|
27
|
-
[P in keyof T]?: Condition<T[P]>;
|
|
28
|
-
} & RootQuerySelector<T>;
|
|
29
24
|
export type QuerySelector<T> = {
|
|
30
25
|
$lt?: T;
|
|
31
26
|
$lte?: T;
|
|
@@ -37,6 +32,11 @@ export type QuerySelector<T> = {
|
|
|
37
32
|
$exists?: boolean;
|
|
38
33
|
$regex?: T extends string ? RegExp | string : never;
|
|
39
34
|
};
|
|
35
|
+
export type ApplyBasicQueryCasting<T> = T | T[] | (T extends (infer U)[] ? U : any) | any;
|
|
36
|
+
export type Condition<T> = ApplyBasicQueryCasting<T> | QuerySelector<ApplyBasicQueryCasting<T>>;
|
|
37
|
+
export type ConditionOptions<T> = {
|
|
38
|
+
[P in keyof T]?: Condition<T[P]>;
|
|
39
|
+
} & RootQuerySelector<T>;
|
|
40
40
|
export type RootQuerySelector<T> = {
|
|
41
41
|
$and?: Array<ConditionOptions<T>>;
|
|
42
42
|
$or?: Array<ConditionOptions<T>>;
|
package/lib/util.d.ts
CHANGED
|
@@ -5,4 +5,3 @@ export declare function formatConditions(conditions: Record<string, any>): Inclu
|
|
|
5
5
|
export declare function formatOrder(sort: Record<string, any>): Order;
|
|
6
6
|
export declare function formatSelection(selection: Record<string, any>): string[];
|
|
7
7
|
export declare function formatParams(params: FindOptions): FindOptions;
|
|
8
|
-
export declare function getAvatarDir(srcDir: any): any;
|
package/lib/util.js
CHANGED
|
@@ -3,9 +3,8 @@ 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.
|
|
6
|
+
exports.formatParams = exports.formatSelection = exports.formatOrder = exports.formatConditions = exports.generateRandomString = exports.generateId = void 0;
|
|
7
7
|
const uuid_1 = require("uuid");
|
|
8
|
-
const path_1 = require("path");
|
|
9
8
|
const sequelize_1 = require("sequelize");
|
|
10
9
|
const lodash_isempty_1 = __importDefault(require("lodash.isempty"));
|
|
11
10
|
function generateId() {
|
|
@@ -116,8 +115,3 @@ function formatParams(params) {
|
|
|
116
115
|
return params;
|
|
117
116
|
}
|
|
118
117
|
exports.formatParams = formatParams;
|
|
119
|
-
// eslint-disable-next-line import/prefer-default-export
|
|
120
|
-
function getAvatarDir(srcDir) {
|
|
121
|
-
return srcDir.endsWith('/core') ? (0, path_1.join)((0, path_1.dirname)(srcDir), 'data/_abtnode') : srcDir;
|
|
122
|
-
}
|
|
123
|
-
exports.getAvatarDir = getAvatarDir;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@abtnode/models",
|
|
3
|
-
"version": "1.16.19-beta-
|
|
3
|
+
"version": "1.16.19-beta-7b2db880",
|
|
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,16 +32,13 @@
|
|
|
32
32
|
"url": "https://github.com/ArcBlock/blocklet-server/issues"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@abtnode/logger": "1.16.19-beta-
|
|
36
|
-
"@abtnode/types": "1.16.19-beta-
|
|
37
|
-
"@blocklet/constant": "1.16.19-beta-e6aac665",
|
|
38
|
-
"@nedb/core": "^2.1.5",
|
|
35
|
+
"@abtnode/logger": "1.16.19-beta-7b2db880",
|
|
36
|
+
"@abtnode/types": "1.16.19-beta-7b2db880",
|
|
39
37
|
"lodash.clonedeep": "^4.5.0",
|
|
40
|
-
"lodash.get": "^4.4.2",
|
|
41
38
|
"lodash.isempty": "^4.4.0",
|
|
42
|
-
"sequelize": "^6.
|
|
39
|
+
"sequelize": "^6.35.0",
|
|
43
40
|
"sqlite3": "^5.1.6",
|
|
44
|
-
"umzug": "3.
|
|
41
|
+
"umzug": "3.4.0",
|
|
45
42
|
"uuid": "^8.3.2"
|
|
46
43
|
},
|
|
47
44
|
"devDependencies": {
|
|
@@ -55,5 +52,5 @@
|
|
|
55
52
|
"typescript": "^5.0.4"
|
|
56
53
|
},
|
|
57
54
|
"resolutions": {},
|
|
58
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "598b576b97dd7accbfa1bb509b75e423ad27e5e2"
|
|
59
56
|
}
|