Skip to content

Commit

Permalink
Primary key icon in ERD
Browse files Browse the repository at this point in the history
- Updated the parser to mark `id` fields as primary keys (`PK: true`) when no explicit primary key is defined in `schema.rb`.
- Updated the `convertToDBStructure` function to correctly handle primary key attributes.
- Adjusted test snapshots and test cases to reflect the inclusion of primary key information.
- Added a new `PrimaryKeyIcon` component to visually indicate primary keys in the ERD renderer.
- Integrated the `PrimaryKeyIcon` into the `TableNode` component in the ERD renderer.
- Included `PrimaryKeyIcon` in the icon library and Storybook for documentation and testing.
  • Loading branch information
hoshinotsuyoshi committed Nov 29, 2024
1 parent 134b5d7 commit 6eda17d
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ exports[`parse > should parse schema.rb to JSON correctly 1`] = `
"increment": false,
"name": "id",
"notNull": false,
"primary": false,
"primary": true,
"type": "varchar",
"unique": false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const convertToDBStructure = (data: any): DBStructure => {
increment: false,
name: field.name,
notNull: 'nullable' in field ? !field.nullable : false,
primary: false,
primary: field.PK || false,
type: field.type.type_name,
unique: false,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe(processor, () => {
users: aTable({
name: 'users',
columns: {
id: aColumn(),
id: aColumn({ primary: true }),
...override?.columns,
},
}),
Expand All @@ -27,7 +27,7 @@ describe(processor, () => {

const expected = userTable({
columns: {
id: aColumn(),
id: aColumn({ primary: true }),
name: aColumn({
name: 'name',
type: 'string',
Expand All @@ -48,7 +48,7 @@ describe(processor, () => {

const expected = userTable({
columns: {
id: aColumn(),
id: aColumn({ primary: true }),
name: aColumn({
name: 'name',
type: 'string',
Expand All @@ -69,7 +69,7 @@ describe(processor, () => {

const expected = userTable({
columns: {
id: aColumn(),
id: aColumn({ primary: true }),
name: aColumn({
name: 'name',
type: 'string',
Expand Down
3 changes: 2 additions & 1 deletion frontend/packages/db-structure/src/parser/schemarb/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2318,7 +2318,8 @@ function peg$parse(input, options) {
if (!idField) {
table.fields.unshift({
name: "id",
type: { type_name: "varchar" }
type: { type_name: "varchar" },
PK: true,
});
}
data.tables.push(table);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
if (!idField) {
table.fields.unshift({
name: "id",
type: { type_name: "varchar" }
type: { type_name: "varchar" },
PK: true,
});
}
data.tables.push(table);
Expand Down
1 change: 1 addition & 0 deletions frontend/packages/erd-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@biomejs/biome": "1.9.3",
"@liam/configs": "workspace:*",
"@liam/db-structure": "workspace:*",
"@liam/ui": "workspace:*",
"@types/react": "18",
"typed-css-modules": "0.9.1",
"typescript": "5"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Table } from '@liam/db-structure'
import { PrimaryKeyIcon } from '@liam/ui'
import type { Node, NodeProps } from '@xyflow/react'
import type { FC } from 'react'
import styles from './TableNode.module.css'
Expand All @@ -18,6 +19,10 @@ export const TableNode: FC<Props> = ({ data: { table } }) => {
<ul>
{Object.values(table.columns).map((column) => (
<li key={column.name}>
<span>
{column.primary && <PrimaryKeyIcon width={16} height={16} />}
</span>
<span> </span>
<span>{column.name}</span>
<span> </span>
<span>{column.type}</span>
Expand Down
29 changes: 29 additions & 0 deletions frontend/packages/ui/src/icons/PrimaryKeyIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { ComponentPropsWithoutRef, FC } from 'react'

type Props = ComponentPropsWithoutRef<'svg'>

/**
* This icon is sourced from Lucide (https://lucide.dev).
* Copyright (c) Lucide contributors. Licensed under the ISC License.
*/
export const PrimaryKeyIcon: FC<Props> = (props) => {
return (
<svg
role="img"
aria-label="Primary Key"
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
{...props}
>
<path d="M2.586 17.414A2 2 0 0 0 2 18.828V21a1 1 0 0 0 1 1h3a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1h1a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1h.172a2 2 0 0 0 1.414-.586l.814-.814a6.5 6.5 0 1 0-4-4z" />
<circle cx="16.5" cy="7.5" r=".5" fill="currentColor" />
</svg>
)
}
3 changes: 2 additions & 1 deletion frontend/packages/ui/src/icons/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import type { FC } from 'react'
import { FacebookIcon } from './FacebookIcon'
import { InfoIcon } from './InfoIcon'
import { LinkedInIcon } from './LinkedInIcon'
import { PrimaryKeyIcon } from './PrimaryKeyIcon'
import { XIcon } from './XIcon'

const icons = [InfoIcon, XIcon, FacebookIcon, LinkedInIcon]
const icons = [InfoIcon, XIcon, FacebookIcon, LinkedInIcon, PrimaryKeyIcon]

const IconList: FC = () => {
return (
Expand Down
1 change: 1 addition & 0 deletions frontend/packages/ui/src/icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './InfoIcon'
export * from './XIcon'
export * from './FacebookIcon'
export * from './LinkedInIcon'
export * from './PrimaryKeyIcon'
3 changes: 3 additions & 0 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6eda17d

Please sign in to comment.