@sqldoc/db-postgres 0.0.10
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 +25 -0
- package/src/index.ts +47 -0
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "module",
|
|
3
|
+
"name": "@sqldoc/db-postgres",
|
|
4
|
+
"version": "0.0.10",
|
|
5
|
+
"description": "PostgreSQL adapter for sqldoc (Node.js, uses postgres.js)",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./src/index.ts",
|
|
9
|
+
"import": "./src/index.ts",
|
|
10
|
+
"default": "./src/index.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"main": "./src/index.ts",
|
|
14
|
+
"types": "./src/index.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"src",
|
|
17
|
+
"package.json"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"postgres": "^3.4.5"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@sqldoc/db": "0.0.10"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { AdapterPluginContext, DatabaseAdapter, DatabaseAdapterPlugin, ExecResult, QueryResult } from '@sqldoc/db'
|
|
2
|
+
import { normalizeValue } from '@sqldoc/db'
|
|
3
|
+
import postgres from 'postgres'
|
|
4
|
+
|
|
5
|
+
const plugin: DatabaseAdapterPlugin = {
|
|
6
|
+
apiVersion: 1,
|
|
7
|
+
name: 'postgres',
|
|
8
|
+
schemes: ['postgres', 'postgresql'],
|
|
9
|
+
dialects: ['postgres'],
|
|
10
|
+
runtime: 'node',
|
|
11
|
+
|
|
12
|
+
async createAdapter(connectionString: string, _context: AdapterPluginContext): Promise<DatabaseAdapter> {
|
|
13
|
+
const sql = postgres(connectionString, { connect_timeout: 5, onnotice: () => {} })
|
|
14
|
+
|
|
15
|
+
// Verify the connection works — postgres.js connects lazily
|
|
16
|
+
try {
|
|
17
|
+
await sql`SELECT 1`
|
|
18
|
+
} catch (err) {
|
|
19
|
+
await sql.end()
|
|
20
|
+
throw err
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
async query(queryText: string, args?: unknown[]): Promise<QueryResult> {
|
|
25
|
+
const result = await sql.unsafe(queryText, args as any[], { prepare: false }).values()
|
|
26
|
+
if (result.length === 0) {
|
|
27
|
+
return { columns: [], rows: [] }
|
|
28
|
+
}
|
|
29
|
+
const colCount = (result[0] as unknown[]).length
|
|
30
|
+
const columns = result.columns?.map((c: any) => c.name) ?? Array.from({ length: colCount }, (_, i) => `col${i}`)
|
|
31
|
+
return {
|
|
32
|
+
columns,
|
|
33
|
+
rows: (result as unknown[][]).map((row) => row.map((v) => normalizeValue(v))),
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
async exec(queryText: string): Promise<ExecResult> {
|
|
37
|
+
const result = await sql.unsafe(queryText, [], { prepare: false })
|
|
38
|
+
return { rowsAffected: result.count }
|
|
39
|
+
},
|
|
40
|
+
async close(): Promise<void> {
|
|
41
|
+
await sql.end()
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export default plugin
|