@nocobase/plugin-custom-variables 2.0.3
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/LICENSE +201 -0
- package/README.md +99 -0
- package/client.d.ts +2 -0
- package/client.js +1 -0
- package/dist/client/AddVariableButton.d.ts +7 -0
- package/dist/client/DrawerDescription.d.ts +10 -0
- package/dist/client/EditBadge.d.ts +11 -0
- package/dist/client/index.d.ts +13 -0
- package/dist/client/index.js +10 -0
- package/dist/client/useCustomVariablesOptions.d.ts +5 -0
- package/dist/client/variableInitializer.d.ts +8 -0
- package/dist/externalVersion.js +22 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +48 -0
- package/dist/locale/de-DE.json +36 -0
- package/dist/locale/en-US.json +36 -0
- package/dist/locale/es-ES.json +36 -0
- package/dist/locale/fr-FR.json +36 -0
- package/dist/locale/hu-HU.json +36 -0
- package/dist/locale/id-ID.json +36 -0
- package/dist/locale/index.d.ts +1 -0
- package/dist/locale/index.js +36 -0
- package/dist/locale/it-IT.json +36 -0
- package/dist/locale/ja-JP.json +36 -0
- package/dist/locale/ko-KR.json +36 -0
- package/dist/locale/nl-NL.json +36 -0
- package/dist/locale/pt-BR.json +36 -0
- package/dist/locale/ru-RU.json +36 -0
- package/dist/locale/tr-TR.json +36 -0
- package/dist/locale/uk-UA.json +36 -0
- package/dist/locale/vi-VN.json +36 -0
- package/dist/locale/zh-CN.json +36 -0
- package/dist/locale/zh-TW.json +36 -0
- package/dist/server/collections/customVariables.d.ts +10 -0
- package/dist/server/collections/customVariables.js +116 -0
- package/dist/server/index.d.ts +18 -0
- package/dist/server/index.js +124 -0
- package/dist/server/parseFilter.d.ts +1 -0
- package/dist/server/parseFilter.js +44 -0
- package/package.json +29 -0
- package/server.d.ts +2 -0
- package/server.js +1 -0
|
@@ -0,0 +1,116 @@
|
|
|
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 __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var customVariables_exports = {};
|
|
28
|
+
__export(customVariables_exports, {
|
|
29
|
+
default: () => customVariables_default
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(customVariables_exports);
|
|
32
|
+
var import_database = require("@nocobase/database");
|
|
33
|
+
var customVariables_default = (0, import_database.defineCollection)({
|
|
34
|
+
name: "customVariables",
|
|
35
|
+
dumpRules: "required",
|
|
36
|
+
migrationRules: ["overwrite", "schema-only"],
|
|
37
|
+
shared: true,
|
|
38
|
+
autoGenId: false,
|
|
39
|
+
timestamps: true,
|
|
40
|
+
createdBy: true,
|
|
41
|
+
updatedBy: true,
|
|
42
|
+
logging: true,
|
|
43
|
+
fields: [
|
|
44
|
+
{
|
|
45
|
+
name: "name",
|
|
46
|
+
type: "string",
|
|
47
|
+
interface: "input",
|
|
48
|
+
primaryKey: true,
|
|
49
|
+
allowNull: false,
|
|
50
|
+
unique: true,
|
|
51
|
+
uiSchema: {
|
|
52
|
+
type: "string",
|
|
53
|
+
title: '{{t("Variable Name")}}',
|
|
54
|
+
"x-component": "Input",
|
|
55
|
+
required: true
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: "label",
|
|
60
|
+
type: "string",
|
|
61
|
+
interface: "input",
|
|
62
|
+
allowNull: false,
|
|
63
|
+
uiSchema: {
|
|
64
|
+
type: "string",
|
|
65
|
+
title: '{{t("Label")}}',
|
|
66
|
+
"x-component": "Input",
|
|
67
|
+
required: true
|
|
68
|
+
},
|
|
69
|
+
translation: true
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
name: "declaredAt",
|
|
73
|
+
type: "uid",
|
|
74
|
+
interface: "input",
|
|
75
|
+
allowNull: false,
|
|
76
|
+
uiSchema: {
|
|
77
|
+
type: "string",
|
|
78
|
+
title: '{{t("Declared At")}}',
|
|
79
|
+
"x-component": "Input",
|
|
80
|
+
"x-disabled": true
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name: "type",
|
|
85
|
+
type: "string",
|
|
86
|
+
interface: "input",
|
|
87
|
+
allowNull: false,
|
|
88
|
+
defaultValue: "aggregation",
|
|
89
|
+
uiSchema: {
|
|
90
|
+
type: "string",
|
|
91
|
+
title: '{{t("Variable Type")}}',
|
|
92
|
+
"x-component": "Input",
|
|
93
|
+
required: true
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: "options",
|
|
98
|
+
type: "json",
|
|
99
|
+
interface: "json",
|
|
100
|
+
defaultValue: {},
|
|
101
|
+
uiSchema: {
|
|
102
|
+
type: "object",
|
|
103
|
+
title: '{{t("Options")}}',
|
|
104
|
+
"x-component": "Input.JSON",
|
|
105
|
+
description: '{{t("Variable configuration options")}}'
|
|
106
|
+
}
|
|
107
|
+
// 聚合选项需包含:
|
|
108
|
+
// - collection:数据集合
|
|
109
|
+
// - field:字段名
|
|
110
|
+
// - aggregator:聚合方法 (count, sum, avg, min, max)
|
|
111
|
+
// - filter:过滤条件 (可选)
|
|
112
|
+
// - distinct:是否去重 (可选,默认 false)
|
|
113
|
+
// - precision:精度 (可选,默认 2,适用于 avg)
|
|
114
|
+
}
|
|
115
|
+
]
|
|
116
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
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 program is offered under a commercial license.
|
|
7
|
+
* For more information, see <https://www.nocobase.com/agreement>
|
|
8
|
+
*/
|
|
9
|
+
import { InstallOptions, Plugin } from '@nocobase/server';
|
|
10
|
+
export declare class PluginCustomVariablesServer extends Plugin {
|
|
11
|
+
afterAdd(): void;
|
|
12
|
+
load(): Promise<void>;
|
|
13
|
+
install(options?: InstallOptions): Promise<void>;
|
|
14
|
+
afterEnable(): Promise<void>;
|
|
15
|
+
afterDisable(): Promise<void>;
|
|
16
|
+
remove(): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
export default PluginCustomVariablesServer;
|
|
@@ -0,0 +1,124 @@
|
|
|
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 __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var server_exports = {};
|
|
28
|
+
__export(server_exports, {
|
|
29
|
+
PluginCustomVariablesServer: () => PluginCustomVariablesServer,
|
|
30
|
+
default: () => server_default
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(server_exports);
|
|
33
|
+
var import_server = require("@nocobase/server");
|
|
34
|
+
var import_mathjs = require("mathjs");
|
|
35
|
+
var import_data_source_manager = require("@nocobase/data-source-manager");
|
|
36
|
+
var import_database = require("@nocobase/database");
|
|
37
|
+
var import_parseFilter = require("./parseFilter");
|
|
38
|
+
const aggregators = {
|
|
39
|
+
count: "count",
|
|
40
|
+
sum: "sum",
|
|
41
|
+
avg: "avg",
|
|
42
|
+
min: "min",
|
|
43
|
+
max: "max"
|
|
44
|
+
};
|
|
45
|
+
class PluginCustomVariablesServer extends import_server.Plugin {
|
|
46
|
+
afterAdd() {
|
|
47
|
+
}
|
|
48
|
+
async load() {
|
|
49
|
+
this.app.resourceManager.registerActionHandler("customVariables:parse", async (ctx, next) => {
|
|
50
|
+
const { name } = ctx.query;
|
|
51
|
+
const { filterCtx } = ctx.request.body;
|
|
52
|
+
if (!name) {
|
|
53
|
+
return ctx.throw(400, "Variable name is required");
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
const variableRepo = this.app.db.getRepository("customVariables");
|
|
57
|
+
const variable = await variableRepo.findOne({
|
|
58
|
+
filter: { name }
|
|
59
|
+
});
|
|
60
|
+
if (!variable) {
|
|
61
|
+
return ctx.throw(404, `Variable '${name}' not found`);
|
|
62
|
+
}
|
|
63
|
+
const { type, options } = variable.toJSON();
|
|
64
|
+
if (type !== "aggregate") {
|
|
65
|
+
return ctx.throw(400, `Variable '${name}' is not an aggregate variable`);
|
|
66
|
+
}
|
|
67
|
+
if (filterCtx && options.params.filter) {
|
|
68
|
+
options.params.filter = (0, import_parseFilter.parseFilter)(options.params.filter, filterCtx);
|
|
69
|
+
}
|
|
70
|
+
const {
|
|
71
|
+
collection,
|
|
72
|
+
params: {
|
|
73
|
+
field,
|
|
74
|
+
filter,
|
|
75
|
+
distinct
|
|
76
|
+
},
|
|
77
|
+
aggregator,
|
|
78
|
+
precision = 2
|
|
79
|
+
} = options;
|
|
80
|
+
if (!collection || !field || !aggregator || !aggregators[aggregator]) {
|
|
81
|
+
return ctx.throw(400, "Invalid variable configuration");
|
|
82
|
+
}
|
|
83
|
+
const [dataSourceName, collectionName] = (0, import_data_source_manager.parseCollectionName)(collection);
|
|
84
|
+
const { collectionManager } = this.app.dataSourceManager.dataSources.get(dataSourceName);
|
|
85
|
+
if (!collectionManager.db) {
|
|
86
|
+
throw new Error("Aggregate variables can only work with data source of type database");
|
|
87
|
+
}
|
|
88
|
+
const repo = collectionManager.getRepository(collectionName);
|
|
89
|
+
const aggregateOptions = {
|
|
90
|
+
method: aggregators[aggregator],
|
|
91
|
+
field,
|
|
92
|
+
filter,
|
|
93
|
+
distinct
|
|
94
|
+
};
|
|
95
|
+
if (aggregator === "avg") {
|
|
96
|
+
aggregateOptions["dataType"] = import_database.DataTypes.DOUBLE;
|
|
97
|
+
}
|
|
98
|
+
const result = await repo.aggregate(aggregateOptions);
|
|
99
|
+
const processedResult = (0, import_mathjs.round)(
|
|
100
|
+
(aggregator === "avg" ? Number(result) : result) || 0,
|
|
101
|
+
Math.max(0, Math.min(precision, 14))
|
|
102
|
+
);
|
|
103
|
+
ctx.body = processedResult;
|
|
104
|
+
} catch (error) {
|
|
105
|
+
ctx.throw(500, `Error parsing variable: ${error.message}`);
|
|
106
|
+
}
|
|
107
|
+
await next();
|
|
108
|
+
});
|
|
109
|
+
this.app.acl.allow("customVariables", ["parse", "create", "update", "destroy", "list", "get"], "loggedIn");
|
|
110
|
+
}
|
|
111
|
+
async install(options) {
|
|
112
|
+
}
|
|
113
|
+
async afterEnable() {
|
|
114
|
+
}
|
|
115
|
+
async afterDisable() {
|
|
116
|
+
}
|
|
117
|
+
async remove() {
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
var server_default = PluginCustomVariablesServer;
|
|
121
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
122
|
+
0 && (module.exports = {
|
|
123
|
+
PluginCustomVariablesServer
|
|
124
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const parseFilter: (filter: any, ctx: Record<string, any>) => any;
|
|
@@ -0,0 +1,44 @@
|
|
|
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 __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var parseFilter_exports = {};
|
|
28
|
+
__export(parseFilter_exports, {
|
|
29
|
+
parseFilter: () => parseFilter
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(parseFilter_exports);
|
|
32
|
+
const parseFilter = (filter, ctx) => {
|
|
33
|
+
if (typeof filter !== "string") {
|
|
34
|
+
filter = JSON.stringify(filter);
|
|
35
|
+
}
|
|
36
|
+
Object.keys(ctx).forEach((key) => {
|
|
37
|
+
filter = filter.replaceAll(`"${key}"`, JSON.stringify(ctx[key]));
|
|
38
|
+
});
|
|
39
|
+
return JSON.parse(filter);
|
|
40
|
+
};
|
|
41
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
42
|
+
0 && (module.exports = {
|
|
43
|
+
parseFilter
|
|
44
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nocobase/plugin-custom-variables",
|
|
3
|
+
"displayName": "Custom variables",
|
|
4
|
+
"displayName.zh-CN": "自定义变量",
|
|
5
|
+
"description": "Support for adding and using custom variables",
|
|
6
|
+
"description.zh-CN": "支持新增和使用自定义变量",
|
|
7
|
+
"version": "2.0.3",
|
|
8
|
+
"main": "./dist/server/index.js",
|
|
9
|
+
"homepage": "https://docs.nocobase.com/handbook/custom-variables",
|
|
10
|
+
"homepage.zh-CN": "https://docs-cn.nocobase.com/handbook/custom-variables",
|
|
11
|
+
"nocobase": {
|
|
12
|
+
"supportedVersions": [
|
|
13
|
+
"1.x"
|
|
14
|
+
],
|
|
15
|
+
"editionLevel": 0
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@nocobase/client": "2.x",
|
|
19
|
+
"@nocobase/database": "2.x",
|
|
20
|
+
"@nocobase/server": "2.x",
|
|
21
|
+
"@nocobase/test": "2.x",
|
|
22
|
+
"antd-style": "3.x"
|
|
23
|
+
},
|
|
24
|
+
"gitHead": "5bcb42fc092f85adb9511c1a351b388bd7aaf66e",
|
|
25
|
+
"keywords": [
|
|
26
|
+
"Custom variables"
|
|
27
|
+
],
|
|
28
|
+
"license": "Apache-2.0"
|
|
29
|
+
}
|
package/server.d.ts
ADDED
package/server.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./dist/server/index.js');
|