Skip to content

Commit

Permalink
refactor: non-id primary key update
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwii committed Nov 8, 2020
1 parent 8036a66 commit 1ab6753
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/adapters/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,16 @@ export class ModelAdapterImpl implements ModelAdapter {
const primaryKey = AppContext.adapters.models.getPrimaryKey(modelName);
logger.debug('[upsert]', 'fields is', fields);

const fixKeys = _.mapKeys(data?.body, (value, key) => fields?.[key]?.ref || key);
const id: any = _.get(data?.body, 'id') ?? _.get(data?.body, primaryKey); // data?.body?.[primaryKey] as any;
const removedPrimaryKey = _.omit(data?.body, 'id') as any; // data?.body?.[primaryKey] as any;
const fixKeys = _.mapKeys(removedPrimaryKey, (value, key) => fields?.[key]?.ref || key);
const transformed = _.mapValues(fixKeys, (value, key) => {
// json 用于描述该字段需要通过字符串转换处理,目前用于服务器端不支持 JSON 数据格式的情况
return _.eq(fields?.[key]?.options?.json, 'str') ? JSON.stringify(value) : value;
});
const convertNullTransformed = _.mapValues(transformed, (v) => (_.isNil(v) ? null : v));
logger.debug('[upsert]', 'transformed is', convertNullTransformed);

const id = data?.body?.[primaryKey] as any;
if (id) {
return this.service.update(auth, modelName, {
id,
Expand Down
6 changes: 4 additions & 2 deletions src/helpers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -772,14 +772,16 @@ export const columnHelper = {
key: castModelKey(key),
...(await generateSearchColumnProps(castModelKey(key), 'boolean')),
render: (isActive, record) => {
const primaryKey = AppContext.adapters.models.getPrimaryKey(extras.modelName);
const id = _.get(record, primaryKey);
const component = extras.readonly ? (
<Checkbox checked={isActive} disabled={true} />
) : (
<Popconfirm
title={isActive ? `是否注销: ${record.id}` : `是否激活: ${record.id}`}
title={isActive ? `是否注销: ${id}` : `是否激活: ${id}`}
onConfirm={async () => {
// const { modelProxy } = require('../adapters');
await AppContext.adapters.models.upsert(extras.modelName, { body: { id: record.id, [key]: !isActive } });
await AppContext.adapters.models.upsert(extras.modelName, { body: { id, [key]: !isActive } });
extras.callRefresh();
}}
>
Expand Down

0 comments on commit 1ab6753

Please sign in to comment.