@nocobase/database 2.1.0-beta.9 → 2.2.0-alpha.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.
- package/lib/collection.d.ts +14 -0
- package/lib/collection.js +13 -3
- package/lib/database.d.ts +1 -0
- package/lib/database.js +4 -0
- package/lib/eager-loading/eager-loading-tree.js +9 -4
- package/lib/errors/association-not-found-error.d.ts +11 -0
- package/lib/errors/association-not-found-error.js +44 -0
- package/lib/fields/password-field.d.ts +11 -3
- package/lib/fields/password-field.js +41 -21
- package/lib/fields/relation-field.js +1 -1
- package/lib/index.d.ts +3 -0
- package/lib/index.js +6 -0
- package/lib/interfaces/date-interface.d.ts +9 -0
- package/lib/interfaces/date-interface.js +27 -0
- package/lib/interfaces/datetime-interface.d.ts +3 -1
- package/lib/interfaces/datetime-interface.js +84 -9
- package/lib/interfaces/datetime-no-tz-interface.d.ts +9 -0
- package/lib/interfaces/datetime-no-tz-interface.js +16 -2
- package/lib/interfaces/time-interface.js +16 -0
- package/lib/interfaces/to-one-interface.js +1 -1
- package/lib/operators/date.js +12 -4
- package/lib/options-parser.d.ts +1 -0
- package/lib/options-parser.js +20 -4
- package/lib/query/builder.d.ts +17 -0
- package/lib/query/builder.js +268 -0
- package/lib/query/formatter.d.ts +29 -0
- package/lib/query/formatter.js +100 -0
- package/lib/query/formatters/mysql.d.ts +14 -0
- package/lib/query/formatters/mysql.js +70 -0
- package/lib/query/formatters/oracle.d.ts +14 -0
- package/lib/query/formatters/oracle.js +64 -0
- package/lib/query/formatters/postgres.d.ts +15 -0
- package/lib/query/formatters/postgres.js +70 -0
- package/lib/query/formatters/sqlite.d.ts +14 -0
- package/lib/query/formatters/sqlite.js +63 -0
- package/lib/query/having.d.ts +22 -0
- package/lib/query/having.js +112 -0
- package/lib/query/types.d.ts +40 -0
- package/lib/query/types.js +24 -0
- package/lib/repository.d.ts +4 -0
- package/lib/repository.js +23 -3
- package/lib/utils/field-validation.js +30 -0
- package/lib/view/field-type-map.d.ts +4 -4
- package/lib/view/field-type-map.js +4 -4
- package/package.json +4 -4
|
@@ -44,8 +44,24 @@ var import_dayjs = __toESM(require("dayjs"));
|
|
|
44
44
|
var import_utc = __toESM(require("dayjs/plugin/utc"));
|
|
45
45
|
var import_base_interface = require("./base-interface");
|
|
46
46
|
import_dayjs.default.extend(import_utc.default);
|
|
47
|
+
function isNumeric(value) {
|
|
48
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
49
|
+
}
|
|
50
|
+
__name(isNumeric, "isNumeric");
|
|
51
|
+
function formatExcelTimeSerial(value) {
|
|
52
|
+
const normalized = (value % 1 + 1) % 1;
|
|
53
|
+
const totalSeconds = Math.round(normalized * 24 * 60 * 60) % (24 * 60 * 60);
|
|
54
|
+
const hours = Math.floor(totalSeconds / 3600).toString().padStart(2, "0");
|
|
55
|
+
const minutes = Math.floor(totalSeconds % 3600 / 60).toString().padStart(2, "0");
|
|
56
|
+
const seconds = (totalSeconds % 60).toString().padStart(2, "0");
|
|
57
|
+
return `${hours}:${minutes}:${seconds}`;
|
|
58
|
+
}
|
|
59
|
+
__name(formatExcelTimeSerial, "formatExcelTimeSerial");
|
|
47
60
|
const _TimeInterface = class _TimeInterface extends import_base_interface.BaseInterface {
|
|
48
61
|
toValue(value, ctx) {
|
|
62
|
+
if (isNumeric(value)) {
|
|
63
|
+
return formatExcelTimeSerial(value);
|
|
64
|
+
}
|
|
49
65
|
if (this.validate(value)) {
|
|
50
66
|
const result = (0, import_dayjs.default)(value).format("HH:mm:ss");
|
|
51
67
|
return result;
|
|
@@ -42,7 +42,7 @@ const _ToOneInterface = class _ToOneInterface extends import_base_interface.Base
|
|
|
42
42
|
const { filterKey, associationField, targetCollection, transaction } = ctx;
|
|
43
43
|
const targetInstance = await targetCollection.repository.findOne({
|
|
44
44
|
filter: {
|
|
45
|
-
[filterKey]: str
|
|
45
|
+
[filterKey]: str.toString()
|
|
46
46
|
},
|
|
47
47
|
transaction
|
|
48
48
|
});
|
package/lib/operators/date.js
CHANGED
|
@@ -47,6 +47,14 @@ function isDate(input) {
|
|
|
47
47
|
return input instanceof Date || Object.prototype.toString.call(input) === "[object Date]";
|
|
48
48
|
}
|
|
49
49
|
__name(isDate, "isDate");
|
|
50
|
+
function isDatetimeNoTzField(field) {
|
|
51
|
+
return (field == null ? void 0 : field.type) === "datetimeNoTz" || (field == null ? void 0 : field.constructor.name) === "DatetimeNoTzField";
|
|
52
|
+
}
|
|
53
|
+
__name(isDatetimeNoTzField, "isDatetimeNoTzField");
|
|
54
|
+
function isDateOnlyField(field) {
|
|
55
|
+
return (field == null ? void 0 : field.type) === "dateOnly" || (field == null ? void 0 : field.constructor.name) === "DateOnlyField";
|
|
56
|
+
}
|
|
57
|
+
__name(isDateOnlyField, "isDateOnlyField");
|
|
50
58
|
const toDate = /* @__PURE__ */ __name((date, options = {}) => {
|
|
51
59
|
const { ctx } = options;
|
|
52
60
|
let val = isDate(date) ? date : new Date(date);
|
|
@@ -57,10 +65,10 @@ const toDate = /* @__PURE__ */ __name((date, options = {}) => {
|
|
|
57
65
|
if (field.constructor.name === "UnixTimestampField") {
|
|
58
66
|
val = field.dateToValue(val);
|
|
59
67
|
}
|
|
60
|
-
if (field
|
|
68
|
+
if (isDatetimeNoTzField(field)) {
|
|
61
69
|
val = (0, import_moment.default)(val).utcOffset("+00:00").format("YYYY-MM-DD HH:mm:ss");
|
|
62
70
|
}
|
|
63
|
-
if (field
|
|
71
|
+
if (isDateOnlyField(field)) {
|
|
64
72
|
val = import_moment.default.utc(val).format("YYYY-MM-DD HH:mm:ss");
|
|
65
73
|
}
|
|
66
74
|
const eventObj = {
|
|
@@ -75,10 +83,10 @@ function parseDateTimezone(ctx) {
|
|
|
75
83
|
if (!field) {
|
|
76
84
|
return ctx.db.options.timezone;
|
|
77
85
|
}
|
|
78
|
-
if (field
|
|
86
|
+
if (isDatetimeNoTzField(field)) {
|
|
79
87
|
return "+00:00";
|
|
80
88
|
}
|
|
81
|
-
if (field
|
|
89
|
+
if (isDateOnlyField(field)) {
|
|
82
90
|
return "+00:00";
|
|
83
91
|
}
|
|
84
92
|
return ctx.db.options.timezone;
|
package/lib/options-parser.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export declare class OptionsParser {
|
|
|
22
22
|
model: ModelStatic<any>;
|
|
23
23
|
filterParser: FilterParser;
|
|
24
24
|
context: OptionsParserContext;
|
|
25
|
+
associationNotFoundWarnings: string[];
|
|
25
26
|
constructor(options: FindOptions, context: OptionsParserContext);
|
|
26
27
|
static appendInheritInspectAttribute(include: any, collection: any): any;
|
|
27
28
|
isAssociation(key: string): boolean;
|
package/lib/options-parser.js
CHANGED
|
@@ -44,6 +44,7 @@ var import_lodash = __toESM(require("lodash"));
|
|
|
44
44
|
var import_sequelize = require("sequelize");
|
|
45
45
|
var import_filter_parser = __toESM(require("./filter-parser"));
|
|
46
46
|
var import_qs = __toESM(require("qs"));
|
|
47
|
+
var import_association_not_found_error = require("./errors/association-not-found-error");
|
|
47
48
|
const debug = require("debug")("noco-database");
|
|
48
49
|
const _OptionsParser = class _OptionsParser {
|
|
49
50
|
options;
|
|
@@ -52,6 +53,7 @@ const _OptionsParser = class _OptionsParser {
|
|
|
52
53
|
model;
|
|
53
54
|
filterParser;
|
|
54
55
|
context;
|
|
56
|
+
associationNotFoundWarnings = [];
|
|
55
57
|
constructor(options, context) {
|
|
56
58
|
const { collection } = context;
|
|
57
59
|
this.collection = collection;
|
|
@@ -301,7 +303,7 @@ const _OptionsParser = class _OptionsParser {
|
|
|
301
303
|
}
|
|
302
304
|
const sortedAppends = import_lodash.default.sortBy(appendList, (append) => append.split(".").length);
|
|
303
305
|
const setInclude = /* @__PURE__ */ __name((model, queryParams, append, parentAs) => {
|
|
304
|
-
var _a;
|
|
306
|
+
var _a, _b;
|
|
305
307
|
const appendWithOptions = this.parseAppendWithOptions(append);
|
|
306
308
|
append = appendWithOptions.name;
|
|
307
309
|
const appendFields = append.split(".");
|
|
@@ -314,7 +316,7 @@ const _OptionsParser = class _OptionsParser {
|
|
|
314
316
|
if (appendFields.length == 2) {
|
|
315
317
|
const association = associations[appendFields[0]];
|
|
316
318
|
if (!association) {
|
|
317
|
-
throw new
|
|
319
|
+
throw new import_association_not_found_error.AssociationNotFoundError(`association ${appendFields[0]} in ${model.name} not found`);
|
|
318
320
|
}
|
|
319
321
|
const associationModel = associations[appendFields[0]].target;
|
|
320
322
|
if (associationModel.rawAttributes[appendFields[1]]) {
|
|
@@ -341,7 +343,13 @@ const _OptionsParser = class _OptionsParser {
|
|
|
341
343
|
if (existIncludeIndex == -1) {
|
|
342
344
|
const association = associations[appendAssociation];
|
|
343
345
|
if (!association) {
|
|
344
|
-
throw new
|
|
346
|
+
throw new import_association_not_found_error.AssociationNotFoundError(`association ${appendAssociation} in ${model.name} not found`);
|
|
347
|
+
}
|
|
348
|
+
const targetCollectionName = (_b = this.database.getCollectionByModelName(association.target.name)) == null ? void 0 : _b.name;
|
|
349
|
+
if (!targetCollectionName) {
|
|
350
|
+
throw new import_association_not_found_error.AssociationNotFoundError(
|
|
351
|
+
`target collection for association ${appendAssociation} in ${model.name} not found`
|
|
352
|
+
);
|
|
345
353
|
}
|
|
346
354
|
let includeOptions = {
|
|
347
355
|
association: appendAssociation,
|
|
@@ -394,7 +402,15 @@ const _OptionsParser = class _OptionsParser {
|
|
|
394
402
|
}
|
|
395
403
|
}, "setInclude");
|
|
396
404
|
for (const append of sortedAppends) {
|
|
397
|
-
|
|
405
|
+
try {
|
|
406
|
+
setInclude(this.model, filterParams, append);
|
|
407
|
+
} catch (error) {
|
|
408
|
+
if (error instanceof import_association_not_found_error.AssociationNotFoundError) {
|
|
409
|
+
this.associationNotFoundWarnings.push(error.message);
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
throw error;
|
|
413
|
+
}
|
|
398
414
|
}
|
|
399
415
|
debug("filter params: %o", filterParams);
|
|
400
416
|
return filterParams;
|
|
@@ -0,0 +1,17 @@
|
|
|
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 { FindOptions as SequelizeFindOptions } from 'sequelize';
|
|
10
|
+
import { Collection } from '../collection';
|
|
11
|
+
import { Database } from '../database';
|
|
12
|
+
import { QueryOptions } from './types';
|
|
13
|
+
export declare function buildQuery(database: Database, collection: Collection, options?: QueryOptions): {
|
|
14
|
+
queryOptions: SequelizeFindOptions<any>;
|
|
15
|
+
fieldMap: Record<string, any>;
|
|
16
|
+
};
|
|
17
|
+
export declare function normalizeQueryResult(data: any[], fieldMap: Record<string, any>): any[];
|
|
@@ -0,0 +1,268 @@
|
|
|
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 __create = Object.create;
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
13
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
15
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
17
|
+
var __export = (target, all) => {
|
|
18
|
+
for (var name in all)
|
|
19
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
+
};
|
|
21
|
+
var __copyProps = (to, from, except, desc) => {
|
|
22
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
+
for (let key of __getOwnPropNames(from))
|
|
24
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
25
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
26
|
+
}
|
|
27
|
+
return to;
|
|
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
|
+
));
|
|
37
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
38
|
+
var builder_exports = {};
|
|
39
|
+
__export(builder_exports, {
|
|
40
|
+
buildQuery: () => buildQuery,
|
|
41
|
+
normalizeQueryResult: () => normalizeQueryResult
|
|
42
|
+
});
|
|
43
|
+
module.exports = __toCommonJS(builder_exports);
|
|
44
|
+
var import_filter_parser = __toESM(require("../filter-parser"));
|
|
45
|
+
var import_formatter = require("./formatter");
|
|
46
|
+
var import_mysql = require("./formatters/mysql");
|
|
47
|
+
var import_oracle = require("./formatters/oracle");
|
|
48
|
+
var import_postgres = require("./formatters/postgres");
|
|
49
|
+
var import_sqlite = require("./formatters/sqlite");
|
|
50
|
+
var import_having = require("./having");
|
|
51
|
+
const ALLOWED_AGG_FUNCS = ["sum", "count", "avg", "min", "max"];
|
|
52
|
+
const ALLOWED_ORDER_DIRECTIONS = ["ASC", "DESC"];
|
|
53
|
+
function createQueryFormatter(database) {
|
|
54
|
+
switch (database.sequelize.getDialect()) {
|
|
55
|
+
case "sqlite":
|
|
56
|
+
return new import_sqlite.SQLiteQueryFormatter(database.sequelize, database.options.rawTimezone);
|
|
57
|
+
case "postgres":
|
|
58
|
+
return new import_postgres.PostgresQueryFormatter(database.sequelize, database.options.rawTimezone);
|
|
59
|
+
case "mysql":
|
|
60
|
+
case "mariadb":
|
|
61
|
+
return new import_mysql.MySQLQueryFormatter(database.sequelize, database.options.rawTimezone);
|
|
62
|
+
case "oracle":
|
|
63
|
+
return new import_oracle.OracleQueryFormatter(database.sequelize, database.options.rawTimezone);
|
|
64
|
+
default:
|
|
65
|
+
return new class extends import_formatter.QueryFormatter {
|
|
66
|
+
formatDate(field, _format, _timezone, _preserveLocalTime) {
|
|
67
|
+
return field;
|
|
68
|
+
}
|
|
69
|
+
formatUnixTimestamp(field, _format, _accuracy, _timezone) {
|
|
70
|
+
return this.sequelize.col(field);
|
|
71
|
+
}
|
|
72
|
+
}(database.sequelize, database.options.rawTimezone);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
__name(createQueryFormatter, "createQueryFormatter");
|
|
76
|
+
function parseSelectedField(database, collection, selected) {
|
|
77
|
+
var _a, _b, _c, _d, _e;
|
|
78
|
+
const collectionName = collection.name;
|
|
79
|
+
const fields = collection.fields;
|
|
80
|
+
let target;
|
|
81
|
+
let name;
|
|
82
|
+
const fieldPath = Array.isArray(selected.field) ? selected.field : selected.field.split(".").filter(Boolean);
|
|
83
|
+
if (fieldPath.length === 1) {
|
|
84
|
+
name = fieldPath[0];
|
|
85
|
+
} else {
|
|
86
|
+
[target, name] = fieldPath;
|
|
87
|
+
}
|
|
88
|
+
const rawAttributes = collection.model.getAttributes();
|
|
89
|
+
let field = ((_a = rawAttributes[name]) == null ? void 0 : _a.field) || name;
|
|
90
|
+
let fieldType = (_b = fields.get(name)) == null ? void 0 : _b.type;
|
|
91
|
+
let fieldOptions = (_c = fields.get(name)) == null ? void 0 : _c.options;
|
|
92
|
+
if (target) {
|
|
93
|
+
const targetField = fields.get(target);
|
|
94
|
+
const targetCollection = database.getCollection(targetField.target);
|
|
95
|
+
const targetFields = targetCollection.fields;
|
|
96
|
+
fieldType = (_d = targetFields.get(name)) == null ? void 0 : _d.type;
|
|
97
|
+
fieldOptions = (_e = targetFields.get(name)) == null ? void 0 : _e.options;
|
|
98
|
+
field = `${target}.${field}`;
|
|
99
|
+
name = `${target}.${name}`;
|
|
100
|
+
} else {
|
|
101
|
+
field = `${collectionName}.${field}`;
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
...selected,
|
|
105
|
+
field,
|
|
106
|
+
name,
|
|
107
|
+
type: fieldType,
|
|
108
|
+
options: fieldOptions,
|
|
109
|
+
alias: selected.alias || name,
|
|
110
|
+
target
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
__name(parseSelectedField, "parseSelectedField");
|
|
114
|
+
function buildQuery(database, collection, options = {}) {
|
|
115
|
+
var _a;
|
|
116
|
+
const sequelize = database.sequelize;
|
|
117
|
+
const formatter = createQueryFormatter(database);
|
|
118
|
+
const measures = (options.measures || []).map((measure) => parseSelectedField(database, collection, measure));
|
|
119
|
+
const dimensions = (options.dimensions || []).map((dimension) => parseSelectedField(database, collection, dimension));
|
|
120
|
+
const orders = (options.orders || []).map((order2) => parseSelectedField(database, collection, order2));
|
|
121
|
+
const models = {};
|
|
122
|
+
[...measures, ...dimensions, ...orders].forEach((item) => {
|
|
123
|
+
var _a2;
|
|
124
|
+
if (item.target && !models[item.target]) {
|
|
125
|
+
models[item.target] = { type: (_a2 = collection.fields.get(item.target)) == null ? void 0 : _a2.type };
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
const include = Object.entries(models).map(([target, { type }]) => {
|
|
129
|
+
let includeOptions = {
|
|
130
|
+
association: target,
|
|
131
|
+
attributes: []
|
|
132
|
+
};
|
|
133
|
+
if (type === "belongsToMany") {
|
|
134
|
+
includeOptions.through = { attributes: [] };
|
|
135
|
+
}
|
|
136
|
+
if (type === "belongsToArray") {
|
|
137
|
+
const association = collection.model.associations[target];
|
|
138
|
+
if (association) {
|
|
139
|
+
includeOptions = {
|
|
140
|
+
...includeOptions,
|
|
141
|
+
...association.generateInclude()
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return includeOptions;
|
|
146
|
+
});
|
|
147
|
+
const filterParser = new import_filter_parser.default(options.filter, { collection });
|
|
148
|
+
const { where, include: filterInclude } = filterParser.toSequelizeParams();
|
|
149
|
+
if (filterInclude) {
|
|
150
|
+
const stack = [...filterInclude];
|
|
151
|
+
while (stack.length) {
|
|
152
|
+
const item = stack.pop();
|
|
153
|
+
const parentCollection = database.getCollection(item.parentCollection || collection.name);
|
|
154
|
+
const field = parentCollection.fields.get(item.association);
|
|
155
|
+
if ((field == null ? void 0 : field.type) === "belongsToMany") {
|
|
156
|
+
item.through = { attributes: [] };
|
|
157
|
+
}
|
|
158
|
+
if ((field == null ? void 0 : field.target) && ((_a = item.include) == null ? void 0 : _a.length)) {
|
|
159
|
+
for (const child of item.include) {
|
|
160
|
+
child.parentCollection = field.target;
|
|
161
|
+
stack.push(child);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
let hasAgg = false;
|
|
167
|
+
const attributes = [];
|
|
168
|
+
const fieldMap = {};
|
|
169
|
+
const projectedFieldMap = {};
|
|
170
|
+
measures.forEach((measure) => {
|
|
171
|
+
const { field, aggregation, alias, distinct } = measure;
|
|
172
|
+
const col = sequelize.col(field);
|
|
173
|
+
const attribute = [];
|
|
174
|
+
if (aggregation) {
|
|
175
|
+
if (!ALLOWED_AGG_FUNCS.includes(aggregation)) {
|
|
176
|
+
throw new Error(`Invalid aggregation function: ${aggregation}`);
|
|
177
|
+
}
|
|
178
|
+
hasAgg = true;
|
|
179
|
+
attribute.push(sequelize.fn(aggregation, distinct ? sequelize.fn("DISTINCT", col) : col));
|
|
180
|
+
} else {
|
|
181
|
+
attribute.push(col);
|
|
182
|
+
}
|
|
183
|
+
if (alias) {
|
|
184
|
+
attribute.push(alias);
|
|
185
|
+
}
|
|
186
|
+
attributes.push(attribute.length > 1 ? attribute : attribute[0]);
|
|
187
|
+
fieldMap[alias || field] = measure;
|
|
188
|
+
projectedFieldMap[alias || field] = attribute[0];
|
|
189
|
+
});
|
|
190
|
+
const group = [];
|
|
191
|
+
dimensions.forEach((dimension) => {
|
|
192
|
+
const { field, format, alias, type, options: fieldOptions } = dimension;
|
|
193
|
+
const attribute = [];
|
|
194
|
+
if (format) {
|
|
195
|
+
attribute.push(formatter.format({ type, field, format, timezone: options.timezone, fieldOptions }));
|
|
196
|
+
} else {
|
|
197
|
+
attribute.push(sequelize.col(field));
|
|
198
|
+
}
|
|
199
|
+
if (alias) {
|
|
200
|
+
attribute.push(alias);
|
|
201
|
+
}
|
|
202
|
+
attributes.push(attribute.length > 1 ? attribute : attribute[0]);
|
|
203
|
+
if (hasAgg) {
|
|
204
|
+
group.push(attribute[0]);
|
|
205
|
+
}
|
|
206
|
+
fieldMap[alias || field] = dimension;
|
|
207
|
+
projectedFieldMap[alias || field] = attribute[0];
|
|
208
|
+
});
|
|
209
|
+
const order = orders.map((item) => {
|
|
210
|
+
var _a2;
|
|
211
|
+
const alias = sequelize.getQueryInterface().quoteIdentifier(item.alias);
|
|
212
|
+
const projectedField = projectedFieldMap[item.alias] || projectedFieldMap[item.name] || projectedFieldMap[item.field];
|
|
213
|
+
const name = hasAgg ? projectedField || sequelize.literal(alias) : sequelize.col(item.field);
|
|
214
|
+
let sort = ALLOWED_ORDER_DIRECTIONS.includes((_a2 = item.order) == null ? void 0 : _a2.toUpperCase()) ? item.order.toUpperCase() : "ASC";
|
|
215
|
+
if (item.nulls === "first") {
|
|
216
|
+
sort += " NULLS FIRST";
|
|
217
|
+
}
|
|
218
|
+
if (item.nulls === "last") {
|
|
219
|
+
sort += " NULLS LAST";
|
|
220
|
+
}
|
|
221
|
+
return [name, sort];
|
|
222
|
+
});
|
|
223
|
+
const queryOptions = {
|
|
224
|
+
where,
|
|
225
|
+
having: (0, import_having.buildHaving)(database, formatter, fieldMap, options.having, options.timezone),
|
|
226
|
+
attributes,
|
|
227
|
+
include: [...include, ...filterInclude || []],
|
|
228
|
+
group,
|
|
229
|
+
order,
|
|
230
|
+
subQuery: false,
|
|
231
|
+
raw: true
|
|
232
|
+
};
|
|
233
|
+
if (!hasAgg || dimensions.length) {
|
|
234
|
+
queryOptions.limit = options.limit || 2e3;
|
|
235
|
+
queryOptions.offset = options.offset || 0;
|
|
236
|
+
}
|
|
237
|
+
return { queryOptions, fieldMap };
|
|
238
|
+
}
|
|
239
|
+
__name(buildQuery, "buildQuery");
|
|
240
|
+
function normalizeQueryResult(data, fieldMap) {
|
|
241
|
+
const dateTimeTypes = ["date", "datetime", "datetimeTz", "datetimeNoTz"];
|
|
242
|
+
return data.map((record) => {
|
|
243
|
+
Object.entries(record).forEach(([key, value]) => {
|
|
244
|
+
if (value === null || value === void 0) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const field = fieldMap[key];
|
|
248
|
+
const type = field == null ? void 0 : field.type;
|
|
249
|
+
if (["bigInt", "integer", "float", "double", "decimal"].includes(type)) {
|
|
250
|
+
record[key] = Number(value);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
if (!(field == null ? void 0 : field.format) && dateTimeTypes.includes(type) && !(value instanceof Date)) {
|
|
254
|
+
const dateValue = new Date(value);
|
|
255
|
+
if (!Number.isNaN(dateValue.getTime())) {
|
|
256
|
+
record[key] = dateValue;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
return record;
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
__name(normalizeQueryResult, "normalizeQueryResult");
|
|
264
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
265
|
+
0 && (module.exports = {
|
|
266
|
+
buildQuery,
|
|
267
|
+
normalizeQueryResult
|
|
268
|
+
});
|
|
@@ -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 { Sequelize } from 'sequelize';
|
|
10
|
+
export type Col = ReturnType<typeof Sequelize.col>;
|
|
11
|
+
export type Literal = ReturnType<typeof Sequelize.literal>;
|
|
12
|
+
export type Fn = ReturnType<typeof Sequelize.fn>;
|
|
13
|
+
export declare abstract class QueryFormatter {
|
|
14
|
+
sequelize: Sequelize;
|
|
15
|
+
rawTimezone?: string;
|
|
16
|
+
constructor(sequelize: Sequelize, rawTimezone?: string);
|
|
17
|
+
abstract formatDate(field: Col, format: string, timezone?: string, preserveLocalTime?: boolean): Fn | Col;
|
|
18
|
+
abstract formatUnixTimestamp(field: string, format: string, accuracy?: 'second' | 'millisecond', timezone?: string): Fn | Literal | Col;
|
|
19
|
+
convertFormat(format: string): string;
|
|
20
|
+
protected getTimezoneByOffset(offset?: string): string;
|
|
21
|
+
protected getOffsetExpression(timezone: string): string;
|
|
22
|
+
format(options: {
|
|
23
|
+
type: string;
|
|
24
|
+
field: string;
|
|
25
|
+
format: string;
|
|
26
|
+
timezone?: string;
|
|
27
|
+
fieldOptions?: any;
|
|
28
|
+
}): import("sequelize/types/utils").Fn | import("sequelize/types/utils").Col | import("sequelize/types/utils").Literal;
|
|
29
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
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 __create = Object.create;
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
13
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
15
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
17
|
+
var __export = (target, all) => {
|
|
18
|
+
for (var name in all)
|
|
19
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
+
};
|
|
21
|
+
var __copyProps = (to, from, except, desc) => {
|
|
22
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
+
for (let key of __getOwnPropNames(from))
|
|
24
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
25
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
26
|
+
}
|
|
27
|
+
return to;
|
|
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
|
+
));
|
|
37
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
38
|
+
var formatter_exports = {};
|
|
39
|
+
__export(formatter_exports, {
|
|
40
|
+
QueryFormatter: () => QueryFormatter
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(formatter_exports);
|
|
43
|
+
var import_moment_timezone = __toESM(require("moment-timezone"));
|
|
44
|
+
const _QueryFormatter = class _QueryFormatter {
|
|
45
|
+
sequelize;
|
|
46
|
+
rawTimezone;
|
|
47
|
+
constructor(sequelize, rawTimezone) {
|
|
48
|
+
this.sequelize = sequelize;
|
|
49
|
+
this.rawTimezone = rawTimezone;
|
|
50
|
+
}
|
|
51
|
+
convertFormat(format) {
|
|
52
|
+
return format;
|
|
53
|
+
}
|
|
54
|
+
getTimezoneByOffset(offset) {
|
|
55
|
+
if (!offset) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (import_moment_timezone.default.tz.zone(offset)) {
|
|
59
|
+
return offset;
|
|
60
|
+
}
|
|
61
|
+
if (/^[+-]\d{1,2}:\d{2}$/.test(offset)) {
|
|
62
|
+
return offset;
|
|
63
|
+
}
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
getOffsetExpression(timezone) {
|
|
67
|
+
const sign = timezone.charAt(0);
|
|
68
|
+
const value = timezone.slice(1);
|
|
69
|
+
const [hours, minutes] = value.split(":").map(Number);
|
|
70
|
+
return `${sign}${hours * 60 + minutes} minutes`;
|
|
71
|
+
}
|
|
72
|
+
format(options) {
|
|
73
|
+
var _a, _b;
|
|
74
|
+
const { type, field, format, timezone, fieldOptions } = options;
|
|
75
|
+
const col = this.sequelize.col(field);
|
|
76
|
+
switch (type) {
|
|
77
|
+
case "date":
|
|
78
|
+
case "datetime":
|
|
79
|
+
case "datetimeTz":
|
|
80
|
+
return this.formatDate(col, format, timezone);
|
|
81
|
+
case "datetimeNoTz":
|
|
82
|
+
return this.formatDate(col, format, void 0, true);
|
|
83
|
+
case "dateOnly":
|
|
84
|
+
case "time":
|
|
85
|
+
return this.formatDate(col, format);
|
|
86
|
+
case "unixTimestamp": {
|
|
87
|
+
const accuracy = ((_b = (_a = fieldOptions == null ? void 0 : fieldOptions.uiSchema) == null ? void 0 : _a["x-component-props"]) == null ? void 0 : _b.accuracy) || (fieldOptions == null ? void 0 : fieldOptions.accuracy) || "second";
|
|
88
|
+
return this.formatUnixTimestamp(field, format, accuracy, timezone);
|
|
89
|
+
}
|
|
90
|
+
default:
|
|
91
|
+
return col;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
__name(_QueryFormatter, "QueryFormatter");
|
|
96
|
+
let QueryFormatter = _QueryFormatter;
|
|
97
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
98
|
+
0 && (module.exports = {
|
|
99
|
+
QueryFormatter
|
|
100
|
+
});
|
|
@@ -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 MySQLQueryFormatter 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,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 mysql_exports = {};
|
|
29
|
+
__export(mysql_exports, {
|
|
30
|
+
MySQLQueryFormatter: () => MySQLQueryFormatter
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(mysql_exports);
|
|
33
|
+
var import_formatter = require("../formatter");
|
|
34
|
+
const _MySQLQueryFormatter = class _MySQLQueryFormatter 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, "%i").replace(/ss/g, "%S");
|
|
37
|
+
}
|
|
38
|
+
formatDate(field, format, timezone, _preserveLocalTime) {
|
|
39
|
+
const fmt = this.convertFormat(format);
|
|
40
|
+
const resolvedTimezone = this.getTimezoneByOffset(timezone);
|
|
41
|
+
if (resolvedTimezone) {
|
|
42
|
+
return this.sequelize.fn(
|
|
43
|
+
"date_format",
|
|
44
|
+
this.sequelize.fn("convert_tz", field, process.env.TZ || "UTC", resolvedTimezone),
|
|
45
|
+
fmt
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
return this.sequelize.fn("date_format", field, fmt);
|
|
49
|
+
}
|
|
50
|
+
formatUnixTimestamp(field, format, accuracy = "second", timezone) {
|
|
51
|
+
const fmt = this.convertFormat(format);
|
|
52
|
+
const quoted = this.sequelize.getQueryInterface().quoteIdentifiers(field);
|
|
53
|
+
const resolvedTimezone = this.getTimezoneByOffset(timezone);
|
|
54
|
+
const timestamp = accuracy === "millisecond" ? this.sequelize.fn("from_unixtime", this.sequelize.literal(`ROUND(${quoted} / 1000)`)) : this.sequelize.fn("from_unixtime", this.sequelize.col(field));
|
|
55
|
+
if (resolvedTimezone) {
|
|
56
|
+
return this.sequelize.fn(
|
|
57
|
+
"date_format",
|
|
58
|
+
this.sequelize.fn("convert_tz", timestamp, process.env.TZ || "UTC", resolvedTimezone),
|
|
59
|
+
fmt
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
return accuracy === "millisecond" ? this.sequelize.fn("from_unixtime", this.sequelize.literal(`ROUND(${quoted} / 1000)`), fmt) : this.sequelize.fn("from_unixtime", this.sequelize.col(field), fmt);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
__name(_MySQLQueryFormatter, "MySQLQueryFormatter");
|
|
66
|
+
let MySQLQueryFormatter = _MySQLQueryFormatter;
|
|
67
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
68
|
+
0 && (module.exports = {
|
|
69
|
+
MySQLQueryFormatter
|
|
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 OracleQueryFormatter 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
|
+
}
|