@nocobase/database 1.3.44-beta → 1.4.0-alpha.0
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.d.ts +5 -2
- package/lib/collection.js +70 -2
- package/lib/database.d.ts +13 -23
- package/lib/database.js +28 -42
- package/lib/dialects/base-dialect.d.ts +20 -0
- package/lib/dialects/base-dialect.js +75 -0
- package/lib/dialects/index.d.ts +9 -0
- package/lib/dialects/index.js +30 -0
- package/lib/dialects/mariadb-dialect.d.ts +17 -0
- package/lib/dialects/mariadb-dialect.js +54 -0
- package/lib/dialects/mysql-dialect.d.ts +17 -0
- package/lib/dialects/mysql-dialect.js +54 -0
- package/lib/dialects/postgres-dialect.d.ts +18 -0
- package/lib/dialects/postgres-dialect.js +77 -0
- package/lib/dialects/sqlite-dialect.d.ts +17 -0
- package/lib/dialects/sqlite-dialect.js +51 -0
- package/lib/features/referential-integrity-check.js +1 -1
- package/lib/fields/date-field.d.ts +7 -2
- package/lib/fields/date-field.js +89 -0
- package/lib/fields/date-only-field.d.ts +15 -0
- package/lib/fields/date-only-field.js +45 -0
- package/lib/fields/datetime-field.d.ts +15 -0
- package/lib/fields/datetime-field.js +41 -0
- package/lib/fields/datetime-no-tz-field.d.ts +24 -0
- package/lib/fields/datetime-no-tz-field.js +128 -0
- package/lib/fields/datetime-tz-field.d.ts +15 -0
- package/lib/fields/datetime-tz-field.js +41 -0
- package/lib/fields/field.d.ts +1 -1
- package/lib/fields/field.js +3 -2
- package/lib/fields/index.d.ts +10 -1
- package/lib/fields/index.js +11 -1
- package/lib/fields/unix-timestamp-field.d.ts +22 -0
- package/lib/fields/unix-timestamp-field.js +94 -0
- package/lib/helpers.d.ts +2 -1
- package/lib/helpers.js +16 -49
- package/lib/index.d.ts +1 -0
- package/lib/index.js +3 -1
- package/lib/interfaces/boolean-interface.js +5 -1
- package/lib/interfaces/multiple-select-interface.js +7 -1
- package/lib/interfaces/select-interface.js +7 -1
- package/lib/interfaces/to-one-interface.js +3 -3
- package/lib/model.d.ts +1 -0
- package/lib/model.js +12 -0
- package/lib/operators/date.js +66 -24
- package/lib/options-parser.d.ts +1 -0
- package/lib/options-parser.js +36 -10
- package/lib/query-interface/query-interface-builder.js +3 -0
- package/lib/relation-repository/hasmany-repository.js +8 -11
- package/lib/relation-repository/multiple-relation-repository.d.ts +1 -0
- package/lib/relation-repository/multiple-relation-repository.js +11 -3
- package/lib/relation-repository/relation-repository.d.ts +6 -3
- package/lib/relation-repository/relation-repository.js +27 -4
- package/lib/repository.d.ts +5 -2
- package/lib/repository.js +27 -16
- package/lib/update-associations.d.ts +2 -1
- package/lib/update-associations.js +4 -3
- package/lib/view/field-type-map.d.ts +2 -2
- package/lib/view/field-type-map.js +17 -17
- package/package.json +4 -4
package/lib/collection.d.ts
CHANGED
|
@@ -38,7 +38,7 @@ export interface CollectionOptions extends Omit<ModelOptions, 'name' | 'hooks'>
|
|
|
38
38
|
inherits?: string[] | string;
|
|
39
39
|
viewName?: string;
|
|
40
40
|
writableView?: boolean;
|
|
41
|
-
filterTargetKey?: string;
|
|
41
|
+
filterTargetKey?: string | string[];
|
|
42
42
|
fields?: FieldOptions[];
|
|
43
43
|
model?: string | ModelStatic<Model>;
|
|
44
44
|
repository?: string | RepositoryType;
|
|
@@ -77,13 +77,14 @@ export declare class Collection<TModelAttributes extends {} = any, TCreationAttr
|
|
|
77
77
|
model: ModelStatic<Model>;
|
|
78
78
|
repository: Repository<TModelAttributes, TCreationAttributes>;
|
|
79
79
|
constructor(options: CollectionOptions, context: CollectionContext);
|
|
80
|
-
get filterTargetKey(): string;
|
|
80
|
+
get filterTargetKey(): string | string[];
|
|
81
81
|
get name(): string;
|
|
82
82
|
get origin(): string;
|
|
83
83
|
get titleField(): string;
|
|
84
84
|
get db(): Database;
|
|
85
85
|
get treeParentField(): BelongsToField | null;
|
|
86
86
|
get treeChildrenField(): HasManyField | null;
|
|
87
|
+
isMultiFilterTargetKey(): boolean;
|
|
87
88
|
tableName(): any;
|
|
88
89
|
/**
|
|
89
90
|
* @internal
|
|
@@ -131,6 +132,8 @@ export declare class Collection<TModelAttributes extends {} = any, TCreationAttr
|
|
|
131
132
|
tableNameAsString(options?: {
|
|
132
133
|
ignorePublicSchema: boolean;
|
|
133
134
|
}): any;
|
|
135
|
+
getRealTableName(quoted?: boolean): any;
|
|
136
|
+
getRealFieldName(name: string, quoted?: boolean): string;
|
|
134
137
|
getTableNameWithSchemaAsString(): string;
|
|
135
138
|
quotedTableName(): any;
|
|
136
139
|
collectionSchema(): string;
|
package/lib/collection.js
CHANGED
|
@@ -51,11 +51,11 @@ module.exports = __toCommonJS(collection_exports);
|
|
|
51
51
|
var import_deepmerge = __toESM(require("deepmerge"));
|
|
52
52
|
var import_events = require("events");
|
|
53
53
|
var import_lodash = __toESM(require("lodash"));
|
|
54
|
+
var import_safe_json_stringify = __toESM(require("safe-json-stringify"));
|
|
54
55
|
var import_sequelize = require("sequelize");
|
|
55
56
|
var import_model = require("./model");
|
|
56
57
|
var import_repository = require("./repository");
|
|
57
58
|
var import_utils = require("./utils");
|
|
58
|
-
var import_safe_json_stringify = __toESM(require("safe-json-stringify"));
|
|
59
59
|
function EnsureAtomicity(target, propertyKey, descriptor) {
|
|
60
60
|
const originalMethod = descriptor.value;
|
|
61
61
|
descriptor.value = function(...args) {
|
|
@@ -101,6 +101,7 @@ const _Collection = class _Collection extends import_events.EventEmitter {
|
|
|
101
101
|
this.bindFieldEventListener();
|
|
102
102
|
this.modelInit();
|
|
103
103
|
this.db.modelCollection.set(this.model, this);
|
|
104
|
+
this.db.modelNameCollectionMap.set(this.model.name, this);
|
|
104
105
|
this.db.tableNameCollectionMap.set(this.getTableNameWithSchemaAsString(), this);
|
|
105
106
|
if (!options.inherits) {
|
|
106
107
|
this.setFields(options.fields);
|
|
@@ -111,6 +112,9 @@ const _Collection = class _Collection extends import_events.EventEmitter {
|
|
|
111
112
|
get filterTargetKey() {
|
|
112
113
|
var _a;
|
|
113
114
|
const targetKey = (_a = this.options) == null ? void 0 : _a.filterTargetKey;
|
|
115
|
+
if (Array.isArray(targetKey)) {
|
|
116
|
+
return targetKey;
|
|
117
|
+
}
|
|
114
118
|
if (targetKey && this.model.getAttributes()[targetKey]) {
|
|
115
119
|
return targetKey;
|
|
116
120
|
}
|
|
@@ -145,6 +149,9 @@ const _Collection = class _Collection extends import_events.EventEmitter {
|
|
|
145
149
|
}
|
|
146
150
|
}
|
|
147
151
|
}
|
|
152
|
+
isMultiFilterTargetKey() {
|
|
153
|
+
return Array.isArray(this.filterTargetKey) && this.filterTargetKey.length > 1;
|
|
154
|
+
}
|
|
148
155
|
tableName() {
|
|
149
156
|
const { name, tableName } = this.options;
|
|
150
157
|
const tName = tableName || name;
|
|
@@ -173,8 +180,57 @@ const _Collection = class _Collection extends import_events.EventEmitter {
|
|
|
173
180
|
} else if (model) {
|
|
174
181
|
M = model;
|
|
175
182
|
}
|
|
183
|
+
const collection = this;
|
|
176
184
|
this.model = class extends M {
|
|
177
185
|
};
|
|
186
|
+
Object.defineProperty(this.model, "primaryKeyAttribute", {
|
|
187
|
+
get: function() {
|
|
188
|
+
const singleFilterTargetKey = (() => {
|
|
189
|
+
if (!collection.options.filterTargetKey) {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
if (Array.isArray(collection.options.filterTargetKey) && collection.options.filterTargetKey.length === 1) {
|
|
193
|
+
return collection.options.filterTargetKey[0];
|
|
194
|
+
}
|
|
195
|
+
return collection.options.filterTargetKey;
|
|
196
|
+
})();
|
|
197
|
+
if (!this._primaryKeyAttribute && singleFilterTargetKey && collection.getField(singleFilterTargetKey)) {
|
|
198
|
+
return singleFilterTargetKey;
|
|
199
|
+
}
|
|
200
|
+
return this._primaryKeyAttribute;
|
|
201
|
+
}.bind(this.model),
|
|
202
|
+
set(value) {
|
|
203
|
+
this._primaryKeyAttribute = value;
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
Object.defineProperty(this.model, "primaryKeyAttributes", {
|
|
207
|
+
get: function() {
|
|
208
|
+
if (Array.isArray(this._primaryKeyAttributes) && this._primaryKeyAttributes.length) {
|
|
209
|
+
return this._primaryKeyAttributes;
|
|
210
|
+
}
|
|
211
|
+
if (collection.options.filterTargetKey) {
|
|
212
|
+
const fields = import_lodash.default.castArray(collection.options.filterTargetKey);
|
|
213
|
+
if (fields.every((field) => collection.getField(field))) {
|
|
214
|
+
return fields;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return this._primaryKeyAttributes;
|
|
218
|
+
}.bind(this.model),
|
|
219
|
+
set(value) {
|
|
220
|
+
this._primaryKeyAttributes = value;
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
Object.defineProperty(this.model, "primaryKeyField", {
|
|
224
|
+
get: function() {
|
|
225
|
+
if (this.primaryKeyAttribute) {
|
|
226
|
+
return this.rawAttributes[this.primaryKeyAttribute].field || this.primaryKeyAttribute;
|
|
227
|
+
}
|
|
228
|
+
return null;
|
|
229
|
+
}.bind(this.model),
|
|
230
|
+
set(val) {
|
|
231
|
+
this._primaryKeyField = val;
|
|
232
|
+
}
|
|
233
|
+
});
|
|
178
234
|
this.model.init(null, this.sequelizeModelOptions());
|
|
179
235
|
this.model.options.modelName = this.options.name;
|
|
180
236
|
if (!autoGenId) {
|
|
@@ -426,6 +482,9 @@ const _Collection = class _Collection extends import_events.EventEmitter {
|
|
|
426
482
|
updateOptions(options, mergeOptions) {
|
|
427
483
|
let newOptions = import_lodash.default.cloneDeep(options);
|
|
428
484
|
newOptions = (0, import_deepmerge.default)(this.options, newOptions, mergeOptions);
|
|
485
|
+
if (options.filterTargetKey) {
|
|
486
|
+
newOptions.filterTargetKey = options.filterTargetKey;
|
|
487
|
+
}
|
|
429
488
|
this.context.database.emit("beforeUpdateCollection", this, newOptions);
|
|
430
489
|
this.options = newOptions;
|
|
431
490
|
this.setFields(options.fields, false);
|
|
@@ -591,6 +650,14 @@ const _Collection = class _Collection extends import_events.EventEmitter {
|
|
|
591
650
|
}
|
|
592
651
|
return `${schema}.${tableName}`;
|
|
593
652
|
}
|
|
653
|
+
getRealTableName(quoted = false) {
|
|
654
|
+
const realname = this.tableNameAsString();
|
|
655
|
+
return !quoted ? realname : this.db.sequelize.getQueryInterface().quoteIdentifiers(realname);
|
|
656
|
+
}
|
|
657
|
+
getRealFieldName(name, quoted = false) {
|
|
658
|
+
const realname = this.model.getAttributes()[name].field;
|
|
659
|
+
return !quoted ? name : this.db.sequelize.getQueryInterface().quoteIdentifier(realname);
|
|
660
|
+
}
|
|
594
661
|
getTableNameWithSchemaAsString() {
|
|
595
662
|
const tableName = this.model.tableName;
|
|
596
663
|
if (this.collectionSchema() && this.db.inDialect("postgres")) {
|
|
@@ -624,12 +691,13 @@ const _Collection = class _Collection extends import_events.EventEmitter {
|
|
|
624
691
|
}
|
|
625
692
|
sequelizeModelOptions() {
|
|
626
693
|
const { name } = this.options;
|
|
627
|
-
|
|
694
|
+
const attr = {
|
|
628
695
|
...import_lodash.default.omit(this.options, ["name", "fields", "model", "targetKey"]),
|
|
629
696
|
modelName: name,
|
|
630
697
|
sequelize: this.context.database.sequelize,
|
|
631
698
|
tableName: this.tableName()
|
|
632
699
|
};
|
|
700
|
+
return attr;
|
|
633
701
|
}
|
|
634
702
|
bindFieldEventListener() {
|
|
635
703
|
this.on("field.afterAdd", (field) => {
|
package/lib/database.d.ts
CHANGED
|
@@ -28,9 +28,10 @@ import { Model } from './model';
|
|
|
28
28
|
import { ModelHook } from './model-hook';
|
|
29
29
|
import QueryInterface from './query-interface/query-interface';
|
|
30
30
|
import { RelationRepository } from './relation-repository/relation-repository';
|
|
31
|
-
import { Repository } from './repository';
|
|
31
|
+
import { Repository, TargetKey } from './repository';
|
|
32
32
|
import { AfterDefineCollectionListener, BeforeDefineCollectionListener, CreateListener, CreateWithAssociationsListener, DatabaseAfterDefineCollectionEventType, DatabaseAfterRemoveCollectionEventType, DatabaseBeforeDefineCollectionEventType, DatabaseBeforeRemoveCollectionEventType, DestroyListener, EventType, ModelCreateEventTypes, ModelCreateWithAssociationsEventTypes, ModelDestroyEventTypes, ModelSaveEventTypes, ModelSaveWithAssociationsEventTypes, ModelUpdateEventTypes, ModelUpdateWithAssociationsEventTypes, ModelValidateEventTypes, RemoveCollectionListener, SaveListener, SaveWithAssociationsListener, SyncListener, UpdateListener, UpdateWithAssociationsListener, ValidateListener } from './types';
|
|
33
33
|
import { BaseValueParser } from './value-parsers';
|
|
34
|
+
import { BaseDialect } from './dialects/base-dialect';
|
|
34
35
|
export type MergeOptions = merge.Options;
|
|
35
36
|
export interface PendingOptions {
|
|
36
37
|
field: RelationField;
|
|
@@ -65,25 +66,8 @@ export type AddMigrationsOptions = {
|
|
|
65
66
|
directory: string;
|
|
66
67
|
};
|
|
67
68
|
type OperatorFunc = (value: any, ctx?: RegisterOperatorsContext) => any;
|
|
68
|
-
export declare const DialectVersionAccessors: {
|
|
69
|
-
sqlite: {
|
|
70
|
-
sql: string;
|
|
71
|
-
get: (v: string) => string;
|
|
72
|
-
};
|
|
73
|
-
mysql: {
|
|
74
|
-
sql: string;
|
|
75
|
-
get: (v: string) => string;
|
|
76
|
-
};
|
|
77
|
-
mariadb: {
|
|
78
|
-
sql: string;
|
|
79
|
-
get: (v: string) => string;
|
|
80
|
-
};
|
|
81
|
-
postgres: {
|
|
82
|
-
sql: string;
|
|
83
|
-
get: (v: string) => string;
|
|
84
|
-
};
|
|
85
|
-
};
|
|
86
69
|
export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
70
|
+
static dialects: Map<string, typeof BaseDialect>;
|
|
87
71
|
sequelize: Sequelize;
|
|
88
72
|
migrator: Umzug;
|
|
89
73
|
migrations: Migrations;
|
|
@@ -96,6 +80,7 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
96
80
|
collections: Map<string, Collection<any, any>>;
|
|
97
81
|
pendingFields: Map<string, FieldTypes.RelationField[]>;
|
|
98
82
|
modelCollection: Map<ModelStatic<any>, Collection<any, any>>;
|
|
83
|
+
modelNameCollectionMap: Map<string, Collection<any, any>>;
|
|
99
84
|
tableNameCollectionMap: Map<string, Collection<any, any>>;
|
|
100
85
|
context: any;
|
|
101
86
|
queryInterface: QueryInterface;
|
|
@@ -111,7 +96,10 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
111
96
|
logger: Logger;
|
|
112
97
|
interfaceManager: InterfaceManager;
|
|
113
98
|
collectionFactory: CollectionFactory;
|
|
99
|
+
dialect: BaseDialect;
|
|
114
100
|
emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
|
101
|
+
static registerDialect(dialect: typeof BaseDialect): void;
|
|
102
|
+
static getDialect(name: string): typeof BaseDialect;
|
|
115
103
|
constructor(options: DatabaseOptions);
|
|
116
104
|
_instanceId: string;
|
|
117
105
|
get instanceId(): string;
|
|
@@ -128,7 +116,7 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
128
116
|
/**
|
|
129
117
|
* @internal
|
|
130
118
|
*/
|
|
131
|
-
sequelizeOptions(options: any):
|
|
119
|
+
sequelizeOptions(options: any): IDatabaseOptions;
|
|
132
120
|
/**
|
|
133
121
|
* @internal
|
|
134
122
|
*/
|
|
@@ -137,6 +125,7 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
137
125
|
addMigrations(options: AddMigrationsOptions): void;
|
|
138
126
|
inDialect(...dialect: string[]): boolean;
|
|
139
127
|
isMySQLCompatibleDialect(): boolean;
|
|
128
|
+
isPostgresCompatibleDialect(): boolean;
|
|
140
129
|
/**
|
|
141
130
|
* Add collection to database
|
|
142
131
|
* @param options
|
|
@@ -144,6 +133,7 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
144
133
|
collection<Attributes = any, CreateAttributes = Attributes>(options: CollectionOptions): Collection<Attributes, CreateAttributes>;
|
|
145
134
|
getTablePrefix(): string;
|
|
146
135
|
getFieldByPath(path: string): any;
|
|
136
|
+
getCollectionByModelName(name: string): Collection<any, any>;
|
|
147
137
|
/**
|
|
148
138
|
* get exists collection by its name
|
|
149
139
|
* @param name
|
|
@@ -153,8 +143,8 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
153
143
|
removeCollection(name: string): Collection<any, any>;
|
|
154
144
|
getModel<M extends Model>(name: string): ModelStatic<M>;
|
|
155
145
|
getRepository<R extends Repository>(name: string): R;
|
|
156
|
-
getRepository<R extends RelationRepository>(name: string, relationId:
|
|
157
|
-
getRepository<R extends ArrayFieldRepository>(name: string, relationId:
|
|
146
|
+
getRepository<R extends RelationRepository>(name: string, relationId: TargetKey): R;
|
|
147
|
+
getRepository<R extends ArrayFieldRepository>(name: string, relationId: TargetKey): R;
|
|
158
148
|
/**
|
|
159
149
|
* @internal
|
|
160
150
|
*/
|
|
@@ -187,7 +177,7 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
|
|
|
187
177
|
/**
|
|
188
178
|
* @internal
|
|
189
179
|
*/
|
|
190
|
-
checkVersion(): Promise<
|
|
180
|
+
checkVersion(): Promise<true | void>;
|
|
191
181
|
/**
|
|
192
182
|
* @internal
|
|
193
183
|
*/
|
package/lib/database.js
CHANGED
|
@@ -13,6 +13,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
13
13
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
14
|
var __getProtoOf = Object.getPrototypeOf;
|
|
15
15
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
16
17
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
17
18
|
var __export = (target, all) => {
|
|
18
19
|
for (var name in all)
|
|
@@ -35,10 +36,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
35
36
|
mod
|
|
36
37
|
));
|
|
37
38
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
39
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
38
40
|
var database_exports = {};
|
|
39
41
|
__export(database_exports, {
|
|
40
42
|
Database: () => Database,
|
|
41
|
-
DialectVersionAccessors: () => DialectVersionAccessors,
|
|
42
43
|
default: () => database_default,
|
|
43
44
|
defineCollection: () => defineCollection,
|
|
44
45
|
extend: () => extend,
|
|
@@ -55,7 +56,6 @@ var import_lodash = __toESM(require("lodash"));
|
|
|
55
56
|
var import_nanoid = require("nanoid");
|
|
56
57
|
var import_path = require("path");
|
|
57
58
|
var import_safe_json_stringify = __toESM(require("safe-json-stringify"));
|
|
58
|
-
var import_semver = __toESM(require("semver"));
|
|
59
59
|
var import_sequelize = require("sequelize");
|
|
60
60
|
var import_umzug = require("umzug");
|
|
61
61
|
var import_collection_factory = require("./collection-factory");
|
|
@@ -77,33 +77,6 @@ var import_query_interface_builder = __toESM(require("./query-interface/query-in
|
|
|
77
77
|
var import_utils3 = require("./utils");
|
|
78
78
|
var import_value_parsers = require("./value-parsers");
|
|
79
79
|
var import_view_collection = require("./view-collection");
|
|
80
|
-
const DialectVersionAccessors = {
|
|
81
|
-
sqlite: {
|
|
82
|
-
sql: "select sqlite_version() as version",
|
|
83
|
-
get: /* @__PURE__ */ __name((v) => v, "get")
|
|
84
|
-
},
|
|
85
|
-
mysql: {
|
|
86
|
-
sql: "select version() as version",
|
|
87
|
-
get: /* @__PURE__ */ __name((v) => {
|
|
88
|
-
const m = /([\d+.]+)/.exec(v);
|
|
89
|
-
return m[0];
|
|
90
|
-
}, "get")
|
|
91
|
-
},
|
|
92
|
-
mariadb: {
|
|
93
|
-
sql: "select version() as version",
|
|
94
|
-
get: /* @__PURE__ */ __name((v) => {
|
|
95
|
-
const m = /([\d+.]+)/.exec(v);
|
|
96
|
-
return m[0];
|
|
97
|
-
}, "get")
|
|
98
|
-
},
|
|
99
|
-
postgres: {
|
|
100
|
-
sql: "select version() as version",
|
|
101
|
-
get: /* @__PURE__ */ __name((v) => {
|
|
102
|
-
const m = /([\d+.]+)/.exec(v);
|
|
103
|
-
return import_semver.default.minVersion(m[0]).version;
|
|
104
|
-
}, "get")
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
80
|
const _Database = class _Database extends import_events.EventEmitter {
|
|
108
81
|
sequelize;
|
|
109
82
|
migrator;
|
|
@@ -117,6 +90,7 @@ const _Database = class _Database extends import_events.EventEmitter {
|
|
|
117
90
|
collections = /* @__PURE__ */ new Map();
|
|
118
91
|
pendingFields = /* @__PURE__ */ new Map();
|
|
119
92
|
modelCollection = /* @__PURE__ */ new Map();
|
|
93
|
+
modelNameCollectionMap = /* @__PURE__ */ new Map();
|
|
120
94
|
tableNameCollectionMap = /* @__PURE__ */ new Map();
|
|
121
95
|
context = {};
|
|
122
96
|
queryInterface;
|
|
@@ -129,8 +103,20 @@ const _Database = class _Database extends import_events.EventEmitter {
|
|
|
129
103
|
logger;
|
|
130
104
|
interfaceManager = new import_interface_manager.InterfaceManager(this);
|
|
131
105
|
collectionFactory = new import_collection_factory.CollectionFactory(this);
|
|
106
|
+
dialect;
|
|
107
|
+
static registerDialect(dialect) {
|
|
108
|
+
this.dialects.set(dialect.dialectName, dialect);
|
|
109
|
+
}
|
|
110
|
+
static getDialect(name) {
|
|
111
|
+
return this.dialects.get(name);
|
|
112
|
+
}
|
|
132
113
|
constructor(options) {
|
|
133
114
|
super();
|
|
115
|
+
const dialectClass = _Database.getDialect(options.dialect);
|
|
116
|
+
if (!dialectClass) {
|
|
117
|
+
throw new Error(`unsupported dialect ${options.dialect}`);
|
|
118
|
+
}
|
|
119
|
+
this.dialect = new dialectClass();
|
|
134
120
|
const opts = {
|
|
135
121
|
sync: {
|
|
136
122
|
alter: {
|
|
@@ -159,6 +145,7 @@ const _Database = class _Database extends import_events.EventEmitter {
|
|
|
159
145
|
opts.storage = (0, import_path.resolve)(process.cwd(), options.storage);
|
|
160
146
|
}
|
|
161
147
|
}
|
|
148
|
+
opts.rawTimezone = opts.timezone;
|
|
162
149
|
if (options.dialect === "sqlite") {
|
|
163
150
|
delete opts.timezone;
|
|
164
151
|
} else if (!opts.timezone) {
|
|
@@ -173,6 +160,9 @@ const _Database = class _Database extends import_events.EventEmitter {
|
|
|
173
160
|
return val;
|
|
174
161
|
});
|
|
175
162
|
}
|
|
163
|
+
if (options.logging && process.env["DB_SQL_BENCHMARK"] == "true") {
|
|
164
|
+
opts.benchmark = true;
|
|
165
|
+
}
|
|
176
166
|
this.options = opts;
|
|
177
167
|
this.logger.debug(
|
|
178
168
|
`create database instance: ${(0, import_safe_json_stringify.default)(
|
|
@@ -284,18 +274,7 @@ const _Database = class _Database extends import_events.EventEmitter {
|
|
|
284
274
|
* @internal
|
|
285
275
|
*/
|
|
286
276
|
sequelizeOptions(options) {
|
|
287
|
-
|
|
288
|
-
if (!options.hooks) {
|
|
289
|
-
options.hooks = {};
|
|
290
|
-
}
|
|
291
|
-
if (!options.hooks["afterConnect"]) {
|
|
292
|
-
options.hooks["afterConnect"] = [];
|
|
293
|
-
}
|
|
294
|
-
options.hooks["afterConnect"].push(async (connection) => {
|
|
295
|
-
await connection.query("SET search_path TO public;");
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
return options;
|
|
277
|
+
return this.dialect.getSequelizeOptions(options);
|
|
299
278
|
}
|
|
300
279
|
/**
|
|
301
280
|
* @internal
|
|
@@ -413,6 +392,9 @@ const _Database = class _Database extends import_events.EventEmitter {
|
|
|
413
392
|
isMySQLCompatibleDialect() {
|
|
414
393
|
return this.inDialect("mysql", "mariadb");
|
|
415
394
|
}
|
|
395
|
+
isPostgresCompatibleDialect() {
|
|
396
|
+
return this.inDialect("postgres");
|
|
397
|
+
}
|
|
416
398
|
/**
|
|
417
399
|
* Add collection to database
|
|
418
400
|
* @param options
|
|
@@ -452,6 +434,9 @@ const _Database = class _Database extends import_events.EventEmitter {
|
|
|
452
434
|
}
|
|
453
435
|
return field;
|
|
454
436
|
}
|
|
437
|
+
getCollectionByModelName(name) {
|
|
438
|
+
return this.modelNameCollectionMap.get(name);
|
|
439
|
+
}
|
|
455
440
|
/**
|
|
456
441
|
* get exists collection by its name
|
|
457
442
|
* @param name
|
|
@@ -768,6 +753,7 @@ const _Database = class _Database extends import_events.EventEmitter {
|
|
|
768
753
|
}
|
|
769
754
|
};
|
|
770
755
|
__name(_Database, "Database");
|
|
756
|
+
__publicField(_Database, "dialects", /* @__PURE__ */ new Map());
|
|
771
757
|
let Database = _Database;
|
|
772
758
|
function extendCollection(collectionOptions, mergeOptions) {
|
|
773
759
|
return {
|
|
@@ -782,11 +768,11 @@ const defineCollection = /* @__PURE__ */ __name((collectionOptions) => {
|
|
|
782
768
|
return collectionOptions;
|
|
783
769
|
}, "defineCollection");
|
|
784
770
|
(0, import_utils.applyMixins)(Database, [import_utils.AsyncEmitter]);
|
|
771
|
+
(0, import_helpers.registerDialects)();
|
|
785
772
|
var database_default = Database;
|
|
786
773
|
// Annotate the CommonJS export names for ESM import in node:
|
|
787
774
|
0 && (module.exports = {
|
|
788
775
|
Database,
|
|
789
|
-
DialectVersionAccessors,
|
|
790
776
|
defineCollection,
|
|
791
777
|
extend,
|
|
792
778
|
extendCollection
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import { Database, DatabaseOptions } from '../database';
|
|
10
|
+
export interface DialectVersionGuard {
|
|
11
|
+
sql: string;
|
|
12
|
+
get: (v: string) => string;
|
|
13
|
+
version: string;
|
|
14
|
+
}
|
|
15
|
+
export declare abstract class BaseDialect {
|
|
16
|
+
static dialectName: string;
|
|
17
|
+
getSequelizeOptions(options: DatabaseOptions): import("../database").IDatabaseOptions;
|
|
18
|
+
checkDatabaseVersion(db: Database): Promise<boolean>;
|
|
19
|
+
getVersionGuard(): DialectVersionGuard;
|
|
20
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __create = Object.create;
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
13
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
15
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
17
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
18
|
+
var __export = (target, all) => {
|
|
19
|
+
for (var name in all)
|
|
20
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
21
|
+
};
|
|
22
|
+
var __copyProps = (to, from, except, desc) => {
|
|
23
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
24
|
+
for (let key of __getOwnPropNames(from))
|
|
25
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
26
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
27
|
+
}
|
|
28
|
+
return to;
|
|
29
|
+
};
|
|
30
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
31
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
32
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
33
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
34
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
35
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
36
|
+
mod
|
|
37
|
+
));
|
|
38
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
39
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
40
|
+
var base_dialect_exports = {};
|
|
41
|
+
__export(base_dialect_exports, {
|
|
42
|
+
BaseDialect: () => BaseDialect
|
|
43
|
+
});
|
|
44
|
+
module.exports = __toCommonJS(base_dialect_exports);
|
|
45
|
+
var import_semver = __toESM(require("semver"));
|
|
46
|
+
const _BaseDialect = class _BaseDialect {
|
|
47
|
+
getSequelizeOptions(options) {
|
|
48
|
+
return options;
|
|
49
|
+
}
|
|
50
|
+
async checkDatabaseVersion(db) {
|
|
51
|
+
var _a;
|
|
52
|
+
const versionGuard = this.getVersionGuard();
|
|
53
|
+
const result = await db.sequelize.query(versionGuard.sql, {
|
|
54
|
+
type: "SELECT"
|
|
55
|
+
});
|
|
56
|
+
const version = versionGuard.get((_a = result == null ? void 0 : result[0]) == null ? void 0 : _a.version);
|
|
57
|
+
const versionResult = import_semver.default.satisfies(version, versionGuard.version);
|
|
58
|
+
if (!versionResult) {
|
|
59
|
+
throw new Error(
|
|
60
|
+
`to use ${this.constructor.dialectName}, please ensure the version is ${versionGuard.version}, current version is ${version}`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
getVersionGuard() {
|
|
66
|
+
throw new Error("not implemented");
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
__name(_BaseDialect, "BaseDialect");
|
|
70
|
+
__publicField(_BaseDialect, "dialectName");
|
|
71
|
+
let BaseDialect = _BaseDialect;
|
|
72
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
73
|
+
0 && (module.exports = {
|
|
74
|
+
BaseDialect
|
|
75
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
export * from './base-dialect';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
+
for (let key of __getOwnPropNames(from))
|
|
17
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var dialects_exports = {};
|
|
25
|
+
module.exports = __toCommonJS(dialects_exports);
|
|
26
|
+
__reExport(dialects_exports, require("./base-dialect"), module.exports);
|
|
27
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
28
|
+
0 && (module.exports = {
|
|
29
|
+
...require("./base-dialect")
|
|
30
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import { BaseDialect } from './base-dialect';
|
|
10
|
+
export declare class MariadbDialect extends BaseDialect {
|
|
11
|
+
static dialectName: string;
|
|
12
|
+
getVersionGuard(): {
|
|
13
|
+
sql: string;
|
|
14
|
+
get: (v: string) => string;
|
|
15
|
+
version: string;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
15
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
16
|
+
var __export = (target, all) => {
|
|
17
|
+
for (var name in all)
|
|
18
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
19
|
+
};
|
|
20
|
+
var __copyProps = (to, from, except, desc) => {
|
|
21
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
22
|
+
for (let key of __getOwnPropNames(from))
|
|
23
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
24
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
25
|
+
}
|
|
26
|
+
return to;
|
|
27
|
+
};
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
30
|
+
var mariadb_dialect_exports = {};
|
|
31
|
+
__export(mariadb_dialect_exports, {
|
|
32
|
+
MariadbDialect: () => MariadbDialect
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(mariadb_dialect_exports);
|
|
35
|
+
var import_base_dialect = require("./base-dialect");
|
|
36
|
+
const _MariadbDialect = class _MariadbDialect extends import_base_dialect.BaseDialect {
|
|
37
|
+
getVersionGuard() {
|
|
38
|
+
return {
|
|
39
|
+
sql: "select version() as version",
|
|
40
|
+
get: /* @__PURE__ */ __name((v) => {
|
|
41
|
+
const m = /([\d+.]+)/.exec(v);
|
|
42
|
+
return m[0];
|
|
43
|
+
}, "get"),
|
|
44
|
+
version: ">=10.9"
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
__name(_MariadbDialect, "MariadbDialect");
|
|
49
|
+
__publicField(_MariadbDialect, "dialectName", "mariadb");
|
|
50
|
+
let MariadbDialect = _MariadbDialect;
|
|
51
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
52
|
+
0 && (module.exports = {
|
|
53
|
+
MariadbDialect
|
|
54
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import { BaseDialect } from './base-dialect';
|
|
10
|
+
export declare class MysqlDialect extends BaseDialect {
|
|
11
|
+
static dialectName: string;
|
|
12
|
+
getVersionGuard(): {
|
|
13
|
+
sql: string;
|
|
14
|
+
get: (v: string) => string;
|
|
15
|
+
version: string;
|
|
16
|
+
};
|
|
17
|
+
}
|