@@ -4,6 +4,10 @@ import { DDL } from "./ddl.ts";
44import type { Class , Identifiable , Parameter , Row , Schema } from "./types.ts" ;
55import { Repository } from "./repository.ts" ;
66
7+ // Import Driver Types
8+ import type { Database as SQLite } from "jsr:@db/sqlite" ;
9+ import type { Client as Postgres } from "jsr:@dewars/postgres" ;
10+
711const TTY = Deno . stderr . isTerminal ( ) ;
812
913// See https://stackoverflow.com/questions/49285864/is-there-a-valueof-similar-to-keyof-in-typescript
@@ -25,12 +29,17 @@ const Hook = {
2529
2630const Provider = {
2731 MYSQL : "mysql" ,
28- MYSQL2 : "mysql2" ,
2932 POSTGRES : "postgres" ,
3033 SQLITE : "sqlite" ,
3134} as const ;
3235type Provider = Values < typeof Provider > ;
3336
37+ const Drivers = {
38+ MYSQL : "npm:mysql2@^3/promise" ,
39+ POSTGRES : "jsr:@dewars/postgres@0" ,
40+ SQLITE : "jsr:@db/sqlite@0" ,
41+ } as const ;
42+
3443export interface Client {
3544 type : string ;
3645 close ( ) : Promise < void > ;
@@ -59,29 +68,8 @@ async function connect(config: ClientConfig): Promise<Client> {
5968
6069 // MySQL
6170 if ( config . type === Provider . MYSQL ) {
62- const mysql = await import ( "https://deno.land/x/mysql@v2.12.1/mod.ts" ) ;
63- if ( ! config . debug ) await mysql . configLogger ( { enable : false } ) ;
64- config = Object . assign ( config , { db : config . database } ) ;
65- if ( ! config . charset ) config . charset = "utf8mb4" ;
66- const nativeClient = await new mysql . Client ( ) . connect ( config ) ;
67- return new class implements Client {
68- type = config . type ;
69- close ( ) {
70- return nativeClient . close ( ) ;
71- }
72- execute ( sql : string , parameters ?: Parameter [ ] ) {
73- return nativeClient . execute ( sql , parameters ) ;
74- }
75- query ( sql : string , parameters ?: Parameter [ ] ) {
76- return nativeClient . query ( sql , parameters ) ;
77- }
78- } ( ) ;
79- }
80-
81- // MySQL2
82- if ( config . type === Provider . MYSQL2 ) {
83- const mysql2 = await import ( "npm:mysql2@^3.11/promise" ) ;
84- const nativeClient = await mysql2 . createConnection ( {
71+ const mysql = await import ( Drivers . MYSQL ) ;
72+ const nativeClient = await mysql . createConnection ( {
8573 host : config . hostname ?? "127.0.0.1" ,
8674 database : config . database ,
8775 user : config . username ,
@@ -91,32 +79,29 @@ async function connect(config: ClientConfig): Promise<Client> {
9179 return new class implements Client {
9280 type = config . type ;
9381 close ( ) {
94- // TODO
95- return Promise . resolve ( ) ;
82+ return nativeClient . close ( ) ;
9683 }
9784 async execute ( sql : string , parameters ?: Parameter [ ] ) {
98- // deno-lint-ignore no-explicit-any
99- const [ rsh ] = await ( nativeClient as any ) . execute ( sql , parameters ) ;
85+ const [ rsh ] = await nativeClient . execute ( sql , parameters ) ;
10086 // deno-lint-ignore no-explicit-any
10187 return { affectedRows : ( rsh as any ) . affectedRows , lastInsertId : ( rsh as any ) . insertId } ;
10288 }
10389 async query ( sql : string , parameters ?: Parameter [ ] ) {
104- // deno-lint-ignore no-explicit-any
105- const [ rows ] = await ( nativeClient as any ) . query ( sql , parameters ) ;
90+ const [ rows ] = await nativeClient . query ( sql , parameters ) ;
10691 return rows as Row [ ] ;
10792 }
10893 } ( ) ;
10994 }
11095
11196 // Postgres
11297 if ( config . type === Provider . POSTGRES ) {
113- const postgres = await import ( "https://deno.land/x/postgres@v0.19.3/mod.ts" ) ;
98+ const postgres = await import ( Drivers . POSTGRES ) ;
11499 config = Object . assign ( config , { user : config . username } ) ;
115- const nativeClient = await new postgres . Pool ( config , config . poolSize ?? 1 ) . connect ( ) ;
100+ const nativeClient = await new postgres . Pool ( config , config . poolSize ?? 1 ) . connect ( ) as Postgres ;
116101 return new class implements Client {
117102 type = config . type ;
118103 close ( ) {
119- return Promise . resolve ( ) ;
104+ return nativeClient . end ( ) ;
120105 }
121106 async execute ( sql : string , parameters ?: Parameter [ ] ) {
122107 const qar = await nativeClient . queryArray ( sql , parameters ) ;
@@ -131,22 +116,19 @@ async function connect(config: ClientConfig): Promise<Client> {
131116
132117 // Sqlite
133118 if ( config . type === Provider . SQLITE ) {
134- const sqlite = await import ( "https://deno.land/x/sqlite@v3.9.1/mod.ts" ) ;
135- const nativeClient = new sqlite . DB ( config . database ?? Deno . env . get ( "DB_FILE" ) ?? ":memory:" ) ;
119+ const sqlite = await import ( Drivers . SQLITE ) ;
120+ const nativeClient = new sqlite . Database ( config . database ?? Deno . env . get ( "DB_FILE" ) ?? ":memory:" ) as SQLite ;
136121 return new class implements Client {
137122 type = config . type ;
138123 close ( ) {
139- nativeClient . close ( ) ;
140- return Promise . resolve ( ) ;
124+ return Promise . resolve ( nativeClient . close ( ) ) ;
141125 }
142126 execute ( sql : string , parameters ?: Parameter [ ] ) {
143- // deno-lint-ignore no-explicit-any
144- nativeClient . query ( sql , parameters as any ) ;
127+ nativeClient . exec ( sql , parameters ) ;
145128 return Promise . resolve ( { affectedRows : nativeClient . changes , lastInsertId : nativeClient . lastInsertRowId } ) ;
146129 }
147130 query ( sql : string , parameters ?: Parameter [ ] ) {
148- // deno-lint-ignore no-explicit-any
149- return Promise . resolve ( nativeClient . queryEntries ( sql , parameters as any ) ) ;
131+ return Promise . resolve ( nativeClient . prepare ( sql ) . all ( parameters ) ) ;
150132 }
151133 } ( ) ;
152134 }
0 commit comments