@nocobase/database 1.9.0-alpha.14 → 1.9.0-alpha.16

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/database.d.ts CHANGED
@@ -118,7 +118,7 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
118
118
  /**
119
119
  * @internal
120
120
  */
121
- sequelizeOptions(options: any): IDatabaseOptions;
121
+ sequelizeOptions(options: DatabaseOptions): IDatabaseOptions;
122
122
  /**
123
123
  * @internal
124
124
  */
@@ -128,6 +128,7 @@ export declare class Database extends EventEmitter implements AsyncEmitter {
128
128
  inDialect(...dialect: string[]): boolean;
129
129
  isMySQLCompatibleDialect(): boolean;
130
130
  isPostgresCompatibleDialect(): boolean;
131
+ wrapSequelizeRunForMySQL(): void;
131
132
  /**
132
133
  * Add collection to database
133
134
  * @param options
package/lib/database.js CHANGED
@@ -176,6 +176,9 @@ const _Database = class _Database extends import_events.EventEmitter {
176
176
  );
177
177
  const sequelizeOptions = this.sequelizeOptions(this.options);
178
178
  this.sequelize = new import_sequelize.Sequelize(sequelizeOptions);
179
+ if (options.dialect === "mysql") {
180
+ this.wrapSequelizeRunForMySQL();
181
+ }
179
182
  this.queryInterface = (0, import_query_interface_builder.default)(this);
180
183
  this.collections = /* @__PURE__ */ new Map();
181
184
  this.modelHook = new import_model_hook.ModelHook(this);
@@ -397,6 +400,30 @@ const _Database = class _Database extends import_events.EventEmitter {
397
400
  isPostgresCompatibleDialect() {
398
401
  return this.inDialect("postgres");
399
402
  }
403
+ /*
404
+ * https://github.com/sidorares/node-mysql2/issues/1239#issuecomment-766867699
405
+ * https://github.com/sidorares/node-mysql2/pull/1407#issuecomment-1325789581
406
+ * > I'm starting to think simple "always send (parameter.toString()) as VAR_STRING" unless the type is explicitly specified by user" might be actually the best behaviour
407
+ */
408
+ wrapSequelizeRunForMySQL() {
409
+ const that = this;
410
+ const run = this.sequelize.dialect.Query.prototype.run;
411
+ this.sequelize.dialect.Query.prototype.run = function(sql, parameters) {
412
+ if (!/^update\s+/i.test(sql.trim()) || !(parameters == null ? void 0 : parameters.length)) {
413
+ return run.apply(this, [sql, parameters]);
414
+ }
415
+ try {
416
+ parameters.forEach((p, index) => {
417
+ if (typeof p === "number") {
418
+ parameters[index] = p.toString();
419
+ }
420
+ });
421
+ } catch (err) {
422
+ that.logger.error(err);
423
+ }
424
+ return run.apply(this, [sql, parameters]);
425
+ };
426
+ }
400
427
  /**
401
428
  * Add collection to database
402
429
  * @param options
@@ -611,8 +638,8 @@ const _Database = class _Database extends import_events.EventEmitter {
611
638
  }
612
639
  /* istanbul ignore next -- @preserve */
613
640
  async auth(options = {}) {
614
- const { retry = 9, ...others } = options;
615
- const startingDelay = 50;
641
+ const { retry = 10, ...others } = options;
642
+ const startingDelay = 1e3;
616
643
  const timeMultiple = 2;
617
644
  let attemptNumber = 1;
618
645
  const authenticate = /* @__PURE__ */ __name(async () => {
@@ -635,7 +662,8 @@ const _Database = class _Database extends import_events.EventEmitter {
635
662
  await (0, import_exponential_backoff.backOff)(authenticate, {
636
663
  numOfAttempts: retry,
637
664
  startingDelay,
638
- timeMultiple
665
+ timeMultiple,
666
+ maxDelay: 30 * 1e3
639
667
  });
640
668
  } catch (error) {
641
669
  throw new Error(`Unable to connect to the database`, { cause: error });
@@ -7,6 +7,7 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
  import { BaseDialect } from './base-dialect';
10
+ import { DatabaseOptions } from '../database';
10
11
  export declare class MysqlDialect extends BaseDialect {
11
12
  static dialectName: string;
12
13
  getVersionGuard(): {
@@ -14,5 +15,5 @@ export declare class MysqlDialect extends BaseDialect {
14
15
  get: (v: string) => string;
15
16
  version: string;
16
17
  };
17
- getSequelizeOptions(options: any): any;
18
+ getSequelizeOptions(options: DatabaseOptions): import("../database").IDatabaseOptions;
18
19
  }
@@ -32,7 +32,6 @@ __export(mysql_dialect_exports, {
32
32
  MysqlDialect: () => MysqlDialect
33
33
  });
34
34
  module.exports = __toCommonJS(mysql_dialect_exports);
35
- var import_utils = require("@nocobase/utils");
36
35
  var import_base_dialect = require("./base-dialect");
37
36
  const _MysqlDialect = class _MysqlDialect extends import_base_dialect.BaseDialect {
38
37
  getVersionGuard() {
@@ -46,7 +45,11 @@ const _MysqlDialect = class _MysqlDialect extends import_base_dialect.BaseDialec
46
45
  };
47
46
  }
48
47
  getSequelizeOptions(options) {
49
- import_utils.lodash.set(options, "dialectOptions.multipleStatements", true);
48
+ const dialectOptions = {
49
+ ...options.dialectOptions || {},
50
+ multipleStatements: true
51
+ };
52
+ options.dialectOptions = dialectOptions;
50
53
  return options;
51
54
  }
52
55
  };
@@ -32,6 +32,7 @@ import { UnixTimestampFieldOptions } from './unix-timestamp-field';
32
32
  import { DateOnlyFieldOptions } from './date-only-field';
33
33
  import { DatetimeNoTzFieldOptions } from './datetime-no-tz-field';
34
34
  import { DatetimeTzFieldOptions } from './datetime-tz-field';
35
+ import { SnowflakeIdFieldOptions } from './snowflake-id-field';
35
36
  export * from './array-field';
36
37
  export * from './belongs-to-field';
37
38
  export * from './belongs-to-many-field';
@@ -60,4 +61,5 @@ export * from './virtual-field';
60
61
  export * from './nanoid-field';
61
62
  export * from './encryption-field';
62
63
  export * from './unix-timestamp-field';
63
- export type FieldOptions = BaseFieldOptions | StringFieldOptions | IntegerFieldOptions | FloatFieldOptions | DecimalFieldOptions | DoubleFieldOptions | RealFieldOptions | JsonFieldOptions | JsonbFieldOptions | BooleanFieldOptions | RadioFieldOptions | TextFieldOptions | VirtualFieldOptions | ArrayFieldOptions | SetFieldOptions | TimeFieldOptions | DateFieldOptions | DatetimeTzFieldOptions | DatetimeNoTzFieldOptions | DateOnlyFieldOptions | UnixTimestampFieldOptions | UidFieldOptions | UUIDFieldOptions | NanoidFieldOptions | PasswordFieldOptions | ContextFieldOptions | BelongsToFieldOptions | HasOneFieldOptions | HasManyFieldOptions | BelongsToManyFieldOptions | EncryptionField;
64
+ export * from './snowflake-id-field';
65
+ export type FieldOptions = BaseFieldOptions | StringFieldOptions | IntegerFieldOptions | FloatFieldOptions | DecimalFieldOptions | DoubleFieldOptions | RealFieldOptions | JsonFieldOptions | JsonbFieldOptions | BooleanFieldOptions | RadioFieldOptions | TextFieldOptions | VirtualFieldOptions | ArrayFieldOptions | SetFieldOptions | TimeFieldOptions | DateFieldOptions | DatetimeTzFieldOptions | DatetimeNoTzFieldOptions | DateOnlyFieldOptions | UnixTimestampFieldOptions | UidFieldOptions | UUIDFieldOptions | NanoidFieldOptions | PasswordFieldOptions | ContextFieldOptions | BelongsToFieldOptions | HasOneFieldOptions | HasManyFieldOptions | BelongsToManyFieldOptions | EncryptionField | SnowflakeIdFieldOptions;
@@ -51,6 +51,7 @@ __reExport(fields_exports, require("./virtual-field"), module.exports);
51
51
  __reExport(fields_exports, require("./nanoid-field"), module.exports);
52
52
  __reExport(fields_exports, require("./encryption-field"), module.exports);
53
53
  __reExport(fields_exports, require("./unix-timestamp-field"), module.exports);
54
+ __reExport(fields_exports, require("./snowflake-id-field"), module.exports);
54
55
  // Annotate the CommonJS export names for ESM import in node:
55
56
  0 && (module.exports = {
56
57
  ...require("./array-field"),
@@ -80,5 +81,6 @@ __reExport(fields_exports, require("./unix-timestamp-field"), module.exports);
80
81
  ...require("./virtual-field"),
81
82
  ...require("./nanoid-field"),
82
83
  ...require("./encryption-field"),
83
- ...require("./unix-timestamp-field")
84
+ ...require("./unix-timestamp-field"),
85
+ ...require("./snowflake-id-field")
84
86
  });
@@ -0,0 +1,29 @@
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 { DataTypes } from 'sequelize';
10
+ import { BaseColumnFieldOptions, Field } from './field';
11
+ interface Application {
12
+ snowflakeIdGenerator: {
13
+ generate(): number | BigInt;
14
+ };
15
+ }
16
+ export declare class SnowflakeIdField extends Field {
17
+ private static app;
18
+ static setApp(app: Application): void;
19
+ get dataType(): DataTypes.BigIntDataTypeConstructor;
20
+ private setId;
21
+ init(): void;
22
+ bind(): void;
23
+ unbind(): void;
24
+ }
25
+ export interface SnowflakeIdFieldOptions extends BaseColumnFieldOptions {
26
+ type: 'snowflakeId';
27
+ epoch?: number;
28
+ }
29
+ export {};
@@ -0,0 +1,79 @@
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 snowflake_id_field_exports = {};
31
+ __export(snowflake_id_field_exports, {
32
+ SnowflakeIdField: () => SnowflakeIdField
33
+ });
34
+ module.exports = __toCommonJS(snowflake_id_field_exports);
35
+ var import_sequelize = require("sequelize");
36
+ var import_field = require("./field");
37
+ const _SnowflakeIdField = class _SnowflakeIdField extends import_field.Field {
38
+ static setApp(app) {
39
+ this.app = app;
40
+ }
41
+ get dataType() {
42
+ return import_sequelize.DataTypes.BIGINT;
43
+ }
44
+ setId(name, instance) {
45
+ const value = instance.get(name);
46
+ if (!value) {
47
+ const generator = this.constructor.app.snowflakeIdGenerator;
48
+ instance.set(name, generator.generate());
49
+ }
50
+ }
51
+ init() {
52
+ const { name } = this.options;
53
+ this.listener = (instance) => this.setId(name, instance);
54
+ this.bulkListener = async (instances) => {
55
+ for (const instance of instances) {
56
+ this.setId(name, instance);
57
+ }
58
+ };
59
+ }
60
+ bind() {
61
+ super.bind();
62
+ this.on("beforeValidate", this.listener);
63
+ this.on("beforeSave", this.listener);
64
+ this.on("beforeBulkCreate", this.bulkListener);
65
+ }
66
+ unbind() {
67
+ super.unbind();
68
+ this.off("beforeValidate", this.listener);
69
+ this.off("beforeSave", this.listener);
70
+ this.off("beforeBulkCreate", this.bulkListener);
71
+ }
72
+ };
73
+ __name(_SnowflakeIdField, "SnowflakeIdField");
74
+ __publicField(_SnowflakeIdField, "app");
75
+ let SnowflakeIdField = _SnowflakeIdField;
76
+ // Annotate the CommonJS export names for ESM import in node:
77
+ 0 && (module.exports = {
78
+ SnowflakeIdField
79
+ });
@@ -43,6 +43,7 @@ module.exports = __toCommonJS(inherited_sync_runner_exports);
43
43
  var import_lodash = __toESM(require("lodash"));
44
44
  const _InheritedSyncRunner = class _InheritedSyncRunner {
45
45
  static async syncInheritModel(model, options) {
46
+ var _a;
46
47
  const { transaction } = options;
47
48
  options.hooks = options.hooks === void 0 ? true : !!options.hooks;
48
49
  const inheritedCollection = model.collection;
@@ -77,6 +78,10 @@ const _InheritedSyncRunner = class _InheritedSyncRunner {
77
78
  let maxSequenceName;
78
79
  if (childAttributes.id && childAttributes.id.autoIncrement) {
79
80
  for (const parent of parents) {
81
+ const parentIdField = parent.getField("id");
82
+ if (!((_a = parentIdField == null ? void 0 : parentIdField.options) == null ? void 0 : _a.autoIncrement)) {
83
+ continue;
84
+ }
80
85
  const sequenceNameResult = await queryInterface.sequelize.query(
81
86
  `SELECT column_default
82
87
  FROM information_schema.columns
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/database",
3
- "version": "1.9.0-alpha.14",
3
+ "version": "1.9.0-alpha.16",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
7
7
  "license": "AGPL-3.0",
8
8
  "dependencies": {
9
- "@nocobase/logger": "1.9.0-alpha.14",
10
- "@nocobase/utils": "1.9.0-alpha.14",
9
+ "@nocobase/logger": "1.9.0-alpha.16",
10
+ "@nocobase/utils": "1.9.0-alpha.16",
11
11
  "async-mutex": "^0.3.2",
12
12
  "chalk": "^4.1.1",
13
13
  "cron-parser": "4.4.0",
@@ -39,5 +39,5 @@
39
39
  "url": "git+https://github.com/nocobase/nocobase.git",
40
40
  "directory": "packages/database"
41
41
  },
42
- "gitHead": "0e2ad0f673bff626b4d59aaa9657ce5d5efb54c6"
42
+ "gitHead": "27dc5eb0e365a63d9fb7481dced69e253c018027"
43
43
  }