@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.
Files changed (59) hide show
  1. package/lib/collection.d.ts +5 -2
  2. package/lib/collection.js +70 -2
  3. package/lib/database.d.ts +13 -23
  4. package/lib/database.js +28 -42
  5. package/lib/dialects/base-dialect.d.ts +20 -0
  6. package/lib/dialects/base-dialect.js +75 -0
  7. package/lib/dialects/index.d.ts +9 -0
  8. package/lib/dialects/index.js +30 -0
  9. package/lib/dialects/mariadb-dialect.d.ts +17 -0
  10. package/lib/dialects/mariadb-dialect.js +54 -0
  11. package/lib/dialects/mysql-dialect.d.ts +17 -0
  12. package/lib/dialects/mysql-dialect.js +54 -0
  13. package/lib/dialects/postgres-dialect.d.ts +18 -0
  14. package/lib/dialects/postgres-dialect.js +77 -0
  15. package/lib/dialects/sqlite-dialect.d.ts +17 -0
  16. package/lib/dialects/sqlite-dialect.js +51 -0
  17. package/lib/features/referential-integrity-check.js +1 -1
  18. package/lib/fields/date-field.d.ts +7 -2
  19. package/lib/fields/date-field.js +89 -0
  20. package/lib/fields/date-only-field.d.ts +15 -0
  21. package/lib/fields/date-only-field.js +45 -0
  22. package/lib/fields/datetime-field.d.ts +15 -0
  23. package/lib/fields/datetime-field.js +41 -0
  24. package/lib/fields/datetime-no-tz-field.d.ts +24 -0
  25. package/lib/fields/datetime-no-tz-field.js +128 -0
  26. package/lib/fields/datetime-tz-field.d.ts +15 -0
  27. package/lib/fields/datetime-tz-field.js +41 -0
  28. package/lib/fields/field.d.ts +1 -1
  29. package/lib/fields/field.js +3 -2
  30. package/lib/fields/index.d.ts +10 -1
  31. package/lib/fields/index.js +11 -1
  32. package/lib/fields/unix-timestamp-field.d.ts +22 -0
  33. package/lib/fields/unix-timestamp-field.js +94 -0
  34. package/lib/helpers.d.ts +2 -1
  35. package/lib/helpers.js +16 -49
  36. package/lib/index.d.ts +1 -0
  37. package/lib/index.js +3 -1
  38. package/lib/interfaces/boolean-interface.js +5 -1
  39. package/lib/interfaces/multiple-select-interface.js +7 -1
  40. package/lib/interfaces/select-interface.js +7 -1
  41. package/lib/interfaces/to-one-interface.js +3 -3
  42. package/lib/model.d.ts +1 -0
  43. package/lib/model.js +12 -0
  44. package/lib/operators/date.js +66 -24
  45. package/lib/options-parser.d.ts +1 -0
  46. package/lib/options-parser.js +36 -10
  47. package/lib/query-interface/query-interface-builder.js +3 -0
  48. package/lib/relation-repository/hasmany-repository.js +8 -11
  49. package/lib/relation-repository/multiple-relation-repository.d.ts +1 -0
  50. package/lib/relation-repository/multiple-relation-repository.js +11 -3
  51. package/lib/relation-repository/relation-repository.d.ts +6 -3
  52. package/lib/relation-repository/relation-repository.js +27 -4
  53. package/lib/repository.d.ts +5 -2
  54. package/lib/repository.js +27 -16
  55. package/lib/update-associations.d.ts +2 -1
  56. package/lib/update-associations.js +4 -3
  57. package/lib/view/field-type-map.d.ts +2 -2
  58. package/lib/view/field-type-map.js +17 -17
  59. package/package.json +4 -4
@@ -0,0 +1,41 @@
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
+ var __export = (target, all) => {
16
+ for (var name in all)
17
+ __defProp(target, name, { get: all[name], enumerable: true });
18
+ };
19
+ var __copyProps = (to, from, except, desc) => {
20
+ if (from && typeof from === "object" || typeof from === "function") {
21
+ for (let key of __getOwnPropNames(from))
22
+ if (!__hasOwnProp.call(to, key) && key !== except)
23
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
24
+ }
25
+ return to;
26
+ };
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var datetime_tz_field_exports = {};
29
+ __export(datetime_tz_field_exports, {
30
+ DatetimeTzField: () => DatetimeTzField
31
+ });
32
+ module.exports = __toCommonJS(datetime_tz_field_exports);
33
+ var import_date_field = require("./date-field");
34
+ const _DatetimeTzField = class _DatetimeTzField extends import_date_field.DateField {
35
+ };
36
+ __name(_DatetimeTzField, "DatetimeTzField");
37
+ let DatetimeTzField = _DatetimeTzField;
38
+ // Annotate the CommonJS export names for ESM import in node:
39
+ 0 && (module.exports = {
40
+ DatetimeTzField
41
+ });
@@ -47,6 +47,6 @@ export declare abstract class Field {
47
47
  bind(): void;
48
48
  unbind(): void;
49
49
  toSequelize(): any;
50
- isSqlite(): boolean;
50
+ additionalSequelizeOptions(): {};
51
51
  typeToString(): any;
52
52
  }
@@ -151,10 +151,11 @@ const _Field = class _Field {
151
151
  if (this.dataType) {
152
152
  Object.assign(opts, { type: this.database.sequelize.normalizeDataType(this.dataType) });
153
153
  }
154
+ Object.assign(opts, this.additionalSequelizeOptions());
154
155
  return opts;
155
156
  }
156
- isSqlite() {
157
- return this.database.sequelize.getDialect() === "sqlite";
157
+ additionalSequelizeOptions() {
158
+ return {};
158
159
  }
159
160
  typeToString() {
160
161
  return this.dataType.toString();
@@ -29,12 +29,20 @@ import { UUIDFieldOptions } from './uuid-field';
29
29
  import { VirtualFieldOptions } from './virtual-field';
30
30
  import { NanoidFieldOptions } from './nanoid-field';
31
31
  import { EncryptionField } from './encryption-field';
32
+ import { UnixTimestampFieldOptions } from './unix-timestamp-field';
33
+ import { DateOnlyFieldOptions } from './date-only-field';
34
+ import { DatetimeNoTzFieldOptions } from './datetime-no-tz-field';
35
+ import { DatetimeTzFieldOptions } from './datetime-tz-field';
32
36
  export * from './array-field';
33
37
  export * from './belongs-to-field';
34
38
  export * from './belongs-to-many-field';
35
39
  export * from './boolean-field';
36
40
  export * from './context-field';
37
41
  export * from './date-field';
42
+ export * from './datetime-field';
43
+ export * from './datetime-tz-field';
44
+ export * from './datetime-no-tz-field';
45
+ export * from './date-only-field';
38
46
  export * from './field';
39
47
  export * from './has-many-field';
40
48
  export * from './has-one-field';
@@ -53,4 +61,5 @@ export * from './uuid-field';
53
61
  export * from './virtual-field';
54
62
  export * from './nanoid-field';
55
63
  export * from './encryption-field';
56
- export type FieldOptions = BaseFieldOptions | StringFieldOptions | IntegerFieldOptions | FloatFieldOptions | DecimalFieldOptions | DoubleFieldOptions | RealFieldOptions | JsonFieldOptions | JsonbFieldOptions | BooleanFieldOptions | RadioFieldOptions | SortFieldOptions | TextFieldOptions | VirtualFieldOptions | ArrayFieldOptions | SetFieldOptions | TimeFieldOptions | DateFieldOptions | UidFieldOptions | UUIDFieldOptions | NanoidFieldOptions | PasswordFieldOptions | ContextFieldOptions | BelongsToFieldOptions | HasOneFieldOptions | HasManyFieldOptions | BelongsToManyFieldOptions | EncryptionField;
64
+ export * from './unix-timestamp-field';
65
+ export type FieldOptions = BaseFieldOptions | StringFieldOptions | IntegerFieldOptions | FloatFieldOptions | DecimalFieldOptions | DoubleFieldOptions | RealFieldOptions | JsonFieldOptions | JsonbFieldOptions | BooleanFieldOptions | RadioFieldOptions | SortFieldOptions | TextFieldOptions | VirtualFieldOptions | ArrayFieldOptions | SetFieldOptions | TimeFieldOptions | DateFieldOptions | DatetimeTzFieldOptions | DatetimeNoTzFieldOptions | DateOnlyFieldOptions | UnixTimestampFieldOptions | UidFieldOptions | UUIDFieldOptions | NanoidFieldOptions | PasswordFieldOptions | ContextFieldOptions | BelongsToFieldOptions | HasOneFieldOptions | HasManyFieldOptions | BelongsToManyFieldOptions | EncryptionField;
@@ -29,6 +29,10 @@ __reExport(fields_exports, require("./belongs-to-many-field"), module.exports);
29
29
  __reExport(fields_exports, require("./boolean-field"), module.exports);
30
30
  __reExport(fields_exports, require("./context-field"), module.exports);
31
31
  __reExport(fields_exports, require("./date-field"), module.exports);
32
+ __reExport(fields_exports, require("./datetime-field"), module.exports);
33
+ __reExport(fields_exports, require("./datetime-tz-field"), module.exports);
34
+ __reExport(fields_exports, require("./datetime-no-tz-field"), module.exports);
35
+ __reExport(fields_exports, require("./date-only-field"), module.exports);
32
36
  __reExport(fields_exports, require("./field"), module.exports);
33
37
  __reExport(fields_exports, require("./has-many-field"), module.exports);
34
38
  __reExport(fields_exports, require("./has-one-field"), module.exports);
@@ -47,6 +51,7 @@ __reExport(fields_exports, require("./uuid-field"), module.exports);
47
51
  __reExport(fields_exports, require("./virtual-field"), module.exports);
48
52
  __reExport(fields_exports, require("./nanoid-field"), module.exports);
49
53
  __reExport(fields_exports, require("./encryption-field"), module.exports);
54
+ __reExport(fields_exports, require("./unix-timestamp-field"), module.exports);
50
55
  // Annotate the CommonJS export names for ESM import in node:
51
56
  0 && (module.exports = {
52
57
  ...require("./array-field"),
@@ -55,6 +60,10 @@ __reExport(fields_exports, require("./encryption-field"), module.exports);
55
60
  ...require("./boolean-field"),
56
61
  ...require("./context-field"),
57
62
  ...require("./date-field"),
63
+ ...require("./datetime-field"),
64
+ ...require("./datetime-tz-field"),
65
+ ...require("./datetime-no-tz-field"),
66
+ ...require("./date-only-field"),
58
67
  ...require("./field"),
59
68
  ...require("./has-many-field"),
60
69
  ...require("./has-one-field"),
@@ -72,5 +81,6 @@ __reExport(fields_exports, require("./encryption-field"), module.exports);
72
81
  ...require("./uuid-field"),
73
82
  ...require("./virtual-field"),
74
83
  ...require("./nanoid-field"),
75
- ...require("./encryption-field")
84
+ ...require("./encryption-field"),
85
+ ...require("./unix-timestamp-field")
76
86
  });
@@ -0,0 +1,22 @@
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 { DateField } from './date-field';
11
+ import { BaseColumnFieldOptions } from './field';
12
+ export declare class UnixTimestampField extends DateField {
13
+ get dataType(): DataTypes.BigIntDataTypeConstructor;
14
+ dateToValue(val: any): any;
15
+ additionalSequelizeOptions(): {
16
+ get(): any;
17
+ set(value: any): void;
18
+ };
19
+ }
20
+ export interface UnixTimestampFieldOptions extends BaseColumnFieldOptions {
21
+ type: 'unixTimestamp';
22
+ }
@@ -0,0 +1,94 @@
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
+ var __export = (target, all) => {
16
+ for (var name in all)
17
+ __defProp(target, name, { get: all[name], enumerable: true });
18
+ };
19
+ var __copyProps = (to, from, except, desc) => {
20
+ if (from && typeof from === "object" || typeof from === "function") {
21
+ for (let key of __getOwnPropNames(from))
22
+ if (!__hasOwnProp.call(to, key) && key !== except)
23
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
24
+ }
25
+ return to;
26
+ };
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var unix_timestamp_field_exports = {};
29
+ __export(unix_timestamp_field_exports, {
30
+ UnixTimestampField: () => UnixTimestampField
31
+ });
32
+ module.exports = __toCommonJS(unix_timestamp_field_exports);
33
+ var import_sequelize = require("sequelize");
34
+ var import_date_field = require("./date-field");
35
+ const _UnixTimestampField = class _UnixTimestampField extends import_date_field.DateField {
36
+ get dataType() {
37
+ return import_sequelize.DataTypes.BIGINT;
38
+ }
39
+ dateToValue(val) {
40
+ var _a, _b, _c, _d, _e;
41
+ if (val === null || val === void 0) {
42
+ return val;
43
+ }
44
+ let { accuracy } = this.options;
45
+ if ((_c = (_b = (_a = this.options) == null ? void 0 : _a.uiSchema) == null ? void 0 : _b["x-component-props"]) == null ? void 0 : _c.accuracy) {
46
+ accuracy = (_e = (_d = this.options) == null ? void 0 : _d.uiSchema["x-component-props"]) == null ? void 0 : _e.accuracy;
47
+ }
48
+ if (!accuracy) {
49
+ accuracy = "second";
50
+ }
51
+ let rationalNumber = 1e3;
52
+ if (accuracy === "millisecond") {
53
+ rationalNumber = 1;
54
+ }
55
+ return Math.floor(new Date(val).getTime() / rationalNumber);
56
+ }
57
+ additionalSequelizeOptions() {
58
+ var _a, _b, _c, _d, _e;
59
+ const { name } = this.options;
60
+ let { accuracy } = this.options;
61
+ if ((_c = (_b = (_a = this.options) == null ? void 0 : _a.uiSchema) == null ? void 0 : _b["x-component-props"]) == null ? void 0 : _c.accuracy) {
62
+ accuracy = (_e = (_d = this.options) == null ? void 0 : _d.uiSchema["x-component-props"]) == null ? void 0 : _e.accuracy;
63
+ }
64
+ if (!accuracy) {
65
+ accuracy = "second";
66
+ }
67
+ let rationalNumber = 1e3;
68
+ if (accuracy === "millisecond") {
69
+ rationalNumber = 1;
70
+ }
71
+ return {
72
+ get() {
73
+ const value = this.getDataValue(name);
74
+ if (value === null || value === void 0) {
75
+ return value;
76
+ }
77
+ return new Date(value * rationalNumber);
78
+ },
79
+ set(value) {
80
+ if (value === null || value === void 0) {
81
+ this.setDataValue(name, value);
82
+ } else {
83
+ this.setDataValue(name, Math.floor(new Date(value).getTime() / rationalNumber));
84
+ }
85
+ }
86
+ };
87
+ }
88
+ };
89
+ __name(_UnixTimestampField, "UnixTimestampField");
90
+ let UnixTimestampField = _UnixTimestampField;
91
+ // Annotate the CommonJS export names for ESM import in node:
92
+ 0 && (module.exports = {
93
+ UnixTimestampField
94
+ });
package/lib/helpers.d.ts CHANGED
@@ -8,4 +8,5 @@
8
8
  */
9
9
  import { Database, IDatabaseOptions } from './database';
10
10
  export declare function parseDatabaseOptionsFromEnv(): Promise<IDatabaseOptions>;
11
- export declare function checkDatabaseVersion(db: Database): Promise<boolean>;
11
+ export declare function checkDatabaseVersion(db: Database): Promise<void>;
12
+ export declare function registerDialects(): void;
package/lib/helpers.js CHANGED
@@ -38,11 +38,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
38
38
  var helpers_exports = {};
39
39
  __export(helpers_exports, {
40
40
  checkDatabaseVersion: () => checkDatabaseVersion,
41
- parseDatabaseOptionsFromEnv: () => parseDatabaseOptionsFromEnv
41
+ parseDatabaseOptionsFromEnv: () => parseDatabaseOptionsFromEnv,
42
+ registerDialects: () => registerDialects
42
43
  });
43
44
  module.exports = __toCommonJS(helpers_exports);
45
+ var import_database = require("./database");
44
46
  var import_fs = __toESM(require("fs"));
45
- var import_semver = __toESM(require("semver"));
47
+ var import_mysql_dialect = require("./dialects/mysql-dialect");
48
+ var import_sqlite_dialect = require("./dialects/sqlite-dialect");
49
+ var import_mariadb_dialect = require("./dialects/mariadb-dialect");
50
+ var import_postgres_dialect = require("./dialects/postgres-dialect");
46
51
  /* istanbul ignore file -- @preserve */
47
52
  function getEnvValue(key, defaultValue) {
48
53
  return process.env[key] || defaultValue;
@@ -121,57 +126,19 @@ function customLogger(queryString, queryObject) {
121
126
  }
122
127
  }
123
128
  __name(customLogger, "customLogger");
124
- const dialectVersionAccessors = {
125
- sqlite: {
126
- sql: "select sqlite_version() as version",
127
- get: /* @__PURE__ */ __name((v) => v, "get"),
128
- version: "3.x"
129
- },
130
- mysql: {
131
- sql: "select version() as version",
132
- get: /* @__PURE__ */ __name((v) => {
133
- const m = /([\d+.]+)/.exec(v);
134
- return m[0];
135
- }, "get"),
136
- version: ">=8.0.17"
137
- },
138
- mariadb: {
139
- sql: "select version() as version",
140
- get: /* @__PURE__ */ __name((v) => {
141
- const m = /([\d+.]+)/.exec(v);
142
- return m[0];
143
- }, "get"),
144
- version: ">=10.9"
145
- },
146
- postgres: {
147
- sql: "select version() as version",
148
- get: /* @__PURE__ */ __name((v) => {
149
- const m = /([\d+.]+)/.exec(v);
150
- return import_semver.default.minVersion(m[0]).version;
151
- }, "get"),
152
- version: ">=10"
153
- }
154
- };
155
129
  async function checkDatabaseVersion(db) {
156
- var _a;
157
- const dialect = db.sequelize.getDialect();
158
- const accessor = dialectVersionAccessors[dialect];
159
- if (!accessor) {
160
- throw new Error(`unsupported dialect ${dialect}`);
161
- }
162
- const result = await db.sequelize.query(accessor.sql, {
163
- type: "SELECT"
164
- });
165
- const version = accessor.get((_a = result == null ? void 0 : result[0]) == null ? void 0 : _a.version);
166
- const versionResult = import_semver.default.satisfies(version, accessor.version);
167
- if (!versionResult) {
168
- throw new Error(`to use ${dialect}, please ensure the version is ${accessor.version}`);
169
- }
170
- return true;
130
+ await db.dialect.checkDatabaseVersion(db);
171
131
  }
172
132
  __name(checkDatabaseVersion, "checkDatabaseVersion");
133
+ function registerDialects() {
134
+ [import_sqlite_dialect.SqliteDialect, import_mysql_dialect.MysqlDialect, import_mariadb_dialect.MariadbDialect, import_postgres_dialect.PostgresDialect].forEach((dialect) => {
135
+ import_database.Database.registerDialect(dialect);
136
+ });
137
+ }
138
+ __name(registerDialects, "registerDialects");
173
139
  // Annotate the CommonJS export names for ESM import in node:
174
140
  0 && (module.exports = {
175
141
  checkDatabaseVersion,
176
- parseDatabaseOptionsFromEnv
142
+ parseDatabaseOptionsFromEnv,
143
+ registerDialects
177
144
  });
package/lib/index.d.ts CHANGED
@@ -37,3 +37,4 @@ export * from './helpers';
37
37
  export { default as sqlParser, SQLParserTypes } from './sql-parser';
38
38
  export * from './interfaces';
39
39
  export { default as fieldTypeMap } from './view/field-type-map';
40
+ export * from './dialects';
package/lib/index.js CHANGED
@@ -92,6 +92,7 @@ __reExport(src_exports, require("./helpers"), module.exports);
92
92
  var import_sql_parser = __toESM(require("./sql-parser"));
93
93
  __reExport(src_exports, require("./interfaces"), module.exports);
94
94
  var import_field_type_map = __toESM(require("./view/field-type-map"));
95
+ __reExport(src_exports, require("./dialects"), module.exports);
95
96
  // Annotate the CommonJS export names for ESM import in node:
96
97
  0 && (module.exports = {
97
98
  BaseError,
@@ -139,5 +140,6 @@ var import_field_type_map = __toESM(require("./view/field-type-map"));
139
140
  ...require("./view-collection"),
140
141
  ...require("./view/view-inference"),
141
142
  ...require("./helpers"),
142
- ...require("./interfaces")
143
+ ...require("./interfaces"),
144
+ ...require("./dialects")
143
145
  });
@@ -58,7 +58,11 @@ const _BooleanInterface = class _BooleanInterface extends import_base_interface.
58
58
  const option = enumConfig.find((item) => item.value === value);
59
59
  return option == null ? void 0 : option.label;
60
60
  } else {
61
- return value ? "\u662F" : value === null || value === void 0 ? "" : "\u5426";
61
+ const label = value ? "True" : value === null || value === void 0 ? "" : "False";
62
+ if (ctx == null ? void 0 : ctx.t) {
63
+ return ctx.t(label, { ns: "action-export" });
64
+ }
65
+ return label;
62
66
  }
63
67
  }
64
68
  };
@@ -64,7 +64,13 @@ const _MultipleSelectInterface = class _MultipleSelectInterface extends import_b
64
64
  const enumConfig = ((_a = this.options.uiSchema) == null ? void 0 : _a.enum) || [];
65
65
  return import_lodash.default.castArray(value).map((value2) => {
66
66
  const option = enumConfig.find((item) => item.value === value2);
67
- return option ? option.label : value2;
67
+ if (option) {
68
+ if (ctx == null ? void 0 : ctx.t) {
69
+ return ctx.t(option.label, { ns: "lm-collections" });
70
+ }
71
+ return option.label;
72
+ }
73
+ return value2;
68
74
  }).join(",");
69
75
  }
70
76
  };
@@ -52,7 +52,13 @@ const _SelectInterface = class _SelectInterface extends import_base_interface.Ba
52
52
  var _a;
53
53
  const enumConfig = ((_a = this.options.uiSchema) == null ? void 0 : _a.enum) || [];
54
54
  const option = enumConfig.find((item) => item.value === value);
55
- return (option == null ? void 0 : option.label) || value;
55
+ if (option) {
56
+ if (ctx == null ? void 0 : ctx.t) {
57
+ return ctx.t(option.label, { ns: "lm-collections" });
58
+ }
59
+ return option.label;
60
+ }
61
+ return value;
56
62
  }
57
63
  };
58
64
  __name(_SelectInterface, "SelectInterface");
@@ -39,7 +39,7 @@ const _ToOneInterface = class _ToOneInterface extends import_base_interface.Base
39
39
  if (!str) {
40
40
  return null;
41
41
  }
42
- const { filterKey, targetCollection, transaction } = ctx;
42
+ const { filterKey, associationField, targetCollection, transaction } = ctx;
43
43
  const targetInstance = await targetCollection.repository.findOne({
44
44
  filter: {
45
45
  [filterKey]: str
@@ -49,8 +49,8 @@ const _ToOneInterface = class _ToOneInterface extends import_base_interface.Base
49
49
  if (!targetInstance) {
50
50
  throw new Error(`"${str}" not found in ${targetCollection.model.name} ${filterKey}`);
51
51
  }
52
- const primaryKeyAttribute = targetCollection.model.primaryKeyAttribute;
53
- return targetInstance[primaryKeyAttribute];
52
+ const targetKey = associationField.targetKey || targetCollection.model.primaryKeyAttribute;
53
+ return targetInstance[targetKey];
54
54
  }
55
55
  };
56
56
  __name(_ToOneInterface, "ToOneInterface");
package/lib/model.d.ts CHANGED
@@ -20,6 +20,7 @@ export declare class Model<TModelAttributes extends {} = any, TCreationAttribute
20
20
  protected _previousDataValuesWithAssociations: {};
21
21
  get db(): Database;
22
22
  static sync(options: any): Promise<any>;
23
+ static callSetters(values: any, options: any): {};
23
24
  toChangedWithAssociations(): void;
24
25
  changedWithAssociations(key?: string, value?: any): boolean | unknown[] | this;
25
26
  clearChangedWithAssociations(): void;
package/lib/model.js CHANGED
@@ -56,6 +56,18 @@ const _Model = class _Model extends import_sequelize.Model {
56
56
  const runner = new import_sync_runner.SyncRunner(this);
57
57
  return await runner.runSync(options);
58
58
  }
59
+ static callSetters(values, options) {
60
+ const result = {};
61
+ for (const key of Object.keys(values)) {
62
+ const field = this.collection.getField(key);
63
+ if (field && field.setter) {
64
+ result[key] = field.setter.call(field, values[key], options, values, key);
65
+ } else {
66
+ result[key] = values[key];
67
+ }
68
+ }
69
+ return result;
70
+ }
59
71
  // TODO
60
72
  toChangedWithAssociations() {
61
73
  this._changedWithAssociations = /* @__PURE__ */ new Set([...this._changedWithAssociations, ...this._changed]);
@@ -7,9 +7,11 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
 
10
+ var __create = Object.create;
10
11
  var __defProp = Object.defineProperty;
11
12
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
13
  var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
13
15
  var __hasOwnProp = Object.prototype.hasOwnProperty;
14
16
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
17
  var __export = (target, all) => {
@@ -24,6 +26,14 @@ var __copyProps = (to, from, except, desc) => {
24
26
  }
25
27
  return to;
26
28
  };
29
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
30
+ // If the importer is in node compatibility mode or this is not an ESM
31
+ // file that has been converted to a CommonJS file using a Babel-
32
+ // compatible transform (i.e. "__esModule" has not been set), then set
33
+ // "default" to the CommonJS "module.exports" for node compatibility.
34
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
35
+ mod
36
+ ));
27
37
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
38
  var date_exports = {};
29
39
  __export(date_exports, {
@@ -32,116 +42,148 @@ __export(date_exports, {
32
42
  module.exports = __toCommonJS(date_exports);
33
43
  var import_utils = require("@nocobase/utils");
34
44
  var import_sequelize = require("sequelize");
45
+ var import_moment = __toESM(require("moment"));
35
46
  function isDate(input) {
36
47
  return input instanceof Date || Object.prototype.toString.call(input) === "[object Date]";
37
48
  }
38
49
  __name(isDate, "isDate");
39
- const toDate = /* @__PURE__ */ __name((date) => {
40
- if (isDate(date)) {
41
- return date;
50
+ const toDate = /* @__PURE__ */ __name((date, options = {}) => {
51
+ const { ctx } = options;
52
+ let val = isDate(date) ? date : new Date(date);
53
+ const field = ctx.db.getFieldByPath(ctx.fieldPath);
54
+ if (!field) {
55
+ return val;
42
56
  }
43
- return new Date(date);
57
+ if (field.constructor.name === "UnixTimestampField") {
58
+ val = field.dateToValue(val);
59
+ }
60
+ if (field.constructor.name === "DatetimeNoTzField") {
61
+ val = (0, import_moment.default)(val).utcOffset("+00:00").format("YYYY-MM-DD HH:mm:ss");
62
+ }
63
+ if (field.constructor.name === "DateOnlyField") {
64
+ val = (0, import_moment.default)(val).format("YYYY-MM-DD HH:mm:ss");
65
+ }
66
+ const eventObj = {
67
+ val,
68
+ fieldType: field.type
69
+ };
70
+ ctx.db.emit("filterToDate", eventObj);
71
+ return eventObj.val;
44
72
  }, "toDate");
73
+ function parseDateTimezone(ctx) {
74
+ const field = ctx.db.getFieldByPath(ctx.fieldPath);
75
+ if (!field) {
76
+ return ctx.db.options.timezone;
77
+ }
78
+ if (field.constructor.name === "DatetimeNoTzField") {
79
+ return "+00:00";
80
+ }
81
+ if (field.constructor.name === "DateOnlyField") {
82
+ return "+00:00";
83
+ }
84
+ return ctx.db.options.timezone;
85
+ }
86
+ __name(parseDateTimezone, "parseDateTimezone");
45
87
  var date_default = {
46
88
  $dateOn(value, ctx) {
47
89
  const r = (0, import_utils.parseDate)(value, {
48
- timezone: ctx.db.options.timezone
90
+ timezone: parseDateTimezone(ctx)
49
91
  });
50
92
  if (typeof r === "string") {
51
93
  return {
52
- [import_sequelize.Op.eq]: toDate(r)
94
+ [import_sequelize.Op.eq]: toDate(r, { ctx })
53
95
  };
54
96
  }
55
97
  if (Array.isArray(r)) {
56
98
  return {
57
- [import_sequelize.Op.and]: [{ [import_sequelize.Op.gte]: toDate(r[0]) }, { [import_sequelize.Op.lt]: toDate(r[1]) }]
99
+ [import_sequelize.Op.and]: [{ [import_sequelize.Op.gte]: toDate(r[0], { ctx }) }, { [import_sequelize.Op.lt]: toDate(r[1], { ctx }) }]
58
100
  };
59
101
  }
60
102
  throw new Error(`Invalid Date ${JSON.stringify(value)}`);
61
103
  },
62
104
  $dateNotOn(value, ctx) {
63
105
  const r = (0, import_utils.parseDate)(value, {
64
- timezone: ctx.db.options.timezone
106
+ timezone: parseDateTimezone(ctx)
65
107
  });
66
108
  if (typeof r === "string") {
67
109
  return {
68
- [import_sequelize.Op.ne]: toDate(r)
110
+ [import_sequelize.Op.ne]: toDate(r, { ctx })
69
111
  };
70
112
  }
71
113
  if (Array.isArray(r)) {
72
114
  return {
73
- [import_sequelize.Op.or]: [{ [import_sequelize.Op.lt]: toDate(r[0]) }, { [import_sequelize.Op.gte]: toDate(r[1]) }]
115
+ [import_sequelize.Op.or]: [{ [import_sequelize.Op.lt]: toDate(r[0], { ctx }) }, { [import_sequelize.Op.gte]: toDate(r[1], { ctx }) }]
74
116
  };
75
117
  }
76
118
  throw new Error(`Invalid Date ${JSON.stringify(value)}`);
77
119
  },
78
120
  $dateBefore(value, ctx) {
79
121
  const r = (0, import_utils.parseDate)(value, {
80
- timezone: ctx.db.options.timezone
122
+ timezone: parseDateTimezone(ctx)
81
123
  });
82
124
  if (typeof r === "string") {
83
125
  return {
84
- [import_sequelize.Op.lt]: toDate(r)
126
+ [import_sequelize.Op.lt]: toDate(r, { ctx })
85
127
  };
86
128
  } else if (Array.isArray(r)) {
87
129
  return {
88
- [import_sequelize.Op.lt]: toDate(r[0])
130
+ [import_sequelize.Op.lt]: toDate(r[0], { ctx })
89
131
  };
90
132
  }
91
133
  throw new Error(`Invalid Date ${JSON.stringify(value)}`);
92
134
  },
93
135
  $dateNotBefore(value, ctx) {
94
136
  const r = (0, import_utils.parseDate)(value, {
95
- timezone: ctx.db.options.timezone
137
+ timezone: parseDateTimezone(ctx)
96
138
  });
97
139
  if (typeof r === "string") {
98
140
  return {
99
- [import_sequelize.Op.gte]: toDate(r)
141
+ [import_sequelize.Op.gte]: toDate(r, { ctx })
100
142
  };
101
143
  } else if (Array.isArray(r)) {
102
144
  return {
103
- [import_sequelize.Op.gte]: toDate(r[0])
145
+ [import_sequelize.Op.gte]: toDate(r[0], { ctx })
104
146
  };
105
147
  }
106
148
  throw new Error(`Invalid Date ${JSON.stringify(value)}`);
107
149
  },
108
150
  $dateAfter(value, ctx) {
109
151
  const r = (0, import_utils.parseDate)(value, {
110
- timezone: ctx.db.options.timezone
152
+ timezone: parseDateTimezone(ctx)
111
153
  });
112
154
  if (typeof r === "string") {
113
155
  return {
114
- [import_sequelize.Op.gt]: toDate(r)
156
+ [import_sequelize.Op.gt]: toDate(r, { ctx })
115
157
  };
116
158
  } else if (Array.isArray(r)) {
117
159
  return {
118
- [import_sequelize.Op.gte]: toDate(r[1])
160
+ [import_sequelize.Op.gte]: toDate(r[1], { ctx })
119
161
  };
120
162
  }
121
163
  throw new Error(`Invalid Date ${JSON.stringify(value)}`);
122
164
  },
123
165
  $dateNotAfter(value, ctx) {
124
166
  const r = (0, import_utils.parseDate)(value, {
125
- timezone: ctx.db.options.timezone
167
+ timezone: parseDateTimezone(ctx)
126
168
  });
127
169
  if (typeof r === "string") {
128
170
  return {
129
- [import_sequelize.Op.lte]: toDate(r)
171
+ [import_sequelize.Op.lte]: toDate(r, { ctx })
130
172
  };
131
173
  } else if (Array.isArray(r)) {
132
174
  return {
133
- [import_sequelize.Op.lt]: toDate(r[1])
175
+ [import_sequelize.Op.lt]: toDate(r[1], { ctx })
134
176
  };
135
177
  }
136
178
  throw new Error(`Invalid Date ${JSON.stringify(value)}`);
137
179
  },
138
180
  $dateBetween(value, ctx) {
139
181
  const r = (0, import_utils.parseDate)(value, {
140
- timezone: ctx.db.options.timezone
182
+ timezone: parseDateTimezone(ctx)
141
183
  });
142
184
  if (r) {
143
185
  return {
144
- [import_sequelize.Op.and]: [{ [import_sequelize.Op.gte]: toDate(r[0]) }, { [import_sequelize.Op.lt]: toDate(r[1]) }]
186
+ [import_sequelize.Op.and]: [{ [import_sequelize.Op.gte]: toDate(r[0], { ctx }) }, { [import_sequelize.Op.lt]: toDate(r[1], { ctx }) }]
145
187
  };
146
188
  }
147
189
  throw new Error(`Invalid Date ${JSON.stringify(value)}`);