From ef6e2dd37809f0839fafa7a2ec2b65978dc90788 Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 7 Jan 2022 20:26:23 +0800 Subject: [PATCH] feat(database): magic attribute model --- packages/database/src/index.ts | 1 + .../database/src/magic-attribute-model.ts | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 packages/database/src/magic-attribute-model.ts diff --git a/packages/database/src/index.ts b/packages/database/src/index.ts index d223f0a602..905f5a80e8 100644 --- a/packages/database/src/index.ts +++ b/packages/database/src/index.ts @@ -12,3 +12,4 @@ export * from './relation-repository/multiple-relation-repository'; export { Model, ModelCtor } from 'sequelize'; export * from './fields'; export * from './update-associations'; +export * from './magic-attribute-model'; diff --git a/packages/database/src/magic-attribute-model.ts b/packages/database/src/magic-attribute-model.ts new file mode 100644 index 0000000000..dae165237f --- /dev/null +++ b/packages/database/src/magic-attribute-model.ts @@ -0,0 +1,46 @@ +import { Model } from 'sequelize'; +import { merge } from '@nocobase/utils'; +import _ from 'lodash'; + +export class MagicAttributeModel extends Model { + set(key: any, value?: any, options?: any) { + if (typeof key === 'string') { + const [column] = key.split('.'); + if ((this.constructor as any).hasAlias(column)) { + return super.set(key, value, options); + } + if ((this.constructor as any).rawAttributes[column]) { + return super.set(key, value, options); + } + if (_.isPlainObject(value)) { + const opts = super.get(`options`) || {}; + return super.set(`options.${key}`, merge(opts?.[key], value), options); + } + return super.set(`options.${key}`, value, options); + } else { + Object.keys(key).forEach((k) => { + this.set(k, key[k], options); + }); + } + return super.set(key, value, options); + } + + get(key?: any, value?: any): any { + if (typeof key === 'string') { + const [column] = key.split('.'); + if ((this.constructor as any).hasAlias(column)) { + return super.get(key, value); + } + if ((this.constructor as any).rawAttributes[column]) { + return super.get(key, value); + } + const options = super.get(`options`); + return _.get(options, key); + } + const data = super.get(key, value); + return { + ..._.omit(data, 'options'), + ...data.options, + }; + } +} \ No newline at end of file