@nocobase/database 1.0.0-alpha.13 → 1.0.0-alpha.15

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.
@@ -401,7 +401,15 @@ async function updateMultipleAssociation(model, key, value, options = {}) {
401
401
  newItems.push(instance);
402
402
  }
403
403
  }
404
- model.setDataValue(key, setItems.concat(newItems));
404
+ for (const newItem of newItems) {
405
+ const existIndexInSetItems = setItems.findIndex((setItem) => setItem[targetKey] === newItem[targetKey]);
406
+ if (existIndexInSetItems !== -1) {
407
+ setItems[existIndexInSetItems] = newItem;
408
+ } else {
409
+ setItems.push(newItem);
410
+ }
411
+ }
412
+ model.setDataValue(key, setItems);
405
413
  }
406
414
  __name(updateMultipleAssociation, "updateMultipleAssociation");
407
415
  // Annotate the CommonJS export names for ESM import in node:
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/database",
3
- "version": "1.0.0-alpha.13",
3
+ "version": "1.0.0-alpha.15",
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.0.0-alpha.13",
10
- "@nocobase/utils": "1.0.0-alpha.13",
9
+ "@nocobase/logger": "1.0.0-alpha.15",
10
+ "@nocobase/utils": "1.0.0-alpha.15",
11
11
  "async-mutex": "^0.3.2",
12
12
  "chalk": "^4.1.1",
13
13
  "cron-parser": "4.4.0",
@@ -22,6 +22,7 @@
22
22
  "mathjs": "^10.6.1",
23
23
  "nanoid": "^3.3.6",
24
24
  "node-fetch": "^2.6.7",
25
+ "node-sql-parser": "^4.18.0",
25
26
  "qs": "^6.11.2",
26
27
  "safe-json-stringify": "^1.2.0",
27
28
  "semver": "^7.3.7",
@@ -37,5 +38,5 @@
37
38
  "url": "git+https://github.com/nocobase/nocobase.git",
38
39
  "directory": "packages/database"
39
40
  },
40
- "gitHead": "bd97adef70762a6691b5b58ca6572672a8cdf838"
41
+ "gitHead": "2793d371368e094ea2bba04cfbe9b9b73c44edba"
41
42
  }
@@ -1,32 +0,0 @@
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 __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
23
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
- var sql_collection_exports = {};
25
- module.exports = __toCommonJS(sql_collection_exports);
26
- __reExport(sql_collection_exports, require("./sql-model"), module.exports);
27
- __reExport(sql_collection_exports, require("./sql-collection"), module.exports);
28
- // Annotate the CommonJS export names for ESM import in node:
29
- 0 && (module.exports = {
30
- ...require("./sql-model"),
31
- ...require("./sql-collection")
32
- });
@@ -1,18 +0,0 @@
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 { GroupOption, Order, ProjectionAlias, WhereOptions } from 'sequelize';
10
- import { SQLModel } from './sql-model';
11
- export declare function selectQuery(tableName: string, options: {
12
- attributes?: (string | ProjectionAlias)[];
13
- where?: WhereOptions;
14
- order?: Order;
15
- group?: GroupOption;
16
- limit?: number;
17
- offset?: number;
18
- }, model: SQLModel): string;
@@ -1,79 +0,0 @@
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 query_generator_exports = {};
29
- __export(query_generator_exports, {
30
- selectQuery: () => selectQuery
31
- });
32
- module.exports = __toCommonJS(query_generator_exports);
33
- var import_utils = require("@nocobase/utils");
34
- function selectQuery(tableName, options, model) {
35
- var _a;
36
- options = options || {};
37
- if (import_utils.lodash.isEmpty(options)) {
38
- return `${model.sql};`;
39
- }
40
- const queryItems = [];
41
- let attributes = options.attributes && options.attributes.slice();
42
- if (attributes) {
43
- const fields = Array.from(((_a = model.collection) == null ? void 0 : _a.fields.keys()) || []);
44
- attributes = attributes.filter((attr) => attr === "*" || typeof attr !== "string" || fields.includes(attr));
45
- }
46
- attributes = this.escapeAttributes(attributes, { model });
47
- attributes = attributes || ["*"];
48
- if (Object.prototype.hasOwnProperty.call(options, "where")) {
49
- options.where = this.getWhereConditions(options.where, tableName, model, options);
50
- if (options.where) {
51
- queryItems.push(` WHERE ${options.where}`);
52
- }
53
- }
54
- if (options.group) {
55
- options.group = Array.isArray(options.group) ? options.group.map((t) => this.aliasGrouping(t, model, tableName, options)).join(", ") : this.aliasGrouping(options.group, model, tableName, options);
56
- if (options.group) {
57
- queryItems.push(` GROUP BY ${options.group}`);
58
- }
59
- }
60
- if (options.order) {
61
- const orders = this.getQueryOrders(options, model, false);
62
- if (orders.mainQueryOrder.length) {
63
- queryItems.push(` ORDER BY ${orders.mainQueryOrder.join(", ")}`);
64
- }
65
- }
66
- const limitOrder = this.addLimitAndOffset(options, model);
67
- if (limitOrder) {
68
- queryItems.push(limitOrder);
69
- }
70
- const query = `SELECT ${attributes.join(", ")} FROM (${model.sql}) ${this.getAliasToken()} ${this.quoteIdentifier(
71
- model.name
72
- )}${queryItems.join("")}`;
73
- return `${query};`;
74
- }
75
- __name(selectQuery, "selectQuery");
76
- // Annotate the CommonJS export names for ESM import in node:
77
- 0 && (module.exports = {
78
- selectQuery
79
- });
@@ -1,16 +0,0 @@
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 { Collection, CollectionContext, CollectionOptions } from '../collection';
10
- export declare class SqlCollection extends Collection {
11
- constructor(options: CollectionOptions, context: CollectionContext);
12
- isSql(): boolean;
13
- collectionSchema(): any;
14
- get filterTargetKey(): string;
15
- modelInit(): void;
16
- }
@@ -1,90 +0,0 @@
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 sql_collection_exports = {};
29
- __export(sql_collection_exports, {
30
- SqlCollection: () => SqlCollection
31
- });
32
- module.exports = __toCommonJS(sql_collection_exports);
33
- var import_collection = require("../collection");
34
- var import_sql_model = require("./sql-model");
35
- const _SqlCollection = class _SqlCollection extends import_collection.Collection {
36
- constructor(options, context) {
37
- options.autoGenId = false;
38
- options.timestamps = false;
39
- options.underscored = false;
40
- super(options, context);
41
- }
42
- isSql() {
43
- return true;
44
- }
45
- collectionSchema() {
46
- return void 0;
47
- }
48
- /* istanbul ignore next -- @preserve */
49
- get filterTargetKey() {
50
- var _a;
51
- const targetKey = ((_a = this.options) == null ? void 0 : _a.filterTargetKey) || "id";
52
- if (targetKey && this.model.getAttributes()[targetKey]) {
53
- return targetKey;
54
- }
55
- if (this.model.primaryKeyAttributes.length > 1) {
56
- return null;
57
- }
58
- return this.model.primaryKeyAttribute;
59
- }
60
- modelInit() {
61
- var _a;
62
- const { autoGenId, sql } = this.options;
63
- const model = (_a = class extends import_sql_model.SQLModel {
64
- }, __name(_a, "model"), _a);
65
- model.init(null, {
66
- ...this.sequelizeModelOptions(),
67
- schema: void 0
68
- });
69
- if (!autoGenId) {
70
- model.removeAttribute("id");
71
- }
72
- model.sql = sql;
73
- model.database = this.context.database;
74
- model.collection = this;
75
- this.model = new Proxy(model, {
76
- get(target, prop) {
77
- if (prop === "_schema") {
78
- return void 0;
79
- }
80
- return Reflect.get(target, prop);
81
- }
82
- });
83
- }
84
- };
85
- __name(_SqlCollection, "SqlCollection");
86
- let SqlCollection = _SqlCollection;
87
- // Annotate the CommonJS export names for ESM import in node:
88
- 0 && (module.exports = {
89
- SqlCollection
90
- });
@@ -1,24 +0,0 @@
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 { Model } from '../model';
10
- export declare class SQLModel extends Model {
11
- static sql: string;
12
- static get queryInterface(): import("sequelize").QueryInterface;
13
- static sync(): Promise<any>;
14
- static inferFields(): {
15
- [field: string]: {
16
- type: string;
17
- source: string;
18
- collection: string;
19
- interface: string;
20
- };
21
- };
22
- private static parseTablesAndColumns;
23
- private static getTableNameWithSchema;
24
- }
@@ -1,190 +0,0 @@
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 __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
17
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
18
- var __export = (target, all) => {
19
- for (var name in all)
20
- __defProp(target, name, { get: all[name], enumerable: true });
21
- };
22
- var __copyProps = (to, from, except, desc) => {
23
- if (from && typeof from === "object" || typeof from === "function") {
24
- for (let key of __getOwnPropNames(from))
25
- if (!__hasOwnProp.call(to, key) && key !== except)
26
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
27
- }
28
- return to;
29
- };
30
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
31
- // If the importer is in node compatibility mode or this is not an ESM
32
- // file that has been converted to a CommonJS file using a Babel-
33
- // compatible transform (i.e. "__esModule" has not been set), then set
34
- // "default" to the CommonJS "module.exports" for node compatibility.
35
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
36
- mod
37
- ));
38
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
39
- var __publicField = (obj, key, value) => {
40
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
41
- return value;
42
- };
43
- var sql_model_exports = {};
44
- __export(sql_model_exports, {
45
- SQLModel: () => SQLModel
46
- });
47
- module.exports = __toCommonJS(sql_model_exports);
48
- var import_model = require("../model");
49
- var import_sql_parser = __toESM(require("../sql-parser"));
50
- var import_query_generator = require("./query-generator");
51
- const _SQLModel = class _SQLModel extends import_model.Model {
52
- static get queryInterface() {
53
- const queryInterface = this.sequelize.getQueryInterface();
54
- const queryGenerator = queryInterface.queryGenerator;
55
- const sqlGenerator = new Proxy(queryGenerator, {
56
- get(target, prop) {
57
- if (prop === "selectQuery") {
58
- return import_query_generator.selectQuery.bind(target);
59
- }
60
- return Reflect.get(target, prop);
61
- }
62
- });
63
- return new Proxy(queryInterface, {
64
- get(target, prop) {
65
- if (prop === "queryGenerator") {
66
- return sqlGenerator;
67
- }
68
- return Reflect.get(target, prop);
69
- }
70
- });
71
- }
72
- static async sync() {
73
- }
74
- static inferFields() {
75
- const tables = this.parseTablesAndColumns();
76
- return tables.reduce((fields, { table, columns }) => {
77
- const tableName = this.getTableNameWithSchema(table);
78
- const collection = this.database.tableNameCollectionMap.get(tableName);
79
- if (!collection) {
80
- return fields;
81
- }
82
- const attributes = collection.model.getAttributes();
83
- const sourceFields = {};
84
- if (columns === "*") {
85
- Object.values(attributes).forEach((attribute) => {
86
- const field = collection.getField(attribute.fieldName);
87
- if (!(field == null ? void 0 : field.options.interface)) {
88
- return;
89
- }
90
- sourceFields[field.name] = {
91
- collection: field.collection.name,
92
- type: field.type,
93
- source: `${field.collection.name}.${field.name}`,
94
- interface: field.options.interface,
95
- uiSchema: field.options.uiSchema
96
- };
97
- });
98
- } else {
99
- columns.forEach((column) => {
100
- const modelField = Object.values(attributes).find((attribute) => attribute.field === column.name);
101
- if (!modelField) {
102
- return;
103
- }
104
- const field = collection.getField(modelField.fieldName);
105
- if (!(field == null ? void 0 : field.options.interface)) {
106
- return;
107
- }
108
- sourceFields[column.as || column.name] = {
109
- collection: field.collection.name,
110
- type: field.type,
111
- source: `${field.collection.name}.${field.name}`,
112
- interface: field.options.interface,
113
- uiSchema: field.options.uiSchema
114
- };
115
- });
116
- }
117
- return { ...fields, ...sourceFields };
118
- }, {});
119
- }
120
- static parseTablesAndColumns() {
121
- let { ast } = import_sql_parser.default.parse(this.sql);
122
- if (Array.isArray(ast)) {
123
- ast = ast[0];
124
- }
125
- ast.from = ast.from || [];
126
- ast.columns = ast.columns || [];
127
- if (ast.with) {
128
- ast.with.forEach((withItem) => {
129
- const as = withItem.name;
130
- const withAst = withItem.stmt.ast;
131
- ast.from.push(...withAst.from.map((f) => ({ ...f, as })));
132
- ast.columns.push(
133
- ...withAst.columns.map((c) => ({
134
- ...c,
135
- expr: {
136
- ...c.expr,
137
- table: as
138
- }
139
- }))
140
- );
141
- });
142
- }
143
- if (ast.columns === "*") {
144
- const tables = /* @__PURE__ */ new Set();
145
- ast.from.forEach((fromItem) => {
146
- tables.add(fromItem.table);
147
- });
148
- return Array.from(tables).map((table) => ({ table, columns: "*" }));
149
- }
150
- const tableAliases = {};
151
- ast.from.forEach((fromItem) => {
152
- if (!fromItem.as) {
153
- return;
154
- }
155
- tableAliases[fromItem.as] = fromItem.table;
156
- });
157
- const columns = ast.columns.reduce(
158
- (tableMp, column) => {
159
- if (column.expr.type !== "column_ref") {
160
- return tableMp;
161
- }
162
- const table = column.expr.table;
163
- const name = tableAliases[table] || table;
164
- const columnAttr = { name: column.expr.column, as: column.as };
165
- if (!tableMp[name]) {
166
- tableMp[name] = [columnAttr];
167
- } else {
168
- tableMp[name].push(columnAttr);
169
- }
170
- return tableMp;
171
- },
172
- {}
173
- );
174
- return Object.entries(columns).filter(([_, columns2]) => columns2).map(([table, columns2]) => ({ table, columns: columns2 }));
175
- }
176
- static getTableNameWithSchema(table) {
177
- if (this.database.inDialect("postgres") && !table.includes(".")) {
178
- const schema = process.env.DB_SCHEMA || "public";
179
- return `${schema}.${table}`;
180
- }
181
- return table;
182
- }
183
- };
184
- __name(_SQLModel, "SQLModel");
185
- __publicField(_SQLModel, "sql");
186
- let SQLModel = _SQLModel;
187
- // Annotate the CommonJS export names for ESM import in node:
188
- 0 && (module.exports = {
189
- SQLModel
190
- });