@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
package/src/types.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export type SqlAdapter = 'postgres' | 'mysql' | 'sqlite';
|
|
2
|
+
|
|
3
|
+
export type SqlQueryKind = 'ROW' | 'ROWS' | 'CONNECTION' | 'COUNT';
|
|
4
|
+
|
|
5
|
+
export type SqlMutationKind = 'INSERT' | 'UPDATE' | 'DELETE';
|
|
6
|
+
|
|
7
|
+
/** Combined ` @sql` kind (reads + writes). */
|
|
8
|
+
export type SqlKind = SqlQueryKind | SqlMutationKind;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Bun SQL–inspired connection / pool options stored on `@transport(options:)`.
|
|
12
|
+
* Only the fields we can honour on Node (`pg` / `mysql2` / `node:sqlite`) or
|
|
13
|
+
* forward to `Bun.SQL` when present are included.
|
|
14
|
+
*/
|
|
15
|
+
export interface SqlTransportOptions {
|
|
16
|
+
adapter?: SqlAdapter;
|
|
17
|
+
/** Max pool size (default 10). Maps to Bun `max`, mysql2 `connectionLimit`, pg `max`. */
|
|
18
|
+
max?: number;
|
|
19
|
+
/** Idle connection lifetime in seconds (0 = off). Bun `idleTimeout`. */
|
|
20
|
+
idleTimeout?: number;
|
|
21
|
+
/** Seconds to wait for a new connection (default 30). Bun `connectionTimeout`. */
|
|
22
|
+
connectionTimeout?: number;
|
|
23
|
+
/** Enable TLS / SSL (`true` or Bun-style mode string). */
|
|
24
|
+
tls?: boolean | string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface SqlMappingValue {
|
|
28
|
+
kind: 'column' | 'pseudo' | 'args' | 'this' | 'literal';
|
|
29
|
+
/** SQL column name, pseudo token ($cursor), or path after $args / $this */
|
|
30
|
+
value: string;
|
|
31
|
+
/** Nested path segments for $args.input.title -> ['input','title'] */
|
|
32
|
+
path?: string[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface SqlMappingEntry {
|
|
36
|
+
/** GraphQL field name, planner key (orderBy/...), or SQL column on the left of args */
|
|
37
|
+
key: string;
|
|
38
|
+
value?: SqlMappingValue;
|
|
39
|
+
children?: SqlMappingEntry[];
|
|
40
|
+
/**
|
|
41
|
+
* Connectors-style spread (`...$args.where`): merge the resolved object into
|
|
42
|
+
* the structured WHERE filter (null/undefined spread is a no-op).
|
|
43
|
+
*/
|
|
44
|
+
spread?: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface ParsedSqlMapping {
|
|
48
|
+
entries: SqlMappingEntry[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface SqlClient {
|
|
52
|
+
readonly adapter: SqlAdapter;
|
|
53
|
+
query<T = Record<string, unknown>>(
|
|
54
|
+
sql: string,
|
|
55
|
+
params?: unknown[],
|
|
56
|
+
): Promise<{ rows: T[]; rowCount: number }>;
|
|
57
|
+
dispose(): void | Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Node `pg.Pool` when using the postgres adapter (required for the
|
|
60
|
+
* Grafast / `@dataplan/pg` fast path). Absent for Bun.SQL / mysql / sqlite.
|
|
61
|
+
*/
|
|
62
|
+
pgPool?: import('pg').Pool;
|
|
63
|
+
}
|
package/src/uri.ts
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import type { SqlAdapter } from './types.js';
|
|
2
|
+
|
|
3
|
+
export interface ParsedSqlUri {
|
|
4
|
+
adapter: SqlAdapter;
|
|
5
|
+
user?: string;
|
|
6
|
+
password?: string;
|
|
7
|
+
host?: string;
|
|
8
|
+
port?: number;
|
|
9
|
+
database?: string;
|
|
10
|
+
ssl?: boolean;
|
|
11
|
+
/** Absolute or relative file path for sqlite */
|
|
12
|
+
filePath?: string;
|
|
13
|
+
searchParams: Record<string, string>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type EnvLike = Record<string, string | undefined>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Bun-compatible connection URL env vars, in precedence order.
|
|
20
|
+
* @see https://bun.com/docs/runtime/sql
|
|
21
|
+
*/
|
|
22
|
+
export const SQL_URL_ENV_VARS = [
|
|
23
|
+
'DATABASE_URL',
|
|
24
|
+
'TLS_DATABASE_URL',
|
|
25
|
+
'POSTGRES_URL',
|
|
26
|
+
'PGURL',
|
|
27
|
+
'PG_URL',
|
|
28
|
+
'TLS_POSTGRES_DATABASE_URL',
|
|
29
|
+
'MYSQL_URL',
|
|
30
|
+
'TLS_MYSQL_DATABASE_URL',
|
|
31
|
+
'MARIADB_URL',
|
|
32
|
+
'TLS_MARIADB_DATABASE_URL',
|
|
33
|
+
'SQLITE_URL',
|
|
34
|
+
] as const;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Resolve a connection string from the environment the way Bun SQL does:
|
|
38
|
+
* URL vars first, then (when no URL) a postgres URL composed from `PG*` fields.
|
|
39
|
+
*
|
|
40
|
+
* `TLS_*` URLs get `sslmode=require` unless the URL already names an ssl mode.
|
|
41
|
+
*/
|
|
42
|
+
export function resolveConnectionStringFromEnv(
|
|
43
|
+
env: EnvLike = typeof process !== 'undefined' ? process.env : {},
|
|
44
|
+
opts?: { adapter?: SqlAdapter },
|
|
45
|
+
): string | undefined {
|
|
46
|
+
for (const key of SQL_URL_ENV_VARS) {
|
|
47
|
+
const value = env[key]?.trim();
|
|
48
|
+
if (!value) continue;
|
|
49
|
+
if (opts?.adapter) {
|
|
50
|
+
try {
|
|
51
|
+
if (inferAdapterFromConnectionString(value) !== opts.adapter) {
|
|
52
|
+
// With an explicit adapter, skip URLs that name another adapter
|
|
53
|
+
// (Bun does the same when `adapter` is set).
|
|
54
|
+
if (key === 'DATABASE_URL' || key === 'TLS_DATABASE_URL') continue;
|
|
55
|
+
if (opts.adapter === 'postgres' && !isPostgresUrlEnvKey(key)) continue;
|
|
56
|
+
if (opts.adapter === 'mysql' && !isMysqlUrlEnvKey(key)) continue;
|
|
57
|
+
if (opts.adapter === 'sqlite' && key !== 'SQLITE_URL') continue;
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
// unknown protocol — still return; caller / infer will throw
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (key.startsWith('TLS_')) {
|
|
64
|
+
return ensureSslModeRequire(value);
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (opts?.adapter === 'mysql' || (!opts?.adapter && env.MYSQL_HOST)) {
|
|
70
|
+
const mysql = composeMysqlUrlFromEnv(env);
|
|
71
|
+
if (mysql) return mysql;
|
|
72
|
+
}
|
|
73
|
+
if (opts?.adapter !== 'mysql' && opts?.adapter !== 'sqlite') {
|
|
74
|
+
const pg = composePostgresUrlFromEnv(env);
|
|
75
|
+
if (pg) return pg;
|
|
76
|
+
}
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function isPostgresUrlEnvKey(key: string): boolean {
|
|
81
|
+
return (
|
|
82
|
+
key === 'POSTGRES_URL' ||
|
|
83
|
+
key === 'PGURL' ||
|
|
84
|
+
key === 'PG_URL' ||
|
|
85
|
+
key === 'TLS_POSTGRES_DATABASE_URL'
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function isMysqlUrlEnvKey(key: string): boolean {
|
|
90
|
+
return (
|
|
91
|
+
key === 'MYSQL_URL' ||
|
|
92
|
+
key === 'TLS_MYSQL_DATABASE_URL' ||
|
|
93
|
+
key === 'MARIADB_URL' ||
|
|
94
|
+
key === 'TLS_MARIADB_DATABASE_URL'
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function ensureSslModeRequire(url: string): string {
|
|
99
|
+
if (/[?&](sslmode|ssl|tls)=/i.test(url)) return url;
|
|
100
|
+
return url.includes('?') ? `${url}&sslmode=require` : `${url}?sslmode=require`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function composePostgresUrlFromEnv(env: EnvLike): string | undefined {
|
|
104
|
+
const host = env.PGHOST;
|
|
105
|
+
const database = env.PGDATABASE;
|
|
106
|
+
const user = env.PGUSERNAME || env.PGUSER || env.USER || env.USERNAME;
|
|
107
|
+
const password = env.PGPASSWORD;
|
|
108
|
+
const port = env.PGPORT;
|
|
109
|
+
const sslmode = env.PGSSLMODE;
|
|
110
|
+
if (!host && !database && !user && !password) return undefined;
|
|
111
|
+
|
|
112
|
+
const auth =
|
|
113
|
+
user != null && user !== ''
|
|
114
|
+
? password != null && password !== ''
|
|
115
|
+
? `${encodeURIComponent(user)}:${encodeURIComponent(password)}@`
|
|
116
|
+
: `${encodeURIComponent(user)}@`
|
|
117
|
+
: '';
|
|
118
|
+
const hostPart = host || 'localhost';
|
|
119
|
+
const portPart = port ? `:${port}` : '';
|
|
120
|
+
const dbPart = database ? `/${encodeURIComponent(database)}` : '';
|
|
121
|
+
const qs = sslmode ? `?sslmode=${encodeURIComponent(sslmode)}` : '';
|
|
122
|
+
return `postgres://${auth}${hostPart}${portPart}${dbPart}${qs}`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function composeMysqlUrlFromEnv(env: EnvLike): string | undefined {
|
|
126
|
+
const host = env.MYSQL_HOST || env.MARIADB_HOST;
|
|
127
|
+
const database = env.MYSQL_DATABASE || env.MARIADB_DATABASE;
|
|
128
|
+
const user = env.MYSQL_USER || env.MARIADB_USER;
|
|
129
|
+
const password = env.MYSQL_PASSWORD || env.MARIADB_PASSWORD;
|
|
130
|
+
const port = env.MYSQL_PORT || env.MARIADB_PORT;
|
|
131
|
+
if (!host && !database && !user && !password) return undefined;
|
|
132
|
+
|
|
133
|
+
const auth =
|
|
134
|
+
user != null && user !== ''
|
|
135
|
+
? password != null && password !== ''
|
|
136
|
+
? `${encodeURIComponent(user)}:${encodeURIComponent(password)}@`
|
|
137
|
+
: `${encodeURIComponent(user)}@`
|
|
138
|
+
: '';
|
|
139
|
+
const hostPart = host || 'localhost';
|
|
140
|
+
const portPart = port ? `:${port}` : '';
|
|
141
|
+
const dbPart = database ? `/${encodeURIComponent(database)}` : '';
|
|
142
|
+
return `mysql://${auth}${hostPart}${portPart}${dbPart}`;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Detect adapter from a connection string protocol — Bun SQL–compatible.
|
|
147
|
+
*
|
|
148
|
+
* | Connection string | Adapter |
|
|
149
|
+
* | --- | --- |
|
|
150
|
+
* | `postgres://`, `postgresql://`, or host-style with no protocol | postgres |
|
|
151
|
+
* | `mysql://`, `mysql2://`, `mysqls://`, `mariadb://` | mysql |
|
|
152
|
+
* | `sqlite://`, `sqlite:`, `file://`, `file:`, `:memory:` | sqlite |
|
|
153
|
+
*
|
|
154
|
+
* Unknown protocols throw. Plain `*.db` / `*.sqlite` paths are treated as sqlite
|
|
155
|
+
* (Mesh convenience; Bun would require an explicit adapter for bare filenames).
|
|
156
|
+
*/
|
|
157
|
+
export function inferAdapterFromConnectionString(location: string): SqlAdapter {
|
|
158
|
+
const trimmed = location.trim();
|
|
159
|
+
const lower = trimmed.toLowerCase();
|
|
160
|
+
|
|
161
|
+
if (lower === ':memory:' || lower.startsWith('sqlite:') || lower.startsWith('file:')) {
|
|
162
|
+
return 'sqlite';
|
|
163
|
+
}
|
|
164
|
+
if (
|
|
165
|
+
lower.startsWith('mysql://') ||
|
|
166
|
+
lower.startsWith('mysql2://') ||
|
|
167
|
+
lower.startsWith('mysqls://') ||
|
|
168
|
+
lower.startsWith('mariadb://')
|
|
169
|
+
) {
|
|
170
|
+
return 'mysql';
|
|
171
|
+
}
|
|
172
|
+
if (lower.startsWith('postgres://') || lower.startsWith('postgresql://')) {
|
|
173
|
+
return 'postgres';
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Explicit unknown scheme with :// (e.g. pg://, sqlite3://) — match Bun and throw.
|
|
177
|
+
// Do not treat `localhost:5432/db` or `user:pass@host/db` as schemes.
|
|
178
|
+
const schemeMatch = /^([a-z][a-z0-9+.-]*):\/\//i.exec(trimmed);
|
|
179
|
+
if (schemeMatch) {
|
|
180
|
+
const scheme = schemeMatch[1].toLowerCase();
|
|
181
|
+
throw new Error(
|
|
182
|
+
`Unsupported SQL connection protocol "${scheme}:" in ${location}. Use postgres(ql)://, mysql(2|s)://, mariadb://, sqlite://, or file:.`,
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// No protocol: Bun defaults to postgres for host-style strings.
|
|
187
|
+
// Mesh also accepts sqlite file paths (extension / relative path without host:port).
|
|
188
|
+
if (looksLikeSqliteFile(trimmed)) {
|
|
189
|
+
return 'sqlite';
|
|
190
|
+
}
|
|
191
|
+
return 'postgres';
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function looksLikeSqliteFile(location: string): boolean {
|
|
195
|
+
const lower = location.toLowerCase();
|
|
196
|
+
if (
|
|
197
|
+
lower.endsWith('.db') ||
|
|
198
|
+
lower.endsWith('.sqlite') ||
|
|
199
|
+
lower.endsWith('.sqlite3') ||
|
|
200
|
+
lower === ':memory:'
|
|
201
|
+
) {
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
// Relative/absolute path without a host:port authority
|
|
205
|
+
if (location.startsWith('./') || location.startsWith('../') || location.startsWith('/')) {
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
// Windows drive path
|
|
209
|
+
if (/^[a-zA-Z]:[\\/]/.test(location)) {
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function parseSqlUri(location: string): ParsedSqlUri {
|
|
216
|
+
const searchParams: Record<string, string> = {};
|
|
217
|
+
const adapter = inferAdapterFromConnectionString(location);
|
|
218
|
+
|
|
219
|
+
if (adapter === 'sqlite') {
|
|
220
|
+
let filePath = location.trim();
|
|
221
|
+
if (filePath.toLowerCase().startsWith('file:')) filePath = filePath.slice('file:'.length);
|
|
222
|
+
else if (filePath.toLowerCase().startsWith('sqlite:')) {
|
|
223
|
+
filePath = filePath.slice('sqlite:'.length);
|
|
224
|
+
// sqlite://path vs sqlite:path
|
|
225
|
+
if (filePath.startsWith('//')) {
|
|
226
|
+
// sqlite:///abs → /abs ; sqlite://rel → keep //rel then strip one slash pair for URL form
|
|
227
|
+
if (filePath.startsWith('///')) {
|
|
228
|
+
filePath = filePath.slice(2); // ///abs → /abs
|
|
229
|
+
} else {
|
|
230
|
+
filePath = filePath.slice(2); // //rel.db → rel.db
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (filePath.startsWith('//') && !filePath.startsWith('///')) {
|
|
235
|
+
// file://absolute leftover
|
|
236
|
+
filePath = filePath.replace(/^\/\/+/, '/');
|
|
237
|
+
}
|
|
238
|
+
// Query string on sqlite URLs (mode=ro etc.) — strip for path, keep in searchParams
|
|
239
|
+
const qIdx = filePath.indexOf('?');
|
|
240
|
+
if (qIdx !== -1) {
|
|
241
|
+
const qs = new URLSearchParams(filePath.slice(qIdx + 1));
|
|
242
|
+
qs.forEach((value, key) => {
|
|
243
|
+
searchParams[key] = value;
|
|
244
|
+
});
|
|
245
|
+
filePath = filePath.slice(0, qIdx);
|
|
246
|
+
}
|
|
247
|
+
return { adapter: 'sqlite', filePath, searchParams };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// URL parser needs a known scheme; normalize aliases Bun also accepts.
|
|
251
|
+
let normalized = location.trim();
|
|
252
|
+
const wantsMysqlSsl = /^mysqls:/i.test(normalized);
|
|
253
|
+
normalized = normalized
|
|
254
|
+
.replace(/^mysqls:/i, 'mysql:')
|
|
255
|
+
.replace(/^mysql2:/i, 'mysql:')
|
|
256
|
+
.replace(/^mariadb:/i, 'mysql:');
|
|
257
|
+
|
|
258
|
+
// Host-style with no protocol → Bun treats as postgres
|
|
259
|
+
if (!/^[a-z][a-z0-9+.-]*:\/\//i.test(normalized) && adapter === 'postgres') {
|
|
260
|
+
normalized = `postgres://${normalized}`;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const url = new URL(normalized);
|
|
264
|
+
url.searchParams.forEach((value, key) => {
|
|
265
|
+
searchParams[key] = value;
|
|
266
|
+
});
|
|
267
|
+
const database = url.pathname.replace(/^\//, '') || undefined;
|
|
268
|
+
|
|
269
|
+
if (adapter === 'postgres') {
|
|
270
|
+
return {
|
|
271
|
+
adapter: 'postgres',
|
|
272
|
+
user: decode(url.username) || undefined,
|
|
273
|
+
password: decode(url.password) || undefined,
|
|
274
|
+
host: url.hostname || 'localhost',
|
|
275
|
+
port: url.port ? Number(url.port) : 5432,
|
|
276
|
+
database,
|
|
277
|
+
ssl: searchParams.ssl === 'true' || searchParams.sslmode === 'require',
|
|
278
|
+
searchParams,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// mysql / mysql2 / mariadb / mysqls
|
|
283
|
+
return {
|
|
284
|
+
adapter: 'mysql',
|
|
285
|
+
user: decode(url.username) || undefined,
|
|
286
|
+
password: decode(url.password) || undefined,
|
|
287
|
+
host: url.hostname || 'localhost',
|
|
288
|
+
port: url.port ? Number(url.port) : 3306,
|
|
289
|
+
database,
|
|
290
|
+
ssl: wantsMysqlSsl || searchParams.ssl === 'true',
|
|
291
|
+
searchParams,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function decode(value: string): string {
|
|
296
|
+
try {
|
|
297
|
+
return decodeURIComponent(value);
|
|
298
|
+
} catch {
|
|
299
|
+
return value;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { isBunSqlAvailable } from '../src/client.js';
|
|
2
|
+
|
|
3
|
+
describe('Bun.SQL availability (Node / Jest)', () => {
|
|
4
|
+
it('is unavailable under plain Node (native coverage lives in client.bun.test.ts)', () => {
|
|
5
|
+
// Jest runs on Node; Bun.SQL only exists when the process is Bun.
|
|
6
|
+
expect(isBunSqlAvailable()).toBe(false);
|
|
7
|
+
});
|
|
8
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native Bun.SQL coverage — run with Bun itself:
|
|
3
|
+
* bun test packages/transports/sql/test/client.bun.test.ts
|
|
4
|
+
*
|
|
5
|
+
* Excluded from Jest (Node) via jest.config.js.
|
|
6
|
+
*/
|
|
7
|
+
import { createSqlClient, isBunSqlAvailable } from '../src/client.ts';
|
|
8
|
+
|
|
9
|
+
describe('createSqlClient under Bun.SQL', () => {
|
|
10
|
+
it('detects Bun.SQL', () => {
|
|
11
|
+
expect(isBunSqlAvailable()).toBe(true);
|
|
12
|
+
expect(typeof Bun.SQL).toBe('function');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('queries sqlite via native Bun.SQL', async () => {
|
|
16
|
+
const client = createSqlClient({
|
|
17
|
+
adapter: 'sqlite',
|
|
18
|
+
location: ':memory:',
|
|
19
|
+
});
|
|
20
|
+
expect(client.adapter).toBe('sqlite');
|
|
21
|
+
|
|
22
|
+
await client.query('CREATE TABLE artists (id INTEGER PRIMARY KEY, name TEXT)');
|
|
23
|
+
await client.query('INSERT INTO artists (name) VALUES (?), (?)', ['AC/DC', 'Accept']);
|
|
24
|
+
const result = await client.query<{ id: number; name: string }>(
|
|
25
|
+
'SELECT id, name FROM artists WHERE id = ?',
|
|
26
|
+
[1],
|
|
27
|
+
);
|
|
28
|
+
expect(result.rows).toEqual([{ id: 1, name: 'AC/DC' }]);
|
|
29
|
+
expect(result.rowCount).toBe(1);
|
|
30
|
+
|
|
31
|
+
await client.dispose();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('honours preferNode and uses node:sqlite instead of Bun.SQL', async () => {
|
|
35
|
+
const client = createSqlClient({
|
|
36
|
+
adapter: 'sqlite',
|
|
37
|
+
location: ':memory:',
|
|
38
|
+
preferNode: true,
|
|
39
|
+
});
|
|
40
|
+
await client.query('CREATE TABLE t (id INTEGER PRIMARY KEY)');
|
|
41
|
+
await client.query('INSERT INTO t DEFAULT VALUES');
|
|
42
|
+
const result = await client.query('SELECT id FROM t');
|
|
43
|
+
expect(result.rows).toEqual([{ id: 1 }]);
|
|
44
|
+
await client.dispose();
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { parse } from 'graphql';
|
|
3
|
+
import { loadGraphQLSchemaFromSql } from '@omnigraph/sql';
|
|
4
|
+
import { getSqlExecutor } from '../src/execution.js';
|
|
5
|
+
import type { SqlClient } from '../src/types.js';
|
|
6
|
+
|
|
7
|
+
const dbPath = join(__dirname, '../../../../e2e/sqlite-chinook/chinook.db');
|
|
8
|
+
|
|
9
|
+
describe('getSqlExecutor sqlite', () => {
|
|
10
|
+
it('resolves albums with artist relation', async () => {
|
|
11
|
+
const schema = await loadGraphQLSchemaFromSql('chinook', {
|
|
12
|
+
endpoint: dbPath,
|
|
13
|
+
includeTables: ['albums', 'artists'],
|
|
14
|
+
});
|
|
15
|
+
const executor = await getSqlExecutor({ subgraph: schema });
|
|
16
|
+
try {
|
|
17
|
+
const result = await executor({
|
|
18
|
+
document: parse(/* GraphQL */ `
|
|
19
|
+
query {
|
|
20
|
+
albums(limit: 2) {
|
|
21
|
+
id
|
|
22
|
+
title
|
|
23
|
+
artist {
|
|
24
|
+
name
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
`),
|
|
29
|
+
});
|
|
30
|
+
expect(result.errors).toBeUndefined();
|
|
31
|
+
expect(result.data).toEqual({
|
|
32
|
+
albums: [
|
|
33
|
+
{
|
|
34
|
+
id: '1',
|
|
35
|
+
title: 'For Those About To Rock We Salute You',
|
|
36
|
+
artist: { name: 'AC/DC' },
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
id: '2',
|
|
40
|
+
title: 'Balls to the Wall',
|
|
41
|
+
artist: { name: 'Accept' },
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
});
|
|
45
|
+
} finally {
|
|
46
|
+
await (executor as any)[Symbol.asyncDispose]?.();
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('resolves Federation _entities via type-level @sql', async () => {
|
|
51
|
+
const schema = await loadGraphQLSchemaFromSql('chinook', {
|
|
52
|
+
endpoint: dbPath,
|
|
53
|
+
includeTables: ['albums', 'artists'],
|
|
54
|
+
});
|
|
55
|
+
const executor = await getSqlExecutor({ subgraph: schema });
|
|
56
|
+
try {
|
|
57
|
+
const result = await executor({
|
|
58
|
+
document: parse(/* GraphQL */ `
|
|
59
|
+
query ($representations: [_Any!]!) {
|
|
60
|
+
_entities(representations: $representations) {
|
|
61
|
+
... on Album {
|
|
62
|
+
id
|
|
63
|
+
title
|
|
64
|
+
}
|
|
65
|
+
... on Artist {
|
|
66
|
+
id
|
|
67
|
+
name
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
`),
|
|
72
|
+
variables: {
|
|
73
|
+
representations: [
|
|
74
|
+
{ __typename: 'Album', id: '1' },
|
|
75
|
+
{ __typename: 'Artist', id: '1' },
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
expect(result.errors).toBeUndefined();
|
|
80
|
+
expect(result.data).toEqual({
|
|
81
|
+
_entities: [
|
|
82
|
+
{ id: '1', title: 'For Those About To Rock We Salute You' },
|
|
83
|
+
{ id: '1', name: 'AC/DC' },
|
|
84
|
+
],
|
|
85
|
+
});
|
|
86
|
+
} finally {
|
|
87
|
+
await (executor as any)[Symbol.asyncDispose]?.();
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('folds belongs-to artist into one JOIN (lookahead plan)', async () => {
|
|
92
|
+
const schema = await loadGraphQLSchemaFromSql('chinook', {
|
|
93
|
+
endpoint: dbPath,
|
|
94
|
+
includeTables: ['albums', 'artists'],
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const { createSqlClient } = await import('../src/client.js');
|
|
98
|
+
const real = createSqlClient({
|
|
99
|
+
adapter: 'sqlite',
|
|
100
|
+
location: `file:${dbPath}`,
|
|
101
|
+
});
|
|
102
|
+
const queries: string[] = [];
|
|
103
|
+
const spyClient: SqlClient = {
|
|
104
|
+
adapter: 'sqlite',
|
|
105
|
+
async query(sql, params) {
|
|
106
|
+
queries.push(sql);
|
|
107
|
+
return real.query(sql, params);
|
|
108
|
+
},
|
|
109
|
+
dispose: () => real.dispose(),
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const executor = await getSqlExecutor({ subgraph: schema, client: spyClient });
|
|
113
|
+
try {
|
|
114
|
+
const result = await executor({
|
|
115
|
+
document: parse(/* GraphQL */ `
|
|
116
|
+
query {
|
|
117
|
+
albums(limit: 2) {
|
|
118
|
+
id
|
|
119
|
+
artist {
|
|
120
|
+
name
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
`),
|
|
125
|
+
context: {},
|
|
126
|
+
});
|
|
127
|
+
expect(result.errors).toBeUndefined();
|
|
128
|
+
expect(result.data).toEqual({
|
|
129
|
+
albums: [
|
|
130
|
+
{
|
|
131
|
+
id: '1',
|
|
132
|
+
artist: { name: 'AC/DC' },
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
id: '2',
|
|
136
|
+
artist: { name: 'Accept' },
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
});
|
|
140
|
+
// Single root query with LEFT JOIN — no separate artists round-trip
|
|
141
|
+
expect(queries.length).toBe(1);
|
|
142
|
+
expect(queries[0]).toMatch(/LEFT JOIN/i);
|
|
143
|
+
expect(queries[0]).toMatch(/artists/i);
|
|
144
|
+
} finally {
|
|
145
|
+
await (executor as any)[Symbol.asyncDispose]?.();
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('folds has-many albums into one JSON subselect', async () => {
|
|
150
|
+
const schema = await loadGraphQLSchemaFromSql('chinook', {
|
|
151
|
+
endpoint: dbPath,
|
|
152
|
+
includeTables: ['albums', 'artists'],
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const { createSqlClient } = await import('../src/client.js');
|
|
156
|
+
const real = createSqlClient({
|
|
157
|
+
adapter: 'sqlite',
|
|
158
|
+
location: `file:${dbPath}`,
|
|
159
|
+
});
|
|
160
|
+
const queries: string[] = [];
|
|
161
|
+
const spyClient: SqlClient = {
|
|
162
|
+
adapter: 'sqlite',
|
|
163
|
+
async query(sql, params) {
|
|
164
|
+
queries.push(sql);
|
|
165
|
+
return real.query(sql, params);
|
|
166
|
+
},
|
|
167
|
+
dispose: () => real.dispose(),
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const executor = await getSqlExecutor({ subgraph: schema, client: spyClient });
|
|
171
|
+
try {
|
|
172
|
+
const result = await executor({
|
|
173
|
+
document: parse(/* GraphQL */ `
|
|
174
|
+
query {
|
|
175
|
+
artists(limit: 2) {
|
|
176
|
+
id
|
|
177
|
+
albums(limit: 5) {
|
|
178
|
+
title
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
`),
|
|
183
|
+
context: {},
|
|
184
|
+
});
|
|
185
|
+
expect(result.errors).toBeUndefined();
|
|
186
|
+
expect(queries.length).toBe(1);
|
|
187
|
+
expect(queries[0]).toMatch(/json_group_array/i);
|
|
188
|
+
expect(result.data).toEqual({
|
|
189
|
+
artists: [
|
|
190
|
+
{
|
|
191
|
+
id: '1',
|
|
192
|
+
albums: [
|
|
193
|
+
{ title: 'For Those About To Rock We Salute You' },
|
|
194
|
+
{ title: 'Let There Be Rock' },
|
|
195
|
+
],
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
id: '2',
|
|
199
|
+
albums: [{ title: 'Balls to the Wall' }, { title: 'Restless and Wild' }],
|
|
200
|
+
},
|
|
201
|
+
],
|
|
202
|
+
});
|
|
203
|
+
} finally {
|
|
204
|
+
await (executor as any)[Symbol.asyncDispose]?.();
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('folds multi-hop track→album→artist into one JOIN chain', async () => {
|
|
209
|
+
const schema = await loadGraphQLSchemaFromSql('chinook', {
|
|
210
|
+
endpoint: dbPath,
|
|
211
|
+
includeTables: ['albums', 'artists', 'tracks', 'genres'],
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
const { createSqlClient } = await import('../src/client.js');
|
|
215
|
+
const real = createSqlClient({
|
|
216
|
+
adapter: 'sqlite',
|
|
217
|
+
location: `file:${dbPath}`,
|
|
218
|
+
});
|
|
219
|
+
const queries: string[] = [];
|
|
220
|
+
const spyClient: SqlClient = {
|
|
221
|
+
adapter: 'sqlite',
|
|
222
|
+
async query(sql, params) {
|
|
223
|
+
queries.push(sql);
|
|
224
|
+
return real.query(sql, params);
|
|
225
|
+
},
|
|
226
|
+
dispose: () => real.dispose(),
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const executor = await getSqlExecutor({ subgraph: schema, client: spyClient });
|
|
230
|
+
try {
|
|
231
|
+
const result = await executor({
|
|
232
|
+
document: parse(/* GraphQL */ `
|
|
233
|
+
query {
|
|
234
|
+
tracks(limit: 2) {
|
|
235
|
+
name
|
|
236
|
+
album {
|
|
237
|
+
title
|
|
238
|
+
artist {
|
|
239
|
+
name
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
genre {
|
|
243
|
+
name
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
`),
|
|
248
|
+
context: {},
|
|
249
|
+
});
|
|
250
|
+
expect(result.errors).toBeUndefined();
|
|
251
|
+
expect(queries.length).toBe(1);
|
|
252
|
+
expect(queries[0]).toMatch(/LEFT JOIN/i);
|
|
253
|
+
expect(result.data).toEqual({
|
|
254
|
+
tracks: [
|
|
255
|
+
{
|
|
256
|
+
name: 'For Those About To Rock (We Salute You)',
|
|
257
|
+
album: {
|
|
258
|
+
title: 'For Those About To Rock We Salute You',
|
|
259
|
+
artist: { name: 'AC/DC' },
|
|
260
|
+
},
|
|
261
|
+
genre: { name: 'Rock' },
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
name: 'Balls to the Wall',
|
|
265
|
+
album: {
|
|
266
|
+
title: 'Balls to the Wall',
|
|
267
|
+
artist: { name: 'Accept' },
|
|
268
|
+
},
|
|
269
|
+
genre: { name: 'Rock' },
|
|
270
|
+
},
|
|
271
|
+
],
|
|
272
|
+
});
|
|
273
|
+
} finally {
|
|
274
|
+
await (executor as any)[Symbol.asyncDispose]?.();
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
});
|