Skip to content

Commit

Permalink
fix: changeColumnForeignKeyAction
Browse files Browse the repository at this point in the history
  • Loading branch information
dineug committed Nov 18, 2023
1 parent 8941b3f commit ad061ef
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
changeColumnCommentAction,
changeColumnDataTypeAction,
changeColumnDefaultAction,
changeColumnForeignKeyAction,
changeColumnNameAction,
changeColumnNotNullAction,
} from '@/engine/modules/tableColumn/atom.actions';
Expand Down Expand Up @@ -151,10 +150,6 @@ export const selectTableAction$ = (
...payload,
value: true,
}),
changeColumnForeignKeyAction({
...payload,
value: true,
}),
changeColumnNameAction({
...payload,
value: startColumn.name,
Expand Down
2 changes: 0 additions & 2 deletions packages/erd-editor/src/engine/modules/tableColumn/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const ActionType = {
changeColumnPrimaryKey: 'column.changePrimaryKey',
changeColumnUnique: 'column.changeUnique',
changeColumnNotNull: 'column.changeNotNull',
changeColumnForeignKey: 'column.changeForeignKey',
} as const;
export type ActionType = ValuesType<typeof ActionType>;

Expand All @@ -36,7 +35,6 @@ export type ActionMap = {
[ActionType.changeColumnPrimaryKey]: ChangeColumnOptionPayload;
[ActionType.changeColumnUnique]: ChangeColumnOptionPayload;
[ActionType.changeColumnNotNull]: ChangeColumnOptionPayload;
[ActionType.changeColumnForeignKey]: ChangeColumnOptionPayload;
};

export type ReducerType<T extends keyof ActionMap> = Reducer<
Expand Down
40 changes: 3 additions & 37 deletions packages/erd-editor/src/engine/modules/tableColumn/atom.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createAction } from '@dineug/r-html';
import { arrayHas } from '@dineug/shared';

import { ColumnOption, ColumnUIKey } from '@/constants/schema';
import { bHas } from '@/utils/bit';
import { query } from '@/utils/collection/query';
import { createTable } from '@/utils/collection/table.entity';
import { createColumn } from '@/utils/collection/tableColumn.entity';
Expand Down Expand Up @@ -183,23 +182,9 @@ const changeColumnPrimaryKey: ReducerType<
column.options = value
? column.options | ColumnOption.primaryKey
: column.options & ~ColumnOption.primaryKey;

if (value) {
if (bHas(column.ui.keys, ColumnUIKey.foreignKey)) {
column.ui.keys = ColumnUIKey.primaryKey | ColumnUIKey.foreignKey;
} else {
column.ui.keys = ColumnUIKey.primaryKey;
}
} else {
if (
bHas(column.ui.keys, ColumnUIKey.primaryKey) &&
bHas(column.ui.keys, ColumnUIKey.foreignKey)
) {
column.ui.keys = ColumnUIKey.foreignKey;
} else {
column.ui.keys = 0;
}
}
column.ui.keys = value
? column.ui.keys | ColumnUIKey.primaryKey
: column.ui.keys & ~ColumnUIKey.primaryKey;
});
});
};
Expand Down Expand Up @@ -243,23 +228,6 @@ const changeColumnNotNull: ReducerType<
});
};

export const changeColumnForeignKeyAction = createAction<
ActionMap[typeof ActionType.changeColumnForeignKey]
>(ActionType.changeColumnForeignKey);

const changeColumnForeignKey: ReducerType<
typeof ActionType.changeColumnForeignKey
> = ({ collections }, { payload: { id, value } }) => {
const collection = query(collections).collection('tableColumnEntities');
collection.getOrCreate(id, id => createColumn({ id }));

collection.updateOne(id, column => {
column.ui.keys = value
? column.ui.keys | ColumnUIKey.foreignKey
: column.ui.keys & ~ColumnUIKey.foreignKey;
});
};

export const tableColumnReducers = {
[ActionType.addColumn]: addColumn,
[ActionType.removeColumn]: removeColumn,
Expand All @@ -271,7 +239,6 @@ export const tableColumnReducers = {
[ActionType.changeColumnPrimaryKey]: changeColumnPrimaryKey,
[ActionType.changeColumnUnique]: changeColumnUnique,
[ActionType.changeColumnNotNull]: changeColumnNotNull,
[ActionType.changeColumnForeignKey]: changeColumnForeignKey,
};

export const actions = {
Expand All @@ -285,5 +252,4 @@ export const actions = {
changeColumnPrimaryKeyAction,
changeColumnUniqueAction,
changeColumnNotNullAction,
changeColumnForeignKeyAction,
};
41 changes: 30 additions & 11 deletions packages/erd-editor/src/engine/modules/tableColumn/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { takeEvery } from '@dineug/go';

import { ColumnOption, ColumnUIKey } from '@/constants/schema';
import type { CO, Hook } from '@/engine/hooks';
import { removeRelationshipAction } from '@/engine/modules/relationship/atom.actions';
import {
addRelationshipAction,
removeRelationshipAction,
} from '@/engine/modules/relationship/atom.actions';
import { changeColumnPrimaryKeyAction } from '@/engine/modules/tableColumn/atom.actions';
import { bHas } from '@/utils/bit';
import { query } from '@/utils/collection/query';
Expand Down Expand Up @@ -30,7 +33,29 @@ const changeColumnNotNullHook: CO = function* (channel, { collections }) {
);
};

const changeColumnForeignKeyHook: CO = function* (
const addColumnForeignKeyHook: CO = function* (
channel,
{ doc: { relationshipIds }, collections }
) {
yield takeEvery(
channel,
function* ({
payload: { id, end },
}: ReturnType<typeof addRelationshipAction>) {
if (!relationshipIds.includes(id)) return;

const columns = query(collections)
.collection('tableColumnEntities')
.selectByIds(end.columnIds);

for (const { ui } of columns) {
ui.keys = ui.keys | ColumnUIKey.foreignKey;
}
}
);
};

const removeColumnForeignKeyHook: CO = function* (
channel,
{ doc: { relationshipIds }, collections }
) {
Expand All @@ -52,20 +77,14 @@ const changeColumnForeignKeyHook: CO = function* (
.selectByIds(end.columnIds);

for (const { ui } of columns) {
if (
bHas(ui.keys, ColumnUIKey.primaryKey) &&
bHas(ui.keys, ColumnUIKey.foreignKey)
) {
ui.keys = ColumnUIKey.primaryKey;
} else if (bHas(ui.keys, ColumnUIKey.foreignKey)) {
ui.keys = 0;
}
ui.keys = ui.keys & ~ColumnUIKey.foreignKey;
}
}
);
};

export const hooks: Hook[] = [
[[changeColumnPrimaryKeyAction], changeColumnNotNullHook],
[[removeRelationshipAction], changeColumnForeignKeyHook],
[[addRelationshipAction], addColumnForeignKeyHook],
[[removeRelationshipAction], removeColumnForeignKeyHook],
];

0 comments on commit ad061ef

Please sign in to comment.