@nocobase/actions 1.5.0-beta.1 → 1.6.0-alpha.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.
@@ -15,6 +15,5 @@ export * from './add';
15
15
  export * from './set';
16
16
  export * from './remove';
17
17
  export * from './toggle';
18
- export * from './move';
19
18
  export * from './first-or-create';
20
19
  export * from './update-or-create';
@@ -32,7 +32,6 @@ __reExport(actions_exports, require("./add"), module.exports);
32
32
  __reExport(actions_exports, require("./set"), module.exports);
33
33
  __reExport(actions_exports, require("./remove"), module.exports);
34
34
  __reExport(actions_exports, require("./toggle"), module.exports);
35
- __reExport(actions_exports, require("./move"), module.exports);
36
35
  __reExport(actions_exports, require("./first-or-create"), module.exports);
37
36
  __reExport(actions_exports, require("./update-or-create"), module.exports);
38
37
  // Annotate the CommonJS export names for ESM import in node:
@@ -46,7 +45,6 @@ __reExport(actions_exports, require("./update-or-create"), module.exports);
46
45
  ...require("./set"),
47
46
  ...require("./remove"),
48
47
  ...require("./toggle"),
49
- ...require("./move"),
50
48
  ...require("./first-or-create"),
51
49
  ...require("./update-or-create")
52
50
  });
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@nocobase/actions",
3
- "version": "1.5.0-beta.1",
3
+ "version": "1.6.0-alpha.2",
4
4
  "description": "",
5
5
  "license": "AGPL-3.0",
6
6
  "main": "./lib/index.js",
7
7
  "types": "./lib/index.d.ts",
8
8
  "dependencies": {
9
- "@nocobase/cache": "1.5.0-beta.1",
10
- "@nocobase/database": "1.5.0-beta.1",
11
- "@nocobase/resourcer": "1.5.0-beta.1"
9
+ "@nocobase/cache": "1.6.0-alpha.2",
10
+ "@nocobase/database": "1.6.0-alpha.2",
11
+ "@nocobase/resourcer": "1.6.0-alpha.2"
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
15
15
  "url": "git+https://github.com/nocobase/nocobase.git",
16
16
  "directory": "packages/actions"
17
17
  },
18
- "gitHead": "10c6f1f3d90e91f3aabfa80449c7ef062e90f6af"
18
+ "gitHead": "08bbc34c21727fc0ad0880f397a42bf7741091ee"
19
19
  }
@@ -1,26 +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 'sequelize';
10
- import { Collection, SortField, TargetKey } from '@nocobase/database';
11
- import { Context } from '..';
12
- export declare function move(ctx: Context, next: any): Promise<void>;
13
- interface MoveOptions {
14
- insertAfter?: boolean;
15
- }
16
- export declare class SortAbleCollection {
17
- collection: Collection;
18
- field: SortField;
19
- scopeKey: string;
20
- constructor(collection: Collection, fieldName?: string);
21
- move(sourceInstanceId: TargetKey, targetInstanceId: TargetKey, options?: MoveOptions): Promise<void>;
22
- changeScope(sourceInstanceId: TargetKey, targetScope: any, method?: string): Promise<void>;
23
- sticky(sourceInstanceId: TargetKey): Promise<void>;
24
- sameScopeMove(sourceInstance: Model, targetInstance: Model, options: MoveOptions): Promise<void>;
25
- }
26
- export {};
@@ -1,166 +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 move_exports = {};
29
- __export(move_exports, {
30
- SortAbleCollection: () => SortAbleCollection,
31
- move: () => move
32
- });
33
- module.exports = __toCommonJS(move_exports);
34
- var import_sequelize = require("sequelize");
35
- var import_database = require("@nocobase/database");
36
- var import_utils = require("../utils");
37
- async function move(ctx, next) {
38
- const repository = ctx.databaseRepository || (0, import_utils.getRepositoryFromParams)(ctx);
39
- const { sourceId, targetId, targetScope, sticky, method } = ctx.action.params;
40
- let sortField = ctx.action.params.sortField;
41
- if (repository instanceof import_database.BelongsToManyRepository) {
42
- throw new Error("Sorting association as 'belongs-to-many' type is not supported.");
43
- }
44
- if (repository instanceof import_database.HasManyRepository && !sortField) {
45
- sortField = `${repository.association.foreignKey}Sort`;
46
- }
47
- const sortAbleCollection = new SortAbleCollection(repository.collection, sortField);
48
- if (sourceId && targetId) {
49
- await sortAbleCollection.move(sourceId, targetId, {
50
- insertAfter: method === "insertAfter"
51
- });
52
- }
53
- if (sourceId && targetScope) {
54
- await sortAbleCollection.changeScope(sourceId, targetScope, method);
55
- }
56
- if (sourceId && sticky) {
57
- await sortAbleCollection.sticky(sourceId);
58
- }
59
- ctx.body = "ok";
60
- await next();
61
- }
62
- __name(move, "move");
63
- const _SortAbleCollection = class _SortAbleCollection {
64
- collection;
65
- field;
66
- scopeKey;
67
- constructor(collection, fieldName = "sort") {
68
- this.collection = collection;
69
- this.field = collection.getField(fieldName);
70
- if (!(this.field instanceof import_database.SortField)) {
71
- throw new Error(`${fieldName} is not a sort field`);
72
- }
73
- this.scopeKey = this.field.get("scopeKey");
74
- }
75
- // insert source position to target position
76
- async move(sourceInstanceId, targetInstanceId, options = {}) {
77
- const sourceInstance = await this.collection.repository.findByTargetKey(sourceInstanceId);
78
- const targetInstance = await this.collection.repository.findByTargetKey(targetInstanceId);
79
- if (this.scopeKey && sourceInstance.get(this.scopeKey) !== targetInstance.get(this.scopeKey)) {
80
- await sourceInstance.update({
81
- [this.scopeKey]: targetInstance.get(this.scopeKey)
82
- });
83
- }
84
- await this.sameScopeMove(sourceInstance, targetInstance, options);
85
- }
86
- async changeScope(sourceInstanceId, targetScope, method) {
87
- const sourceInstance = await this.collection.repository.findByTargetKey(sourceInstanceId);
88
- const targetScopeValue = targetScope[this.scopeKey];
89
- if (targetScopeValue && sourceInstance.get(this.scopeKey) !== targetScopeValue) {
90
- await sourceInstance.update(
91
- {
92
- [this.scopeKey]: targetScopeValue
93
- },
94
- {
95
- silent: false
96
- }
97
- );
98
- if (method === "prepend") {
99
- await this.sticky(sourceInstanceId);
100
- }
101
- }
102
- }
103
- async sticky(sourceInstanceId) {
104
- const sourceInstance = await this.collection.repository.findByTargetKey(sourceInstanceId);
105
- await sourceInstance.update(
106
- {
107
- [this.field.get("name")]: 0
108
- },
109
- {
110
- silent: true
111
- }
112
- );
113
- }
114
- async sameScopeMove(sourceInstance, targetInstance, options) {
115
- const fieldName = this.field.get("name");
116
- const sourceSort = sourceInstance.get(fieldName);
117
- let targetSort = targetInstance.get(fieldName);
118
- if (options.insertAfter) {
119
- targetSort = targetSort + 1;
120
- }
121
- const scopeValue = this.scopeKey ? sourceInstance.get(this.scopeKey) : null;
122
- let updateCondition;
123
- let change;
124
- if (targetSort > sourceSort) {
125
- updateCondition = {
126
- [import_sequelize.Op.gt]: sourceSort,
127
- [import_sequelize.Op.lte]: targetSort
128
- };
129
- change = -1;
130
- } else {
131
- updateCondition = {
132
- [import_sequelize.Op.lt]: sourceSort,
133
- [import_sequelize.Op.gte]: targetSort
134
- };
135
- change = 1;
136
- }
137
- const where = {
138
- [fieldName]: updateCondition
139
- };
140
- if (scopeValue) {
141
- where[this.scopeKey] = {
142
- [import_sequelize.Op.eq]: scopeValue
143
- };
144
- }
145
- await this.collection.model.increment(fieldName, {
146
- where,
147
- by: change,
148
- silent: true
149
- });
150
- await sourceInstance.update(
151
- {
152
- [fieldName]: targetSort
153
- },
154
- {
155
- silent: true
156
- }
157
- );
158
- }
159
- };
160
- __name(_SortAbleCollection, "SortAbleCollection");
161
- let SortAbleCollection = _SortAbleCollection;
162
- // Annotate the CommonJS export names for ESM import in node:
163
- 0 && (module.exports = {
164
- SortAbleCollection,
165
- move
166
- });