Skip to content

Commit 1df8e4d

Browse files
committed
Allow connecting serial and int field (#169)
1 parent d9654f3 commit 1df8e4d

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/components/EditorCanvas/Canvas.jsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from "../../hooks";
2424
import { useTranslation } from "react-i18next";
2525
import { useEventListener } from "usehooks-ts";
26+
import { areFieldsCompatible } from "../../utils/utils";
2627

2728
export default function Canvas() {
2829
const { t } = useTranslation();
@@ -34,7 +35,8 @@ export default function Canvas() {
3435
pointer,
3536
} = canvasContextValue;
3637

37-
const { tables, updateTable, relationships, addRelationship } = useDiagram();
38+
const { tables, updateTable, relationships, addRelationship, database } =
39+
useDiagram();
3840
const { areas, updateArea } = useAreas();
3941
const { notes, updateNote } = useNotes();
4042
const { layout } = useLayout();
@@ -399,8 +401,11 @@ export default function Canvas() {
399401
if (hoveredTable.tableId < 0) return;
400402
if (hoveredTable.field < 0) return;
401403
if (
402-
tables[linkingLine.startTableId].fields[linkingLine.startFieldId].type !==
403-
tables[hoveredTable.tableId].fields[hoveredTable.field].type
404+
!areFieldsCompatible(
405+
database,
406+
tables[linkingLine.startTableId].fields[linkingLine.startFieldId],
407+
tables[hoveredTable.tableId].fields[hoveredTable.field],
408+
)
404409
) {
405410
Toast.info(t("cannot_connect"));
406411
return;

src/data/datatypes.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ const postgresTypesBase = {
698698
isSized: false,
699699
hasPrecision: false,
700700
canIncrement: true,
701+
compatibleWith: ["SMALLSERIAL", "SERIAL", "BIGSERIAL", "INTEGER", "BIGINT"],
701702
},
702703
INTEGER: {
703704
type: "INTEGER",
@@ -708,6 +709,13 @@ const postgresTypesBase = {
708709
isSized: false,
709710
hasPrecision: false,
710711
canIncrement: true,
712+
compatibleWith: [
713+
"SMALLSERIAL",
714+
"SERIAL",
715+
"BIGSERIAL",
716+
"SMALLINT",
717+
"BIGINT",
718+
],
711719
},
712720
BIGINT: {
713721
type: "BIGINT",
@@ -718,6 +726,13 @@ const postgresTypesBase = {
718726
isSized: false,
719727
hasPrecision: false,
720728
canIncrement: true,
729+
compatibleWith: [
730+
"SMALLSERIAL",
731+
"SERIAL",
732+
"BIGSERIAL",
733+
"INTEGER",
734+
"SMALLINT",
735+
],
721736
},
722737
DECIMAL: {
723738
type: "DECIMAL",
@@ -763,6 +778,7 @@ const postgresTypesBase = {
763778
hasCheck: true,
764779
isSized: false,
765780
hasPrecision: false,
781+
compatibleWith: ["INTEGER", "SERIAL", "BIGSERIAL", "SMALLINT", "BIGINT"],
766782
},
767783
SERIAL: {
768784
type: "SERIAL",
@@ -772,6 +788,13 @@ const postgresTypesBase = {
772788
hasCheck: true,
773789
isSized: false,
774790
hasPrecision: false,
791+
compatibleWith: [
792+
"INTEGER",
793+
"SMALLSERIAL",
794+
"BIGSERIAL",
795+
"SMALLINT",
796+
"BIGINT",
797+
],
775798
},
776799
BIGSERIAL: {
777800
type: "BIGSERIAL",
@@ -781,6 +804,7 @@ const postgresTypesBase = {
781804
hasCheck: true,
782805
isSized: false,
783806
hasPrecision: false,
807+
compatibleWith: ["INTEGER", "SERIAL", "SMALLSERIAL", "SMALLINT", "BIGINT"],
784808
},
785809
MONEY: {
786810
type: "MONEY",

src/utils/utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { dbToTypes } from "../data/datatypes";
2+
13
export function dataURItoBlob(dataUrl) {
24
const byteString = atob(dataUrl.split(",")[1]);
35
const mimeString = dataUrl.split(",")[0].split(":")[1].split(";")[0];
@@ -34,3 +36,13 @@ export function isKeyword(str) {
3436
export function isFunction(str) {
3537
return /\w+\([^)]*\)$/.test(str);
3638
}
39+
40+
export function areFieldsCompatible(db, field1, field2) {
41+
const same = field1.type === field2.type;
42+
if (dbToTypes[db][field1.type].compatibleWith) {
43+
return (
44+
dbToTypes[db][field1.type].compatibleWith.includes(field2.type) || same
45+
);
46+
}
47+
return same;
48+
}

0 commit comments

Comments
 (0)