@onreza/prisma-adapter-bun 0.3.2 → 0.4.1
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/package.json +4 -5
- package/src/conversion.ts +17 -0
- package/src/factory.ts +10 -1
- package/src/validation.ts +86 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onreza/prisma-adapter-bun",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Prisma 7+ driver adapter for Bun.sql — use Bun's built-in PostgreSQL client instead of pg",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -21,9 +21,7 @@
|
|
|
21
21
|
"engines": {
|
|
22
22
|
"bun": ">=1.2.0"
|
|
23
23
|
},
|
|
24
|
-
"dependencies": {
|
|
25
|
-
"@prisma/driver-adapter-utils": "^7.3.0"
|
|
26
|
-
},
|
|
24
|
+
"dependencies": {},
|
|
27
25
|
"devDependencies": {
|
|
28
26
|
"@biomejs/biome": "^2.3.14",
|
|
29
27
|
"@commitlint/types": "^20.4.0",
|
|
@@ -34,6 +32,7 @@
|
|
|
34
32
|
"prisma": "^7.3.0"
|
|
35
33
|
},
|
|
36
34
|
"peerDependencies": {
|
|
35
|
+
"@prisma/driver-adapter-utils": "^7.0.0",
|
|
37
36
|
"typescript": "^5"
|
|
38
37
|
},
|
|
39
38
|
"scripts": {
|
|
@@ -42,7 +41,7 @@
|
|
|
42
41
|
"format": "biome format --write",
|
|
43
42
|
"typecheck": "tsc --noEmit",
|
|
44
43
|
"check": "biome check && tsc --noEmit",
|
|
45
|
-
"test": "bun test tests/conversion.test.ts tests/errors.test.ts",
|
|
44
|
+
"test": "bun test tests/conversion.test.ts tests/errors.test.ts tests/validation.test.ts",
|
|
46
45
|
"test:integration": "bun test tests/adapter.test.ts",
|
|
47
46
|
"test:e2e": "bun test --timeout 120000 tests/factory.test.ts tests/prisma.test.ts",
|
|
48
47
|
"test:all": "bun test --timeout 120000",
|
package/src/conversion.ts
CHANGED
|
@@ -302,6 +302,22 @@ function inferArrayOid(arr: unknown[]): number {
|
|
|
302
302
|
return PgOid.TEXT_ARRAY;
|
|
303
303
|
}
|
|
304
304
|
|
|
305
|
+
/**
|
|
306
|
+
* Detect whether a string value is a JSON object or array.
|
|
307
|
+
* Used to distinguish JSON columns (returned as strings by Bun.sql
|
|
308
|
+
* in relation joins) from plain TEXT columns.
|
|
309
|
+
*/
|
|
310
|
+
function isJsonString(value: string): boolean {
|
|
311
|
+
const ch = value.charAt(0);
|
|
312
|
+
if (ch !== "{" && ch !== "[") return false;
|
|
313
|
+
try {
|
|
314
|
+
const parsed = JSON.parse(value);
|
|
315
|
+
return typeof parsed === "object" && parsed !== null;
|
|
316
|
+
} catch {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
305
321
|
/**
|
|
306
322
|
* Infer a PostgreSQL OID from a JavaScript value.
|
|
307
323
|
* Used as a fallback when Bun.sql doesn't expose column metadata.
|
|
@@ -318,6 +334,7 @@ export function inferOidFromValue(value: unknown): number {
|
|
|
318
334
|
if (value instanceof Uint8Array || value instanceof Buffer) return PgOid.BYTEA;
|
|
319
335
|
if (Array.isArray(value)) return inferArrayOid(value);
|
|
320
336
|
if (typeof value === "object") return PgOid.JSONB;
|
|
337
|
+
if (typeof value === "string" && isJsonString(value)) return PgOid.JSON;
|
|
321
338
|
return PgOid.TEXT;
|
|
322
339
|
}
|
|
323
340
|
|
package/src/factory.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { SqlDriverAdapter, SqlMigrationAwareDriverAdapterFactory } from "@p
|
|
|
2
2
|
import { SQL } from "bun";
|
|
3
3
|
import { PrismaBunAdapter } from "./adapter.ts";
|
|
4
4
|
import type { BunSqlConfig, PrismaBunOptions } from "./types.ts";
|
|
5
|
+
import { validateConnectionUrl } from "./validation.ts";
|
|
5
6
|
|
|
6
7
|
const ADAPTER_NAME = "@onreza/prisma-adapter-bun";
|
|
7
8
|
|
|
@@ -30,12 +31,20 @@ export class PrismaBunFactory implements SqlMigrationAwareDriverAdapterFactory {
|
|
|
30
31
|
constructor(config: BunSqlConfig, options?: PrismaBunOptions);
|
|
31
32
|
constructor(client: SQL, options?: PrismaBunOptions);
|
|
32
33
|
constructor(configOrClient: BunSqlConfig | SQL, options?: PrismaBunOptions) {
|
|
33
|
-
this.bunOptions = options;
|
|
34
34
|
if (isSqlClient(configOrClient)) {
|
|
35
35
|
this.externalClient = configOrClient;
|
|
36
36
|
this.config = {};
|
|
37
|
+
this.bunOptions = options;
|
|
38
|
+
} else if (typeof configOrClient === "string" || configOrClient instanceof URL) {
|
|
39
|
+
const validated = validateConnectionUrl(configOrClient);
|
|
40
|
+
this.config = validated.url;
|
|
41
|
+
this.bunOptions = {
|
|
42
|
+
...options,
|
|
43
|
+
schema: options?.schema ?? validated.schema,
|
|
44
|
+
};
|
|
37
45
|
} else {
|
|
38
46
|
this.config = configOrClient;
|
|
47
|
+
this.bunOptions = options;
|
|
39
48
|
}
|
|
40
49
|
}
|
|
41
50
|
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
export type ValidatedUrl = {
|
|
2
|
+
url: string;
|
|
3
|
+
schema: string | undefined;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Standard libpq connection parameters from PostgreSQL 17 documentation.
|
|
8
|
+
* @see https://postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
|
|
9
|
+
*/
|
|
10
|
+
const VALID_LIBPQ_PARAMS: ReadonlySet<string> = new Set([
|
|
11
|
+
"host",
|
|
12
|
+
"hostaddr",
|
|
13
|
+
"port",
|
|
14
|
+
"dbname",
|
|
15
|
+
"user",
|
|
16
|
+
"password",
|
|
17
|
+
"passfile",
|
|
18
|
+
"connect_timeout",
|
|
19
|
+
"client_encoding",
|
|
20
|
+
"application_name",
|
|
21
|
+
"fallback_application_name",
|
|
22
|
+
"keepalives",
|
|
23
|
+
"keepalives_idle",
|
|
24
|
+
"keepalives_interval",
|
|
25
|
+
"keepalives_count",
|
|
26
|
+
"tcp_user_timeout",
|
|
27
|
+
"sslmode",
|
|
28
|
+
"sslcert",
|
|
29
|
+
"sslkey",
|
|
30
|
+
"sslrootcert",
|
|
31
|
+
"sslpassword",
|
|
32
|
+
"sslcompression",
|
|
33
|
+
"sslcertmode",
|
|
34
|
+
"sslkeylogfile",
|
|
35
|
+
"sslcrl",
|
|
36
|
+
"sslcrldir",
|
|
37
|
+
"sslsni",
|
|
38
|
+
"ssl_min_protocol_version",
|
|
39
|
+
"ssl_max_protocol_version",
|
|
40
|
+
"sslnegotiation",
|
|
41
|
+
"require_auth",
|
|
42
|
+
"channel_binding",
|
|
43
|
+
"krbsrvname",
|
|
44
|
+
"gssencmode",
|
|
45
|
+
"gsslib",
|
|
46
|
+
"gssdelegation",
|
|
47
|
+
"options",
|
|
48
|
+
"replication",
|
|
49
|
+
"target_session_attrs",
|
|
50
|
+
"load_balance_hosts",
|
|
51
|
+
"service",
|
|
52
|
+
"requirepeer",
|
|
53
|
+
"min_protocol_version",
|
|
54
|
+
"max_protocol_version",
|
|
55
|
+
"oauth_issuer",
|
|
56
|
+
"oauth_client_id",
|
|
57
|
+
"oauth_client_secret",
|
|
58
|
+
"oauth_scope",
|
|
59
|
+
"scram_client_key",
|
|
60
|
+
"scram_server_key",
|
|
61
|
+
]);
|
|
62
|
+
|
|
63
|
+
export function validateConnectionUrl(input: string | URL): ValidatedUrl {
|
|
64
|
+
const url = new URL(input.toString());
|
|
65
|
+
|
|
66
|
+
let schema: string | undefined;
|
|
67
|
+
const keysToDelete: string[] = [];
|
|
68
|
+
|
|
69
|
+
for (const [key, value] of url.searchParams) {
|
|
70
|
+
if (key === "schema") {
|
|
71
|
+
schema = value || undefined;
|
|
72
|
+
keysToDelete.push(key);
|
|
73
|
+
} else if (!VALID_LIBPQ_PARAMS.has(key)) {
|
|
74
|
+
console.warn(
|
|
75
|
+
`[@onreza/prisma-adapter-bun] Non-standard PostgreSQL connection parameter "${key}" was removed from URL. Use only standard libpq parameters: https://postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS`,
|
|
76
|
+
);
|
|
77
|
+
keysToDelete.push(key);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
for (const key of keysToDelete) {
|
|
82
|
+
url.searchParams.delete(key);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return { schema, url: url.toString() };
|
|
86
|
+
}
|