@graphql-mesh/transport-sql 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/dist/cjs/adapter.js +60 -0
- package/dist/cjs/batch.js +113 -0
- package/dist/cjs/client.js +205 -0
- package/dist/cjs/client.js.map +7 -0
- package/dist/cjs/dialect.js +60 -0
- package/dist/cjs/dialect.js.map +7 -0
- package/dist/cjs/entity.js +186 -0
- package/dist/cjs/execution.js +307 -0
- package/dist/cjs/execution.js.map +7 -0
- package/dist/cjs/index.js +23 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/mapping.js +260 -0
- package/dist/cjs/mapping.js.map +7 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/pg-executor.js +53 -0
- package/dist/cjs/pg-plans.js +103 -0
- package/dist/cjs/pg-registry.js +239 -0
- package/dist/cjs/plan.js +359 -0
- package/dist/cjs/sql-builder.js +653 -0
- package/dist/cjs/sql-builder.js.map +7 -0
- package/dist/cjs/types.js +0 -0
- package/dist/cjs/types.js.map +7 -0
- package/dist/cjs/uri.js +267 -0
- package/dist/esm/adapter.js +54 -0
- package/dist/esm/batch.js +107 -0
- package/dist/esm/client.js +197 -0
- package/dist/esm/client.js.map +7 -0
- package/dist/esm/dialect.js +54 -0
- package/dist/esm/dialect.js.map +7 -0
- package/dist/esm/entity.js +180 -0
- package/dist/esm/execution.js +303 -0
- package/dist/esm/execution.js.map +7 -0
- package/dist/esm/index.js +20 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/esm/mapping.js +255 -0
- package/dist/esm/mapping.js.map +7 -0
- package/dist/esm/pg-executor.js +49 -0
- package/dist/esm/pg-plans.js +100 -0
- package/dist/esm/pg-registry.js +236 -0
- package/dist/esm/plan.js +351 -0
- package/dist/esm/sql-builder.js +636 -0
- package/dist/esm/sql-builder.js.map +7 -0
- package/dist/esm/types.js +0 -0
- package/dist/esm/types.js.map +7 -0
- package/dist/esm/uri.js +261 -0
- package/dist/package.json +57 -0
- package/dist/typings/adapter.d.cts +9 -0
- package/dist/typings/adapter.d.ts +9 -0
- package/dist/typings/batch.d.cts +21 -0
- package/dist/typings/batch.d.ts +21 -0
- package/dist/typings/client.d.cts +36 -0
- package/dist/typings/client.d.ts +36 -0
- package/dist/typings/dialect.d.cts +9 -0
- package/dist/typings/dialect.d.ts +9 -0
- package/dist/typings/entity.d.cts +24 -0
- package/dist/typings/entity.d.ts +24 -0
- package/dist/typings/execution.d.cts +9 -0
- package/dist/typings/execution.d.ts +9 -0
- package/dist/typings/index.d.cts +15 -0
- package/dist/typings/index.d.ts +15 -0
- package/dist/typings/mapping.d.cts +13 -0
- package/dist/typings/mapping.d.ts +13 -0
- package/dist/typings/pg-executor.d.cts +19 -0
- package/dist/typings/pg-executor.d.ts +19 -0
- package/dist/typings/pg-plans.d.cts +12 -0
- package/dist/typings/pg-plans.d.ts +12 -0
- package/dist/typings/pg-registry.d.cts +16 -0
- package/dist/typings/pg-registry.d.ts +16 -0
- package/dist/typings/plan.d.cts +47 -0
- package/dist/typings/plan.d.ts +47 -0
- package/dist/typings/sql-builder.d.cts +75 -0
- package/dist/typings/sql-builder.d.ts +75 -0
- package/dist/typings/types.d.cts +50 -0
- package/dist/typings/types.d.ts +50 -0
- package/dist/typings/uri.d.cts +42 -0
- package/dist/typings/uri.d.ts +42 -0
- package/package.json +67 -4
- package/src/adapter.ts +56 -0
- package/src/batch.ts +150 -0
- package/src/client.ts +233 -0
- package/src/entity.ts +235 -0
- package/src/execution.ts +411 -0
- package/src/index.ts +25 -0
- package/src/mapping.ts +291 -0
- package/src/pg-executor.ts +68 -0
- package/src/pg-plans.ts +118 -0
- package/src/pg-registry.ts +290 -0
- package/src/plan.ts +466 -0
- package/src/sql-builder.ts +809 -0
- package/src/types.ts +63 -0
- package/src/uri.ts +301 -0
- package/test/client-bun.test.ts +8 -0
- package/test/client.bun.test.ts +46 -0
- package/test/execution.test.ts +277 -0
- package/test/mapping.test.ts +79 -0
- package/test/pg-registry.test.ts +103 -0
- package/test/sql-builder.test.ts +131 -0
- package/test/uri.test.ts +123 -0
- package/README.md +0 -3
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { columnMapFromSelection, parseSqlMapping, resolveMappingValue } from '../src/mapping.js';
|
|
2
|
+
|
|
3
|
+
describe('parseSqlMapping', () => {
|
|
4
|
+
it('parses column selection and skips comments', () => {
|
|
5
|
+
const parsed = parseSqlMapping(`
|
|
6
|
+
# Album columns
|
|
7
|
+
id: AlbumId
|
|
8
|
+
title: Title
|
|
9
|
+
artistId: ArtistId
|
|
10
|
+
`);
|
|
11
|
+
expect(columnMapFromSelection(parsed)).toEqual(
|
|
12
|
+
new Map([
|
|
13
|
+
['id', 'AlbumId'],
|
|
14
|
+
['title', 'Title'],
|
|
15
|
+
['artistId', 'ArtistId'],
|
|
16
|
+
]),
|
|
17
|
+
);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('parses $args / $this, spreads, and nested blocks', () => {
|
|
21
|
+
const parsed = parseSqlMapping(`
|
|
22
|
+
ArtistId: $this.artistId
|
|
23
|
+
...$args.where
|
|
24
|
+
set {
|
|
25
|
+
Title: $args.input.title
|
|
26
|
+
}
|
|
27
|
+
where {
|
|
28
|
+
AlbumId: $args.id
|
|
29
|
+
}
|
|
30
|
+
`);
|
|
31
|
+
expect(parsed.entries[0].value).toEqual({
|
|
32
|
+
kind: 'this',
|
|
33
|
+
value: 'artistId',
|
|
34
|
+
path: ['artistId'],
|
|
35
|
+
});
|
|
36
|
+
expect(parsed.entries[1]).toMatchObject({
|
|
37
|
+
spread: true,
|
|
38
|
+
key: '...$args.where',
|
|
39
|
+
value: {
|
|
40
|
+
kind: 'args',
|
|
41
|
+
value: 'where',
|
|
42
|
+
path: ['where'],
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
const set = parsed.entries.find(e => e.key === 'set');
|
|
46
|
+
expect(set?.children?.[0].key).toBe('Title');
|
|
47
|
+
expect(
|
|
48
|
+
resolveMappingValue(set?.children?.[0].value, {
|
|
49
|
+
args: { input: { title: 'X' } },
|
|
50
|
+
}),
|
|
51
|
+
).toBe('X');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('parses bare $args.where as a whole-value binder', () => {
|
|
55
|
+
const parsed = parseSqlMapping(`$args.where`);
|
|
56
|
+
expect(parsed.entries).toEqual([
|
|
57
|
+
{
|
|
58
|
+
key: '$args.where',
|
|
59
|
+
value: { kind: 'args', value: 'where', path: ['where'] },
|
|
60
|
+
},
|
|
61
|
+
]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('parses connection wrappers and pseudos', () => {
|
|
65
|
+
const parsed = parseSqlMapping(`
|
|
66
|
+
nodes {
|
|
67
|
+
id: AlbumId
|
|
68
|
+
}
|
|
69
|
+
totalCount: $totalCount
|
|
70
|
+
$
|
|
71
|
+
`);
|
|
72
|
+
expect(parsed.entries[0].children?.[0].value).toEqual({
|
|
73
|
+
kind: 'column',
|
|
74
|
+
value: 'AlbumId',
|
|
75
|
+
});
|
|
76
|
+
expect(parsed.entries[1].value).toEqual({ kind: 'pseudo', value: 'totalCount' });
|
|
77
|
+
expect(parsed.entries[2].value).toEqual({ kind: 'pseudo', value: '$' });
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import {
|
|
2
|
+
GraphQLInt,
|
|
3
|
+
GraphQLList,
|
|
4
|
+
GraphQLNonNull,
|
|
5
|
+
GraphQLObjectType,
|
|
6
|
+
GraphQLSchema,
|
|
7
|
+
GraphQLString,
|
|
8
|
+
} from 'graphql';
|
|
9
|
+
import { buildPgRegistryFromSqlSchema } from '../src/pg-registry.js';
|
|
10
|
+
|
|
11
|
+
const albumSelection = ['id: AlbumId', 'title: Title', 'artistId: ArtistId'].join('\n');
|
|
12
|
+
const artistSelection = ['id: ArtistId', 'name: Name'].join('\n');
|
|
13
|
+
|
|
14
|
+
describe('buildPgRegistryFromSqlSchema', () => {
|
|
15
|
+
it('builds codecs/resources/relations from @sql directives', () => {
|
|
16
|
+
const Artist: GraphQLObjectType = new GraphQLObjectType({
|
|
17
|
+
name: 'Artist',
|
|
18
|
+
fields: () => ({
|
|
19
|
+
id: { type: new GraphQLNonNull(GraphQLInt) },
|
|
20
|
+
name: { type: GraphQLString },
|
|
21
|
+
albums: {
|
|
22
|
+
type: new GraphQLList(Album),
|
|
23
|
+
extensions: {
|
|
24
|
+
directives: {
|
|
25
|
+
sql: {
|
|
26
|
+
table: 'albums',
|
|
27
|
+
kind: 'ROWS',
|
|
28
|
+
where: 'ArtistId: $this.id',
|
|
29
|
+
selection: albumSelection,
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
}),
|
|
35
|
+
extensions: {
|
|
36
|
+
directives: {
|
|
37
|
+
sql: {
|
|
38
|
+
table: 'artists',
|
|
39
|
+
selection: artistSelection,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
const Album: GraphQLObjectType = new GraphQLObjectType({
|
|
45
|
+
name: 'Album',
|
|
46
|
+
fields: () => ({
|
|
47
|
+
id: { type: new GraphQLNonNull(GraphQLInt) },
|
|
48
|
+
title: { type: GraphQLString },
|
|
49
|
+
artistId: { type: GraphQLInt },
|
|
50
|
+
artist: {
|
|
51
|
+
type: Artist,
|
|
52
|
+
extensions: {
|
|
53
|
+
directives: {
|
|
54
|
+
sql: {
|
|
55
|
+
table: 'artists',
|
|
56
|
+
kind: 'ROW',
|
|
57
|
+
where: 'ArtistId: $this.artistId',
|
|
58
|
+
selection: artistSelection,
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
}),
|
|
64
|
+
extensions: {
|
|
65
|
+
directives: {
|
|
66
|
+
sql: {
|
|
67
|
+
table: 'albums',
|
|
68
|
+
selection: albumSelection,
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
const schema = new GraphQLSchema({
|
|
74
|
+
query: new GraphQLObjectType({
|
|
75
|
+
name: 'Query',
|
|
76
|
+
fields: {
|
|
77
|
+
albums: {
|
|
78
|
+
type: new GraphQLList(Album),
|
|
79
|
+
extensions: {
|
|
80
|
+
directives: {
|
|
81
|
+
sql: {
|
|
82
|
+
table: 'albums',
|
|
83
|
+
kind: 'ROWS',
|
|
84
|
+
selection: albumSelection,
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
}),
|
|
91
|
+
types: [Album, Artist],
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const { registry, tableByType, relationNames } = buildPgRegistryFromSqlSchema(schema);
|
|
95
|
+
|
|
96
|
+
expect(tableByType.get('Album')).toBe('albums');
|
|
97
|
+
expect(tableByType.get('Artist')).toBe('artists');
|
|
98
|
+
expect(registry.pgResources.albums).toBeDefined();
|
|
99
|
+
expect(registry.pgResources.artists).toBeDefined();
|
|
100
|
+
expect(relationNames.get('Album')?.get('artist')?.kind).toBe('ROW');
|
|
101
|
+
expect(relationNames.get('Artist')?.get('albums')?.kind).toBe('ROWS');
|
|
102
|
+
});
|
|
103
|
+
});
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { parseSqlMapping } from '../src/mapping.js';
|
|
2
|
+
import { buildSelect, escapeSqlStringLiteral, mapRowToGraphql } from '../src/sql-builder.js';
|
|
3
|
+
|
|
4
|
+
describe('buildSelect', () => {
|
|
5
|
+
const selection = parseSqlMapping(`
|
|
6
|
+
id: AlbumId
|
|
7
|
+
title: Title
|
|
8
|
+
artistId: ArtistId
|
|
9
|
+
`);
|
|
10
|
+
|
|
11
|
+
it('builds ROW by id', () => {
|
|
12
|
+
const { sql, params } = buildSelect('sqlite', {
|
|
13
|
+
table: 'albums',
|
|
14
|
+
kind: 'ROW',
|
|
15
|
+
selection,
|
|
16
|
+
whereMapping: parseSqlMapping(`AlbumId: $args.id`),
|
|
17
|
+
graphqlArgs: { id: '1' },
|
|
18
|
+
});
|
|
19
|
+
expect(sql).toContain('FROM "albums"');
|
|
20
|
+
expect(sql).toContain('"AlbumId" = ?');
|
|
21
|
+
expect(sql).toContain('LIMIT ?');
|
|
22
|
+
expect(params).toEqual(['1', 1]);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('builds filtered ROWS with bare $args.where (no spread needed alone)', () => {
|
|
26
|
+
const { sql, params } = buildSelect('sqlite', {
|
|
27
|
+
table: 'albums',
|
|
28
|
+
kind: 'ROWS',
|
|
29
|
+
selection,
|
|
30
|
+
whereMapping: parseSqlMapping(`$args.where`),
|
|
31
|
+
planners: {
|
|
32
|
+
orderBy: '$args.orderBy',
|
|
33
|
+
limit: '$args.limit',
|
|
34
|
+
offset: '$args.offset',
|
|
35
|
+
},
|
|
36
|
+
graphqlArgs: {
|
|
37
|
+
where: { title: { startsWith: 'For' } },
|
|
38
|
+
orderBy: ['TITLE_ASC'],
|
|
39
|
+
limit: 2,
|
|
40
|
+
offset: 0,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
expect(sql).toContain('"Title" LIKE ?');
|
|
44
|
+
expect(sql).toContain('ORDER BY "Title" ASC');
|
|
45
|
+
expect(params[0]).toBe('For%');
|
|
46
|
+
expect(params).toContain(2);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('merges join equality with ...$args.where spread', () => {
|
|
50
|
+
const { sql, params } = buildSelect('sqlite', {
|
|
51
|
+
table: 'albums',
|
|
52
|
+
kind: 'ROWS',
|
|
53
|
+
selection,
|
|
54
|
+
whereMapping: parseSqlMapping(`
|
|
55
|
+
ArtistId: $this.id
|
|
56
|
+
...$args.where
|
|
57
|
+
`),
|
|
58
|
+
planners: {
|
|
59
|
+
orderBy: '$args.orderBy',
|
|
60
|
+
limit: '$args.limit',
|
|
61
|
+
},
|
|
62
|
+
parent: { id: 42 },
|
|
63
|
+
graphqlArgs: {
|
|
64
|
+
where: { title: { eq: 'X' } },
|
|
65
|
+
orderBy: ['TITLE_ASC'],
|
|
66
|
+
limit: 5,
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
expect(sql).toContain('"ArtistId" = ?');
|
|
70
|
+
expect(sql).toContain('"Title" = ?');
|
|
71
|
+
expect(params[0]).toBe(42);
|
|
72
|
+
expect(params[1]).toBe('X');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('maps rows to GraphQL fields and stringifies id', () => {
|
|
76
|
+
expect(mapRowToGraphql({ AlbumId: 1, Title: 'A', ArtistId: 2 }, selection)).toEqual({
|
|
77
|
+
id: '1',
|
|
78
|
+
title: 'A',
|
|
79
|
+
artistId: 2,
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('escapes backslash before quotes in SQL string literals', () => {
|
|
84
|
+
// CodeQL js/incomplete-sanitization: `\` must be doubled first.
|
|
85
|
+
expect(escapeSqlStringLiteral("a\\b'c", 'mysql')).toBe("a\\\\b\\'c");
|
|
86
|
+
expect(escapeSqlStringLiteral("a\\b'c", 'sqlite')).toBe("a\\\\b''c");
|
|
87
|
+
expect(escapeSqlStringLiteral('\\', 'mysql')).toBe('\\\\');
|
|
88
|
+
expect(escapeSqlStringLiteral("'", 'mysql')).toBe("\\'");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('embeds escaped JSON keys in mysql/sqlite has-many subselects', () => {
|
|
92
|
+
const hasMany = [
|
|
93
|
+
{
|
|
94
|
+
fieldName: 'albums',
|
|
95
|
+
table: 'albums',
|
|
96
|
+
joinColumn: 'ArtistId',
|
|
97
|
+
parentGraphqlField: 'id',
|
|
98
|
+
parentColumn: 'ArtistId',
|
|
99
|
+
selection: {
|
|
100
|
+
entries: [
|
|
101
|
+
{
|
|
102
|
+
key: "title\\'x",
|
|
103
|
+
value: { kind: 'column' as const, value: 'Title' },
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
},
|
|
107
|
+
limit: 2,
|
|
108
|
+
},
|
|
109
|
+
];
|
|
110
|
+
|
|
111
|
+
const mysql = buildSelect('mysql', {
|
|
112
|
+
table: 'artists',
|
|
113
|
+
kind: 'ROWS',
|
|
114
|
+
selection: parseSqlMapping(`id: ArtistId`),
|
|
115
|
+
whereMapping: parseSqlMapping(`limit: $args.limit`),
|
|
116
|
+
graphqlArgs: { limit: 1 },
|
|
117
|
+
hasMany,
|
|
118
|
+
});
|
|
119
|
+
expect(mysql.sql).toContain(`'${escapeSqlStringLiteral("title\\'x", 'mysql')}'`);
|
|
120
|
+
|
|
121
|
+
const sqlite = buildSelect('sqlite', {
|
|
122
|
+
table: 'artists',
|
|
123
|
+
kind: 'ROWS',
|
|
124
|
+
selection: parseSqlMapping(`id: ArtistId`),
|
|
125
|
+
whereMapping: parseSqlMapping(`limit: $args.limit`),
|
|
126
|
+
graphqlArgs: { limit: 1 },
|
|
127
|
+
hasMany,
|
|
128
|
+
});
|
|
129
|
+
expect(sqlite.sql).toContain(`'${escapeSqlStringLiteral("title\\'x", 'sqlite')}'`);
|
|
130
|
+
});
|
|
131
|
+
});
|
package/test/uri.test.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {
|
|
2
|
+
inferAdapterFromConnectionString,
|
|
3
|
+
parseSqlUri,
|
|
4
|
+
resolveConnectionStringFromEnv,
|
|
5
|
+
} from '../src/uri.js';
|
|
6
|
+
|
|
7
|
+
describe('inferAdapterFromConnectionString (Bun-compatible)', () => {
|
|
8
|
+
it('detects postgres protocols and host-style URIs', () => {
|
|
9
|
+
expect(inferAdapterFromConnectionString('postgres://u:p@localhost:5432/db')).toBe('postgres');
|
|
10
|
+
expect(inferAdapterFromConnectionString('postgresql://localhost/db')).toBe('postgres');
|
|
11
|
+
expect(inferAdapterFromConnectionString('localhost:5432/mydb')).toBe('postgres');
|
|
12
|
+
expect(inferAdapterFromConnectionString('user:pass@localhost:5432/shop')).toBe('postgres');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('detects mysql / mysql2 / mariadb / mysqls', () => {
|
|
16
|
+
expect(inferAdapterFromConnectionString('mysql://u:p@localhost:3306/db')).toBe('mysql');
|
|
17
|
+
expect(inferAdapterFromConnectionString('mysql2://u:p@localhost/db')).toBe('mysql');
|
|
18
|
+
expect(inferAdapterFromConnectionString('mariadb://u:p@localhost/db')).toBe('mysql');
|
|
19
|
+
expect(inferAdapterFromConnectionString('mysqls://u:p@localhost/db')).toBe('mysql');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('detects sqlite / file / :memory: / *.db', () => {
|
|
23
|
+
expect(inferAdapterFromConnectionString('sqlite://myapp.db')).toBe('sqlite');
|
|
24
|
+
expect(inferAdapterFromConnectionString('sqlite:myapp.db')).toBe('sqlite');
|
|
25
|
+
expect(inferAdapterFromConnectionString('file:./data.db')).toBe('sqlite');
|
|
26
|
+
expect(inferAdapterFromConnectionString(':memory:')).toBe('sqlite');
|
|
27
|
+
expect(inferAdapterFromConnectionString('chinook.db')).toBe('sqlite');
|
|
28
|
+
expect(inferAdapterFromConnectionString('./local.sqlite')).toBe('sqlite');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('throws on unknown protocols', () => {
|
|
32
|
+
expect(() => inferAdapterFromConnectionString('pg://localhost/db')).toThrow(/Unsupported/);
|
|
33
|
+
expect(() => inferAdapterFromConnectionString('sqlite3://x.db')).toThrow(/Unsupported/);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('parseSqlUri', () => {
|
|
38
|
+
it('parses mysql2 and mariadb as mysql adapter', () => {
|
|
39
|
+
expect(parseSqlUri('mysql2://root:secret@db:3307/app').adapter).toBe('mysql');
|
|
40
|
+
expect(parseSqlUri('mysql2://root:secret@db:3307/app').port).toBe(3307);
|
|
41
|
+
expect(parseSqlUri('mariadb://u@localhost/db').database).toBe('db');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('parses host-style postgres without protocol', () => {
|
|
45
|
+
const parsed = parseSqlUri('localhost:5432/mydb');
|
|
46
|
+
expect(parsed.adapter).toBe('postgres');
|
|
47
|
+
expect(parsed.host).toBe('localhost');
|
|
48
|
+
expect(parsed.port).toBe(5432);
|
|
49
|
+
expect(parsed.database).toBe('mydb');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('parses sqlite file paths', () => {
|
|
53
|
+
expect(parseSqlUri('sqlite://./app.db').filePath).toBe('./app.db');
|
|
54
|
+
expect(parseSqlUri(':memory:').filePath).toBe(':memory:');
|
|
55
|
+
expect(parseSqlUri('chinook.db').filePath).toBe('chinook.db');
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('resolveConnectionStringFromEnv (Bun-compatible)', () => {
|
|
60
|
+
it('prefers DATABASE_URL', () => {
|
|
61
|
+
expect(
|
|
62
|
+
resolveConnectionStringFromEnv({
|
|
63
|
+
DATABASE_URL: 'postgres://u@localhost/db',
|
|
64
|
+
MYSQL_URL: 'mysql://u@localhost/db',
|
|
65
|
+
}),
|
|
66
|
+
).toBe('postgres://u@localhost/db');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('falls through URL env vars in Bun order', () => {
|
|
70
|
+
expect(
|
|
71
|
+
resolveConnectionStringFromEnv({
|
|
72
|
+
MYSQL_URL: 'mysql://root@localhost/app',
|
|
73
|
+
}),
|
|
74
|
+
).toBe('mysql://root@localhost/app');
|
|
75
|
+
expect(resolveConnectionStringFromEnv({ SQLITE_URL: 'file:./x.db' })).toBe('file:./x.db');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('adds sslmode=require for TLS_* URLs', () => {
|
|
79
|
+
expect(
|
|
80
|
+
resolveConnectionStringFromEnv({
|
|
81
|
+
TLS_DATABASE_URL: 'postgres://u@localhost/db',
|
|
82
|
+
}),
|
|
83
|
+
).toBe('postgres://u@localhost/db?sslmode=require');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('composes postgres URL from PG* fields', () => {
|
|
87
|
+
expect(
|
|
88
|
+
resolveConnectionStringFromEnv({
|
|
89
|
+
PGHOST: 'db.internal',
|
|
90
|
+
PGPORT: '6543',
|
|
91
|
+
PGUSER: 'app',
|
|
92
|
+
PGPASSWORD: 's3cret',
|
|
93
|
+
PGDATABASE: 'shop',
|
|
94
|
+
PGSSLMODE: 'require',
|
|
95
|
+
}),
|
|
96
|
+
).toBe('postgres://app:s3cret@db.internal:6543/shop?sslmode=require');
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('composes mysql URL when adapter is mysql', () => {
|
|
100
|
+
expect(
|
|
101
|
+
resolveConnectionStringFromEnv(
|
|
102
|
+
{
|
|
103
|
+
MYSQL_HOST: 'mysql',
|
|
104
|
+
MYSQL_USER: 'root',
|
|
105
|
+
MYSQL_DATABASE: 'employees',
|
|
106
|
+
},
|
|
107
|
+
{ adapter: 'mysql' },
|
|
108
|
+
),
|
|
109
|
+
).toBe('mysql://root@mysql/employees');
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('skips DATABASE_URL whose protocol disagrees with adapter', () => {
|
|
113
|
+
expect(
|
|
114
|
+
resolveConnectionStringFromEnv(
|
|
115
|
+
{
|
|
116
|
+
DATABASE_URL: 'sqlite://./x.db',
|
|
117
|
+
POSTGRES_URL: 'postgres://u@localhost/db',
|
|
118
|
+
},
|
|
119
|
+
{ adapter: 'postgres' },
|
|
120
|
+
),
|
|
121
|
+
).toBe('postgres://u@localhost/db');
|
|
122
|
+
});
|
|
123
|
+
});
|
package/README.md
DELETED