Skip to content

Commit

Permalink
feat: sequence ids
Browse files Browse the repository at this point in the history
  • Loading branch information
dineug committed Dec 13, 2023
1 parent ec8ee79 commit fef5e39
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion erd-editor.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"files.autoSave": "onFocusChange",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true
"source.fixAll": "explicit"
},
"editor.tabSize": 2,
"editor.suggestSelection": "first",
Expand Down
2 changes: 2 additions & 0 deletions packages/erd-editor-schema/src/convert/v2ToV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function assignTable(
newTable.name = table.name;
newTable.comment = table.comment;
newTable.columnIds = table.columns.map(({ id }) => id);
newTable.seqColumnIds = [...newTable.columnIds];
newTable.ui.y = table.ui.top;
newTable.ui.x = table.ui.left;
newTable.ui.zIndex = table.ui.zIndex;
Expand Down Expand Up @@ -172,6 +173,7 @@ function assignTable(
const newIndexColumn = createIndexColumn();

newIndex.indexColumnIds.push(id);
newIndex.seqIndexColumnIds.push(id);
newIndexColumn.id = id;
newIndexColumn.indexId = index.id;
newIndexColumn.columnId = indexColumn.id;
Expand Down
2 changes: 2 additions & 0 deletions packages/erd-editor-schema/src/v3/parser/index.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const createIndex = (): Index => ({
name: '',
tableId: '',
indexColumnIds: [],
seqIndexColumnIds: [],
unique: false,
meta: getDefaultEntityMeta(),
});
Expand All @@ -31,6 +32,7 @@ export function createAndMergeIndexEntities(
assignString('tableId');
assignBoolean('unique');
assignArray('indexColumnIds');
assignArray('seqIndexColumnIds');

assignMeta(target.meta, value.meta);

Expand Down
2 changes: 2 additions & 0 deletions packages/erd-editor-schema/src/v3/parser/table.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const createTable = (): Table => ({
name: '',
comment: '',
columnIds: [],
seqColumnIds: [],
ui: {
x: 200,
y: 100,
Expand Down Expand Up @@ -38,6 +39,7 @@ export function createAndMergeTableEntities(
assignString('name');
assignString('comment');
assignArray('columnIds');
assignArray('seqColumnIds');

uiAssignString('color');
uiAssignNumber('x');
Expand Down
1 change: 1 addition & 0 deletions packages/erd-editor-schema/src/v3/schema/index.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export type Index = EntityType<{
name: string;
tableId: string;
indexColumnIds: string[];
seqIndexColumnIds: string[];
unique: boolean;
}>;
1 change: 1 addition & 0 deletions packages/erd-editor-schema/src/v3/schema/table.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type Table = EntityType<{
name: string;
comment: string;
columnIds: string[];
seqColumnIds: string[];
ui: TableUI;
}>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { arrayHas } from '@dineug/shared';

import { ColumnOption, ColumnUIKey } from '@/constants/schema';
import { query } from '@/utils/collection/query';
import { addAndSort } from '@/utils/collection/sequence';
import { createTable } from '@/utils/collection/table.entity';
import { createColumn } from '@/utils/collection/tableColumn.entity';
import { textInRange } from '@/utils/validation';
Expand All @@ -27,7 +28,7 @@ const addColumn: ReducerType<typeof ActionType.addColumn> = (
.addOperator(lww, timestamp, id, () => {
if (!arrayHas(table.columnIds)(id)) {
tableCollection.updateOne(tableId, table => {
table.columnIds.push(id);
addAndSort(table.columnIds, table.seqColumnIds, id);
});
}
});
Expand Down
1 change: 1 addition & 0 deletions packages/erd-editor/src/utils/collection/index.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const createIndex = (value?: DeepPartial<Index>): Index =>
name: '',
tableId: '',
indexColumnIds: [],
seqIndexColumnIds: [],
unique: false,
meta: getDefaultEntityMeta(),
},
Expand Down
13 changes: 13 additions & 0 deletions packages/erd-editor/src/utils/collection/sequence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { arrayHas } from '@dineug/shared';

export function addAndSort(ids: string[], seqIds: string[], id: string) {
const hasSeqId = arrayHas(seqIds);

if (hasSeqId(id)) {
ids.push(id);
ids.sort((a, b) => seqIds.indexOf(a) - seqIds.indexOf(b));
} else {
ids.push(id);
seqIds.push(id);
}
}
1 change: 1 addition & 0 deletions packages/erd-editor/src/utils/collection/table.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const createTable = (value?: DeepPartial<Table>): Table =>
name: '',
comment: '',
columnIds: [],
seqColumnIds: [],
ui: {
x: 200,
y: 100,
Expand Down

0 comments on commit fef5e39

Please sign in to comment.