sequelize.js
20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
"use strict";
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
const url = require("url");
const path = require("path");
const pgConnectionString = require("pg-connection-string");
const retry = require("retry-as-promised");
const _ = require("lodash");
const Utils = require("./utils");
const Model = require("./model");
const DataTypes = require("./data-types");
const Deferrable = require("./deferrable");
const ModelManager = require("./model-manager");
const Transaction = require("./transaction");
const QueryTypes = require("./query-types");
const TableHints = require("./table-hints");
const IndexHints = require("./index-hints");
const sequelizeErrors = require("./errors");
const Hooks = require("./hooks");
const Association = require("./associations/index");
const Validator = require("./utils/validator-extras").validator;
const Op = require("./operators");
const deprecations = require("./utils/deprecations");
const { QueryInterface } = require("./dialects/abstract/query-interface");
const { BelongsTo } = require("./associations/belongs-to");
const HasOne = require("./associations/has-one");
const { BelongsToMany } = require("./associations/belongs-to-many");
const { HasMany } = require("./associations/has-many");
class Sequelize {
constructor(database, username, password, options) {
let config;
if (arguments.length === 1 && typeof database === "object") {
options = database;
config = _.pick(options, "host", "port", "database", "username", "password");
} else if (arguments.length === 1 && typeof database === "string" || arguments.length === 2 && typeof username === "object") {
config = {};
options = username || {};
const urlParts = url.parse(arguments[0], true);
options.dialect = urlParts.protocol.replace(/:$/, "");
options.host = urlParts.hostname;
if (options.dialect === "sqlite" && urlParts.pathname && !urlParts.pathname.startsWith("/:memory")) {
const storagePath = path.join(options.host, urlParts.pathname);
options.storage = path.resolve(options.storage || storagePath);
}
if (urlParts.pathname) {
config.database = urlParts.pathname.replace(/^\//, "");
}
if (urlParts.port) {
options.port = urlParts.port;
}
if (urlParts.auth) {
const authParts = urlParts.auth.split(":");
config.username = authParts[0];
if (authParts.length > 1)
config.password = authParts.slice(1).join(":");
}
if (urlParts.query) {
if (urlParts.query.host) {
options.host = urlParts.query.host;
}
if (options.dialectOptions) {
Object.assign(options.dialectOptions, urlParts.query);
} else {
options.dialectOptions = urlParts.query;
if (urlParts.query.options) {
try {
const o = JSON.parse(urlParts.query.options);
options.dialectOptions.options = o;
} catch (e) {
}
}
}
}
if (["postgres", "postgresql"].includes(options.dialect)) {
Object.assign(options.dialectOptions, pgConnectionString.parse(arguments[0]));
}
} else {
options = options || {};
config = { database, username, password };
}
Sequelize.runHooks("beforeInit", config, options);
this.options = __spreadValues({
dialect: null,
dialectModule: null,
dialectModulePath: null,
host: "localhost",
protocol: "tcp",
define: {},
query: {},
sync: {},
timezone: "+00:00",
standardConformingStrings: true,
logging: console.log,
omitNull: false,
native: false,
replication: false,
ssl: void 0,
pool: {},
quoteIdentifiers: true,
hooks: {},
retry: {
max: 5,
match: [
"SQLITE_BUSY: database is locked"
]
},
transactionType: Transaction.TYPES.DEFERRED,
isolationLevel: null,
databaseVersion: 0,
typeValidation: false,
benchmark: false,
minifyAliases: false,
logQueryParameters: false
}, options);
if (!this.options.dialect) {
throw new Error("Dialect needs to be explicitly supplied as of v4.0.0");
}
if (this.options.dialect === "postgresql") {
this.options.dialect = "postgres";
}
if (this.options.dialect === "sqlite" && this.options.timezone !== "+00:00") {
throw new Error("Setting a custom timezone is not supported by SQLite, dates are always returned as UTC. Please remove the custom timezone parameter.");
}
if (this.options.logging === true) {
deprecations.noTrueLogging();
this.options.logging = console.log;
}
this._setupHooks(options.hooks);
this.config = {
database: config.database || this.options.database,
username: config.username || this.options.username,
password: config.password || this.options.password || null,
host: config.host || this.options.host,
port: config.port || this.options.port,
pool: this.options.pool,
protocol: this.options.protocol,
native: this.options.native,
ssl: this.options.ssl,
replication: this.options.replication,
dialectModule: this.options.dialectModule,
dialectModulePath: this.options.dialectModulePath,
keepDefaultTimezone: this.options.keepDefaultTimezone,
dialectOptions: this.options.dialectOptions
};
let Dialect;
switch (this.getDialect()) {
case "mariadb":
Dialect = require("./dialects/mariadb");
break;
case "mssql":
Dialect = require("./dialects/mssql");
break;
case "mysql":
Dialect = require("./dialects/mysql");
break;
case "postgres":
Dialect = require("./dialects/postgres");
break;
case "sqlite":
Dialect = require("./dialects/sqlite");
break;
case "db2":
Dialect = require("./dialects/db2");
break;
case "snowflake":
Dialect = require("./dialects/snowflake");
break;
default:
throw new Error(`The dialect ${this.getDialect()} is not supported. Supported dialects: mssql, mariadb, mysql, postgres, db2 and sqlite.`);
}
this.dialect = new Dialect(this);
this.dialect.queryGenerator.typeValidation = options.typeValidation;
if (_.isPlainObject(this.options.operatorsAliases)) {
deprecations.noStringOperators();
this.dialect.queryGenerator.setOperatorsAliases(this.options.operatorsAliases);
} else if (typeof this.options.operatorsAliases === "boolean") {
deprecations.noBoolOperatorAliases();
}
this.queryInterface = this.dialect.queryInterface;
this.models = {};
this.modelManager = new ModelManager(this);
this.connectionManager = this.dialect.connectionManager;
Sequelize.runHooks("afterInit", this);
}
refreshTypes() {
this.connectionManager.refreshTypeParser(DataTypes);
}
getDialect() {
return this.options.dialect;
}
getDatabaseName() {
return this.config.database;
}
getQueryInterface() {
return this.queryInterface;
}
define(modelName, attributes, options = {}) {
options.modelName = modelName;
options.sequelize = this;
const model = class extends Model {
};
model.init(attributes, options);
return model;
}
model(modelName) {
if (!this.isDefined(modelName)) {
throw new Error(`${modelName} has not been defined`);
}
return this.modelManager.getModel(modelName);
}
isDefined(modelName) {
return !!this.modelManager.models.find((model) => model.name === modelName);
}
async query(sql, options) {
options = __spreadValues(__spreadValues({}, this.options.query), options);
if (options.instance && !options.model) {
options.model = options.instance.constructor;
}
if (!options.instance && !options.model) {
options.raw = true;
}
if (options.mapToModel) {
options.fieldMap = _.get(options, "model.fieldAttributeMap", {});
}
options = _.defaults(options, {
logging: Object.prototype.hasOwnProperty.call(this.options, "logging") ? this.options.logging : console.log,
searchPath: Object.prototype.hasOwnProperty.call(this.options, "searchPath") ? this.options.searchPath : "DEFAULT"
});
if (!options.type) {
if (options.model || options.nest || options.plain) {
options.type = QueryTypes.SELECT;
} else {
options.type = QueryTypes.RAW;
}
}
if (!this.dialect.supports.searchPath || !this.options.dialectOptions || !this.options.dialectOptions.prependSearchPath || options.supportsSearchPath === false) {
delete options.searchPath;
} else if (!options.searchPath) {
options.searchPath = "DEFAULT";
}
if (typeof sql === "object") {
if (sql.values !== void 0) {
if (options.replacements !== void 0) {
throw new Error("Both `sql.values` and `options.replacements` cannot be set at the same time");
}
options.replacements = sql.values;
}
if (sql.bind !== void 0) {
if (options.bind !== void 0) {
throw new Error("Both `sql.bind` and `options.bind` cannot be set at the same time");
}
options.bind = sql.bind;
}
if (sql.query !== void 0) {
sql = sql.query;
}
}
sql = sql.trim();
if (options.replacements && options.bind) {
throw new Error("Both `replacements` and `bind` cannot be set at the same time");
}
if (options.replacements) {
if (Array.isArray(options.replacements)) {
sql = Utils.format([sql].concat(options.replacements), this.options.dialect);
} else {
sql = Utils.formatNamedParameters(sql, options.replacements, this.options.dialect);
}
}
let bindParameters;
if (options.bind) {
[sql, bindParameters] = this.dialect.Query.formatBindParameters(sql, options.bind, this.options.dialect);
}
const checkTransaction = () => {
if (options.transaction && options.transaction.finished && !options.completesTransaction) {
const error = new Error(`${options.transaction.finished} has been called on this transaction(${options.transaction.id}), you can no longer use it. (The rejected query is attached as the 'sql' property of this error)`);
error.sql = sql;
throw error;
}
};
const retryOptions = __spreadValues(__spreadValues({}, this.options.retry), options.retry);
return retry(async () => {
if (options.transaction === void 0 && Sequelize._cls) {
options.transaction = Sequelize._cls.get("transaction");
}
checkTransaction();
const connection = await (options.transaction ? options.transaction.connection : this.connectionManager.getConnection(options));
if (this.options.dialect === "db2" && options.alter) {
if (options.alter.drop === false) {
connection.dropTable = false;
}
}
const query = new this.dialect.Query(connection, this, options);
try {
await this.runHooks("beforeQuery", options, query);
checkTransaction();
return await query.run(sql, bindParameters);
} finally {
await this.runHooks("afterQuery", options, query);
if (!options.transaction) {
await this.connectionManager.releaseConnection(connection);
}
}
}, retryOptions);
}
async set(variables, options) {
options = __spreadValues(__spreadValues({}, this.options.set), typeof options === "object" && options);
if (!["mysql", "mariadb"].includes(this.options.dialect)) {
throw new Error("sequelize.set is only supported for mysql or mariadb");
}
if (!options.transaction || !(options.transaction instanceof Transaction)) {
throw new TypeError("options.transaction is required");
}
options.raw = true;
options.plain = true;
options.type = "SET";
const query = `SET ${_.map(variables, (v, k) => `@${k} := ${typeof v === "string" ? `"${v}"` : v}`).join(", ")}`;
return await this.query(query, options);
}
escape(value) {
return this.dialect.queryGenerator.escape(value);
}
async createSchema(schema, options) {
return await this.getQueryInterface().createSchema(schema, options);
}
async showAllSchemas(options) {
return await this.getQueryInterface().showAllSchemas(options);
}
async dropSchema(schema, options) {
return await this.getQueryInterface().dropSchema(schema, options);
}
async dropAllSchemas(options) {
return await this.getQueryInterface().dropAllSchemas(options);
}
async sync(options) {
options = __spreadProps(__spreadValues(__spreadValues(__spreadValues({}, this.options), this.options.sync), options), {
hooks: options ? options.hooks !== false : true
});
if (options.match) {
if (!options.match.test(this.config.database)) {
throw new Error(`Database "${this.config.database}" does not match sync match parameter "${options.match}"`);
}
}
if (options.hooks) {
await this.runHooks("beforeBulkSync", options);
}
if (options.force) {
await this.drop(options);
}
const models = [];
this.modelManager.forEachModel((model) => {
if (model) {
models.push(model);
} else {
}
});
if (!models.length) {
await this.authenticate(options);
} else {
for (const model of models)
await model.sync(options);
}
if (options.hooks) {
await this.runHooks("afterBulkSync", options);
}
return this;
}
async truncate(options) {
const models = [];
this.modelManager.forEachModel((model) => {
if (model) {
models.push(model);
}
}, { reverse: false });
if (options && options.cascade) {
for (const model of models)
await model.truncate(options);
} else {
await Promise.all(models.map((model) => model.truncate(options)));
}
}
async drop(options) {
const models = [];
this.modelManager.forEachModel((model) => {
if (model) {
models.push(model);
}
}, { reverse: false });
for (const model of models)
await model.drop(options);
}
async authenticate(options) {
options = __spreadValues({
raw: true,
plain: true,
type: QueryTypes.SELECT
}, options);
await this.query("SELECT 1+1 AS result", options);
return;
}
async databaseVersion(options) {
return await this.getQueryInterface().databaseVersion(options);
}
random() {
if (["postgres", "sqlite", "snowflake"].includes(this.getDialect())) {
return this.fn("RANDOM");
}
return this.fn("RAND");
}
static fn(fn, ...args) {
return new Utils.Fn(fn, args);
}
static col(col) {
return new Utils.Col(col);
}
static cast(val, type) {
return new Utils.Cast(val, type);
}
static literal(val) {
return new Utils.Literal(val);
}
static and(...args) {
return { [Op.and]: args };
}
static or(...args) {
return { [Op.or]: args };
}
static json(conditionsOrPath, value) {
return new Utils.Json(conditionsOrPath, value);
}
static where(attr, comparator, logic) {
return new Utils.Where(attr, comparator, logic);
}
async transaction(options, autoCallback) {
if (typeof options === "function") {
autoCallback = options;
options = void 0;
}
const transaction = new Transaction(this, options);
if (!autoCallback) {
await transaction.prepareEnvironment(false);
return transaction;
}
return Sequelize._clsRun(async () => {
try {
await transaction.prepareEnvironment();
const result = await autoCallback(transaction);
await transaction.commit();
return await result;
} catch (err) {
try {
if (!transaction.finished) {
await transaction.rollback();
} else {
await transaction.cleanup();
}
} catch (err0) {
}
throw err;
}
});
}
static useCLS(ns) {
if (!ns || typeof ns !== "object" || typeof ns.bind !== "function" || typeof ns.run !== "function")
throw new Error("Must provide CLS namespace");
Sequelize._cls = ns;
return this;
}
static _clsRun(fn) {
const ns = Sequelize._cls;
if (!ns)
return fn();
let res;
ns.run((context) => res = fn(context));
return res;
}
log(...args) {
let options;
const last = _.last(args);
if (last && _.isPlainObject(last) && Object.prototype.hasOwnProperty.call(last, "logging")) {
options = last;
if (options.logging === console.log) {
args.splice(args.length - 1, 1);
}
} else {
options = this.options;
}
if (options.logging) {
if (options.logging === true) {
deprecations.noTrueLogging();
options.logging = console.log;
}
if ((this.options.benchmark || options.benchmark) && options.logging === console.log) {
args = [`${args[0]} Elapsed time: ${args[1]}ms`];
}
options.logging(...args);
}
}
close() {
return this.connectionManager.close();
}
normalizeDataType(Type) {
let type = typeof Type === "function" ? new Type() : Type;
const dialectTypes = this.dialect.DataTypes || {};
if (dialectTypes[type.key]) {
type = dialectTypes[type.key].extend(type);
}
if (type instanceof DataTypes.ARRAY) {
if (!type.type) {
throw new Error("ARRAY is missing type definition for its values.");
}
if (dialectTypes[type.type.key]) {
type.type = dialectTypes[type.type.key].extend(type.type);
}
}
return type;
}
normalizeAttribute(attribute) {
if (!_.isPlainObject(attribute)) {
attribute = { type: attribute };
}
if (!attribute.type)
return attribute;
attribute.type = this.normalizeDataType(attribute.type);
if (Object.prototype.hasOwnProperty.call(attribute, "defaultValue")) {
if (typeof attribute.defaultValue === "function" && [DataTypes.NOW, DataTypes.UUIDV1, DataTypes.UUIDV4].includes(attribute.defaultValue)) {
attribute.defaultValue = new attribute.defaultValue();
}
}
if (attribute.type instanceof DataTypes.ENUM) {
if (attribute.values) {
attribute.type.values = attribute.type.options.values = attribute.values;
} else {
attribute.values = attribute.type.values;
}
if (!attribute.values.length) {
throw new Error("Values for ENUM have not been defined.");
}
}
return attribute;
}
}
Sequelize.prototype.fn = Sequelize.fn;
Sequelize.prototype.col = Sequelize.col;
Sequelize.prototype.cast = Sequelize.cast;
Sequelize.prototype.literal = Sequelize.literal;
Sequelize.prototype.and = Sequelize.and;
Sequelize.prototype.or = Sequelize.or;
Sequelize.prototype.json = Sequelize.json;
Sequelize.prototype.where = Sequelize.where;
Sequelize.prototype.validate = Sequelize.prototype.authenticate;
Object.defineProperty(Sequelize, "version", {
enumerable: true,
get() {
return require("../package.json").version;
}
});
Sequelize.options = { hooks: {} };
Sequelize.Utils = Utils;
Sequelize.Op = Op;
Sequelize.TableHints = TableHints;
Sequelize.IndexHints = IndexHints;
Sequelize.Transaction = Transaction;
Sequelize.prototype.Sequelize = Sequelize;
Sequelize.prototype.QueryTypes = Sequelize.QueryTypes = QueryTypes;
Sequelize.prototype.Validator = Sequelize.Validator = Validator;
Sequelize.Model = Model;
Sequelize.QueryInterface = QueryInterface;
Sequelize.BelongsTo = BelongsTo;
Sequelize.HasOne = HasOne;
Sequelize.HasMany = HasMany;
Sequelize.BelongsToMany = BelongsToMany;
Sequelize.DataTypes = DataTypes;
for (const dataType in DataTypes) {
Sequelize[dataType] = DataTypes[dataType];
}
Sequelize.Deferrable = Deferrable;
Sequelize.prototype.Association = Sequelize.Association = Association;
Sequelize.useInflection = Utils.useInflection;
Hooks.applyTo(Sequelize);
Hooks.applyTo(Sequelize.prototype);
Sequelize.Error = sequelizeErrors.BaseError;
for (const error of Object.keys(sequelizeErrors)) {
Sequelize[error] = sequelizeErrors[error];
}
module.exports = Sequelize;
module.exports.Sequelize = Sequelize;
module.exports.default = Sequelize;
//# sourceMappingURL=sequelize.js.map