@nocobase/database 2.1.0-beta.9 → 2.2.0-beta.1

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 (45) hide show
  1. package/lib/collection.d.ts +14 -0
  2. package/lib/collection.js +13 -3
  3. package/lib/database.d.ts +1 -0
  4. package/lib/database.js +4 -0
  5. package/lib/eager-loading/eager-loading-tree.js +9 -4
  6. package/lib/errors/association-not-found-error.d.ts +11 -0
  7. package/lib/errors/association-not-found-error.js +44 -0
  8. package/lib/fields/password-field.d.ts +11 -3
  9. package/lib/fields/password-field.js +41 -21
  10. package/lib/fields/relation-field.js +1 -1
  11. package/lib/index.d.ts +3 -0
  12. package/lib/index.js +6 -0
  13. package/lib/interfaces/date-interface.d.ts +9 -0
  14. package/lib/interfaces/date-interface.js +27 -0
  15. package/lib/interfaces/datetime-interface.d.ts +3 -1
  16. package/lib/interfaces/datetime-interface.js +84 -9
  17. package/lib/interfaces/datetime-no-tz-interface.d.ts +9 -0
  18. package/lib/interfaces/datetime-no-tz-interface.js +16 -2
  19. package/lib/interfaces/time-interface.js +16 -0
  20. package/lib/interfaces/to-one-interface.js +1 -1
  21. package/lib/operators/date.js +12 -4
  22. package/lib/options-parser.d.ts +1 -0
  23. package/lib/options-parser.js +20 -4
  24. package/lib/query/builder.d.ts +17 -0
  25. package/lib/query/builder.js +268 -0
  26. package/lib/query/formatter.d.ts +29 -0
  27. package/lib/query/formatter.js +100 -0
  28. package/lib/query/formatters/mysql.d.ts +14 -0
  29. package/lib/query/formatters/mysql.js +70 -0
  30. package/lib/query/formatters/oracle.d.ts +14 -0
  31. package/lib/query/formatters/oracle.js +64 -0
  32. package/lib/query/formatters/postgres.d.ts +15 -0
  33. package/lib/query/formatters/postgres.js +70 -0
  34. package/lib/query/formatters/sqlite.d.ts +14 -0
  35. package/lib/query/formatters/sqlite.js +63 -0
  36. package/lib/query/having.d.ts +22 -0
  37. package/lib/query/having.js +112 -0
  38. package/lib/query/types.d.ts +40 -0
  39. package/lib/query/types.js +24 -0
  40. package/lib/repository.d.ts +4 -0
  41. package/lib/repository.js +23 -3
  42. package/lib/utils/field-validation.js +30 -0
  43. package/lib/view/field-type-map.d.ts +4 -4
  44. package/lib/view/field-type-map.js +4 -4
  45. package/package.json +4 -4
@@ -0,0 +1,64 @@
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 oracle_exports = {};
29
+ __export(oracle_exports, {
30
+ OracleQueryFormatter: () => OracleQueryFormatter
31
+ });
32
+ module.exports = __toCommonJS(oracle_exports);
33
+ var import_formatter = require("../formatter");
34
+ const _OracleQueryFormatter = class _OracleQueryFormatter extends import_formatter.QueryFormatter {
35
+ convertFormat(format) {
36
+ return format.replace(/hh/g, "HH24").replace(/mm/g, "MI").replace(/ss/g, "SS");
37
+ }
38
+ formatDate(field, format, timezone, _preserveLocalTime) {
39
+ const fmt = this.convertFormat(format);
40
+ const resolvedTimezone = this.getTimezoneByOffset(timezone);
41
+ if (resolvedTimezone) {
42
+ const quoted = this.sequelize.getQueryInterface().quoteIdentifiers(field.col);
43
+ return this.sequelize.fn(
44
+ "to_char",
45
+ this.sequelize.literal(`(${quoted} AT TIME ZONE '${resolvedTimezone}')`),
46
+ fmt
47
+ );
48
+ }
49
+ return this.sequelize.fn("to_char", field, fmt);
50
+ }
51
+ formatUnixTimestamp(field, format, accuracy = "second", timezone) {
52
+ const quoted = this.sequelize.getQueryInterface().quoteIdentifiers(field);
53
+ const timestamp = accuracy === "millisecond" ? `to_timestamp(ROUND(${quoted} / 1000))` : `to_timestamp(${quoted})`;
54
+ const resolvedTimezone = this.getTimezoneByOffset(timezone);
55
+ const literal = resolvedTimezone ? `${timestamp} AT TIME ZONE '${resolvedTimezone}'` : timestamp;
56
+ return this.sequelize.fn("to_char", this.sequelize.literal(literal), this.convertFormat(format));
57
+ }
58
+ };
59
+ __name(_OracleQueryFormatter, "OracleQueryFormatter");
60
+ let OracleQueryFormatter = _OracleQueryFormatter;
61
+ // Annotate the CommonJS export names for ESM import in node:
62
+ 0 && (module.exports = {
63
+ OracleQueryFormatter
64
+ });
@@ -0,0 +1,15 @@
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 { QueryFormatter, Col } from '../formatter';
10
+ export declare class PostgresQueryFormatter extends QueryFormatter {
11
+ convertFormat(format: string): string;
12
+ private buildTimezoneLiteral;
13
+ formatDate(field: Col, format: string, timezone?: string, _preserveLocalTime?: boolean): import("sequelize/types/utils").Fn;
14
+ formatUnixTimestamp(field: string, format: string, accuracy?: 'second' | 'millisecond', timezone?: string): import("sequelize/types/utils").Fn;
15
+ }
@@ -0,0 +1,70 @@
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 postgres_exports = {};
29
+ __export(postgres_exports, {
30
+ PostgresQueryFormatter: () => PostgresQueryFormatter
31
+ });
32
+ module.exports = __toCommonJS(postgres_exports);
33
+ var import_formatter = require("../formatter");
34
+ const _PostgresQueryFormatter = class _PostgresQueryFormatter extends import_formatter.QueryFormatter {
35
+ convertFormat(format) {
36
+ return format.replace(/hh/g, "HH24").replace(/mm/g, "MI").replace(/ss/g, "SS");
37
+ }
38
+ buildTimezoneLiteral(target) {
39
+ if (/^[+-]\d{1,2}:\d{2}$/.test(target)) {
40
+ return `INTERVAL '${target}'`;
41
+ }
42
+ return `'${target}'`;
43
+ }
44
+ formatDate(field, format, timezone, _preserveLocalTime) {
45
+ const fmt = this.convertFormat(format);
46
+ const resolvedTimezone = this.getTimezoneByOffset(timezone);
47
+ if (resolvedTimezone) {
48
+ const quoted = this.sequelize.getQueryInterface().quoteIdentifiers(field.col);
49
+ return this.sequelize.fn(
50
+ "to_char",
51
+ this.sequelize.literal(`timezone(${this.buildTimezoneLiteral(resolvedTimezone)}, ${quoted})`),
52
+ fmt
53
+ );
54
+ }
55
+ return this.sequelize.fn("to_char", field, fmt);
56
+ }
57
+ formatUnixTimestamp(field, format, accuracy = "second", timezone) {
58
+ const quoted = this.sequelize.getQueryInterface().quoteIdentifiers(field);
59
+ const timestamp = accuracy === "millisecond" ? `to_timestamp(ROUND(${quoted} / 1000))` : `to_timestamp(${quoted})`;
60
+ const resolvedTimezone = this.getTimezoneByOffset(timezone);
61
+ const literal = resolvedTimezone ? `timezone(${this.buildTimezoneLiteral(resolvedTimezone)}, ${timestamp})` : timestamp;
62
+ return this.sequelize.fn("to_char", this.sequelize.literal(literal), this.convertFormat(format));
63
+ }
64
+ };
65
+ __name(_PostgresQueryFormatter, "PostgresQueryFormatter");
66
+ let PostgresQueryFormatter = _PostgresQueryFormatter;
67
+ // Annotate the CommonJS export names for ESM import in node:
68
+ 0 && (module.exports = {
69
+ PostgresQueryFormatter
70
+ });
@@ -0,0 +1,14 @@
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 { QueryFormatter, Col } from '../formatter';
10
+ export declare class SQLiteQueryFormatter extends QueryFormatter {
11
+ convertFormat(format: string): string;
12
+ formatDate(field: Col, format: string, timezone?: string, preserveLocalTime?: boolean): import("sequelize/types/utils").Fn;
13
+ formatUnixTimestamp(field: string, format: string, accuracy?: 'second' | 'millisecond', timezone?: string): import("sequelize/types/utils").Fn;
14
+ }
@@ -0,0 +1,63 @@
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 sqlite_exports = {};
29
+ __export(sqlite_exports, {
30
+ SQLiteQueryFormatter: () => SQLiteQueryFormatter
31
+ });
32
+ module.exports = __toCommonJS(sqlite_exports);
33
+ var import_formatter = require("../formatter");
34
+ const _SQLiteQueryFormatter = class _SQLiteQueryFormatter extends import_formatter.QueryFormatter {
35
+ convertFormat(format) {
36
+ return format.replace(/YYYY/g, "%Y").replace(/MM/g, "%m").replace(/DD/g, "%d").replace(/hh/g, "%H").replace(/mm/g, "%M").replace(/ss/g, "%S");
37
+ }
38
+ formatDate(field, format, timezone, preserveLocalTime) {
39
+ const fmt = this.convertFormat(format);
40
+ if (timezone && /^[+-]\d{1,2}:\d{2}$/.test(timezone)) {
41
+ return this.sequelize.fn("strftime", fmt, field, this.getOffsetExpression(timezone));
42
+ }
43
+ if (preserveLocalTime && this.rawTimezone && /^[+-]\d{1,2}:\d{2}$/.test(this.rawTimezone)) {
44
+ return this.sequelize.fn("strftime", fmt, field, this.getOffsetExpression(this.rawTimezone));
45
+ }
46
+ return this.sequelize.fn("strftime", fmt, field);
47
+ }
48
+ formatUnixTimestamp(field, format, accuracy = "second", timezone) {
49
+ const quoted = this.sequelize.getQueryInterface().quoteIdentifiers(field);
50
+ const base = accuracy === "millisecond" ? this.sequelize.literal(`ROUND(${quoted} / 1000)`) : this.sequelize.col(field);
51
+ const args = [base, "unixepoch"];
52
+ if (timezone && /^[+-]\d{1,2}:\d{2}$/.test(timezone)) {
53
+ args.push(this.getOffsetExpression(timezone));
54
+ }
55
+ return this.sequelize.fn("strftime", this.convertFormat(format), this.sequelize.fn("DATETIME", ...args));
56
+ }
57
+ };
58
+ __name(_SQLiteQueryFormatter, "SQLiteQueryFormatter");
59
+ let SQLiteQueryFormatter = _SQLiteQueryFormatter;
60
+ // Annotate the CommonJS export names for ESM import in node:
61
+ 0 && (module.exports = {
62
+ SQLiteQueryFormatter
63
+ });
@@ -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 { WhereOptions } from 'sequelize';
10
+ import { Database } from '../database';
11
+ import { QueryFormatter } from './formatter';
12
+ type QueryFieldMeta = {
13
+ field: string;
14
+ alias?: string;
15
+ aggregation?: string;
16
+ distinct?: boolean;
17
+ format?: string;
18
+ type?: string;
19
+ options?: any;
20
+ };
21
+ export declare function buildHaving(database: Database, formatter: QueryFormatter, fieldMap: Record<string, QueryFieldMeta>, having?: any, timezone?: string): WhereOptions;
22
+ export {};
@@ -0,0 +1,112 @@
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 having_exports = {};
29
+ __export(having_exports, {
30
+ buildHaving: () => buildHaving
31
+ });
32
+ module.exports = __toCommonJS(having_exports);
33
+ var import_lodash = require("lodash");
34
+ var import_sequelize = require("sequelize");
35
+ function buildFieldExpression(database, formatter, fieldMap, key, timezone) {
36
+ const meta = fieldMap[key];
37
+ if (!meta) {
38
+ throw new Error(`Invalid having field: ${key}`);
39
+ }
40
+ const col = database.sequelize.col(meta.field);
41
+ if (meta.aggregation) {
42
+ return database.sequelize.fn(meta.aggregation, meta.distinct ? database.sequelize.fn("DISTINCT", col) : col);
43
+ }
44
+ if (meta.format) {
45
+ return formatter.format({
46
+ type: meta.type,
47
+ field: meta.field,
48
+ format: meta.format,
49
+ timezone,
50
+ fieldOptions: meta.options
51
+ });
52
+ }
53
+ return col;
54
+ }
55
+ __name(buildFieldExpression, "buildFieldExpression");
56
+ function isOperatorCondition(database, value) {
57
+ return (0, import_lodash.isPlainObject)(value) && Object.keys(value).some((key) => database.operators.has(key));
58
+ }
59
+ __name(isOperatorCondition, "isOperatorCondition");
60
+ function buildFieldCondition(database, formatter, fieldMap, key, value, timezone) {
61
+ const field = buildFieldExpression(database, formatter, fieldMap, key, timezone);
62
+ if (!isOperatorCondition(database, value)) {
63
+ return database.sequelize.where(field, { [import_sequelize.Op.eq]: value });
64
+ }
65
+ const condition = {};
66
+ for (const [operatorKey, operatorValue] of Object.entries(value)) {
67
+ const operator = database.operators.get(operatorKey);
68
+ if (typeof operator !== "symbol") {
69
+ throw new Error(`Unsupported having operator: ${operatorKey}`);
70
+ }
71
+ condition[operator] = operatorValue;
72
+ }
73
+ return database.sequelize.where(field, condition);
74
+ }
75
+ __name(buildFieldCondition, "buildFieldCondition");
76
+ function buildHavingNode(database, formatter, fieldMap, node, timezone) {
77
+ if (!(0, import_lodash.isPlainObject)(node)) {
78
+ throw new Error("Having must be an object");
79
+ }
80
+ const conditions = [];
81
+ for (const [key, value] of Object.entries(node)) {
82
+ if ((key === "$and" || key === "$or") && Array.isArray(value)) {
83
+ const operator = database.operators.get(key);
84
+ if (typeof operator !== "symbol") {
85
+ throw new Error(`Unsupported having operator: ${key}`);
86
+ }
87
+ conditions.push({
88
+ [operator]: value.map((item) => buildHavingNode(database, formatter, fieldMap, item, timezone))
89
+ });
90
+ continue;
91
+ }
92
+ conditions.push(buildFieldCondition(database, formatter, fieldMap, key, value, timezone));
93
+ }
94
+ if (conditions.length === 1) {
95
+ return conditions[0];
96
+ }
97
+ return {
98
+ [import_sequelize.Op.and]: conditions
99
+ };
100
+ }
101
+ __name(buildHavingNode, "buildHavingNode");
102
+ function buildHaving(database, formatter, fieldMap, having, timezone) {
103
+ if (!having) {
104
+ return void 0;
105
+ }
106
+ return buildHavingNode(database, formatter, fieldMap, having, timezone);
107
+ }
108
+ __name(buildHaving, "buildHaving");
109
+ // Annotate the CommonJS export names for ESM import in node:
110
+ 0 && (module.exports = {
111
+ buildHaving
112
+ });
@@ -0,0 +1,40 @@
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
+ export type QueryField = string | string[];
10
+ export interface QueryMeasure {
11
+ field: QueryField;
12
+ type?: string;
13
+ aggregation?: string;
14
+ alias?: string;
15
+ distinct?: boolean;
16
+ }
17
+ export interface QueryDimension {
18
+ field: QueryField;
19
+ type?: string;
20
+ alias?: string;
21
+ format?: string;
22
+ options?: any;
23
+ }
24
+ export interface QueryOrder {
25
+ field: QueryField;
26
+ alias?: string;
27
+ order?: 'asc' | 'desc';
28
+ nulls?: 'default' | 'first' | 'last';
29
+ }
30
+ export interface QueryOptions {
31
+ measures?: QueryMeasure[];
32
+ dimensions?: QueryDimension[];
33
+ orders?: QueryOrder[];
34
+ filter?: any;
35
+ having?: any;
36
+ limit?: number;
37
+ offset?: number;
38
+ timezone?: string;
39
+ context?: any;
40
+ }
@@ -0,0 +1,24 @@
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 __copyProps = (to, from, except, desc) => {
15
+ if (from && typeof from === "object" || typeof from === "function") {
16
+ for (let key of __getOwnPropNames(from))
17
+ if (!__hasOwnProp.call(to, key) && key !== except)
18
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
19
+ }
20
+ return to;
21
+ };
22
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
+ var types_exports = {};
24
+ module.exports = __toCommonJS(types_exports);
@@ -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;
@@ -59,6 +72,23 @@ function buildJoiSchema(validation, options) {
59
72
  if (rules) {
60
73
  rules.forEach((rule) => {
61
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
+ }
62
92
  if (!import_lodash.default.isEmpty(args)) {
63
93
  if (rule.name === "pattern" && !import_lodash.default.isRegExp(args.regex)) {
64
94
  const lastSlash = args.regex.lastIndexOf("/");
@@ -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.9",
3
+ "version": "2.2.0-beta.1",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
7
7
  "license": "Apache-2.0",
8
8
  "dependencies": {
9
- "@nocobase/logger": "2.1.0-beta.9",
10
- "@nocobase/utils": "2.1.0-beta.9",
9
+ "@nocobase/logger": "2.2.0-beta.1",
10
+ "@nocobase/utils": "2.2.0-beta.1",
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": "c3a2875e4cbbb43b1f2361e6f9f5f84a7d3f3c3c"
41
+ "gitHead": "f82fa9d0c3aa8e00e53dd94e404a312483b4866b"
42
42
  }