Skip to content

Commit

Permalink
Merge pull request #388 from liam-hq/support-schema-rb-for-web
Browse files Browse the repository at this point in the history
web: support schema.rb for rendering ER diagrams
  • Loading branch information
MH4GF authored Dec 26, 2024
2 parents 51d70d5 + fe9aace commit 016ac22
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 4 deletions.
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
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'))

// Currently supports schema.rb only
const { value: dbStructure, errors } = await parse(input, 'schemarb')
if (errors.length > 0) {
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'],
},
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",
"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

0 comments on commit 016ac22

Please sign in to comment.