@nocobase/database 1.5.0-alpha.5 → 1.5.0-beta.2

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.
@@ -20,6 +20,7 @@ import { DecimalFieldOptions, DoubleFieldOptions, FloatFieldOptions, IntegerFiel
20
20
  import { PasswordFieldOptions } from './password-field';
21
21
  import { RadioFieldOptions } from './radio-field';
22
22
  import { SetFieldOptions } from './set-field';
23
+ import { SortFieldOptions } from './sort-field';
23
24
  import { StringFieldOptions } from './string-field';
24
25
  import { TextFieldOptions } from './text-field';
25
26
  import { TimeFieldOptions } from './time-field';
@@ -51,6 +52,7 @@ export * from './password-field';
51
52
  export * from './radio-field';
52
53
  export * from './relation-field';
53
54
  export * from './set-field';
55
+ export * from './sort-field';
54
56
  export * from './string-field';
55
57
  export * from './text-field';
56
58
  export * from './time-field';
@@ -60,4 +62,4 @@ export * from './virtual-field';
60
62
  export * from './nanoid-field';
61
63
  export * from './encryption-field';
62
64
  export * from './unix-timestamp-field';
63
- export type FieldOptions = BaseFieldOptions | StringFieldOptions | IntegerFieldOptions | FloatFieldOptions | DecimalFieldOptions | DoubleFieldOptions | RealFieldOptions | JsonFieldOptions | JsonbFieldOptions | BooleanFieldOptions | RadioFieldOptions | TextFieldOptions | VirtualFieldOptions | ArrayFieldOptions | SetFieldOptions | TimeFieldOptions | DateFieldOptions | DatetimeTzFieldOptions | DatetimeNoTzFieldOptions | DateOnlyFieldOptions | UnixTimestampFieldOptions | UidFieldOptions | UUIDFieldOptions | NanoidFieldOptions | PasswordFieldOptions | ContextFieldOptions | BelongsToFieldOptions | HasOneFieldOptions | HasManyFieldOptions | BelongsToManyFieldOptions | EncryptionField;
65
+ export type FieldOptions = BaseFieldOptions | StringFieldOptions | IntegerFieldOptions | FloatFieldOptions | DecimalFieldOptions | DoubleFieldOptions | RealFieldOptions | JsonFieldOptions | JsonbFieldOptions | BooleanFieldOptions | RadioFieldOptions | SortFieldOptions | TextFieldOptions | VirtualFieldOptions | ArrayFieldOptions | SetFieldOptions | TimeFieldOptions | DateFieldOptions | DatetimeTzFieldOptions | DatetimeNoTzFieldOptions | DateOnlyFieldOptions | UnixTimestampFieldOptions | UidFieldOptions | UUIDFieldOptions | NanoidFieldOptions | PasswordFieldOptions | ContextFieldOptions | BelongsToFieldOptions | HasOneFieldOptions | HasManyFieldOptions | BelongsToManyFieldOptions | EncryptionField;
@@ -42,6 +42,7 @@ __reExport(fields_exports, require("./password-field"), module.exports);
42
42
  __reExport(fields_exports, require("./radio-field"), module.exports);
43
43
  __reExport(fields_exports, require("./relation-field"), module.exports);
44
44
  __reExport(fields_exports, require("./set-field"), module.exports);
45
+ __reExport(fields_exports, require("./sort-field"), module.exports);
45
46
  __reExport(fields_exports, require("./string-field"), module.exports);
46
47
  __reExport(fields_exports, require("./text-field"), module.exports);
47
48
  __reExport(fields_exports, require("./time-field"), module.exports);
@@ -72,6 +73,7 @@ __reExport(fields_exports, require("./unix-timestamp-field"), module.exports);
72
73
  ...require("./radio-field"),
73
74
  ...require("./relation-field"),
74
75
  ...require("./set-field"),
76
+ ...require("./sort-field"),
75
77
  ...require("./string-field"),
76
78
  ...require("./text-field"),
77
79
  ...require("./time-field"),
@@ -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 { DataTypes } from 'sequelize';
10
+ import { BaseColumnFieldOptions, Field } from './field';
11
+ export declare class SortField extends Field {
12
+ get dataType(): DataTypes.BigIntDataTypeConstructor;
13
+ setSortValue: (instance: any, options: any) => Promise<void>;
14
+ onScopeChange: (instance: any, options: any) => Promise<void>;
15
+ initRecordsSortValue: (options: any) => Promise<void>;
16
+ bind(): void;
17
+ unbind(): void;
18
+ }
19
+ export interface SortFieldOptions extends BaseColumnFieldOptions {
20
+ type: 'sort';
21
+ scopeKey?: string;
22
+ }
@@ -0,0 +1,197 @@
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 sort_field_exports = {};
29
+ __export(sort_field_exports, {
30
+ SortField: () => SortField
31
+ });
32
+ module.exports = __toCommonJS(sort_field_exports);
33
+ var import_async_mutex = require("async-mutex");
34
+ var import_lodash = require("lodash");
35
+ var import_sequelize = require("sequelize");
36
+ var import_field = require("./field");
37
+ const sortFieldMutex = new import_async_mutex.Mutex();
38
+ const _SortField = class _SortField extends import_field.Field {
39
+ get dataType() {
40
+ return import_sequelize.DataTypes.BIGINT;
41
+ }
42
+ setSortValue = /* @__PURE__ */ __name(async (instance, options) => {
43
+ const { name, scopeKey } = this.options;
44
+ const { model } = this.context.collection;
45
+ if ((0, import_lodash.isNumber)(instance.get(name)) && instance._previousDataValues[scopeKey] == instance[scopeKey]) {
46
+ return;
47
+ }
48
+ const where = {};
49
+ if (scopeKey) {
50
+ const value = instance.get(scopeKey);
51
+ if (value !== void 0 && value !== null) {
52
+ where[scopeKey] = value;
53
+ }
54
+ }
55
+ await sortFieldMutex.runExclusive(async () => {
56
+ const max = await model.max(name, { ...options, where });
57
+ const newValue = (max || 0) + 1;
58
+ instance.set(name, newValue);
59
+ });
60
+ }, "setSortValue");
61
+ onScopeChange = /* @__PURE__ */ __name(async (instance, options) => {
62
+ const { scopeKey } = this.options;
63
+ if (scopeKey && !instance.isNewRecord && instance._previousDataValues[scopeKey] != instance[scopeKey]) {
64
+ await this.setSortValue(instance, options);
65
+ }
66
+ }, "onScopeChange");
67
+ initRecordsSortValue = /* @__PURE__ */ __name(async (options) => {
68
+ const { transaction } = options;
69
+ const orderField = (() => {
70
+ const model = this.collection.model;
71
+ if (model.primaryKeyAttribute) {
72
+ return model.primaryKeyAttribute;
73
+ }
74
+ if (model.rawAttributes["createdAt"]) {
75
+ return model.rawAttributes["createdAt"].field;
76
+ }
77
+ throw new Error(`can not find order key for collection ${this.collection.name}`);
78
+ })();
79
+ const needInit = /* @__PURE__ */ __name(async (scopeKey2 = null, scopeValue = null) => {
80
+ const filter = {};
81
+ if (scopeKey2 && scopeValue) {
82
+ filter[scopeKey2] = scopeValue;
83
+ }
84
+ const totalCount = await this.collection.repository.count({
85
+ filter,
86
+ transaction
87
+ });
88
+ const emptyCount = await this.collection.repository.count({
89
+ filter: {
90
+ [this.name]: null,
91
+ ...filter
92
+ },
93
+ transaction
94
+ });
95
+ return emptyCount === totalCount && emptyCount > 0;
96
+ }, "needInit");
97
+ const doInit = /* @__PURE__ */ __name(async (scopeKey2 = null, scopeValue = null) => {
98
+ const queryInterface = this.collection.db.sequelize.getQueryInterface();
99
+ if (scopeKey2) {
100
+ const scopeAttribute = this.collection.model.rawAttributes[scopeKey2];
101
+ if (!scopeAttribute) {
102
+ throw new Error(`can not find scope field ${scopeKey2} for collection ${this.collection.name}`);
103
+ }
104
+ scopeKey2 = scopeAttribute.field;
105
+ }
106
+ const quotedOrderField = queryInterface.quoteIdentifier(orderField);
107
+ const sortColumnName = queryInterface.quoteIdentifier(this.collection.model.rawAttributes[this.name].field);
108
+ let sql;
109
+ const whereClause = scopeKey2 && scopeValue ? (() => {
110
+ const filteredScopeValue = scopeValue.filter((v) => v !== null);
111
+ if (filteredScopeValue.length === 0) {
112
+ return "";
113
+ }
114
+ const initialClause = `
115
+ WHERE ${queryInterface.quoteIdentifier(scopeKey2)} IN (${filteredScopeValue.map((v) => `'${v}'`).join(", ")})`;
116
+ const nullCheck = scopeValue.includes(null) ? ` OR ${queryInterface.quoteIdentifier(scopeKey2)} IS NULL` : "";
117
+ return initialClause + nullCheck;
118
+ })() : "";
119
+ if (this.collection.db.inDialect("postgres")) {
120
+ sql = `
121
+ UPDATE ${this.collection.quotedTableName()}
122
+ SET ${sortColumnName} = ordered_table.new_sequence_number
123
+ FROM (
124
+ SELECT *, ROW_NUMBER() OVER (${scopeKey2 ? `PARTITION BY ${queryInterface.quoteIdentifier(scopeKey2)}` : ""} ORDER BY ${quotedOrderField}) AS new_sequence_number
125
+ FROM ${this.collection.quotedTableName()}
126
+ ${whereClause}
127
+ ) AS ordered_table
128
+ WHERE ${this.collection.quotedTableName()}.${quotedOrderField} = ordered_table.${quotedOrderField};
129
+ `;
130
+ } else if (this.collection.db.inDialect("sqlite")) {
131
+ sql = `
132
+ UPDATE ${this.collection.quotedTableName()}
133
+ SET ${sortColumnName} = (
134
+ SELECT new_sequence_number
135
+ FROM (
136
+ SELECT *, ROW_NUMBER() OVER (${scopeKey2 ? `PARTITION BY ${queryInterface.quoteIdentifier(scopeKey2)}` : ""} ORDER BY ${quotedOrderField}) AS new_sequence_number
137
+ FROM ${this.collection.quotedTableName()}
138
+ ${whereClause}
139
+ ) AS ordered_table
140
+ WHERE ${this.collection.quotedTableName()}.${quotedOrderField} = ordered_table.${quotedOrderField}
141
+ );
142
+ `;
143
+ } else if (this.collection.db.inDialect("mysql") || this.collection.db.inDialect("mariadb")) {
144
+ sql = `
145
+ UPDATE ${this.collection.quotedTableName()}
146
+ JOIN (
147
+ SELECT *, ROW_NUMBER() OVER (${scopeKey2 ? `PARTITION BY ${queryInterface.quoteIdentifier(scopeKey2)}` : ""} ORDER BY ${quotedOrderField}) AS new_sequence_number
148
+ FROM ${this.collection.quotedTableName()}
149
+ ${whereClause}
150
+ ) AS ordered_table ON ${this.collection.quotedTableName()}.${quotedOrderField} = ordered_table.${quotedOrderField}
151
+ SET ${this.collection.quotedTableName()}.${sortColumnName} = ordered_table.new_sequence_number;
152
+ `;
153
+ }
154
+ await this.collection.db.sequelize.query(sql, {
155
+ transaction
156
+ });
157
+ }, "doInit");
158
+ const scopeKey = this.options.scopeKey;
159
+ if (scopeKey) {
160
+ const scopeKeyColumn = this.collection.model.rawAttributes[scopeKey].field;
161
+ const groups = await this.collection.model.findAll({
162
+ attributes: [[import_sequelize.Sequelize.fn("DISTINCT", import_sequelize.Sequelize.col(scopeKeyColumn)), scopeKey]],
163
+ raw: true,
164
+ transaction
165
+ });
166
+ const needInitGroups = [];
167
+ for (const group of groups) {
168
+ if (await needInit(scopeKey, group[scopeKey])) {
169
+ needInitGroups.push(group[scopeKey]);
170
+ }
171
+ }
172
+ if (needInitGroups.length > 0) {
173
+ await doInit(scopeKey, needInitGroups);
174
+ }
175
+ } else if (await needInit()) {
176
+ await doInit();
177
+ }
178
+ }, "initRecordsSortValue");
179
+ bind() {
180
+ super.bind();
181
+ this.on("afterSync", this.initRecordsSortValue);
182
+ this.on("beforeUpdate", this.onScopeChange);
183
+ this.on("beforeCreate", this.setSortValue);
184
+ }
185
+ unbind() {
186
+ super.unbind();
187
+ this.off("beforeUpdate", this.onScopeChange);
188
+ this.off("beforeCreate", this.setSortValue);
189
+ this.off("afterSync", this.initRecordsSortValue);
190
+ }
191
+ };
192
+ __name(_SortField, "SortField");
193
+ let SortField = _SortField;
194
+ // Annotate the CommonJS export names for ESM import in node:
195
+ 0 && (module.exports = {
196
+ SortField
197
+ });
@@ -258,7 +258,11 @@ async function updateSingleAssociation(model, key, value, options = {}) {
258
258
  return;
259
259
  }
260
260
  if (updateAssociationValues.includes(key)) {
261
- await instance2.update(value, { ...options, transaction });
261
+ const updateValues = { ...value };
262
+ if (association.associationType === "HasOne") {
263
+ delete updateValues[association.foreignKey];
264
+ }
265
+ await instance2.update(updateValues, { ...options, transaction });
262
266
  }
263
267
  await updateAssociations(instance2, value, {
264
268
  ...options,
@@ -298,7 +302,7 @@ async function updateMultipleAssociation(model, key, value, options = {}) {
298
302
  const setAccessor = association.accessors.set;
299
303
  const createAccessor = association.accessors.create;
300
304
  if (isUndefinedOrNull(value)) {
301
- await model[setAccessor](null, { transaction, context, individualHooks: true });
305
+ await model[setAccessor](null, { transaction, context, individualHooks: true, validate: false });
302
306
  model.setDataValue(key, null);
303
307
  return;
304
308
  }
@@ -306,7 +310,7 @@ async function updateMultipleAssociation(model, key, value, options = {}) {
306
310
  throw new Error(`The source key ${association.sourceKeyAttribute} is not set in ${model.constructor.name}`);
307
311
  }
308
312
  if (isStringOrNumber(value)) {
309
- await model[setAccessor](value, { transaction, context, individualHooks: true });
313
+ await model[setAccessor](value, { transaction, context, individualHooks: true, validate: false });
310
314
  return;
311
315
  }
312
316
  value = import_lodash.default.castArray(value);
@@ -334,7 +338,7 @@ async function updateMultipleAssociation(model, key, value, options = {}) {
334
338
  objectItems.push(item);
335
339
  }
336
340
  }
337
- await model[setAccessor](setItems, { transaction, context, individualHooks: true });
341
+ await model[setAccessor](setItems, { transaction, context, individualHooks: true, validate: false });
338
342
  const newItems = [];
339
343
  const pk = association.target.primaryKeyAttribute;
340
344
  let targetKey = pk;
@@ -393,6 +397,9 @@ async function updateMultipleAssociation(model, key, value, options = {}) {
393
397
  continue;
394
398
  }
395
399
  if (updateAssociationValues.includes(key)) {
400
+ if (association.associationType === "HasMany") {
401
+ delete item[association.foreignKey];
402
+ }
396
403
  await instance.update(item, { ...options, transaction });
397
404
  }
398
405
  await updateAssociations(instance, item, {
@@ -62,7 +62,7 @@ const _ViewFieldInference = class _ViewFieldInference {
62
62
  });
63
63
  const rawFields = [];
64
64
  for (const [name, column] of Object.entries(columns)) {
65
- const inferResult = { name, rawType: column.type };
65
+ const inferResult = { name, rawType: column.type, field: name };
66
66
  const usage = columnUsage[name];
67
67
  if (usage) {
68
68
  const collection = db.tableNameCollectionMap.get(
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/database",
3
- "version": "1.5.0-alpha.5",
3
+ "version": "1.5.0-beta.2",
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.5.0-alpha.5",
10
- "@nocobase/utils": "1.5.0-alpha.5",
9
+ "@nocobase/logger": "1.5.0-beta.2",
10
+ "@nocobase/utils": "1.5.0-beta.2",
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": "8d24e8d2c0f3f57ceaa271438fd93db2b4150011"
41
+ "gitHead": "8a9c29fdac5c6295dcc7abf00c3fa81bd9e01a36"
42
42
  }