@nocobase/actions 0.5.0-alpha.31 → 0.5.0-alpha.35
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/lib/actions/add.d.ts.map +1 -1
- package/lib/actions/add.js +3 -3
- package/lib/actions/add.js.map +1 -1
- package/lib/actions/create.js +1 -1
- package/lib/actions/create.js.map +1 -1
- package/lib/actions/destroy.d.ts.map +1 -1
- package/lib/actions/destroy.js +8 -8
- package/lib/actions/destroy.js.map +1 -1
- package/lib/actions/get.d.ts.map +1 -1
- package/lib/actions/get.js +4 -4
- package/lib/actions/get.js.map +1 -1
- package/lib/actions/remove.d.ts.map +1 -1
- package/lib/actions/remove.js +3 -3
- package/lib/actions/remove.js.map +1 -1
- package/lib/actions/set.d.ts.map +1 -1
- package/lib/actions/set.js +3 -3
- package/lib/actions/set.js.map +1 -1
- package/lib/actions/sort.d.ts.map +1 -1
- package/lib/actions/sort.js +6 -6
- package/lib/actions/sort.js.map +1 -1
- package/lib/actions/toggle.d.ts.map +1 -1
- package/lib/actions/toggle.js +6 -6
- package/lib/actions/toggle.js.map +1 -1
- package/lib/actions/update.d.ts.map +1 -1
- package/lib/actions/update.js +6 -6
- package/lib/actions/update.js.map +1 -1
- package/lib/middlewares/associated.d.ts.map +1 -1
- package/lib/middlewares/associated.js +3 -3
- package/lib/middlewares/associated.js.map +1 -1
- package/package.json +5 -5
package/lib/actions/add.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/add.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAcnC,wBAAsB,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,iBA+BjD;AAED,eAAe,GAAG,CAAC","file":"add.d.ts","sourcesContent":["import { Context, Next } from '..';\nimport {\n Model,\n Relation,\n} from '@nocobase/database';\n\n/**\n * 附加关联\n * \n * BlongsToMany\n * \n * @param ctx \n * @param next \n */\nexport async function add(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n } = ctx.action.params as {\n associated: Model,\n associatedName: string,\n resourceField: Relation,\n values: any,\n };\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n throw new Error(`${associatedName} associated model invalid`);\n }\n const { add: addAccessor } = resourceField.getAccessors();\n const {
|
|
1
|
+
{"version":3,"sources":["actions/add.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAcnC,wBAAsB,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,iBA+BjD;AAED,eAAe,GAAG,CAAC","file":"add.d.ts","sourcesContent":["import { Context, Next } from '..';\nimport {\n Model,\n Relation,\n} from '@nocobase/database';\n\n/**\n * 附加关联\n * \n * BlongsToMany\n * \n * @param ctx \n * @param next \n */\nexport async function add(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n } = ctx.action.params as {\n associated: Model,\n associatedName: string,\n resourceField: Relation,\n values: any,\n };\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n throw new Error(`${associatedName} associated model invalid`);\n }\n const { add: addAccessor } = resourceField.getAccessors();\n const { resourceIndex, resourceIndexAttribute, fields = [] } = ctx.action.params;\n const TargetModel = ctx.db.getModel(resourceField.getTarget());\n // const options = TargetModel.parseApiJson({\n // fields,\n // });\n const model = await TargetModel.findOne({\n // ...options,\n where: {\n [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex,\n },\n // @ts-ignore\n context: ctx,\n });\n ctx.body = await associated[addAccessor](model);\n await next();\n}\n\nexport default add;\n"]}
|
package/lib/actions/add.js
CHANGED
|
@@ -65,14 +65,14 @@ function add(ctx, next) {
|
|
|
65
65
|
addAccessor = _resourceField$getAcc.add;
|
|
66
66
|
|
|
67
67
|
const _ctx$action$params2 = ctx.action.params,
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
resourceIndex = _ctx$action$params2.resourceIndex,
|
|
69
|
+
resourceIndexAttribute = _ctx$action$params2.resourceIndexAttribute,
|
|
70
70
|
_ctx$action$params2$f = _ctx$action$params2.fields,
|
|
71
71
|
fields = _ctx$action$params2$f === void 0 ? [] : _ctx$action$params2$f;
|
|
72
72
|
const TargetModel = ctx.db.getModel(resourceField.getTarget());
|
|
73
73
|
const model = yield TargetModel.findOne({
|
|
74
74
|
where: {
|
|
75
|
-
[
|
|
75
|
+
[resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex
|
|
76
76
|
},
|
|
77
77
|
context: ctx
|
|
78
78
|
});
|
package/lib/actions/add.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/add.ts"],"names":[],"mappings":";;;;;;;;;;;;AAcA,SAAsB,GAAG,CAAC,GAAY,EAAE,IAAU;;QAChD,MAAM,EACJ,UAAU,EACV,aAAa,EACb,cAAc,GACf,GAAG,GAAG,CAAC,MAAM,CAAC,MAKd,CAAC;QACF,MAAM,eAAe,GAAG,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;QACxD,IAAI,CAAC,CAAC,UAAU,YAAY,eAAe,CAAC,EAAE;YAC5C,MAAM,IAAI,KAAK,CAAC,GAAG,cAAc,2BAA2B,CAAC,CAAC;SAC/D;QACD,MAAM,EAAE,GAAG,EAAE,WAAW,EAAE,GAAG,aAAa,CAAC,YAAY,EAAE,CAAC;QAC1D,MAAM,EAAE,
|
|
1
|
+
{"version":3,"sources":["actions/add.ts"],"names":[],"mappings":";;;;;;;;;;;;AAcA,SAAsB,GAAG,CAAC,GAAY,EAAE,IAAU;;QAChD,MAAM,EACJ,UAAU,EACV,aAAa,EACb,cAAc,GACf,GAAG,GAAG,CAAC,MAAM,CAAC,MAKd,CAAC;QACF,MAAM,eAAe,GAAG,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;QACxD,IAAI,CAAC,CAAC,UAAU,YAAY,eAAe,CAAC,EAAE;YAC5C,MAAM,IAAI,KAAK,CAAC,GAAG,cAAc,2BAA2B,CAAC,CAAC;SAC/D;QACD,MAAM,EAAE,GAAG,EAAE,WAAW,EAAE,GAAG,aAAa,CAAC,YAAY,EAAE,CAAC;QAC1D,MAAM,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,GAAG,EAAE,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC;QACjF,MAAM,WAAW,GAAG,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,aAAa,CAAC,SAAS,EAAE,CAAC,CAAC;QAI/D,MAAM,KAAK,GAAG,MAAM,WAAW,CAAC,OAAO,CAAC;YAEtC,KAAK,EAAE;gBACL,CAAC,sBAAsB,IAAI,aAAa,CAAC,OAAO,CAAC,SAAS,IAAI,WAAW,CAAC,mBAAmB,CAAC,EAAE,aAAa;aAC9G;YAED,OAAO,EAAE,GAAG;SACb,CAAC,CAAC;QACH,GAAG,CAAC,IAAI,GAAG,MAAM,UAAU,CAAC,WAAW,CAAC,CAAC,KAAK,CAAC,CAAC;QAChD,MAAM,IAAI,EAAE,CAAC;IACf,CAAC;CAAA;AA/BD,kBA+BC;AAED,kBAAe,GAAG,CAAC","file":"add.js","sourcesContent":["import { Context, Next } from '..';\nimport {\n Model,\n Relation,\n} from '@nocobase/database';\n\n/**\n * 附加关联\n * \n * BlongsToMany\n * \n * @param ctx \n * @param next \n */\nexport async function add(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n } = ctx.action.params as {\n associated: Model,\n associatedName: string,\n resourceField: Relation,\n values: any,\n };\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n throw new Error(`${associatedName} associated model invalid`);\n }\n const { add: addAccessor } = resourceField.getAccessors();\n const { resourceIndex, resourceIndexAttribute, fields = [] } = ctx.action.params;\n const TargetModel = ctx.db.getModel(resourceField.getTarget());\n // const options = TargetModel.parseApiJson({\n // fields,\n // });\n const model = await TargetModel.findOne({\n // ...options,\n where: {\n [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex,\n },\n // @ts-ignore\n context: ctx,\n });\n ctx.body = await associated[addAccessor](model);\n await next();\n}\n\nexport default add;\n"]}
|
package/lib/actions/create.js
CHANGED
|
@@ -61,7 +61,7 @@ function create(ctx, next) {
|
|
|
61
61
|
_ctx$action$params$va = _ctx$action$params.values,
|
|
62
62
|
data = _ctx$action$params$va === void 0 ? {} : _ctx$action$params$va,
|
|
63
63
|
fields = _ctx$action$params.fields;
|
|
64
|
-
const values = utils_1.filterByFields(data, fields);
|
|
64
|
+
const values = (0, utils_1.filterByFields)(data, fields);
|
|
65
65
|
const transaction = yield ctx.db.sequelize.transaction();
|
|
66
66
|
const options = {
|
|
67
67
|
transaction,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/create.ts"],"names":[],"mappings":";;;;;;;;;;;;AAGA,oCAA0C;AAgB1C,SAAsB,MAAM,CAAC,GAAY,EAAE,IAAU;;QACnD,MAAM,EACJ,UAAU,EACV,aAAa,EACb,cAAc,EACd,YAAY,EACZ,MAAM,EAAE,IAAI,GAAG,EAAE,EACjB,MAAM,GACP,GAAG,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC;QACtB,MAAM,MAAM,GAAG,sBAAc,
|
|
1
|
+
{"version":3,"sources":["actions/create.ts"],"names":[],"mappings":";;;;;;;;;;;;AAGA,oCAA0C;AAgB1C,SAAsB,MAAM,CAAC,GAAY,EAAE,IAAU;;QACnD,MAAM,EACJ,UAAU,EACV,aAAa,EACb,cAAc,EACd,YAAY,EACZ,MAAM,EAAE,IAAI,GAAG,EAAE,EACjB,MAAM,GACP,GAAG,GAAG,CAAC,MAAM,CAAC,MA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, Next } from '..';\nimport { Model } from '@nocobase/database';\nimport { filterByFields } from '../utils';\n\n/**\n * 新增数据\n *\n * Signle\n * HasMany\n *\n * resource action 层面一般不开放 HasOne、BelongsTo、BelongsToMany 的新增数据操作\n * 如果需要这类操作建议使用 model.updateAssociations 方法\n *\n * TODO 字段验证\n *\n * @param ctx\n * @param next\n */\nexport async function create(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n resourceName,\n values: data = {},\n fields,\n } = ctx.action.params;\n const values = filterByFields(data, fields);\n const transaction = await ctx.db.sequelize.transaction();\n const options = { transaction, context: ctx };\n let model: Model;\n try {\n if (associated && resourceField) {\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n throw new Error(`${associatedName} associated model invalid`);\n }\n const { create } = resourceField.getAccessors();\n model = await associated[create](values, options);\n } else {\n const ResourceModel = ctx.db.getModel(resourceName);\n model = await ResourceModel.create(values, options);\n }\n await model.updateAssociations(values, options);\n await transaction.commit();\n ctx.body = model;\n await next();\n } catch (error) {\n await transaction.rollback();\n throw error;\n }\n}\n\nexport default create;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/destroy.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAcnC,wBAAsB,OAAO,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,iBAqErD;AAED,eAAe,OAAO,CAAC","file":"destroy.d.ts","sourcesContent":["import _ from 'lodash';\nimport { Op } from 'sequelize';\nimport {\n Model,\n HASONE,\n HASMANY,\n BELONGSTO,\n BELONGSTOMANY,\n} from '@nocobase/database';\nimport { Context, Next } from '..';\n\n/**\n * 删除数据,支持批量\n *\n * Single\n * HasOne\n * HasMany\n *\n * TODO 关联数据的删除,建议在 onUpdate/onDelete 层面处理\n *\n * @param ctx\n * @param next\n */\nexport async function destroy(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n resourceName,\n
|
|
1
|
+
{"version":3,"sources":["actions/destroy.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAcnC,wBAAsB,OAAO,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,iBAqErD;AAED,eAAe,OAAO,CAAC","file":"destroy.d.ts","sourcesContent":["import _ from 'lodash';\nimport { Op } from 'sequelize';\nimport {\n Model,\n HASONE,\n HASMANY,\n BELONGSTO,\n BELONGSTOMANY,\n} from '@nocobase/database';\nimport { Context, Next } from '..';\n\n/**\n * 删除数据,支持批量\n *\n * Single\n * HasOne\n * HasMany\n *\n * TODO 关联数据的删除,建议在 onUpdate/onDelete 层面处理\n *\n * @param ctx\n * @param next\n */\nexport async function destroy(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n resourceName,\n resourceIndex,\n resourceIndexAttribute,\n filter,\n } = ctx.action.params;\n const transaction = await ctx.db.sequelize.transaction();\n const commonOptions = { transaction, context: ctx };\n let count;\n if (associated && resourceField) {\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n await transaction.rollback();\n throw new Error(`${associatedName} associated model invalid`);\n }\n const {\n get: getAccessor,\n remove: removeAccessor,\n set: setAccessor,\n } = resourceField.getAccessors();\n const TargetModel = ctx.db.getModel(resourceField.getTarget());\n const { where } = TargetModel.parseApiJson({ filter, context: ctx });\n if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {\n const model: Model = await associated[getAccessor](commonOptions);\n // TODO:不能程序上解除关系,直接通过 onDelete 触发,或者通过 afterDestroy 处理\n // await associated[setAccessor](null, commonOptions);\n // @ts-ignore\n count = await model.destroy(commonOptions);\n } else if (\n resourceField instanceof HASMANY ||\n resourceField instanceof BELONGSTOMANY\n ) {\n const primaryKey =\n resourceIndexAttribute ||\n resourceField.options.targetKey ||\n TargetModel.primaryKeyAttribute;\n const models: Model[] = await associated[getAccessor]({\n where: resourceIndex ? { [primaryKey]: resourceIndex } : where,\n ...commonOptions,\n });\n // TODO:不能程序上解除关系,直接通过 onDelete 触发,或者通过 afterDestroy 处理\n // await associated[removeAccessor](models, commonOptions);\n // @ts-ignore\n count = await TargetModel.destroy({\n where: {\n [primaryKey]: { [Op.in]: models.map((item) => item[primaryKey]) },\n },\n ...commonOptions,\n individualHooks: true,\n });\n }\n } else {\n const Model = ctx.db.getModel(resourceName);\n const { where } = Model.parseApiJson({ filter, context: ctx });\n const primaryKey = resourceIndexAttribute || Model.primaryKeyAttribute;\n count = await Model.destroy({\n where: resourceIndex ? { [primaryKey]: resourceIndex } : where,\n // @ts-ignore hooks 里添加 context\n ...commonOptions,\n individualHooks: true,\n });\n }\n ctx.body = { count };\n await transaction.commit();\n await next();\n}\n\nexport default destroy;\n"]}
|
package/lib/actions/destroy.js
CHANGED
|
@@ -60,8 +60,8 @@ function destroy(ctx, next) {
|
|
|
60
60
|
resourceField = _ctx$action$params.resourceField,
|
|
61
61
|
associatedName = _ctx$action$params.associatedName,
|
|
62
62
|
resourceName = _ctx$action$params.resourceName,
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
resourceIndex = _ctx$action$params.resourceIndex,
|
|
64
|
+
resourceIndexAttribute = _ctx$action$params.resourceIndexAttribute,
|
|
65
65
|
filter = _ctx$action$params.filter;
|
|
66
66
|
const transaction = yield ctx.db.sequelize.transaction();
|
|
67
67
|
const commonOptions = {
|
|
@@ -95,10 +95,10 @@ function destroy(ctx, next) {
|
|
|
95
95
|
const model = yield associated[getAccessor](commonOptions);
|
|
96
96
|
count = yield model.destroy(commonOptions);
|
|
97
97
|
} else if (resourceField instanceof database_1.HASMANY || resourceField instanceof database_1.BELONGSTOMANY) {
|
|
98
|
-
const primaryKey =
|
|
98
|
+
const primaryKey = resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute;
|
|
99
99
|
const models = yield associated[getAccessor](Object.assign({
|
|
100
|
-
where:
|
|
101
|
-
[primaryKey]:
|
|
100
|
+
where: resourceIndex ? {
|
|
101
|
+
[primaryKey]: resourceIndex
|
|
102
102
|
} : where
|
|
103
103
|
}, commonOptions));
|
|
104
104
|
count = yield TargetModel.destroy(Object.assign(Object.assign({
|
|
@@ -120,10 +120,10 @@ function destroy(ctx, next) {
|
|
|
120
120
|
}),
|
|
121
121
|
where = _Model$parseApiJson.where;
|
|
122
122
|
|
|
123
|
-
const primaryKey =
|
|
123
|
+
const primaryKey = resourceIndexAttribute || Model.primaryKeyAttribute;
|
|
124
124
|
count = yield Model.destroy(Object.assign(Object.assign({
|
|
125
|
-
where:
|
|
126
|
-
[primaryKey]:
|
|
125
|
+
where: resourceIndex ? {
|
|
126
|
+
[primaryKey]: resourceIndex
|
|
127
127
|
} : where
|
|
128
128
|
}, commonOptions), {
|
|
129
129
|
individualHooks: true
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/destroy.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,yCAA+B;AAC/B,iDAM4B;AAe5B,SAAsB,OAAO,CAAC,GAAY,EAAE,IAAU;;QACpD,MAAM,EACJ,UAAU,EACV,aAAa,EACb,cAAc,EACd,YAAY,EACZ,
|
|
1
|
+
{"version":3,"sources":["actions/destroy.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,yCAA+B;AAC/B,iDAM4B;AAe5B,SAAsB,OAAO,CAAC,GAAY,EAAE,IAAU;;QACpD,MAAM,EACJ,UAAU,EACV,aAAa,EACb,cAAc,EACd,YAAY,EACZ,aAAa,EACb,sBAAsB,EACtB,MAAM,GACP,GAAG,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC;QACtB,MAAM,WAAW,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,SAAS,CAAC,WAAW,EAAE,CAAC;QACzD,MAAM,aAAa,GAAG,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC;QACpD,IAAI,KAAK,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WAAW,CAAC,OAAO,+BAC/B,KAAK,EAAE;wBACL,CAAC,UAAU,CAAC,EAAE,EAAE,CAAC,cAAE,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,EAAE;qBAClE,IACE,aAAa,KAChB,eAAe,EAAE,IAAI,IACrB,CAAC;aACJ;SACF;aAAM;YACL,MAAM,KAAK,GAAG,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;YAC5C,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC,YAAY,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC;YAC/D,MAAM,UAAU,GAAG,sBAAsB,IAAI,KAAK,CAAC,mBAAmB,CAAC;YACvE,KAAK,GAAG,MAAM,KAAK,CAAC,OAAO,+BACzB,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,EAAE,aAAa,EAAE,CAAC,CAAC,CAAC,KAAK,IAE3D,aAAa,KAChB,eAAe,EAAE,IAAI,IACrB,CAAC;SACJ;QACD,GAAG,CAAC,IAAI,GAAG,EAAE,KAAK,EAAE,CAAC;QACrB,MAAM,WAAW,CAAC,MAAM,EAAE,CAAC;QAC3B,MAAM,IAAI,EAAE,CAAC;IACf,CAAC;CAAA;AArED,0BAqEC;AAED,kBAAe,OAAO,CAAC","file":"destroy.js","sourcesContent":["import _ from 'lodash';\nimport { Op } from 'sequelize';\nimport {\n Model,\n HASONE,\n HASMANY,\n BELONGSTO,\n BELONGSTOMANY,\n} from '@nocobase/database';\nimport { Context, Next } from '..';\n\n/**\n * 删除数据,支持批量\n *\n * Single\n * HasOne\n * HasMany\n *\n * TODO 关联数据的删除,建议在 onUpdate/onDelete 层面处理\n *\n * @param ctx\n * @param next\n */\nexport async function destroy(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n resourceName,\n resourceIndex,\n resourceIndexAttribute,\n filter,\n } = ctx.action.params;\n const transaction = await ctx.db.sequelize.transaction();\n const commonOptions = { transaction, context: ctx };\n let count;\n if (associated && resourceField) {\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n await transaction.rollback();\n throw new Error(`${associatedName} associated model invalid`);\n }\n const {\n get: getAccessor,\n remove: removeAccessor,\n set: setAccessor,\n } = resourceField.getAccessors();\n const TargetModel = ctx.db.getModel(resourceField.getTarget());\n const { where } = TargetModel.parseApiJson({ filter, context: ctx });\n if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {\n const model: Model = await associated[getAccessor](commonOptions);\n // TODO:不能程序上解除关系,直接通过 onDelete 触发,或者通过 afterDestroy 处理\n // await associated[setAccessor](null, commonOptions);\n // @ts-ignore\n count = await model.destroy(commonOptions);\n } else if (\n resourceField instanceof HASMANY ||\n resourceField instanceof BELONGSTOMANY\n ) {\n const primaryKey =\n resourceIndexAttribute ||\n resourceField.options.targetKey ||\n TargetModel.primaryKeyAttribute;\n const models: Model[] = await associated[getAccessor]({\n where: resourceIndex ? { [primaryKey]: resourceIndex } : where,\n ...commonOptions,\n });\n // TODO:不能程序上解除关系,直接通过 onDelete 触发,或者通过 afterDestroy 处理\n // await associated[removeAccessor](models, commonOptions);\n // @ts-ignore\n count = await TargetModel.destroy({\n where: {\n [primaryKey]: { [Op.in]: models.map((item) => item[primaryKey]) },\n },\n ...commonOptions,\n individualHooks: true,\n });\n }\n } else {\n const Model = ctx.db.getModel(resourceName);\n const { where } = Model.parseApiJson({ filter, context: ctx });\n const primaryKey = resourceIndexAttribute || Model.primaryKeyAttribute;\n count = await Model.destroy({\n where: resourceIndex ? { [primaryKey]: resourceIndex } : where,\n // @ts-ignore hooks 里添加 context\n ...commonOptions,\n individualHooks: true,\n });\n }\n ctx.body = { count };\n await transaction.commit();\n await next();\n}\n\nexport default destroy;\n"]}
|
package/lib/actions/get.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/get.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAenC,wBAAsB,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,iBA0DjD;AAED,eAAe,GAAG,CAAC","file":"get.d.ts","sourcesContent":["import _ from 'lodash';\nimport { Context, Next } from '..';\nimport {\n Model,\n HASONE,\n HASMANY,\n BELONGSTO,\n BELONGSTOMANY,\n} from '@nocobase/database';\n\n/**\n * 查询数据详情\n *\n * @param ctx \n * @param next \n */\nexport async function get(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n resourceName,\n
|
|
1
|
+
{"version":3,"sources":["actions/get.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAenC,wBAAsB,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,iBA0DjD;AAED,eAAe,GAAG,CAAC","file":"get.d.ts","sourcesContent":["import _ from 'lodash';\nimport { Context, Next } from '..';\nimport {\n Model,\n HASONE,\n HASMANY,\n BELONGSTO,\n BELONGSTOMANY,\n} from '@nocobase/database';\n\n/**\n * 查询数据详情\n *\n * @param ctx \n * @param next \n */\nexport async function get(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n resourceName,\n resourceIndex,\n resourceIndexAttribute,\n fields = []\n } = ctx.action.params;\n if (associated && resourceField) {\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n throw new Error(`${associatedName} associated model invalid`);\n }\n const getAccessor = resourceField.getAccessors().get;\n const TargetModel = ctx.db.getModel(resourceField.getTarget());\n const options = TargetModel.parseApiJson({\n fields,\n });\n if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {\n let model: Model = await associated[getAccessor]({ context: ctx });\n if (model) {\n model = await TargetModel.findOne({\n ...options,\n context: ctx,\n where: {\n [TargetModel.primaryKeyAttribute]: model[TargetModel.primaryKeyAttribute],\n },\n });\n }\n ctx.body = model;\n } else if (resourceField instanceof HASMANY || resourceField instanceof BELONGSTOMANY) {\n const [model]: Model[] = await associated[getAccessor]({\n ...options,\n where: {\n [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex,\n },\n context: ctx,\n });\n ctx.body = model;\n }\n } else {\n const Model = ctx.db.getModel(resourceName);\n const options = Model.parseApiJson({\n fields,\n });\n const data = await Model.findOne({\n ...options,\n where: {\n [resourceIndexAttribute || Model.primaryKeyAttribute]: resourceIndex,\n },\n // @ts-ignore hooks 里添加 context\n context: ctx,\n });\n ctx.body = data;\n }\n await next();\n}\n\nexport default get;\n"]}
|
package/lib/actions/get.js
CHANGED
|
@@ -70,8 +70,8 @@ function get(ctx, next) {
|
|
|
70
70
|
resourceField = _ctx$action$params.resourceField,
|
|
71
71
|
associatedName = _ctx$action$params.associatedName,
|
|
72
72
|
resourceName = _ctx$action$params.resourceName,
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
resourceIndex = _ctx$action$params.resourceIndex,
|
|
74
|
+
resourceIndexAttribute = _ctx$action$params.resourceIndexAttribute,
|
|
75
75
|
_ctx$action$params$fi = _ctx$action$params.fields,
|
|
76
76
|
fields = _ctx$action$params$fi === void 0 ? [] : _ctx$action$params$fi;
|
|
77
77
|
|
|
@@ -106,7 +106,7 @@ function get(ctx, next) {
|
|
|
106
106
|
} else if (resourceField instanceof database_1.HASMANY || resourceField instanceof database_1.BELONGSTOMANY) {
|
|
107
107
|
const _yield$associated$get = yield associated[getAccessor](Object.assign(Object.assign({}, options), {
|
|
108
108
|
where: {
|
|
109
|
-
[
|
|
109
|
+
[resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex
|
|
110
110
|
},
|
|
111
111
|
context: ctx
|
|
112
112
|
})),
|
|
@@ -122,7 +122,7 @@ function get(ctx, next) {
|
|
|
122
122
|
});
|
|
123
123
|
const data = yield Model.findOne(Object.assign(Object.assign({}, options), {
|
|
124
124
|
where: {
|
|
125
|
-
[
|
|
125
|
+
[resourceIndexAttribute || Model.primaryKeyAttribute]: resourceIndex
|
|
126
126
|
},
|
|
127
127
|
context: ctx
|
|
128
128
|
}));
|
package/lib/actions/get.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/get.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,iDAM4B;AAQ5B,SAAsB,GAAG,CAAC,GAAY,EAAE,IAAU;;QAChD,MAAM,EACJ,UAAU,EACV,aAAa,EACb,cAAc,EACd,YAAY,EACZ,
|
|
1
|
+
{"version":3,"sources":["actions/get.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,iDAM4B;AAQ5B,SAAsB,GAAG,CAAC,GAAY,EAAE,IAAU;;QAChD,MAAM,EACJ,UAAU,EACV,aAAa,EACb,cAAc,EACd,YAAY,EACZ,aAAa,EACb,sBAAsB,EACtB,MAAM,GAAG,EAAE,EACZ,GAAG,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC;QACtB,IAAI,UAAU,IAAI,aAAa,EAAE;YAC/B,MAAM,eAAe,GAAG,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;YACxD,IAAI,CAAC,CAAC,UAAU,YAAY,eAAe,CAAC,EAAE;gBAC5C,MAAM,IAAI,KAAK,CAAC,GAAG,cAAc,2BAA2B,CAAC,CAAC;aAC/D;YACD,MAAM,WAAW,GAAG,aAAa,CAAC,YAAY,EAAE,CAAC,GAAG,CAAC;YACrD,MAAM,WAAW,GAAG,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,aAAa,CAAC,SAAS,EAAE,CAAC,CAAC;YAC/D,MAAM,OAAO,GAAG,WAAW,CAAC,YAAY,CAAC;gBACvC,MAAM;aACP,CAAC,CAAC;YACH,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C;CAAA;AA1DD,kBA0DC;AAED,kBAAe,GAAG,CAAC","file":"get.js","sourcesContent":["import _ from 'lodash';\nimport { Context, Next } from '..';\nimport {\n Model,\n HASONE,\n HASMANY,\n BELONGSTO,\n BELONGSTOMANY,\n} from '@nocobase/database';\n\n/**\n * 查询数据详情\n *\n * @param ctx \n * @param next \n */\nexport async function get(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n resourceName,\n resourceIndex,\n resourceIndexAttribute,\n fields = []\n } = ctx.action.params;\n if (associated && resourceField) {\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n throw new Error(`${associatedName} associated model invalid`);\n }\n const getAccessor = resourceField.getAccessors().get;\n const TargetModel = ctx.db.getModel(resourceField.getTarget());\n const options = TargetModel.parseApiJson({\n fields,\n });\n if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {\n let model: Model = await associated[getAccessor]({ context: ctx });\n if (model) {\n model = await TargetModel.findOne({\n ...options,\n context: ctx,\n where: {\n [TargetModel.primaryKeyAttribute]: model[TargetModel.primaryKeyAttribute],\n },\n });\n }\n ctx.body = model;\n } else if (resourceField instanceof HASMANY || resourceField instanceof BELONGSTOMANY) {\n const [model]: Model[] = await associated[getAccessor]({\n ...options,\n where: {\n [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex,\n },\n context: ctx,\n });\n ctx.body = model;\n }\n } else {\n const Model = ctx.db.getModel(resourceName);\n const options = Model.parseApiJson({\n fields,\n });\n const data = await Model.findOne({\n ...options,\n where: {\n [resourceIndexAttribute || Model.primaryKeyAttribute]: resourceIndex,\n },\n // @ts-ignore hooks 里添加 context\n context: ctx,\n });\n ctx.body = data;\n }\n await next();\n}\n\nexport default get;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/remove.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAmBnC,wBAAsB,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,iBAmCpD;AAED,eAAe,MAAM,CAAC","file":"remove.d.ts","sourcesContent":["import { Context, Next } from '..';\nimport {\n Model,\n Relation,\n HASONE,\n BELONGSTO,\n BELONGSTOMANY,\n HASMANY,\n} from '@nocobase/database';\n\n/**\n * 删除关联\n * \n * BlongsTo\n * BlongsToMany\n * \n * @param ctx \n * @param next \n */\nexport async function remove(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n } = ctx.action.params as {\n associated: Model,\n associatedName: string,\n resourceField: Relation,\n values: any,\n };\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n throw new Error(`${associatedName} associated model invalid`);\n }\n const { get: getAccessor, remove: removeAccessor, set: setAccessor } = resourceField.getAccessors();\n const {
|
|
1
|
+
{"version":3,"sources":["actions/remove.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAmBnC,wBAAsB,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,iBAmCpD;AAED,eAAe,MAAM,CAAC","file":"remove.d.ts","sourcesContent":["import { Context, Next } from '..';\nimport {\n Model,\n Relation,\n HASONE,\n BELONGSTO,\n BELONGSTOMANY,\n HASMANY,\n} from '@nocobase/database';\n\n/**\n * 删除关联\n * \n * BlongsTo\n * BlongsToMany\n * \n * @param ctx \n * @param next \n */\nexport async function remove(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n } = ctx.action.params as {\n associated: Model,\n associatedName: string,\n resourceField: Relation,\n values: any,\n };\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n throw new Error(`${associatedName} associated model invalid`);\n }\n const { get: getAccessor, remove: removeAccessor, set: setAccessor } = resourceField.getAccessors();\n const { resourceIndex, resourceIndexAttribute, fields = [] } = ctx.action.params;\n const TargetModel = ctx.db.getModel(resourceField.getTarget());\n const options = TargetModel.parseApiJson({\n fields,\n });\n if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {\n ctx.body = await associated[setAccessor](null);\n } else if (resourceField instanceof HASMANY || resourceField instanceof BELONGSTOMANY) {\n const [model]: Model[] = await associated[getAccessor]({\n ...options,\n where: {\n [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex,\n },\n context: ctx,\n });\n await associated[removeAccessor](model);\n ctx.body = { id: model.id };\n }\n await next();\n}\n\nexport default remove;\n"]}
|
package/lib/actions/remove.js
CHANGED
|
@@ -81,8 +81,8 @@ function remove(ctx, next) {
|
|
|
81
81
|
setAccessor = _resourceField$getAcc.set;
|
|
82
82
|
|
|
83
83
|
const _ctx$action$params2 = ctx.action.params,
|
|
84
|
-
|
|
85
|
-
|
|
84
|
+
resourceIndex = _ctx$action$params2.resourceIndex,
|
|
85
|
+
resourceIndexAttribute = _ctx$action$params2.resourceIndexAttribute,
|
|
86
86
|
_ctx$action$params2$f = _ctx$action$params2.fields,
|
|
87
87
|
fields = _ctx$action$params2$f === void 0 ? [] : _ctx$action$params2$f;
|
|
88
88
|
const TargetModel = ctx.db.getModel(resourceField.getTarget());
|
|
@@ -95,7 +95,7 @@ function remove(ctx, next) {
|
|
|
95
95
|
} else if (resourceField instanceof database_1.HASMANY || resourceField instanceof database_1.BELONGSTOMANY) {
|
|
96
96
|
const _yield$associated$get = yield associated[getAccessor](Object.assign(Object.assign({}, options), {
|
|
97
97
|
where: {
|
|
98
|
-
[
|
|
98
|
+
[resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex
|
|
99
99
|
},
|
|
100
100
|
context: ctx
|
|
101
101
|
})),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/remove.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,iDAO4B;AAW5B,SA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/D,MAAM,OAAO,GAAG,WAAW,CAAC,YAAY,CAAC;YACvC,MAAM;SACP,CAAC,CAAC;QACH,IAAI,aAAa,YAAY,iBAAM,IAAI,aAAa,YAAY,oBAAS,EAAE;YACzE,GAAG,CAAC,IAAI,GAAG,MAAM,UAAU,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,CAAC;SAChD;aAAM,IAAI,aAAa,YAAY,kBAAO,IAAI,aAAa,YAAY,wBAAa,EAAE;YACrF,MAAM,CAAC,KAAK,CAAC,GAAY,MAAM,UAAU,CAAC,WAAW,CAAC,iCACjD,OAAO,KACV,KAAK,EAAE;oBACL,CAAC,sBAAsB,IAAI,aAAa,CAAC,OAAO,CAAC,SAAS,IAAI,WAAW,CAAC,mBAAmB,CAAC,EAAE,aAAa;iBAC9G,EACD,OAAO,EAAE,GAAG,IACZ,CAAC;YACH,MAAM,UAAU,CAAC,cAAc,CAAC,CAAC,KAAK,CAAC,CAAC;YACxC,GAAG,CAAC,IAAI,GAAG,EAAE,EAAE,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC;SAC7B;QACD,MAAM,IAAI,EAAE,CAAC;IACf,CAAC;CAAA;AAnCD,wBAmCC;AAED,kBAAe,MAAM,CAAC","file":"remove.js","sourcesContent":["import { Context, Next } from '..';\nimport {\n Model,\n Relation,\n HASONE,\n BELONGSTO,\n BELONGSTOMANY,\n HASMANY,\n} from '@nocobase/database';\n\n/**\n * 删除关联\n * \n * BlongsTo\n * BlongsToMany\n * \n * @param ctx \n * @param next \n */\nexport async function remove(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n } = ctx.action.params as {\n associated: Model,\n associatedName: string,\n resourceField: Relation,\n values: any,\n };\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n throw new Error(`${associatedName} associated model invalid`);\n }\n const { get: getAccessor, remove: removeAccessor, set: setAccessor } = resourceField.getAccessors();\n const { resourceIndex, resourceIndexAttribute, fields = [] } = ctx.action.params;\n const TargetModel = ctx.db.getModel(resourceField.getTarget());\n const options = TargetModel.parseApiJson({\n fields,\n });\n if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {\n ctx.body = await associated[setAccessor](null);\n } else if (resourceField instanceof HASMANY || resourceField instanceof BELONGSTOMANY) {\n const [model]: Model[] = await associated[getAccessor]({\n ...options,\n where: {\n [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex,\n },\n context: ctx,\n });\n await associated[removeAccessor](model);\n ctx.body = { id: model.id };\n }\n await next();\n}\n\nexport default remove;\n"]}
|
package/lib/actions/set.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/set.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAmBnC,wBAAsB,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,iBA8BjD;AAED,eAAe,GAAG,CAAC","file":"set.d.ts","sourcesContent":["import { Context, Next } from '..';\nimport {\n Model,\n Relation,\n HASONE,\n BELONGSTO,\n BELONGSTOMANY,\n HASMANY,\n} from '@nocobase/database';\n\n/**\n * 建立关联\n * \n * BlongsTo\n * BlongsToMany\n * \n * @param ctx \n * @param next \n */\nexport async function set(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n } = ctx.action.params as {\n associated: Model,\n associatedName: string,\n resourceField: Relation,\n values: any,\n };\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n throw new Error(`${associatedName} associated model invalid`);\n }\n const { set: setAccessor } = resourceField.getAccessors();\n const {
|
|
1
|
+
{"version":3,"sources":["actions/set.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAmBnC,wBAAsB,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,iBA8BjD;AAED,eAAe,GAAG,CAAC","file":"set.d.ts","sourcesContent":["import { Context, Next } from '..';\nimport {\n Model,\n Relation,\n HASONE,\n BELONGSTO,\n BELONGSTOMANY,\n HASMANY,\n} from '@nocobase/database';\n\n/**\n * 建立关联\n * \n * BlongsTo\n * BlongsToMany\n * \n * @param ctx \n * @param next \n */\nexport async function set(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n } = ctx.action.params as {\n associated: Model,\n associatedName: string,\n resourceField: Relation,\n values: any,\n };\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n throw new Error(`${associatedName} associated model invalid`);\n }\n const { set: setAccessor } = resourceField.getAccessors();\n const { resourceIndex, resourceIndexAttribute, fields = [] } = ctx.action.params;\n const TargetModel = ctx.db.getModel(resourceField.getTarget());\n // const options = TargetModel.parseApiJson({\n // fields,\n // });\n const model = await TargetModel.findOne({\n where: {\n [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex,\n },\n // @ts-ignore\n context: ctx,\n });\n ctx.body = await associated[setAccessor](model);\n await next();\n}\n\nexport default set;\n"]}
|
package/lib/actions/set.js
CHANGED
|
@@ -65,14 +65,14 @@ function set(ctx, next) {
|
|
|
65
65
|
setAccessor = _resourceField$getAcc.set;
|
|
66
66
|
|
|
67
67
|
const _ctx$action$params2 = ctx.action.params,
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
resourceIndex = _ctx$action$params2.resourceIndex,
|
|
69
|
+
resourceIndexAttribute = _ctx$action$params2.resourceIndexAttribute,
|
|
70
70
|
_ctx$action$params2$f = _ctx$action$params2.fields,
|
|
71
71
|
fields = _ctx$action$params2$f === void 0 ? [] : _ctx$action$params2$f;
|
|
72
72
|
const TargetModel = ctx.db.getModel(resourceField.getTarget());
|
|
73
73
|
const model = yield TargetModel.findOne({
|
|
74
74
|
where: {
|
|
75
|
-
[
|
|
75
|
+
[resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex
|
|
76
76
|
},
|
|
77
77
|
context: ctx
|
|
78
78
|
});
|
package/lib/actions/set.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/set.ts"],"names":[],"mappings":";;;;;;;;;;;;AAmBA,SAAsB,GAAG,CAAC,GAAY,EAAE,IAAU;;QAChD,MAAM,EACJ,UAAU,EACV,aAAa,EACb,cAAc,GACf,GAAG,GAAG,CAAC,MAAM,CAAC,MAKd,CAAC;QACF,MAAM,eAAe,GAAG,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;QACxD,IAAI,CAAC,CAAC,UAAU,YAAY,eAAe,CAAC,EAAE;YAC5C,MAAM,IAAI,KAAK,CAAC,GAAG,cAAc,2BAA2B,CAAC,CAAC;SAC/D;QACD,MAAM,EAAE,GAAG,EAAE,WAAW,EAAE,GAAG,aAAa,CAAC,YAAY,EAAE,CAAC;QAC1D,MAAM,EAAE,
|
|
1
|
+
{"version":3,"sources":["actions/set.ts"],"names":[],"mappings":";;;;;;;;;;;;AAmBA,SAAsB,GAAG,CAAC,GAAY,EAAE,IAAU;;QAChD,MAAM,EACJ,UAAU,EACV,aAAa,EACb,cAAc,GACf,GAAG,GAAG,CAAC,MAAM,CAAC,MAKd,CAAC;QACF,MAAM,eAAe,GAAG,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;QACxD,IAAI,CAAC,CAAC,UAAU,YAAY,eAAe,CAAC,EAAE;YAC5C,MAAM,IAAI,KAAK,CAAC,GAAG,cAAc,2BAA2B,CAAC,CAAC;SAC/D;QACD,MAAM,EAAE,GAAG,EAAE,WAAW,EAAE,GAAG,aAAa,CAAC,YAAY,EAAE,CAAC;QAC1D,MAAM,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,GAAG,EAAE,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC;QACjF,MAAM,WAAW,GAAG,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,aAAa,CAAC,SAAS,EAAE,CAAC,CAAC;QAI/D,MAAM,KAAK,GAAG,MAAM,WAAW,CAAC,OAAO,CAAC;YACtC,KAAK,EAAE;gBACL,CAAC,sBAAsB,IAAI,aAAa,CAAC,OAAO,CAAC,SAAS,IAAI,WAAW,CAAC,mBAAmB,CAAC,EAAE,aAAa;aAC9G;YAED,OAAO,EAAE,GAAG;SACb,CAAC,CAAC;QACH,GAAG,CAAC,IAAI,GAAG,MAAM,UAAU,CAAC,WAAW,CAAC,CAAC,KAAK,CAAC,CAAC;QAChD,MAAM,IAAI,EAAE,CAAC;IACf,CAAC;CAAA;AA9BD,kBA8BC;AAED,kBAAe,GAAG,CAAC","file":"set.js","sourcesContent":["import { Context, Next } from '..';\nimport {\n Model,\n Relation,\n HASONE,\n BELONGSTO,\n BELONGSTOMANY,\n HASMANY,\n} from '@nocobase/database';\n\n/**\n * 建立关联\n * \n * BlongsTo\n * BlongsToMany\n * \n * @param ctx \n * @param next \n */\nexport async function set(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n } = ctx.action.params as {\n associated: Model,\n associatedName: string,\n resourceField: Relation,\n values: any,\n };\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n throw new Error(`${associatedName} associated model invalid`);\n }\n const { set: setAccessor } = resourceField.getAccessors();\n const { resourceIndex, resourceIndexAttribute, fields = [] } = ctx.action.params;\n const TargetModel = ctx.db.getModel(resourceField.getTarget());\n // const options = TargetModel.parseApiJson({\n // fields,\n // });\n const model = await TargetModel.findOne({\n where: {\n [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex,\n },\n // @ts-ignore\n context: ctx,\n });\n ctx.body = await associated[setAccessor](model);\n await next();\n}\n\nexport default set;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/sort.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAenC,wBAAsB,IAAI,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,gBAsJlD;AAED,eAAe,IAAI,CAAC","file":"sort.d.ts","sourcesContent":["import { Utils, Op, Sequelize } from 'sequelize';\nimport _ from 'lodash';\nimport { Context, Next } from '..';\nimport {\n HASONE,\n HASMANY,\n BELONGSTO,\n BELONGSTOMANY,\n whereCompare\n} from '@nocobase/database';\n\n/**\n * 人工排序\n * \n * 同 scope 时,往前挪动,插入到目标位置前面;往后挪动,插入到目标位置后面\n * 不同 scope 时,挪动到往目标位置时,默认插入后面位置,可指定 insertBefore\n */\nexport async function sort(ctx: Context, next: Next) {\n const {\n resourceName,\n
|
|
1
|
+
{"version":3,"sources":["actions/sort.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAenC,wBAAsB,IAAI,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,gBAsJlD;AAED,eAAe,IAAI,CAAC","file":"sort.d.ts","sourcesContent":["import { Utils, Op, Sequelize } from 'sequelize';\nimport _ from 'lodash';\nimport { Context, Next } from '..';\nimport {\n HASONE,\n HASMANY,\n BELONGSTO,\n BELONGSTOMANY,\n whereCompare\n} from '@nocobase/database';\n\n/**\n * 人工排序\n * \n * 同 scope 时,往前挪动,插入到目标位置前面;往后挪动,插入到目标位置后面\n * 不同 scope 时,挪动到往目标位置时,默认插入后面位置,可指定 insertBefore\n */\nexport async function sort(ctx: Context, next: Next) {\n const {\n resourceName,\n resourceIndex,\n resourceField,\n associatedName,\n associatedIndex,\n associated,\n values = {},\n ...others\n } = ctx.action.params;\n\n if (associated && resourceField) {\n if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {\n throw new Error(`the association (${resourceName} belongs to ${associatedName}) cannot be sorted`);\n }\n // TODO(feature)\n if (resourceField instanceof BELONGSTOMANY) {\n throw new Error('sorting for belongs to many association has not been implemented');\n }\n }\n\n const Model = ctx.db.getModel(resourceName);\n const table = ctx.db.getTable(resourceName);\n const { primaryKeyAttribute } = Model;\n\n const sourceId = others.sourceId || resourceIndex;\n const field = others.sortField || values?.sortField || values?.field || 'sort';\n const targetId = others.targetId || values?.targetId || values?.target?.[primaryKeyAttribute];\n const method = others.method || values?.method;\n const insertAfter = method === 'insertAfter';\n const sticky = others.sticky || values?.sticky || method === 'prepend';\n const targetScope = others.targetScope || values?.targetScope;\n\n if (!sourceId) {\n throw new Error('source id invalid');\n }\n\n if (!(sticky || targetId || targetScope)) {\n throw new Error('target id/scope invalid');\n }\n\n const sortField = table.getField(field);\n\n if (!sortField) {\n return next();\n }\n\n const { name: sortAttr, scope = [] } = sortField.options;\n\n const transaction = await ctx.db.sequelize.transaction();\n\n const where = {};\n\n if (associated && resourceField instanceof HASMANY) {\n where[resourceField.options.foreignKey] = associatedIndex;\n }\n\n // 找到操作对象\n const source = await Model.findOne({\n where: {\n ...where,\n [primaryKeyAttribute]: sourceId\n },\n transaction\n });\n\n if (!source) {\n await transaction.rollback();\n throw new Error(`resource ${sourceId} does not exist`);\n }\n\n const sourceScopeWhere = source.getValuesByFieldNames(scope);\n\n let targetScopeWhere: any;\n let targetObject;\n\n if (targetId) {\n targetObject = await Model.findByPk(targetId, { transaction });\n if (!targetObject) {\n await transaction.rollback();\n throw new Error(`resource ${targetId} does not exist`);\n }\n targetScopeWhere = targetObject.getValuesByFieldNames(scope);\n } else {\n targetScopeWhere = { ...sourceScopeWhere, ...targetScope };\n }\n\n const sameScope = whereCompare(sourceScopeWhere, targetScopeWhere);\n const updates = { ...targetScopeWhere };\n\n if (targetObject) {\n let increment: number;\n const updateWhere = { ...targetScopeWhere };\n if (sameScope) {\n const direction = source[sortAttr] < targetObject[sortAttr] ? {\n sourceOp: Op.gt,\n targetOp: insertAfter ? Op.lt : Op.lte,\n increment: -1\n } : {\n sourceOp: Op.lt,\n targetOp: insertAfter ? Op.gt : Op.gte,\n increment: 1\n };\n\n increment = direction.increment;\n\n Object.assign(updateWhere, {\n [sortAttr]: {\n [direction.sourceOp]: source[sortAttr],\n [direction.targetOp]: targetObject[sortAttr]\n }\n });\n } else {\n increment = 1;\n Object.assign(updateWhere, {\n [sortAttr]: {\n [insertAfter ? Op.gt : Op.gte]: targetObject[sortAttr]\n }\n });\n }\n\n console.log({ insertAfter, updateWhere })\n\n await Model.increment(sortAttr, {\n by: increment,\n where: updateWhere,\n transaction\n });\n\n Object.assign(updates, {\n [sortAttr]: insertAfter ? targetObject[sortAttr] + 1 : targetObject[sortAttr]\n });\n } else {\n Object.assign(updates, {\n [sortAttr]: await sortField.getNextValue({\n next: sticky ? 'min' : 'max',\n where: targetScopeWhere,\n transaction\n })\n });\n }\n\n await source.update(updates, { transaction });\n\n await transaction.commit();\n\n ctx.body = source;\n\n await next();\n}\n\nexport default sort;\n"]}
|
package/lib/actions/sort.js
CHANGED
|
@@ -70,14 +70,14 @@ function sort(ctx, next) {
|
|
|
70
70
|
return __awaiter(this, void 0, void 0, function* () {
|
|
71
71
|
const _b = ctx.action.params,
|
|
72
72
|
resourceName = _b.resourceName,
|
|
73
|
-
|
|
73
|
+
resourceIndex = _b.resourceIndex,
|
|
74
74
|
resourceField = _b.resourceField,
|
|
75
75
|
associatedName = _b.associatedName,
|
|
76
|
-
|
|
76
|
+
associatedIndex = _b.associatedIndex,
|
|
77
77
|
associated = _b.associated,
|
|
78
78
|
_b$values = _b.values,
|
|
79
79
|
values = _b$values === void 0 ? {} : _b$values,
|
|
80
|
-
others = __rest(_b, ["resourceName", "
|
|
80
|
+
others = __rest(_b, ["resourceName", "resourceIndex", "resourceField", "associatedName", "associatedIndex", "associated", "values"]);
|
|
81
81
|
|
|
82
82
|
if (associated && resourceField) {
|
|
83
83
|
if (resourceField instanceof database_1.HASONE || resourceField instanceof database_1.BELONGSTO) {
|
|
@@ -92,7 +92,7 @@ function sort(ctx, next) {
|
|
|
92
92
|
const Model = ctx.db.getModel(resourceName);
|
|
93
93
|
const table = ctx.db.getTable(resourceName);
|
|
94
94
|
const primaryKeyAttribute = Model.primaryKeyAttribute;
|
|
95
|
-
const sourceId = others.sourceId ||
|
|
95
|
+
const sourceId = others.sourceId || resourceIndex;
|
|
96
96
|
const field = others.sortField || (values === null || values === void 0 ? void 0 : values.sortField) || (values === null || values === void 0 ? void 0 : values.field) || 'sort';
|
|
97
97
|
const targetId = others.targetId || (values === null || values === void 0 ? void 0 : values.targetId) || ((_a = values === null || values === void 0 ? void 0 : values.target) === null || _a === void 0 ? void 0 : _a[primaryKeyAttribute]);
|
|
98
98
|
const method = others.method || (values === null || values === void 0 ? void 0 : values.method);
|
|
@@ -122,7 +122,7 @@ function sort(ctx, next) {
|
|
|
122
122
|
const where = {};
|
|
123
123
|
|
|
124
124
|
if (associated && resourceField instanceof database_1.HASMANY) {
|
|
125
|
-
where[resourceField.options.foreignKey] =
|
|
125
|
+
where[resourceField.options.foreignKey] = associatedIndex;
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
const source = yield Model.findOne({
|
|
@@ -156,7 +156,7 @@ function sort(ctx, next) {
|
|
|
156
156
|
targetScopeWhere = Object.assign(Object.assign({}, sourceScopeWhere), targetScope);
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
-
const sameScope = database_1.whereCompare(sourceScopeWhere, targetScopeWhere);
|
|
159
|
+
const sameScope = (0, database_1.whereCompare)(sourceScopeWhere, targetScopeWhere);
|
|
160
160
|
const updates = Object.assign({}, targetScopeWhere);
|
|
161
161
|
|
|
162
162
|
if (targetObject) {
|
package/lib/actions/sort.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/sort.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA,yCAAiD;AAGjD,iDAM4B;AAQ5B,SAAsB,IAAI,CAAC,GAAY,EAAE,IAAU;;;QACjD,M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AAM,CAAC,OAAO,EAAE;gBACrB,CAAC,QAAQ,CAAC,EAAE,MAAM,SAAS,CAAC,YAAY,CAAC;oBACvC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK;oBAC5B,KAAK,EAAE,gBAAgB;oBACvB,WAAW;iBACZ,CAAC;aACH,CAAC,CAAC;SACJ;QAED,MAAM,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,WAAW,EAAE,CAAC,CAAC;QAE9C,MAAM,WAAW,CAAC,MAAM,EAAE,CAAC;QAE3B,GAAG,CAAC,IAAI,GAAG,MAAM,CAAC;QAElB,MAAM,IAAI,EAAE,CAAC;;CACd;AAtJD,oBAsJC;AAED,kBAAe,IAAI,CAAC","file":"sort.js","sourcesContent":["import { Utils, Op, Sequelize } from 'sequelize';\nimport _ from 'lodash';\nimport { Context, Next } from '..';\nimport {\n HASONE,\n HASMANY,\n BELONGSTO,\n BELONGSTOMANY,\n whereCompare\n} from '@nocobase/database';\n\n/**\n * 人工排序\n * \n * 同 scope 时,往前挪动,插入到目标位置前面;往后挪动,插入到目标位置后面\n * 不同 scope 时,挪动到往目标位置时,默认插入后面位置,可指定 insertBefore\n */\nexport async function sort(ctx: Context, next: Next) {\n const {\n resourceName,\n resourceIndex,\n resourceField,\n associatedName,\n associatedIndex,\n associated,\n values = {},\n ...others\n } = ctx.action.params;\n\n if (associated && resourceField) {\n if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {\n throw new Error(`the association (${resourceName} belongs to ${associatedName}) cannot be sorted`);\n }\n // TODO(feature)\n if (resourceField instanceof BELONGSTOMANY) {\n throw new Error('sorting for belongs to many association has not been implemented');\n }\n }\n\n const Model = ctx.db.getModel(resourceName);\n const table = ctx.db.getTable(resourceName);\n const { primaryKeyAttribute } = Model;\n\n const sourceId = others.sourceId || resourceIndex;\n const field = others.sortField || values?.sortField || values?.field || 'sort';\n const targetId = others.targetId || values?.targetId || values?.target?.[primaryKeyAttribute];\n const method = others.method || values?.method;\n const insertAfter = method === 'insertAfter';\n const sticky = others.sticky || values?.sticky || method === 'prepend';\n const targetScope = others.targetScope || values?.targetScope;\n\n if (!sourceId) {\n throw new Error('source id invalid');\n }\n\n if (!(sticky || targetId || targetScope)) {\n throw new Error('target id/scope invalid');\n }\n\n const sortField = table.getField(field);\n\n if (!sortField) {\n return next();\n }\n\n const { name: sortAttr, scope = [] } = sortField.options;\n\n const transaction = await ctx.db.sequelize.transaction();\n\n const where = {};\n\n if (associated && resourceField instanceof HASMANY) {\n where[resourceField.options.foreignKey] = associatedIndex;\n }\n\n // 找到操作对象\n const source = await Model.findOne({\n where: {\n ...where,\n [primaryKeyAttribute]: sourceId\n },\n transaction\n });\n\n if (!source) {\n await transaction.rollback();\n throw new Error(`resource ${sourceId} does not exist`);\n }\n\n const sourceScopeWhere = source.getValuesByFieldNames(scope);\n\n let targetScopeWhere: any;\n let targetObject;\n\n if (targetId) {\n targetObject = await Model.findByPk(targetId, { transaction });\n if (!targetObject) {\n await transaction.rollback();\n throw new Error(`resource ${targetId} does not exist`);\n }\n targetScopeWhere = targetObject.getValuesByFieldNames(scope);\n } else {\n targetScopeWhere = { ...sourceScopeWhere, ...targetScope };\n }\n\n const sameScope = whereCompare(sourceScopeWhere, targetScopeWhere);\n const updates = { ...targetScopeWhere };\n\n if (targetObject) {\n let increment: number;\n const updateWhere = { ...targetScopeWhere };\n if (sameScope) {\n const direction = source[sortAttr] < targetObject[sortAttr] ? {\n sourceOp: Op.gt,\n targetOp: insertAfter ? Op.lt : Op.lte,\n increment: -1\n } : {\n sourceOp: Op.lt,\n targetOp: insertAfter ? Op.gt : Op.gte,\n increment: 1\n };\n\n increment = direction.increment;\n\n Object.assign(updateWhere, {\n [sortAttr]: {\n [direction.sourceOp]: source[sortAttr],\n [direction.targetOp]: targetObject[sortAttr]\n }\n });\n } else {\n increment = 1;\n Object.assign(updateWhere, {\n [sortAttr]: {\n [insertAfter ? Op.gt : Op.gte]: targetObject[sortAttr]\n }\n });\n }\n\n console.log({ insertAfter, updateWhere })\n\n await Model.increment(sortAttr, {\n by: increment,\n where: updateWhere,\n transaction\n });\n\n Object.assign(updates, {\n [sortAttr]: insertAfter ? targetObject[sortAttr] + 1 : targetObject[sortAttr]\n });\n } else {\n Object.assign(updates, {\n [sortAttr]: await sortField.getNextValue({\n next: sticky ? 'min' : 'max',\n where: targetScopeWhere,\n transaction\n })\n });\n }\n\n await source.update(updates, { transaction });\n\n await transaction.commit();\n\n ctx.body = source;\n\n await next();\n}\n\nexport default sort;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/toggle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAUnC,wBAAsB,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,iBA2DpD;AAED,eAAe,MAAM,CAAC","file":"toggle.d.ts","sourcesContent":["import { Context, Next } from '..';\nimport {\n Model,\n Relation,\n HASONE,\n BELONGSTO,\n BELONGSTOMANY,\n HASMANY,\n} from '@nocobase/database';\n\nexport async function toggle(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n } = ctx.action.params as {\n associated: Model,\n associatedName: string,\n resourceField: Relation,\n values: any,\n };\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n throw new Error(`${associatedName} associated model invalid`);\n }\n const { get: getAccessor, remove: removeAccessor, set: setAccessor, add: addAccessor } = resourceField.getAccessors();\n const {
|
|
1
|
+
{"version":3,"sources":["actions/toggle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAUnC,wBAAsB,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,iBA2DpD;AAED,eAAe,MAAM,CAAC","file":"toggle.d.ts","sourcesContent":["import { Context, Next } from '..';\nimport {\n Model,\n Relation,\n HASONE,\n BELONGSTO,\n BELONGSTOMANY,\n HASMANY,\n} from '@nocobase/database';\n\nexport async function toggle(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n } = ctx.action.params as {\n associated: Model,\n associatedName: string,\n resourceField: Relation,\n values: any,\n };\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n throw new Error(`${associatedName} associated model invalid`);\n }\n const { get: getAccessor, remove: removeAccessor, set: setAccessor, add: addAccessor } = resourceField.getAccessors();\n const { resourceIndex, resourceIndexAttribute, fields = [] } = ctx.action.params;\n const TargetModel = ctx.db.getModel(resourceField.getTarget());\n const options = TargetModel.parseApiJson({\n fields,\n });\n if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {\n const m1 = await associated[getAccessor]();\n if (m1 && m1[resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute] == resourceIndex) {\n ctx.body = await associated[setAccessor](null);\n } else {\n const m2 = await TargetModel.findOne({\n // ...options,\n where: {\n [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex,\n },\n // @ts-ignore\n context: ctx,\n });\n ctx.body = await associated[setAccessor](m2);\n }\n } else if (resourceField instanceof HASMANY || resourceField instanceof BELONGSTOMANY) {\n const [model]: Model[] = await associated[getAccessor]({\n ...options,\n where: {\n [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex,\n },\n context: ctx,\n });\n if (model) {\n ctx.body = await associated[removeAccessor](model);\n } else {\n const m2 = await TargetModel.findOne({\n // ...options,\n where: {\n [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex,\n },\n // @ts-ignore\n context: ctx,\n });\n ctx.body = await associated[addAccessor](m2);\n }\n }\n await next();\n}\n\nexport default toggle;\n"]}
|
package/lib/actions/toggle.js
CHANGED
|
@@ -82,8 +82,8 @@ function toggle(ctx, next) {
|
|
|
82
82
|
addAccessor = _resourceField$getAcc.add;
|
|
83
83
|
|
|
84
84
|
const _ctx$action$params2 = ctx.action.params,
|
|
85
|
-
|
|
86
|
-
|
|
85
|
+
resourceIndex = _ctx$action$params2.resourceIndex,
|
|
86
|
+
resourceIndexAttribute = _ctx$action$params2.resourceIndexAttribute,
|
|
87
87
|
_ctx$action$params2$f = _ctx$action$params2.fields,
|
|
88
88
|
fields = _ctx$action$params2$f === void 0 ? [] : _ctx$action$params2$f;
|
|
89
89
|
const TargetModel = ctx.db.getModel(resourceField.getTarget());
|
|
@@ -94,12 +94,12 @@ function toggle(ctx, next) {
|
|
|
94
94
|
if (resourceField instanceof database_1.HASONE || resourceField instanceof database_1.BELONGSTO) {
|
|
95
95
|
const m1 = yield associated[getAccessor]();
|
|
96
96
|
|
|
97
|
-
if (m1 && m1[
|
|
97
|
+
if (m1 && m1[resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute] == resourceIndex) {
|
|
98
98
|
ctx.body = yield associated[setAccessor](null);
|
|
99
99
|
} else {
|
|
100
100
|
const m2 = yield TargetModel.findOne({
|
|
101
101
|
where: {
|
|
102
|
-
[
|
|
102
|
+
[resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex
|
|
103
103
|
},
|
|
104
104
|
context: ctx
|
|
105
105
|
});
|
|
@@ -108,7 +108,7 @@ function toggle(ctx, next) {
|
|
|
108
108
|
} else if (resourceField instanceof database_1.HASMANY || resourceField instanceof database_1.BELONGSTOMANY) {
|
|
109
109
|
const _yield$associated$get = yield associated[getAccessor](Object.assign(Object.assign({}, options), {
|
|
110
110
|
where: {
|
|
111
|
-
[
|
|
111
|
+
[resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex
|
|
112
112
|
},
|
|
113
113
|
context: ctx
|
|
114
114
|
})),
|
|
@@ -120,7 +120,7 @@ function toggle(ctx, next) {
|
|
|
120
120
|
} else {
|
|
121
121
|
const m2 = yield TargetModel.findOne({
|
|
122
122
|
where: {
|
|
123
|
-
[
|
|
123
|
+
[resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex
|
|
124
124
|
},
|
|
125
125
|
context: ctx
|
|
126
126
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/toggle.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,iDAO4B;AAE5B,SAAsB,MAAM,CAAC,GAAY,EAAE,IAAU;;QACnD,MAAM,EACJ,UAAU,EACV,aAAa,EACb,cAAc,GACf,GAAG,GAAG,CAAC,MAAM,CAAC,MAKd,CAAC;QACF,MAAM,eAAe,GAAG,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;QACxD,IAAI,CAAC,CAAC,UAAU,YAAY,eAAe,CAAC,EAAE;YAC5C,MAAM,IAAI,KAAK,CAAC,GAAG,cAAc,2BAA2B,CAAC,CAAC;SAC/D;QACD,MAAM,EAAE,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,WAAW,EAAE,GAAG,aAAa,CAAC,YAAY,EAAE,CAAC;QACtH,MAAM,EAAE,
|
|
1
|
+
{"version":3,"sources":["actions/toggle.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,iDAO4B;AAE5B,SAAsB,MAAM,CAAC,GAAY,EAAE,IAAU;;QACnD,MAAM,EACJ,UAAU,EACV,aAAa,EACb,cAAc,GACf,GAAG,GAAG,CAAC,MAAM,CAAC,MAKd,CAAC;QACF,MAAM,eAAe,GAAG,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;QACxD,IAAI,CAAC,CAAC,UAAU,YAAY,eAAe,CAAC,EAAE;YAC5C,MAAM,IAAI,KAAK,CAAC,GAAG,cAAc,2BAA2B,CAAC,CAAC;SAC/D;QACD,MAAM,EAAE,G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jD,OAAO,KACV,KAAK,EAAE;oBACL,CAAC,sBAAsB,IAAI,aAAa,CAAC,OAAO,CAAC,SAAS,IAAI,WAAW,CAAC,mBAAmB,CAAC,EAAE,aAAa;iBAC9G,EACD,OAAO,EAAE,GAAG,IACZ,CAAC;YACH,IAAI,KAAK,EAAE;gBACT,GAAG,CAAC,IAAI,GAAG,MAAM,UAAU,CAAC,cAAc,CAAC,CAAC,KAAK,CAAC,CAAC;aACpD;iBAAM;gBACL,MAAM,EAAE,GAAG,MAAM,WAAW,CAAC,OAAO,CAAC;oBAEnC,KAAK,EAAE;wBACL,CAAC,sBAAsB,IAAI,aAAa,CAAC,OAAO,CAAC,SAAS,IAAI,WAAW,CAAC,mBAAmB,CAAC,EAAE,aAAa;qBAC9G;oBAED,OAAO,EAAE,GAAG;iBACb,CAAC,CAAC;gBACH,GAAG,CAAC,IAAI,GAAG,MAAM,UAAU,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC;aAC9C;SACF;QACD,MAAM,IAAI,EAAE,CAAC;IACf,CAAC;CAAA;AA3DD,wBA2DC;AAED,kBAAe,MAAM,CAAC","file":"toggle.js","sourcesContent":["import { Context, Next } from '..';\nimport {\n Model,\n Relation,\n HASONE,\n BELONGSTO,\n BELONGSTOMANY,\n HASMANY,\n} from '@nocobase/database';\n\nexport async function toggle(ctx: Context, next: Next) {\n const {\n associated,\n resourceField,\n associatedName,\n } = ctx.action.params as {\n associated: Model,\n associatedName: string,\n resourceField: Relation,\n values: any,\n };\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n throw new Error(`${associatedName} associated model invalid`);\n }\n const { get: getAccessor, remove: removeAccessor, set: setAccessor, add: addAccessor } = resourceField.getAccessors();\n const { resourceIndex, resourceIndexAttribute, fields = [] } = ctx.action.params;\n const TargetModel = ctx.db.getModel(resourceField.getTarget());\n const options = TargetModel.parseApiJson({\n fields,\n });\n if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {\n const m1 = await associated[getAccessor]();\n if (m1 && m1[resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute] == resourceIndex) {\n ctx.body = await associated[setAccessor](null);\n } else {\n const m2 = await TargetModel.findOne({\n // ...options,\n where: {\n [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex,\n },\n // @ts-ignore\n context: ctx,\n });\n ctx.body = await associated[setAccessor](m2);\n }\n } else if (resourceField instanceof HASMANY || resourceField instanceof BELONGSTOMANY) {\n const [model]: Model[] = await associated[getAccessor]({\n ...options,\n where: {\n [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex,\n },\n context: ctx,\n });\n if (model) {\n ctx.body = await associated[removeAccessor](model);\n } else {\n const m2 = await TargetModel.findOne({\n // ...options,\n where: {\n [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex,\n },\n // @ts-ignore\n context: ctx,\n });\n ctx.body = await associated[addAccessor](m2);\n }\n }\n await next();\n}\n\nexport default toggle;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/update.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAmBnC,wBAAsB,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,iBAiFpD;AAED,eAAe,MAAM,CAAC","file":"update.d.ts","sourcesContent":["import _ from 'lodash';\nimport { Context, Next } from '..';\nimport {\n Model,\n HASONE,\n HASMANY,\n BELONGSTO,\n BELONGSTOMANY,\n whereCompare\n} from '@nocobase/database';\nimport { filterByFields } from '../utils';\n\n/**\n * 更新数据\n *\n * TODO 字段验证\n * \n * @param ctx \n * @param next \n */\nexport async function update(ctx: Context, next: Next) {\n const {\n associated,\n associatedName,\n resourceField,\n resourceName,\n
|
|
1
|
+
{"version":3,"sources":["actions/update.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAmBnC,wBAAsB,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,iBAiFpD;AAED,eAAe,MAAM,CAAC","file":"update.d.ts","sourcesContent":["import _ from 'lodash';\nimport { Context, Next } from '..';\nimport {\n Model,\n HASONE,\n HASMANY,\n BELONGSTO,\n BELONGSTOMANY,\n whereCompare\n} from '@nocobase/database';\nimport { filterByFields } from '../utils';\n\n/**\n * 更新数据\n *\n * TODO 字段验证\n * \n * @param ctx \n * @param next \n */\nexport async function update(ctx: Context, next: Next) {\n const {\n associated,\n associatedName,\n resourceField,\n resourceName,\n resourceIndex,\n // TODO(question): 这个属性从哪设置的?\n resourceIndexAttribute,\n fields,\n values: data\n } = ctx.action.params;\n const values = filterByFields(data, fields);\n const transaction = await ctx.db.sequelize.transaction();\n const options = { transaction, context: ctx };\n if (associated && resourceField) {\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n await transaction.rollback();\n throw new Error(`${associatedName} associated model invalid`);\n }\n const { get: getAccessor } = resourceField.getAccessors();\n if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {\n let model: Model = await associated[getAccessor](options);\n if (model) {\n // @ts-ignore\n await model.update(values, options);\n await model.updateAssociations(values, options);\n ctx.body = model;\n }\n } else if (resourceField instanceof HASMANY || resourceField instanceof BELONGSTOMANY) {\n const TargetModel = ctx.db.getModel(resourceField.getTarget());\n const [model]: Model[] = await associated[getAccessor]({\n ...options,\n where: {\n [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex,\n }\n });\n\n if (resourceField instanceof BELONGSTOMANY) {\n const throughName = resourceField.getThroughName();\n if (typeof values[throughName] === 'object') {\n const ThroughModel = resourceField.getThroughModel();\n const throughValues = values[throughName];\n const { foreignKey, sourceKey, otherKey } = resourceField.options;\n const through = await ThroughModel.findOne({\n where: {\n [foreignKey]: associated[sourceKey],\n [otherKey]: resourceIndex,\n },\n transaction\n });\n // TODO: 中间表的 Model 有问题,关联数据更新有 BUG\n // await through.updateAssociations(throughValues, options);\n await through.update(throughValues, options);\n delete values[throughName];\n }\n }\n if (!_.isEmpty(values)) {\n // @ts-ignore\n await model.update(values, options);\n await model.updateAssociations(values, options);\n }\n ctx.body = model;\n }\n } else {\n const Model = ctx.db.getModel(resourceName);\n const model = await Model.findOne({\n ...options,\n where: {\n [resourceIndexAttribute || Model.primaryKeyAttribute]: resourceIndex,\n }\n });\n // @ts-ignore\n await model.update(values, options);\n // @ts-ignore\n await model.updateAssociations(values, options);\n ctx.body = model;\n }\n await transaction.commit();\n await next();\n}\n\nexport default update;\n"]}
|
package/lib/actions/update.js
CHANGED
|
@@ -80,11 +80,11 @@ function update(ctx, next) {
|
|
|
80
80
|
associatedName = _ctx$action$params.associatedName,
|
|
81
81
|
resourceField = _ctx$action$params.resourceField,
|
|
82
82
|
resourceName = _ctx$action$params.resourceName,
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
resourceIndex = _ctx$action$params.resourceIndex,
|
|
84
|
+
resourceIndexAttribute = _ctx$action$params.resourceIndexAttribute,
|
|
85
85
|
fields = _ctx$action$params.fields,
|
|
86
86
|
data = _ctx$action$params.values;
|
|
87
|
-
const values = utils_1.filterByFields(data, fields);
|
|
87
|
+
const values = (0, utils_1.filterByFields)(data, fields);
|
|
88
88
|
const transaction = yield ctx.db.sequelize.transaction();
|
|
89
89
|
const options = {
|
|
90
90
|
transaction,
|
|
@@ -115,7 +115,7 @@ function update(ctx, next) {
|
|
|
115
115
|
|
|
116
116
|
const _yield$associated$get = yield associated[getAccessor](Object.assign(Object.assign({}, options), {
|
|
117
117
|
where: {
|
|
118
|
-
[
|
|
118
|
+
[resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex
|
|
119
119
|
}
|
|
120
120
|
})),
|
|
121
121
|
_yield$associated$get2 = _slicedToArray(_yield$associated$get, 1),
|
|
@@ -134,7 +134,7 @@ function update(ctx, next) {
|
|
|
134
134
|
const through = yield ThroughModel.findOne({
|
|
135
135
|
where: {
|
|
136
136
|
[foreignKey]: associated[sourceKey],
|
|
137
|
-
[otherKey]:
|
|
137
|
+
[otherKey]: resourceIndex
|
|
138
138
|
},
|
|
139
139
|
transaction
|
|
140
140
|
});
|
|
@@ -154,7 +154,7 @@ function update(ctx, next) {
|
|
|
154
154
|
const Model = ctx.db.getModel(resourceName);
|
|
155
155
|
const model = yield Model.findOne(Object.assign(Object.assign({}, options), {
|
|
156
156
|
where: {
|
|
157
|
-
[
|
|
157
|
+
[resourceIndexAttribute || Model.primaryKeyAttribute]: resourceIndex
|
|
158
158
|
}
|
|
159
159
|
}));
|
|
160
160
|
yield model.update(values, options);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["actions/update.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,oDAAuB;AAEvB,iDAO4B;AAC5B,oCAA0C;AAU1C,SAAsB,MAAM,CAAC,GAAY,EAAE,IAAU;;QACnD,MAAM,EACJ,UAAU,EACV,cAAc,EACd,aAAa,EACb,YAAY,EACZ,
|
|
1
|
+
{"version":3,"sources":["actions/update.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,oDAAuB;AAEvB,iDAO4B;AAC5B,oCAA0C;AAU1C,SAAsB,MAAM,CAAC,GAAY,EAAE,IAAU;;QACnD,MAAM,EACJ,UAAU,EACV,cAAc,EACd,aAAa,EACb,YAAY,EACZ,aAAa,EAEb,sBAAsB,EACtB,MAAM,EACN,MAAM,EAAE,IAAI,EACb,GAAG,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC;QACtB,MAAM,MAAM,GAAG,IAAA,sBAAc,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAC5C,MAAM,WAAW,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,SAAS,CAAC,WAAW,EAAE,CAAC;Q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CAAC,kBAAkB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;YAChD,GAAG,CAAC,IAAI,GAAG,KAAK,CAAC;SAClB;QACD,MAAM,WAAW,CAAC,MAAM,EAAE,CAAC;QAC3B,MAAM,IAAI,EAAE,CAAC;IACf,CAAC;CAAA;AAjFD,wBAiFC;AAED,kBAAe,MAAM,CAAC","file":"update.js","sourcesContent":["import _ from 'lodash';\nimport { Context, Next } from '..';\nimport {\n Model,\n HASONE,\n HASMANY,\n BELONGSTO,\n BELONGSTOMANY,\n whereCompare\n} from '@nocobase/database';\nimport { filterByFields } from '../utils';\n\n/**\n * 更新数据\n *\n * TODO 字段验证\n * \n * @param ctx \n * @param next \n */\nexport async function update(ctx: Context, next: Next) {\n const {\n associated,\n associatedName,\n resourceField,\n resourceName,\n resourceIndex,\n // TODO(question): 这个属性从哪设置的?\n resourceIndexAttribute,\n fields,\n values: data\n } = ctx.action.params;\n const values = filterByFields(data, fields);\n const transaction = await ctx.db.sequelize.transaction();\n const options = { transaction, context: ctx };\n if (associated && resourceField) {\n const AssociatedModel = ctx.db.getModel(associatedName);\n if (!(associated instanceof AssociatedModel)) {\n await transaction.rollback();\n throw new Error(`${associatedName} associated model invalid`);\n }\n const { get: getAccessor } = resourceField.getAccessors();\n if (resourceField instanceof HASONE || resourceField instanceof BELONGSTO) {\n let model: Model = await associated[getAccessor](options);\n if (model) {\n // @ts-ignore\n await model.update(values, options);\n await model.updateAssociations(values, options);\n ctx.body = model;\n }\n } else if (resourceField instanceof HASMANY || resourceField instanceof BELONGSTOMANY) {\n const TargetModel = ctx.db.getModel(resourceField.getTarget());\n const [model]: Model[] = await associated[getAccessor]({\n ...options,\n where: {\n [resourceIndexAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute]: resourceIndex,\n }\n });\n\n if (resourceField instanceof BELONGSTOMANY) {\n const throughName = resourceField.getThroughName();\n if (typeof values[throughName] === 'object') {\n const ThroughModel = resourceField.getThroughModel();\n const throughValues = values[throughName];\n const { foreignKey, sourceKey, otherKey } = resourceField.options;\n const through = await ThroughModel.findOne({\n where: {\n [foreignKey]: associated[sourceKey],\n [otherKey]: resourceIndex,\n },\n transaction\n });\n // TODO: 中间表的 Model 有问题,关联数据更新有 BUG\n // await through.updateAssociations(throughValues, options);\n await through.update(throughValues, options);\n delete values[throughName];\n }\n }\n if (!_.isEmpty(values)) {\n // @ts-ignore\n await model.update(values, options);\n await model.updateAssociations(values, options);\n }\n ctx.body = model;\n }\n } else {\n const Model = ctx.db.getModel(resourceName);\n const model = await Model.findOne({\n ...options,\n where: {\n [resourceIndexAttribute || Model.primaryKeyAttribute]: resourceIndex,\n }\n });\n // @ts-ignore\n await model.update(values, options);\n // @ts-ignore\n await model.updateAssociations(values, options);\n ctx.body = model;\n }\n await transaction.commit();\n await next();\n}\n\nexport default update;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["middlewares/associated.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAInC,wBAAsB,UAAU,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,gBA4CxD;AAED,eAAe,UAAU,CAAC","file":"associated.d.ts","sourcesContent":["import { Context, Next } from '..';\nimport { Action } from '@nocobase/resourcer';\nimport { HASONE, HASMANY, BELONGSTO, BELONGSTOMANY } from '@nocobase/database';\n\nexport async function associated(ctx: Context, next: Next) {\n if (!(ctx.action instanceof Action)) {\n return next();\n }\n\n const { associated, associatedName,
|
|
1
|
+
{"version":3,"sources":["middlewares/associated.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAInC,wBAAsB,UAAU,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,gBA4CxD;AAED,eAAe,UAAU,CAAC","file":"associated.d.ts","sourcesContent":["import { Context, Next } from '..';\nimport { Action } from '@nocobase/resourcer';\nimport { HASONE, HASMANY, BELONGSTO, BELONGSTOMANY } from '@nocobase/database';\n\nexport async function associated(ctx: Context, next: Next) {\n if (!(ctx.action instanceof Action)) {\n return next();\n }\n\n const { associated, associatedName, associatedIndex, resourceName } = ctx.action.params;\n\n if (!associatedName || !associatedIndex) {\n return next();\n }\n\n if (associated) {\n return next();\n }\n\n const Model = ctx.db.getModel(associatedName);\n const field = ctx.db.getTable(associatedName).getField(resourceName);\n\n let key: string;\n\n switch (true) {\n case field instanceof BELONGSTO:\n // 如:fields.collection,对应的 API 为 /fields/119/collection,此时 key 为 PK\n key = Model.primaryKeyAttribute;\n break;\n case field instanceof HASONE:\n case field instanceof HASMANY:\n case field instanceof BELONGSTOMANY:\n key = field.options.sourceKey;\n break;\n }\n\n if (key) {\n const model = await Model.findOne({\n where: {\n [key]: associatedIndex,\n }\n });\n if (model) {\n ctx.action.mergeParams({ associated: model, resourceField: field });\n }\n }\n\n await next();\n}\n\nexport default associated;\n"]}
|
|
@@ -62,10 +62,10 @@ function associated(ctx, next) {
|
|
|
62
62
|
const _ctx$action$params = ctx.action.params,
|
|
63
63
|
associated = _ctx$action$params.associated,
|
|
64
64
|
associatedName = _ctx$action$params.associatedName,
|
|
65
|
-
|
|
65
|
+
associatedIndex = _ctx$action$params.associatedIndex,
|
|
66
66
|
resourceName = _ctx$action$params.resourceName;
|
|
67
67
|
|
|
68
|
-
if (!associatedName || !
|
|
68
|
+
if (!associatedName || !associatedIndex) {
|
|
69
69
|
return next();
|
|
70
70
|
}
|
|
71
71
|
|
|
@@ -92,7 +92,7 @@ function associated(ctx, next) {
|
|
|
92
92
|
if (key) {
|
|
93
93
|
const model = yield Model.findOne({
|
|
94
94
|
where: {
|
|
95
|
-
[key]:
|
|
95
|
+
[key]: associatedIndex
|
|
96
96
|
}
|
|
97
97
|
});
|
|
98
98
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["middlewares/associated.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA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AG,EAAE;YACP,MAAM,KAAK,GAAG,MAAM,KAAK,CAAC,OAAO,CAAC;gBAChC,KAAK,EAAE;oBACL,CAAC,GAAG,CAAC,EAAE,eAAe;iBACvB;aACF,CAAC,CAAC;YACH,IAAI,KAAK,EAAE;gBACT,GAAG,CAAC,MAAM,CAAC,WAAW,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,CAAC,CAAC;aACrE;SACF;QAED,MAAM,IAAI,EAAE,CAAC;IACf,CAAC;CAAA;AA5CD,gCA4CC;AAED,kBAAe,UAAU,CAAC","file":"associated.js","sourcesContent":["import { Context, Next } from '..';\nimport { Action } from '@nocobase/resourcer';\nimport { HASONE, HASMANY, BELONGSTO, BELONGSTOMANY } from '@nocobase/database';\n\nexport async function associated(ctx: Context, next: Next) {\n if (!(ctx.action instanceof Action)) {\n return next();\n }\n\n const { associated, associatedName, associatedIndex, resourceName } = ctx.action.params;\n\n if (!associatedName || !associatedIndex) {\n return next();\n }\n\n if (associated) {\n return next();\n }\n\n const Model = ctx.db.getModel(associatedName);\n const field = ctx.db.getTable(associatedName).getField(resourceName);\n\n let key: string;\n\n switch (true) {\n case field instanceof BELONGSTO:\n // 如:fields.collection,对应的 API 为 /fields/119/collection,此时 key 为 PK\n key = Model.primaryKeyAttribute;\n break;\n case field instanceof HASONE:\n case field instanceof HASMANY:\n case field instanceof BELONGSTOMANY:\n key = field.options.sourceKey;\n break;\n }\n\n if (key) {\n const model = await Model.findOne({\n where: {\n [key]: associatedIndex,\n }\n });\n if (model) {\n ctx.action.mergeParams({ associated: model, resourceField: field });\n }\n }\n\n await next();\n}\n\nexport default associated;\n"]}
|
package/package.json
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/actions",
|
|
3
|
-
"version": "0.5.0-alpha.
|
|
3
|
+
"version": "0.5.0-alpha.35",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "./lib/index.js",
|
|
7
7
|
"types": "./lib/index.d.ts",
|
|
8
8
|
"scripts": {},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@nocobase/database": "^0.5.0-alpha.
|
|
11
|
-
"@nocobase/resourcer": "^0.5.0-alpha.
|
|
10
|
+
"@nocobase/database": "^0.5.0-alpha.35",
|
|
11
|
+
"@nocobase/resourcer": "^0.5.0-alpha.35"
|
|
12
12
|
},
|
|
13
13
|
"devDependencies": {
|
|
14
|
-
"@nocobase/test": "^0.5.0-alpha.
|
|
14
|
+
"@nocobase/test": "^0.5.0-alpha.35"
|
|
15
15
|
},
|
|
16
16
|
"repository": {
|
|
17
17
|
"type": "git",
|
|
18
18
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
19
19
|
"directory": "packages/actions"
|
|
20
20
|
},
|
|
21
|
-
"gitHead": "
|
|
21
|
+
"gitHead": "2b81a6af327ee784a6943aff6e028fde3298dec9"
|
|
22
22
|
}
|