-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnested-ts-descriptor.test.ts
87 lines (83 loc) · 2.15 KB
/
nested-ts-descriptor.test.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import assert from 'node:assert';
import { describeNestedQuery } from '../src/describe-nested-query';
import { createMysqlClientForTest, loadMysqlSchema } from '../src/queryExectutor';
import { extractQueryInfo } from '../src/mysql-query-analyzer/parse';
import { type NestedTsDescriptor, createNestedTsDescriptor } from '../src/ts-nested-descriptor';
import type { MySqlDialect } from '../src/types';
describe('Test nested-ts-descriptor', () => {
let client!: MySqlDialect;
before(async () => {
client = await createMysqlClientForTest('mysql://root:password@localhost/mydb');
});
it('SELECT FROM users u INNER JOIN posts p', async () => {
const dbSchema = await loadMysqlSchema(client.client, client.schema);
const sql = `
SELECT
u.id as user_id,
u.name as user_name,
p.id as post_id,
p.title as post_title
FROM users u
INNER JOIN posts p on p.fk_user = u.id
`;
if (dbSchema.isErr()) {
assert.fail(`Shouldn't return an error: ${dbSchema.error.description}`);
}
const model = describeNestedQuery(sql, dbSchema.value);
const queryInfo = extractQueryInfo(sql, dbSchema.value);
assert(queryInfo.kind === 'Select');
const actual = createNestedTsDescriptor(queryInfo.columns, model);
const expected: NestedTsDescriptor = {
relations: [
{
name: 'users',
groupKeyIndex: 0,
fields: [
{
type: 'field',
name: 'user_id',
index: 0,
tsType: 'number',
notNull: true
},
{
type: 'field',
name: 'user_name',
index: 1,
tsType: 'string',
notNull: true
},
{
type: 'relation',
name: 'posts',
list: true,
tsType: 'posts[]',
notNull: true
}
]
},
{
name: 'posts',
groupKeyIndex: 2,
fields: [
{
type: 'field',
name: 'post_id',
index: 2,
tsType: 'number',
notNull: true
},
{
type: 'field',
name: 'post_title',
index: 3,
tsType: 'string',
notNull: true
}
]
}
]
};
assert.deepStrictEqual(actual, expected);
});
});