Repository navigation
Expand file tree
/
Copy pathschema-parser.test.ts
More file actions
62 lines (59 loc) · 1.94 KB
/
Copy pathschema-parser.test.ts
File metadata and controls
62 lines (59 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { describe, expect, it } from 'vitest';
import { parseDatabaseSchema } from './schema-parser.js';
describe('parseDatabaseSchema', () => {
it('parses sqlite and postgres tables regardless of formatting', () => {
const tables = parseDatabaseSchema(`
export const account = sqliteTable(
'account',
{ id: text('id').primaryKey().notNull(),
owner_id: integer('owner_id', { mode: 'number' }).references(() => user.id),
metadata: jsonb('metadata').$default(() => ({})), },
);
export const user = pgTable('user', {
id: uuid('id').defaultRandom().primaryKey(),
email: varchar('email', { length: 255 }).notNull().unique(),
});
`);
expect(tables).toEqual([
{
name: 'account',
columns: [
{
name: 'id',
databaseName: 'id',
type: 'text',
constraints: ['PRIMARY KEY', 'NOT NULL'],
},
{ name: 'owner_id', databaseName: 'owner_id', type: 'integer', constraints: ['FK'] },
{ name: 'metadata', databaseName: 'metadata', type: 'jsonb', constraints: ['DEFAULT'] },
],
},
{
name: 'user',
columns: [
{ name: 'id', databaseName: 'id', type: 'uuid', constraints: ['PRIMARY KEY'] },
{
name: 'email',
databaseName: 'email',
type: 'varchar',
constraints: ['NOT NULL', 'UNIQUE'],
},
],
},
]);
});
it('ignores unrelated objects and supports quoted column keys', () => {
const tables = parseDatabaseSchema(`
const config = { id: text('not a table') };
export const thing = sqliteTable('thing', { 'display-name': text('display-name') });
`);
expect(tables).toEqual([
{
name: 'thing',
columns: [
{ name: 'display-name', databaseName: 'display-name', type: 'text', constraints: [] },
],
},
]);
});
});