Skip to content

Commit

Permalink
feat: changeColumnForeignKeyHook
Browse files Browse the repository at this point in the history
  • Loading branch information
dineug committed Nov 18, 2023
1 parent d5bea68 commit 8941b3f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AppContext } from '@/components/appContext';
import { Show } from '@/constants/schema';
import { changeShowAction } from '@/engine/modules/settings/atom.actions';
import { bHas } from '@/utils/bit';

type Menu = {
name: string;
Expand Down Expand Up @@ -46,7 +47,7 @@ export function createShowMenus({ store }: AppContext) {
const { settings } = store.state;

return menus.map(menu => {
const checked = menu.show & settings.show;
const checked = bHas(settings.show, menu.show);

return {
checked,
Expand Down
9 changes: 5 additions & 4 deletions packages/erd-editor/src/engine/modules/relationship/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
changeColumnPrimaryKeyAction,
removeColumnAction,
} from '@/engine/modules/tableColumn/atom.actions';
import { bHas } from '@/utils/bit';
import { query } from '@/utils/collection/query';
import { relationshipSort } from '@/utils/draw-relationship/sort';

Expand All @@ -44,8 +45,8 @@ const identificationHook: CO = function* (channel, { doc, collections }) {
.filter(column => has(column.id));
if (!columns.length) continue;

const value = columns.every(
column => column.options & ColumnOption.primaryKey
const value = columns.every(column =>
bHas(column.options, ColumnOption.primaryKey)
);

if (value === identification) {
Expand Down Expand Up @@ -82,8 +83,8 @@ const startRelationshipHook: CO = function* (channel, { doc, collections }) {
.filter(column => has(column.id));
if (!columns.length) continue;

const value = columns.every(
column => column.options & ColumnOption.notNull
const value = columns.every(column =>
bHas(column.options, ColumnOption.notNull)
)
? StartRelationshipType.dash
: StartRelationshipType.ring;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 @@ -184,15 +185,15 @@ const changeColumnPrimaryKey: ReducerType<
: column.options & ~ColumnOption.primaryKey;

if (value) {
if (column.ui.keys & ColumnUIKey.foreignKey) {
if (bHas(column.ui.keys, ColumnUIKey.foreignKey)) {
column.ui.keys = ColumnUIKey.primaryKey | ColumnUIKey.foreignKey;
} else {
column.ui.keys = ColumnUIKey.primaryKey;
}
} else {
if (
column.ui.keys & ColumnUIKey.primaryKey &&
column.ui.keys & ColumnUIKey.foreignKey
bHas(column.ui.keys, ColumnUIKey.primaryKey) &&
bHas(column.ui.keys, ColumnUIKey.foreignKey)
) {
column.ui.keys = ColumnUIKey.foreignKey;
} else {
Expand Down
70 changes: 56 additions & 14 deletions packages/erd-editor/src/engine/modules/tableColumn/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,71 @@
import { takeEvery } from '@dineug/go';

import { ColumnOption } from '@/constants/schema';
import { ColumnOption, ColumnUIKey } from '@/constants/schema';
import type { CO, Hook } from '@/engine/hooks';
import { 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';

const changeColumnNotNullHook: CO = function* (channel, { doc, collections }) {
yield takeEvery(channel, function* ({ payload: { id } }) {
const collection = query(collections).collection('tableColumnEntities');
const column = collection.selectById(id);
if (!column) return;
const changeColumnNotNullHook: CO = function* (channel, { collections }) {
yield takeEvery(
channel,
function* ({
payload: { id },
}: ReturnType<typeof changeColumnPrimaryKeyAction>) {
const collection = query(collections).collection('tableColumnEntities');
const column = collection.selectById(id);
if (!column) return;

const isPrimaryKey = bHas(column.options, ColumnOption.primaryKey);
if (!isPrimaryKey) return;
const isPrimaryKey = bHas(column.options, ColumnOption.primaryKey);
if (!isPrimaryKey) return;

const isNotNull = bHas(column.options, ColumnOption.notNull);
if (isNotNull) return;
const isNotNull = bHas(column.options, ColumnOption.notNull);
if (isNotNull) return;

collection.updateOne(id, column => {
column.options = column.options | ColumnOption.notNull;
});
});
collection.updateOne(id, column => {
column.options = column.options | ColumnOption.notNull;
});
}
);
};

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

const relationship = query(collections)
.collection('relationshipEntities')
.selectById(id);
if (!relationship) return;

const { end } = relationship;
const columns = query(collections)
.collection('tableColumnEntities')
.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;
}
}
}
);
};

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

0 comments on commit 8941b3f

Please sign in to comment.