-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from liam-hq/add_parser_for_schemarb
feat: Add parser for schemarb
- Loading branch information
Showing
11 changed files
with
2,881 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
{ | ||
"extends": ["../../packages/configs/biome.jsonc"] | ||
"extends": ["../../packages/configs/biome.jsonc"], | ||
"files": { | ||
"ignore": [ | ||
"src/parser/schemarb/parser.js" // Because it's generated | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
frontend/packages/db-structure/src/parser/__snapshots__/index.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`parse > should parse schema.rb to JSON correctly 1`] = ` | ||
{ | ||
"relationships": {}, | ||
"tables": { | ||
"xero_users": { | ||
"color": null, | ||
"comment": null, | ||
"fields": [ | ||
{ | ||
"check": null, | ||
"comment": null, | ||
"default": null, | ||
"increment": false, | ||
"name": "id", | ||
"notNull": false, | ||
"primary": false, | ||
"type": "varchar", | ||
"unique": false, | ||
}, | ||
{ | ||
"check": null, | ||
"comment": null, | ||
"default": null, | ||
"increment": false, | ||
"name": "email_address", | ||
"notNull": false, | ||
"primary": false, | ||
"type": "string", | ||
"unique": false, | ||
}, | ||
], | ||
"indices": [], | ||
"name": "xero_users", | ||
"x": 0, | ||
"y": 0, | ||
}, | ||
}, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import { describe, expect, it } from 'vitest' | ||
import { parse } from '.' | ||
|
||
describe(parse, () => { | ||
it('should parse schema.rb to JSON correctly', () => { | ||
const schemaText = fs.readFileSync( | ||
path.resolve(__dirname, './schemarb/input/schema1.in.rb'), | ||
'utf-8', | ||
) | ||
|
||
const result = parse(schemaText, 'schemarb') | ||
expect(result).toMatchSnapshot() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { DBStructure, Table } from 'src/schema' | ||
import { schemaRbParser } from './schemarb' | ||
|
||
type SupportedFormat = 'schemarb' | 'postgres' | ||
|
||
// biome-ignore lint/suspicious/noExplicitAny: TODO: Generate types with pegjs | ||
const convertToDBStructure = (data: any): DBStructure => { | ||
return { | ||
// biome-ignore lint/suspicious/noExplicitAny: TODO: Generate types with pegjs | ||
tables: data.tables.reduce((acc: Record<string, Table>, table: any) => { | ||
acc[table.name] = { | ||
comment: null, | ||
// biome-ignore lint/suspicious/noExplicitAny: TODO: Generate types with pegjs | ||
fields: table.fields.map((field: any) => ({ | ||
check: null, | ||
comment: null, | ||
default: null, | ||
increment: false, | ||
name: field.name, | ||
notNull: false, | ||
primary: false, | ||
type: field.type.type_name, | ||
unique: false, | ||
})), | ||
indices: [], | ||
name: table.name, | ||
x: 0, | ||
y: 0, | ||
color: null, | ||
} | ||
return acc | ||
}, {}), | ||
relationships: {}, | ||
} | ||
} | ||
|
||
// biome-ignore lint/suspicious/noExplicitAny: TODO: Generate types with pegjs | ||
const selectParser = (format: SupportedFormat): any => { | ||
switch (format) { | ||
case 'schemarb': | ||
return schemaRbParser | ||
default: | ||
throw new Error(`Unsupported format: ${format}`) | ||
} | ||
} | ||
|
||
export const parse = (str: string, format: SupportedFormat): DBStructure => { | ||
try { | ||
const parser = selectParser(format) | ||
const parsedSchema = parser.parse(str) | ||
const dbStructure = convertToDBStructure(parsedSchema) | ||
return dbStructure | ||
} catch (_error) { | ||
throw new Error('Failed to parse schema') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import schemaRbParser from './parser' | ||
|
||
export { schemaRbParser } |
3 changes: 3 additions & 0 deletions
3
frontend/packages/db-structure/src/parser/schemarb/input/schema1.in.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
create_table "xero_users", id: :uuid, default: -> { "uuid_generate_v4()" }, force: :cascade do |t| | ||
t.string "email_address" | ||
end |
Oops, something went wrong.