@nocobase/database 2.1.0-beta.2 → 2.1.0-beta.21

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.
@@ -15,6 +15,7 @@ import { Database } from './database';
15
15
  import { ArrayFieldRepository } from './field-repository/array-field-repository';
16
16
  import { Model } from './model';
17
17
  import operators from './operators';
18
+ import type { QueryOptions } from './query/types';
18
19
  import { BelongsToManyRepository } from './relation-repository/belongs-to-many-repository';
19
20
  import { BelongsToRepository } from './relation-repository/belongs-to-repository';
20
21
  import { HasManyRepository } from './relation-repository/hasmany-repository';
@@ -136,6 +137,8 @@ export interface AggregateOptions {
136
137
  filter?: Filter;
137
138
  distinct?: boolean;
138
139
  }
140
+ export interface QueryOptionsWithTransaction extends QueryOptions, Transactionable {
141
+ }
139
142
  export interface FirstOrCreateOptions extends Transactionable {
140
143
  filterKeys: string[];
141
144
  values?: Values;
@@ -156,6 +159,7 @@ export declare class Repository<TModelAttributes extends {} = any, TCreationAttr
156
159
  count(countOptions?: CountOptions): Promise<number>;
157
160
  getEstimatedRowCount(): Promise<number>;
158
161
  private getOracleSchema;
162
+ query(options?: QueryOptionsWithTransaction): Promise<any[]>;
159
163
  aggregate(options: AggregateOptions & {
160
164
  optionsTransformer?: (options: any) => any;
161
165
  }): Promise<any>;
package/lib/repository.js CHANGED
@@ -65,6 +65,7 @@ var import_array_field_repository = require("./field-repository/array-field-repo
65
65
  var import_fields = require("./fields");
66
66
  var import_filter_parser = __toESM(require("./filter-parser"));
67
67
  var import_options_parser = require("./options-parser");
68
+ var import_builder = require("./query/builder");
68
69
  var import_belongs_to_many_repository = require("./relation-repository/belongs-to-many-repository");
69
70
  var import_belongs_to_repository = require("./relation-repository/belongs-to-repository");
70
71
  var import_hasmany_repository = require("./relation-repository/hasmany-repository");
@@ -151,12 +152,13 @@ const _Repository = class _Repository {
151
152
  [import_sequelize.Op.and]: [options["where"] || {}, optionParser.filterByTkToWhereOption()]
152
153
  };
153
154
  }
155
+ const hasInclude = Array.isArray(options["include"]) && options["include"].length > 0;
154
156
  const queryOptions = {
155
- ...options,
156
- distinct: Boolean(this.collection.model.primaryKeyAttribute) && !this.collection.isMultiFilterTargetKey()
157
+ ...options
157
158
  };
158
- if (Array.isArray(queryOptions.include) && queryOptions.include.length > 0) {
159
+ if (hasInclude) {
159
160
  queryOptions.include = (0, import_utils2.processIncludes)(queryOptions.include, this.collection.model);
161
+ queryOptions.distinct = Boolean(this.collection.model.primaryKeyAttribute) && !this.collection.isMultiFilterTargetKey();
160
162
  } else {
161
163
  delete queryOptions.include;
162
164
  }
@@ -240,6 +242,21 @@ const _Repository = class _Repository {
240
242
  });
241
243
  return (result == null ? void 0 : result["USER"]) ?? "";
242
244
  }
245
+ async query(options = {}) {
246
+ const transaction2 = await this.getTransaction(options);
247
+ const { queryOptions, fieldMap } = (0, import_builder.buildQuery)(this.database, this.collection, options);
248
+ const finalQueryOptions = {
249
+ ...queryOptions,
250
+ transaction: transaction2
251
+ };
252
+ if (Array.isArray(finalQueryOptions.include) && finalQueryOptions.include.length > 0) {
253
+ finalQueryOptions.include = (0, import_utils2.processIncludes)(finalQueryOptions.include, this.collection.model);
254
+ } else {
255
+ delete finalQueryOptions.include;
256
+ }
257
+ const data = await this.model.findAll(finalQueryOptions);
258
+ return (0, import_builder.normalizeQueryResult)(data, fieldMap);
259
+ }
243
260
  async aggregate(options) {
244
261
  var _a;
245
262
  const { method, field } = options;
@@ -647,6 +664,9 @@ const _Repository = class _Repository {
647
664
  collection: this.collection
648
665
  });
649
666
  const params = parser.toSequelizeParams({ parseSort: import_lodash2.default.isBoolean(options == null ? void 0 : options.parseSort) ? options.parseSort : true });
667
+ if (parser.associationNotFoundWarnings.length > 0) {
668
+ this.database.logger.warn(parser.associationNotFoundWarnings.join("; "));
669
+ }
650
670
  debug("sequelize query params %o", params);
651
671
  if (options.where && params.where) {
652
672
  params.where = {
@@ -42,6 +42,19 @@ __export(field_validation_exports, {
42
42
  module.exports = __toCommonJS(field_validation_exports);
43
43
  var import_joi = __toESM(require("joi"));
44
44
  var import_lodash = __toESM(require("lodash"));
45
+ function getFractionLength(value) {
46
+ const normalized = value.trim().replace(/,/g, "");
47
+ if (!normalized || /e/i.test(normalized)) {
48
+ return 0;
49
+ }
50
+ const unsignedValue = normalized.startsWith("+") || normalized.startsWith("-") ? normalized.slice(1) : normalized;
51
+ const dotIndex = unsignedValue.indexOf(".");
52
+ if (dotIndex < 0) {
53
+ return 0;
54
+ }
55
+ return unsignedValue.slice(dotIndex + 1).length;
56
+ }
57
+ __name(getFractionLength, "getFractionLength");
45
58
  function buildJoiSchema(validation, options) {
46
59
  const { type, rules } = validation;
47
60
  const { label, value } = options;
@@ -58,16 +71,34 @@ function buildJoiSchema(validation, options) {
58
71
  }
59
72
  if (rules) {
60
73
  rules.forEach((rule) => {
61
- if (!import_lodash.default.isEmpty(rule.args)) {
62
- if (rule.name === "pattern" && !import_lodash.default.isRegExp(rule.args.regex)) {
63
- const lastSlash = rule.args.regex.lastIndexOf("/");
64
- const isRegExpStr = rule.args.regex.startsWith("/") && lastSlash > 0;
74
+ const args = import_lodash.default.cloneDeep(rule.args);
75
+ if (rule.name === "precision") {
76
+ const limit = Number(args == null ? void 0 : args.limit);
77
+ schema = schema.custom((currentValue, helpers) => {
78
+ if (Number.isNaN(limit)) {
79
+ return currentValue;
80
+ }
81
+ const originalValue = helpers.original;
82
+ if (originalValue === null || originalValue === void 0 || originalValue === "") {
83
+ return currentValue;
84
+ }
85
+ if (getFractionLength(String(originalValue)) > limit) {
86
+ return helpers.error("number.precision", { limit });
87
+ }
88
+ return currentValue;
89
+ });
90
+ return;
91
+ }
92
+ if (!import_lodash.default.isEmpty(args)) {
93
+ if (rule.name === "pattern" && !import_lodash.default.isRegExp(args.regex)) {
94
+ const lastSlash = args.regex.lastIndexOf("/");
95
+ const isRegExpStr = args.regex.startsWith("/") && lastSlash > 0;
65
96
  if (isRegExpStr) {
66
- rule.args.regex = rule.args.regex.slice(1, lastSlash);
97
+ args.regex = args.regex.slice(1, lastSlash);
67
98
  }
68
- rule.args.regex = new RegExp(rule.args.regex);
99
+ args.regex = new RegExp(args.regex);
69
100
  }
70
- schema = rule.paramsType === "object" ? schema[rule.name](rule.args) : schema[rule.name](...Object.values(rule.args));
101
+ schema = rule.paramsType === "object" ? schema[rule.name](args) : schema[rule.name](...Object.values(args));
71
102
  } else {
72
103
  schema = schema[rule.name]();
73
104
  }
@@ -23,7 +23,7 @@ declare const fieldTypeMap: {
23
23
  real: string;
24
24
  'double precision': string;
25
25
  'timestamp without time zone': string;
26
- 'timestamp with time zone': string;
26
+ 'timestamp with time zone': string[];
27
27
  'time without time zone': string;
28
28
  date: string;
29
29
  boolean: string;
@@ -63,7 +63,7 @@ declare const fieldTypeMap: {
63
63
  decimal: string;
64
64
  year: string[];
65
65
  datetime: string[];
66
- timestamp: string;
66
+ timestamp: string[];
67
67
  json: string[];
68
68
  enum: string;
69
69
  };
@@ -72,7 +72,7 @@ declare const fieldTypeMap: {
72
72
  varchar: string[];
73
73
  integer: string;
74
74
  real: string;
75
- datetime: string;
75
+ datetime: string[];
76
76
  date: string;
77
77
  time: string;
78
78
  boolean: string;
@@ -105,7 +105,7 @@ declare const fieldTypeMap: {
105
105
  decimal: string;
106
106
  year: string[];
107
107
  datetime: string[];
108
- timestamp: string;
108
+ timestamp: string[];
109
109
  json: string[];
110
110
  enum: string;
111
111
  };
@@ -45,7 +45,7 @@ const postgres = {
45
45
  real: "float",
46
46
  "double precision": "float",
47
47
  "timestamp without time zone": "datetimeNoTz",
48
- "timestamp with time zone": "datetimeTz",
48
+ "timestamp with time zone": ["datetimeTz", "date"],
49
49
  "time without time zone": "time",
50
50
  date: "dateOnly",
51
51
  boolean: "boolean",
@@ -84,8 +84,8 @@ const mysql = {
84
84
  boolean: "boolean",
85
85
  decimal: "decimal",
86
86
  year: ["string", "integer"],
87
- datetime: ["datetimeNoTz", "datetimeTz"],
88
- timestamp: "datetimeTz",
87
+ datetime: ["datetimeNoTz", "datetimeTz", "date"],
88
+ timestamp: ["datetimeTz", "date"],
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: "datetimeTz",
97
+ datetime: ["datetimeTz", "date"],
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": "2.1.0-beta.2",
3
+ "version": "2.1.0-beta.21",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
7
- "license": "AGPL-3.0",
7
+ "license": "Apache-2.0",
8
8
  "dependencies": {
9
- "@nocobase/logger": "2.1.0-beta.2",
10
- "@nocobase/utils": "2.1.0-beta.2",
9
+ "@nocobase/logger": "2.1.0-beta.21",
10
+ "@nocobase/utils": "2.1.0-beta.21",
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": "d80433799fb4a8d59ded4d7eea114d585a137ea0"
41
+ "gitHead": "324bd82f33fca58e98711688a17ceb65c186b65e"
42
42
  }