@nocobase/database 0.14.0-alpha.7 → 0.14.0-alpha.8
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/collection.js +6 -1
- package/lib/database.d.ts +6 -0
- package/lib/database.js +34 -9
- package/lib/mock-database.js +46 -2
- package/lib/model.js +3 -0
- package/lib/repository.js +1 -1
- package/lib/update-guard.js +3 -0
- package/package.json +7 -5
package/lib/collection.js
CHANGED
|
@@ -466,7 +466,12 @@ const _Collection = class _Collection extends import_events.EventEmitter {
|
|
|
466
466
|
}
|
|
467
467
|
});
|
|
468
468
|
for (const model of models) {
|
|
469
|
-
await model.sync(syncOptions
|
|
469
|
+
await model.sync(syncOptions || {
|
|
470
|
+
force: false,
|
|
471
|
+
alter: {
|
|
472
|
+
drop: false
|
|
473
|
+
}
|
|
474
|
+
});
|
|
470
475
|
}
|
|
471
476
|
}
|
|
472
477
|
isInherited() {
|
package/lib/database.d.ts
CHANGED
|
@@ -35,6 +35,8 @@ export interface IDatabaseOptions extends Options {
|
|
|
35
35
|
migrator?: any;
|
|
36
36
|
usingBigIntForId?: boolean;
|
|
37
37
|
underscored?: boolean;
|
|
38
|
+
customHooks?: any;
|
|
39
|
+
instanceId?: string;
|
|
38
40
|
}
|
|
39
41
|
export type DatabaseOptions = IDatabaseOptions;
|
|
40
42
|
interface RegisterOperatorsContext {
|
|
@@ -86,7 +88,9 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
86
88
|
pendingFields: Map<string, FieldTypes.RelationField[]>;
|
|
87
89
|
modelCollection: Map<ModelStatic<any>, Collection<any, any>>;
|
|
88
90
|
tableNameCollectionMap: Map<string, Collection<any, any>>;
|
|
91
|
+
context: any;
|
|
89
92
|
queryInterface: QueryInterface;
|
|
93
|
+
_instanceId: string;
|
|
90
94
|
utils: DatabaseUtils;
|
|
91
95
|
referenceMap: ReferencesMap;
|
|
92
96
|
inheritanceMap: InheritanceMap;
|
|
@@ -101,6 +105,8 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
101
105
|
collectionGroupManager: CollectionGroupManager;
|
|
102
106
|
emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
|
103
107
|
constructor(options: DatabaseOptions);
|
|
108
|
+
get instanceId(): string;
|
|
109
|
+
setContext(context: any): void;
|
|
104
110
|
setLogger(logger: Logger): void;
|
|
105
111
|
sequelizeOptions(options: any): any;
|
|
106
112
|
initListener(): void;
|
package/lib/database.js
CHANGED
|
@@ -41,6 +41,7 @@ var import_events = require("events");
|
|
|
41
41
|
var import_exponential_backoff = require("exponential-backoff");
|
|
42
42
|
var import_glob = __toESM(require("glob"));
|
|
43
43
|
var import_lodash = __toESM(require("lodash"));
|
|
44
|
+
var import_nanoid = require("nanoid");
|
|
44
45
|
var import_path = require("path");
|
|
45
46
|
var import_semver = __toESM(require("semver"));
|
|
46
47
|
var import_sequelize = require("sequelize");
|
|
@@ -59,10 +60,10 @@ var import_migration = require("./migration");
|
|
|
59
60
|
var import_model_hook = require("./model-hook");
|
|
60
61
|
var import_operators = __toESM(require("./operators"));
|
|
61
62
|
var import_query_interface_builder = __toESM(require("./query-interface/query-interface-builder"));
|
|
63
|
+
var import_sql_collection = require("./sql-collection/sql-collection");
|
|
62
64
|
var import_utils2 = require("./utils");
|
|
63
65
|
var import_value_parsers = require("./value-parsers");
|
|
64
66
|
var import_view_collection = require("./view-collection");
|
|
65
|
-
var import_sql_collection = require("./sql-collection/sql-collection");
|
|
66
67
|
const DialectVersionAccessors = {
|
|
67
68
|
sqlite: {
|
|
68
69
|
sql: "select sqlite_version() as version",
|
|
@@ -122,7 +123,9 @@ const _Database = class _Database extends import_events.EventEmitter {
|
|
|
122
123
|
pendingFields = /* @__PURE__ */ new Map();
|
|
123
124
|
modelCollection = /* @__PURE__ */ new Map();
|
|
124
125
|
tableNameCollectionMap = /* @__PURE__ */ new Map();
|
|
126
|
+
context = {};
|
|
125
127
|
queryInterface;
|
|
128
|
+
_instanceId;
|
|
126
129
|
utils = new import_database_utils.default(this);
|
|
127
130
|
referenceMap = new import_ReferencesMap.default();
|
|
128
131
|
inheritanceMap = new import_inherited_map.default();
|
|
@@ -144,6 +147,11 @@ const _Database = class _Database extends import_events.EventEmitter {
|
|
|
144
147
|
},
|
|
145
148
|
...import_lodash.default.clone(options)
|
|
146
149
|
};
|
|
150
|
+
if (!options.instanceId) {
|
|
151
|
+
this._instanceId = (0, import_nanoid.nanoid)();
|
|
152
|
+
} else {
|
|
153
|
+
this._instanceId = options.instanceId;
|
|
154
|
+
}
|
|
147
155
|
if (options.storage && options.storage !== ":memory:") {
|
|
148
156
|
if (!(0, import_path.isAbsolute)(options.storage)) {
|
|
149
157
|
opts.storage = (0, import_path.resolve)(process.cwd(), options.storage);
|
|
@@ -158,7 +166,8 @@ const _Database = class _Database extends import_events.EventEmitter {
|
|
|
158
166
|
require("pg").defaults.parseInt8 = true;
|
|
159
167
|
}
|
|
160
168
|
this.options = opts;
|
|
161
|
-
|
|
169
|
+
const sequelizeOptions = this.sequelizeOptions(this.options);
|
|
170
|
+
this.sequelize = new import_sequelize.Sequelize(sequelizeOptions);
|
|
162
171
|
this.queryInterface = (0, import_query_interface_builder.default)(this);
|
|
163
172
|
this.collections = /* @__PURE__ */ new Map();
|
|
164
173
|
this.modelHook = new import_model_hook.ModelHook(this);
|
|
@@ -216,16 +225,26 @@ const _Database = class _Database extends import_events.EventEmitter {
|
|
|
216
225
|
this.initListener();
|
|
217
226
|
(0, import_utils2.patchSequelizeQueryInterface)(this);
|
|
218
227
|
}
|
|
228
|
+
get instanceId() {
|
|
229
|
+
return this._instanceId;
|
|
230
|
+
}
|
|
231
|
+
setContext(context) {
|
|
232
|
+
this.context = context;
|
|
233
|
+
}
|
|
219
234
|
setLogger(logger) {
|
|
220
235
|
this.logger = logger;
|
|
221
236
|
}
|
|
222
237
|
sequelizeOptions(options) {
|
|
223
238
|
if (options.dialect === "postgres") {
|
|
224
|
-
options.hooks
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
239
|
+
if (!options.hooks) {
|
|
240
|
+
options.hooks = {};
|
|
241
|
+
}
|
|
242
|
+
if (!options.hooks["afterConnect"]) {
|
|
243
|
+
options.hooks["afterConnect"] = [];
|
|
244
|
+
}
|
|
245
|
+
options.hooks["afterConnect"].push(async (connection) => {
|
|
246
|
+
await connection.query("SET search_path TO public;");
|
|
247
|
+
});
|
|
229
248
|
}
|
|
230
249
|
return options;
|
|
231
250
|
}
|
|
@@ -444,7 +463,7 @@ const _Database = class _Database extends import_events.EventEmitter {
|
|
|
444
463
|
}
|
|
445
464
|
}
|
|
446
465
|
buildFieldValueParser(field, ctx) {
|
|
447
|
-
const Parser = this.fieldValueParsers.has(field.type) ? this.fieldValueParsers.get(field.type) : this.fieldValueParsers.get("default");
|
|
466
|
+
const Parser = field && this.fieldValueParsers.has(field.type) ? this.fieldValueParsers.get(field.type) : this.fieldValueParsers.get("default");
|
|
448
467
|
const parser = new Parser(field, ctx);
|
|
449
468
|
return parser;
|
|
450
469
|
}
|
|
@@ -577,10 +596,16 @@ const _Database = class _Database extends import_events.EventEmitter {
|
|
|
577
596
|
return this.sequelize.connectionManager.pool._draining;
|
|
578
597
|
}
|
|
579
598
|
async close() {
|
|
599
|
+
var _a, _b;
|
|
580
600
|
if (this.isSqliteMemory()) {
|
|
581
601
|
return;
|
|
582
602
|
}
|
|
583
|
-
|
|
603
|
+
await this.emitAsync("beforeClose", this);
|
|
604
|
+
const closeResult = this.sequelize.close();
|
|
605
|
+
if ((_b = (_a = this.options) == null ? void 0 : _a.customHooks) == null ? void 0 : _b["afterClose"]) {
|
|
606
|
+
await this.options.customHooks["afterClose"](this);
|
|
607
|
+
}
|
|
608
|
+
return closeResult;
|
|
584
609
|
}
|
|
585
610
|
on(event, listener) {
|
|
586
611
|
const type = this.modelHook.match(event);
|
package/lib/mock-database.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
2
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
7
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
8
|
var __export = (target, all) => {
|
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
var mock_database_exports = {};
|
|
20
30
|
__export(mock_database_exports, {
|
|
@@ -26,11 +36,13 @@ module.exports = __toCommonJS(mock_database_exports);
|
|
|
26
36
|
var import_utils = require("@nocobase/utils");
|
|
27
37
|
var import_path = require("path");
|
|
28
38
|
var import_database = require("./database");
|
|
39
|
+
var import_node_fetch = __toESM(require("node-fetch"));
|
|
40
|
+
var import_path2 = __toESM(require("path"));
|
|
41
|
+
var import_nanoid = require("nanoid");
|
|
29
42
|
const _MockDatabase = class _MockDatabase extends import_database.Database {
|
|
30
43
|
constructor(options) {
|
|
31
44
|
super({
|
|
32
45
|
storage: ":memory:",
|
|
33
|
-
tablePrefix: `mock_${(0, import_utils.uid)(6)}_`,
|
|
34
46
|
dialect: "sqlite",
|
|
35
47
|
...options
|
|
36
48
|
});
|
|
@@ -72,7 +84,39 @@ function customLogger(queryString, queryObject) {
|
|
|
72
84
|
__name(customLogger, "customLogger");
|
|
73
85
|
function mockDatabase(options = {}) {
|
|
74
86
|
const dbOptions = (0, import_utils.merge)(getConfigByEnv(), options);
|
|
75
|
-
|
|
87
|
+
if (process.env["DB_TEST_PREFIX"]) {
|
|
88
|
+
let configKey = "database";
|
|
89
|
+
if (dbOptions.dialect === "sqlite") {
|
|
90
|
+
configKey = "storage";
|
|
91
|
+
} else {
|
|
92
|
+
configKey = "database";
|
|
93
|
+
}
|
|
94
|
+
const shouldChange = /* @__PURE__ */ __name(() => {
|
|
95
|
+
if (dbOptions.dialect === "sqlite") {
|
|
96
|
+
return !dbOptions[configKey].includes(process.env["DB_TEST_PREFIX"]);
|
|
97
|
+
}
|
|
98
|
+
return !dbOptions[configKey].startsWith(process.env["DB_TEST_PREFIX"]);
|
|
99
|
+
}, "shouldChange");
|
|
100
|
+
if (dbOptions[configKey] && shouldChange()) {
|
|
101
|
+
const nanoid = (0, import_nanoid.customAlphabet)("1234567890abcdefghijklmnopqrstuvwxyz", 10);
|
|
102
|
+
const instanceId = `d_${nanoid()}`;
|
|
103
|
+
const databaseName = `${process.env["DB_TEST_PREFIX"]}_${instanceId}`;
|
|
104
|
+
if (dbOptions.dialect === "sqlite") {
|
|
105
|
+
dbOptions.storage = import_path2.default.resolve(import_path2.default.dirname(dbOptions.storage), databaseName);
|
|
106
|
+
} else {
|
|
107
|
+
dbOptions.database = databaseName;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (process.env["DB_TEST_DISTRIBUTOR_PORT"]) {
|
|
111
|
+
dbOptions.hooks = dbOptions.hooks || {};
|
|
112
|
+
dbOptions.hooks.beforeConnect = async (config) => {
|
|
113
|
+
const url = `http://127.0.0.1:${process.env["DB_TEST_DISTRIBUTOR_PORT"]}/acquire?via=${db.instanceId}&name=${config.database}`;
|
|
114
|
+
await (0, import_node_fetch.default)(url);
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const db = new MockDatabase(dbOptions);
|
|
119
|
+
return db;
|
|
76
120
|
}
|
|
77
121
|
__name(mockDatabase, "mockDatabase");
|
|
78
122
|
// Annotate the CommonJS export names for ESM import in node:
|
package/lib/model.js
CHANGED
|
@@ -141,6 +141,9 @@ const _Model = class _Model extends import_sequelize.Model {
|
|
|
141
141
|
if (this.collection.isView()) {
|
|
142
142
|
return;
|
|
143
143
|
}
|
|
144
|
+
if (this.collection.options.sync === false) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
144
147
|
const model = this;
|
|
145
148
|
const _schema = model._schema;
|
|
146
149
|
if (_schema && _schema != "public") {
|
package/lib/repository.js
CHANGED
|
@@ -361,7 +361,7 @@ const _Repository = class _Repository {
|
|
|
361
361
|
const { records } = options;
|
|
362
362
|
const instances = [];
|
|
363
363
|
for (const values of records) {
|
|
364
|
-
const instance = await this.create({ values, transaction: transaction2 });
|
|
364
|
+
const instance = await this.create({ ...options, values, transaction: transaction2 });
|
|
365
365
|
instances.push(instance);
|
|
366
366
|
}
|
|
367
367
|
return instances;
|
package/lib/update-guard.js
CHANGED
|
@@ -105,6 +105,9 @@ const _UpdateGuard = class _UpdateGuard {
|
|
|
105
105
|
* @param values
|
|
106
106
|
*/
|
|
107
107
|
sanitize(values) {
|
|
108
|
+
if (values === null || values === void 0) {
|
|
109
|
+
return values;
|
|
110
|
+
}
|
|
108
111
|
values = import_lodash.default.clone(values);
|
|
109
112
|
if (!this.model) {
|
|
110
113
|
throw new Error("please set model first");
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/database",
|
|
3
|
-
"version": "0.14.0-alpha.
|
|
3
|
+
"version": "0.14.0-alpha.8",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@nocobase/logger": "0.14.0-alpha.
|
|
10
|
-
"@nocobase/utils": "0.14.0-alpha.
|
|
9
|
+
"@nocobase/logger": "0.14.0-alpha.8",
|
|
10
|
+
"@nocobase/utils": "0.14.0-alpha.8",
|
|
11
11
|
"async-mutex": "^0.3.2",
|
|
12
12
|
"cron-parser": "4.4.0",
|
|
13
13
|
"dayjs": "^1.11.8",
|
|
@@ -18,18 +18,20 @@
|
|
|
18
18
|
"glob": "^7.1.6",
|
|
19
19
|
"graphlib": "^2.1.8",
|
|
20
20
|
"mathjs": "^10.6.1",
|
|
21
|
+
"nanoid": "^3.3.6",
|
|
21
22
|
"qs": "^6.11.2",
|
|
22
23
|
"semver": "^7.3.7",
|
|
23
24
|
"sequelize": "^6.26.0",
|
|
24
25
|
"umzug": "^3.1.1"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
|
-
"@types/glob": "^7.2.0"
|
|
28
|
+
"@types/glob": "^7.2.0",
|
|
29
|
+
"node-fetch": "^2.6.7"
|
|
28
30
|
},
|
|
29
31
|
"repository": {
|
|
30
32
|
"type": "git",
|
|
31
33
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
32
34
|
"directory": "packages/database"
|
|
33
35
|
},
|
|
34
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "59c82fef6e34707802b5841f5ec4d9b3b6b68abb"
|
|
35
37
|
}
|