@nocobase/database 1.3.38-beta → 1.4.0-alpha

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 (53) hide show
  1. package/lib/collection.d.ts +3 -2
  2. package/lib/collection.js +6 -0
  3. package/lib/database.d.ts +11 -23
  4. package/lib/database.js +21 -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/fields/date-field.d.ts +7 -2
  18. package/lib/fields/date-field.js +89 -0
  19. package/lib/fields/date-only-field.d.ts +15 -0
  20. package/lib/fields/date-only-field.js +45 -0
  21. package/lib/fields/datetime-field.d.ts +15 -0
  22. package/lib/fields/datetime-field.js +41 -0
  23. package/lib/fields/datetime-no-tz-field.d.ts +24 -0
  24. package/lib/fields/datetime-no-tz-field.js +128 -0
  25. package/lib/fields/datetime-tz-field.d.ts +15 -0
  26. package/lib/fields/datetime-tz-field.js +41 -0
  27. package/lib/fields/field.d.ts +1 -1
  28. package/lib/fields/field.js +3 -2
  29. package/lib/fields/index.d.ts +10 -1
  30. package/lib/fields/index.js +11 -1
  31. package/lib/fields/unix-timestamp-field.d.ts +22 -0
  32. package/lib/fields/unix-timestamp-field.js +94 -0
  33. package/lib/helpers.d.ts +2 -1
  34. package/lib/helpers.js +16 -49
  35. package/lib/index.d.ts +1 -0
  36. package/lib/index.js +3 -1
  37. package/lib/model.d.ts +1 -0
  38. package/lib/model.js +12 -0
  39. package/lib/operators/date.js +66 -24
  40. package/lib/options-parser.d.ts +1 -0
  41. package/lib/options-parser.js +32 -9
  42. package/lib/query-interface/query-interface-builder.js +3 -0
  43. package/lib/relation-repository/hasmany-repository.js +8 -11
  44. package/lib/relation-repository/multiple-relation-repository.d.ts +1 -0
  45. package/lib/relation-repository/multiple-relation-repository.js +11 -3
  46. package/lib/relation-repository/relation-repository.d.ts +6 -3
  47. package/lib/relation-repository/relation-repository.js +27 -4
  48. package/lib/repository.d.ts +5 -2
  49. package/lib/repository.js +27 -16
  50. package/lib/update-associations.d.ts +2 -1
  51. package/lib/view/field-type-map.d.ts +2 -2
  52. package/lib/view/field-type-map.js +17 -17
  53. package/package.json +4 -4
@@ -73,13 +73,27 @@ const _RelationRepository = class _RelationRepository {
73
73
  this.db = sourceCollection.context.database;
74
74
  this.database = this.db;
75
75
  this.sourceCollection = sourceCollection;
76
- this.sourceKeyValue = sourceKeyValue;
76
+ this.setSourceKeyValue(sourceKeyValue);
77
77
  this.associationName = association;
78
78
  this.association = this.sourceCollection.model.associations[association];
79
79
  this.associationField = this.sourceCollection.getField(association);
80
80
  this.targetModel = this.association.target;
81
81
  this.targetCollection = this.sourceCollection.context.database.modelCollection.get(this.targetModel);
82
82
  }
83
+ decodeMultiTargetKey(str) {
84
+ try {
85
+ const decoded = decodeURIComponent(str);
86
+ return JSON.parse(decoded);
87
+ } catch (e) {
88
+ return false;
89
+ }
90
+ }
91
+ setSourceKeyValue(sourceKeyValue) {
92
+ this.sourceKeyValue = typeof sourceKeyValue === "string" ? this.decodeMultiTargetKey(sourceKeyValue) || sourceKeyValue : sourceKeyValue;
93
+ }
94
+ isMultiTargetKey(value) {
95
+ return import_lodash.default.isPlainObject(value || this.sourceKeyValue);
96
+ }
83
97
  get collection() {
84
98
  return this.db.getCollection(this.targetModel.name);
85
99
  }
@@ -109,7 +123,7 @@ const _RelationRepository = class _RelationRepository {
109
123
  }
110
124
  convertTk(options) {
111
125
  let tk = options;
112
- if (typeof options === "object" && options["tk"]) {
126
+ if (typeof options === "object" && "tk" in options) {
113
127
  tk = options["tk"];
114
128
  }
115
129
  return tk;
@@ -119,7 +133,10 @@ const _RelationRepository = class _RelationRepository {
119
133
  if (typeof tk === "string") {
120
134
  tk = tk.split(",");
121
135
  }
122
- return import_lodash.default.castArray(tk);
136
+ if (tk) {
137
+ return import_lodash.default.castArray(tk);
138
+ }
139
+ return [];
123
140
  }
124
141
  targetKey() {
125
142
  return this.associationField.targetKey;
@@ -147,7 +164,13 @@ const _RelationRepository = class _RelationRepository {
147
164
  }
148
165
  async getSourceModel(transaction2) {
149
166
  if (!this.sourceInstance) {
150
- this.sourceInstance = await this.sourceCollection.model.findOne({
167
+ this.sourceInstance = this.isMultiTargetKey() ? await this.sourceCollection.repository.findOne({
168
+ filter: {
169
+ // @ts-ignore
170
+ ...this.sourceKeyValue
171
+ },
172
+ transaction: transaction2
173
+ }) : await this.sourceCollection.model.findOne({
151
174
  where: {
152
175
  [this.associationField.sourceKey]: this.sourceKeyValue
153
176
  },
@@ -26,7 +26,9 @@ export { Transactionable } from 'sequelize';
26
26
  export interface FilterAble {
27
27
  filter: Filter;
28
28
  }
29
- export type TargetKey = string | number;
29
+ export type BaseTargetKey = string | number;
30
+ export type MultiTargetKey = Record<string, BaseTargetKey>;
31
+ export type TargetKey = BaseTargetKey | MultiTargetKey;
30
32
  export type TK = TargetKey | TargetKey[];
31
33
  type FieldValue = string | number | bigint | boolean | Date | Buffer | null | FieldValue[] | FilterWithOperator;
32
34
  type Operators = keyof typeof operators & keyof WhereOperators;
@@ -116,7 +118,7 @@ declare class RelationRepositoryBuilder<R extends RelationRepository> {
116
118
  BelongsToArray: typeof BelongsToArrayRepository;
117
119
  };
118
120
  constructor(collection: Collection, associationName: string);
119
- of(id: string | number): R;
121
+ of(id: TargetKey): R;
120
122
  protected builder(): {
121
123
  HasOne: typeof HasOneRepository;
122
124
  BelongsTo: typeof BelongsToRepository;
@@ -171,6 +173,7 @@ export declare class Repository<TModelAttributes extends {} = any, TCreationAttr
171
173
  *
172
174
  */
173
175
  findById(id: string | number): Promise<Model<any, any>>;
176
+ findByTargetKey(targetKey: TargetKey): Promise<any>;
174
177
  /**
175
178
  * Find one record from database
176
179
  *
package/lib/repository.js CHANGED
@@ -175,27 +175,24 @@ const _Repository = class _Repository {
175
175
  };
176
176
  }
177
177
  if (countOptions == null ? void 0 : countOptions.filterByTk) {
178
+ const optionParser = new import_options_parser.OptionsParser(options, {
179
+ collection: this.collection
180
+ });
178
181
  options["where"] = {
179
- [import_sequelize.Op.and]: [
180
- options["where"] || {},
181
- {
182
- [this.collection.filterTargetKey]: options.filterByTk
183
- }
184
- ]
182
+ [import_sequelize.Op.and]: [options["where"] || {}, optionParser.filterByTkToWhereOption()]
185
183
  };
186
184
  }
187
185
  const queryOptions = {
188
186
  ...options,
189
- distinct: Boolean(this.collection.model.primaryKeyAttribute)
187
+ distinct: Boolean(this.collection.model.primaryKeyAttribute) && !this.collection.isMultiFilterTargetKey()
190
188
  };
191
189
  if (((_a = queryOptions.include) == null ? void 0 : _a.length) === 0) {
192
190
  delete queryOptions.include;
193
191
  }
194
- const count = await this.collection.model.count({
192
+ return await this.collection.model.count({
195
193
  ...queryOptions,
196
194
  transaction: transaction2
197
195
  });
198
- return count;
199
196
  }
200
197
  async aggregate(options) {
201
198
  var _a;
@@ -325,6 +322,9 @@ const _Repository = class _Repository {
325
322
  findById(id) {
326
323
  return this.collection.model.findByPk(id);
327
324
  }
325
+ findByTargetKey(targetKey) {
326
+ return this.findOne({ filterByTk: targetKey });
327
+ }
328
328
  /**
329
329
  * Find one record from database
330
330
  *
@@ -374,7 +374,7 @@ const _Repository = class _Repository {
374
374
  action: "create",
375
375
  underscored: this.collection.options.underscored
376
376
  });
377
- const values = guard.sanitize(options.values || {});
377
+ const values = this.model.callSetters(guard.sanitize(options.values || {}), options);
378
378
  const instance = await this.model.create(values, {
379
379
  ...options,
380
380
  transaction: transaction2
@@ -418,7 +418,7 @@ const _Repository = class _Repository {
418
418
  }
419
419
  const transaction2 = await this.getTransaction(options);
420
420
  const guard = import_update_guard.UpdateGuard.fromOptions(this.model, { ...options, underscored: this.collection.options.underscored });
421
- const values = guard.sanitize(options.values);
421
+ const values = this.model.callSetters(guard.sanitize(options.values || {}), options);
422
422
  if (options.individualHooks === false) {
423
423
  const { model: Model2 } = this.collection;
424
424
  const primaryKeyField = Model2.primaryKeyField || Model2.primaryKeyAttribute;
@@ -501,15 +501,26 @@ const _Repository = class _Repository {
501
501
  }
502
502
  }
503
503
  if (filterByTk && !options.filter) {
504
- return await this.model.destroy({
504
+ const where = [];
505
+ for (const tk of filterByTk) {
506
+ const optionParser = new import_options_parser.OptionsParser(
507
+ {
508
+ filterByTk: tk
509
+ },
510
+ {
511
+ collection: this.collection
512
+ }
513
+ );
514
+ where.push(optionParser.filterByTkToWhereOption());
515
+ }
516
+ const destroyOptions = {
505
517
  ...options,
506
518
  where: {
507
- [modelFilterKey]: {
508
- [import_sequelize.Op.in]: filterByTk
509
- }
519
+ [import_sequelize.Op.or]: where
510
520
  },
511
521
  transaction: transaction2
512
- });
522
+ };
523
+ return await this.model.destroy(destroyOptions);
513
524
  }
514
525
  if (options.filter && (0, import_utils.isValidFilter)(options.filter)) {
515
526
  if (this.collection.model.primaryKeyAttributes.length !== 1 && !import_lodash.default.get(this.collection.options, "filterTargetKey")) {
@@ -8,6 +8,7 @@
8
8
  */
9
9
  import { Association, BelongsToMany, Hookable, Transactionable } from 'sequelize';
10
10
  import { Model } from './model';
11
+ import { TargetKey } from './repository';
11
12
  export declare function modelAssociations(instance: Model): {
12
13
  [key: string]: Association<import("sequelize").Model<any, any>, import("sequelize").Model<any, any>>;
13
14
  };
@@ -18,7 +19,7 @@ type UpdateValue = {
18
19
  };
19
20
  interface UpdateOptions extends Transactionable {
20
21
  filter?: any;
21
- filterByTk?: number | string;
22
+ filterByTk?: TargetKey;
22
23
  whitelist?: string[];
23
24
  blacklist?: string[];
24
25
  updateAssociationValues?: string[];
@@ -62,7 +62,7 @@ declare const fieldTypeMap: {
62
62
  boolean: string;
63
63
  decimal: string;
64
64
  year: string[];
65
- datetime: string;
65
+ datetime: string[];
66
66
  timestamp: string;
67
67
  json: string[];
68
68
  enum: string;
@@ -104,7 +104,7 @@ declare const fieldTypeMap: {
104
104
  boolean: string;
105
105
  decimal: string;
106
106
  year: string[];
107
- datetime: string;
107
+ datetime: string[];
108
108
  timestamp: string;
109
109
  json: string[];
110
110
  enum: string;
@@ -30,24 +30,24 @@ __export(field_type_map_exports, {
30
30
  });
31
31
  module.exports = __toCommonJS(field_type_map_exports);
32
32
  const postgres = {
33
- "character varying": ["string", "uuid", "nanoid", "encryption"],
34
- varchar: ["string", "uuid", "nanoid", "encryption"],
35
- char: ["string", "uuid", "nanoid", "encryption"],
33
+ "character varying": ["string", "uuid", "nanoid", "encryption", "datetimeNoTz"],
34
+ varchar: ["string", "uuid", "nanoid", "encryption", "datetimeNoTz"],
35
+ char: ["string", "uuid", "nanoid", "encryption", "datetimeNoTz"],
36
36
  character: "string",
37
37
  text: "text",
38
38
  oid: "string",
39
39
  name: "string",
40
40
  smallint: ["integer", "sort"],
41
- integer: ["integer", "sort"],
42
- bigint: ["bigInt", "sort"],
41
+ integer: ["integer", "unixTimestamp", "sort"],
42
+ bigint: ["bigInt", "unixTimestamp", "sort"],
43
43
  decimal: "decimal",
44
44
  numeric: "float",
45
45
  real: "float",
46
46
  "double precision": "float",
47
- "timestamp without time zone": "date",
48
- "timestamp with time zone": "date",
47
+ "timestamp without time zone": "datetimeNoTz",
48
+ "timestamp with time zone": "datetimeTz",
49
49
  "time without time zone": "time",
50
- date: "date",
50
+ date: "dateOnly",
51
51
  boolean: "boolean",
52
52
  json: ["json", "array"],
53
53
  jsonb: ["json", "array", "jsonb"],
@@ -68,24 +68,24 @@ const mysql = {
68
68
  "mediumint unsigned": ["integer", "boolean", "sort"],
69
69
  char: ["string", "uuid", "nanoid", "encryption"],
70
70
  varchar: ["string", "uuid", "nanoid", "encryption"],
71
- date: "date",
71
+ date: "dateOnly",
72
72
  time: "time",
73
73
  tinytext: "text",
74
74
  text: "text",
75
75
  mediumtext: "text",
76
76
  longtext: "text",
77
- int: ["integer", "sort"],
78
- "int unsigned": ["integer", "sort"],
79
- integer: ["integer", "sort"],
80
- bigint: ["bigInt", "sort"],
81
- "bigint unsigned": ["bigInt", "sort"],
77
+ int: ["integer", "unixTimestamp", "sort"],
78
+ "int unsigned": ["integer", "unixTimestamp", "sort"],
79
+ integer: ["integer", "unixTimestamp", "sort"],
80
+ bigint: ["bigInt", "unixTimestamp", "sort"],
81
+ "bigint unsigned": ["bigInt", "unixTimestamp", "sort"],
82
82
  float: "float",
83
83
  double: "float",
84
84
  boolean: "boolean",
85
85
  decimal: "decimal",
86
86
  year: ["string", "integer"],
87
- datetime: "date",
88
- timestamp: "date",
87
+ datetime: ["datetimeNoTz", "datetimeTz"],
88
+ timestamp: "datetimeTz",
89
89
  json: ["json", "array"],
90
90
  enum: "string"
91
91
  };
@@ -94,7 +94,7 @@ const sqlite = {
94
94
  varchar: ["string", "uuid", "nanoid", "encryption"],
95
95
  integer: "integer",
96
96
  real: "real",
97
- datetime: "date",
97
+ datetime: "datetimeTz",
98
98
  date: "date",
99
99
  time: "time",
100
100
  boolean: "boolean",
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/database",
3
- "version": "1.3.38-beta",
3
+ "version": "1.4.0-alpha",
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.3.38-beta",
10
- "@nocobase/utils": "1.3.38-beta",
9
+ "@nocobase/logger": "1.4.0-alpha",
10
+ "@nocobase/utils": "1.4.0-alpha",
11
11
  "async-mutex": "^0.3.2",
12
12
  "chalk": "^4.1.1",
13
13
  "cron-parser": "4.4.0",
@@ -38,5 +38,5 @@
38
38
  "url": "git+https://github.com/nocobase/nocobase.git",
39
39
  "directory": "packages/database"
40
40
  },
41
- "gitHead": "1b9490667c9224cd0b179cbf75e10a581c49b230"
41
+ "gitHead": "f097a2bddec152522b5645bd5d451f4c866d2060"
42
42
  }