Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web: support schema.rb for rendering ER diagrams #388

Merged
merged 6 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nervous-eyes-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@liam-hq/db-structure": patch
---

:sparkles: prism's wasm URL can now be overridden
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Only db-structure is bumped because it is not used in cli.

2 changes: 2 additions & 0 deletions frontend/apps/erd-web/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

prism.wasm
10 changes: 7 additions & 3 deletions frontend/apps/erd-web/app/erd/p/[...slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { parse } from '@liam-hq/db-structure/parser'
// biome-ignore lint/correctness/noNodejsModules: Required for the server component to read the wasm file
import path from 'node:path'
import { parse, setPrismWasmUrl } from '@liam-hq/db-structure/parser'
import { cookies } from 'next/headers'
import { notFound } from 'next/navigation'
import ERDViewer from './erdViewer'
Expand All @@ -23,8 +25,10 @@ export default async function Page({

const input = await res.text()

// Currently supports Postgres only
const { value: dbStructure, errors } = await parse(input, 'postgres')
setPrismWasmUrl(path.resolve(process.cwd(), 'prism.wasm'))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process.cwd() gives the root path of Next.js.
By following the file from here, the wasm file can be accessed in the local environment as well as on Vercel.

ref: Reading files on Vercel during Next.js ISR | François Best

// Currently supports schema.rb only
const { value: dbStructure, errors } = await parse(input, 'schemarb')
if (errors.length > 0) {
Comment on lines +30 to +31
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Temporarily changed to support only schema.rb.
I plan to create a PR to implement the format identification process after this.

for (const error of errors) {
console.error(error)
Expand Down
3 changes: 3 additions & 0 deletions frontend/apps/erd-web/next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const gitCommitHash = execSync('git rev-parse --short HEAD').toString().trim()
const releaseDate = new Date().toISOString().split('T')[0]

const nextConfig: NextConfig = {
outputFileTracingIncludes: {
'/erd/p/\\[\\.\\.\\.slug\\]': ['./prism.wasm'],
},
Comment on lines +9 to +11
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Define the file to be referenced from the request handler function corresponding to the path on Vercel.

ref: How can I use files in Vercel Functions?

env: {
NEXT_PUBLIC_GIT_HASH: gitCommitHash,
NEXT_PUBLIC_RELEASE_DATE: releaseDate,
Expand Down
1 change: 1 addition & 0 deletions frontend/apps/erd-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"fmt:biome": "biome check --write --unsafe .",
"lint": "pnpm run '/^lint:.*/'",
"lint:biome": "biome check .",
"postinstall": "cp ../../packages/db-structure/src/parser/schemarb/prism.wasm prism.wasm",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it is ran with postinstall.
There is a possible way to include it in the webpack bundle with CopyWebpackPlugin, but the new turbopack for Next.js that we are currently using is not compatible with the webpack plugin, so I did not adopt it.

"start": "next start"
}
}
1 change: 1 addition & 0 deletions frontend/packages/db-structure/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export {
parse,
type SupportedFormat,
supportedFormatSchema,
setPrismWasmUrl,
} from './parser/index.js'
2 changes: 2 additions & 0 deletions frontend/packages/db-structure/src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { processor as schemarbProcessor } from './schemarb/index.js'
import { processor as postgresqlProcessor } from './sql/index.js'
import type { ProcessResult } from './types.js'

export { setPrismWasmUrl } from './schemarb/index.js'

export const supportedFormatSchema = v.union([
v.literal('schemarb'),
v.literal('postgres'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export {
processor,
UnsupportedTokenError,
} from './parser.js'
export { setPrismWasmUrl } from './loadPrism.js'
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ import { WASI } from 'node:wasi'
import type { ParseResult } from '@ruby/prism/src/deserialize.js'
import { parsePrism } from '@ruby/prism/src/parsePrism.js'

let overrideWasmUrl: string | undefined = undefined

export const setPrismWasmUrl = (url: string): void => {
overrideWasmUrl = url
}

export async function loadPrism(): Promise<(source: string) => ParseResult> {
const path = fileURLToPath(new URL('prism.wasm', import.meta.url))
const path =
overrideWasmUrl ?? fileURLToPath(new URL('prism.wasm', import.meta.url))
const wasm = await WebAssembly.compile(await readFile(path))

const wasi = new WASI({ version: 'preview1' })
Expand Down
5 changes: 5 additions & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"cache": false,
"persistent": true
},
"@liam-hq/erd-web#dev": {
"dependsOn": ["^build"],
"cache": false,
"persistent": true
},
"gen": {
"dependsOn": ["^gen"]
},
Expand Down
Loading