@nocobase/database 1.5.0-beta.1 → 1.6.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.
@@ -20,7 +20,6 @@ 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';
24
23
  import { StringFieldOptions } from './string-field';
25
24
  import { TextFieldOptions } from './text-field';
26
25
  import { TimeFieldOptions } from './time-field';
@@ -52,7 +51,6 @@ export * from './password-field';
52
51
  export * from './radio-field';
53
52
  export * from './relation-field';
54
53
  export * from './set-field';
55
- export * from './sort-field';
56
54
  export * from './string-field';
57
55
  export * from './text-field';
58
56
  export * from './time-field';
@@ -62,4 +60,4 @@ export * from './virtual-field';
62
60
  export * from './nanoid-field';
63
61
  export * from './encryption-field';
64
62
  export * from './unix-timestamp-field';
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;
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;
@@ -42,7 +42,6 @@ __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);
46
45
  __reExport(fields_exports, require("./string-field"), module.exports);
47
46
  __reExport(fields_exports, require("./text-field"), module.exports);
48
47
  __reExport(fields_exports, require("./time-field"), module.exports);
@@ -73,7 +72,6 @@ __reExport(fields_exports, require("./unix-timestamp-field"), module.exports);
73
72
  ...require("./radio-field"),
74
73
  ...require("./relation-field"),
75
74
  ...require("./set-field"),
76
- ...require("./sort-field"),
77
75
  ...require("./string-field"),
78
76
  ...require("./text-field"),
79
77
  ...require("./time-field"),
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/database",
3
- "version": "1.5.0-beta.1",
3
+ "version": "1.6.0-alpha.1",
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-beta.1",
10
- "@nocobase/utils": "1.5.0-beta.1",
9
+ "@nocobase/logger": "1.6.0-alpha.1",
10
+ "@nocobase/utils": "1.6.0-alpha.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": "10c6f1f3d90e91f3aabfa80449c7ef062e90f6af"
41
+ "gitHead": "a4c91015e34ec0c9a427451b5fbdfb5fedc4f3d7"
42
42
  }
@@ -1,22 +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 { 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
- }
@@ -1,197 +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 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
- });