-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update drizzle integration
- Loading branch information
Showing
14 changed files
with
270 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
node_modules | ||
coverage | ||
dist | ||
drivers | ||
/server* | ||
/integrations | ||
/connectors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,11 @@ node_modules | |
coverage | ||
dist | ||
tmp | ||
/connectors | ||
/test.* | ||
__* | ||
.data | ||
.tmp | ||
.env | ||
|
||
/connectors | ||
/integrations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
connectors | ||
integrations | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { | ||
entityKind, | ||
Logger, | ||
RelationalSchemaConfig, | ||
type Query, | ||
type TablesRelationalConfig, | ||
NoopLogger, | ||
} from "drizzle-orm"; | ||
|
||
import { | ||
SQLiteAsyncDialect, | ||
SQLiteSession, | ||
SQLitePreparedQuery, | ||
} from "drizzle-orm/sqlite-core"; | ||
|
||
import type { | ||
PreparedQueryConfig, | ||
SelectedFieldsOrdered, | ||
SQLiteExecuteMethod, | ||
SQLiteTransactionConfig, | ||
} from "drizzle-orm/sqlite-core"; | ||
|
||
import type { Database, Statement } from "../../types"; | ||
|
||
// Used as reference: https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/d1/session.ts | ||
|
||
export interface DB0SessionOptions { | ||
logger?: Logger; | ||
} | ||
|
||
export class DB0Session< | ||
TFullSchema extends Record<string, unknown>, | ||
TSchema extends TablesRelationalConfig, | ||
> extends SQLiteSession<"async", unknown, TFullSchema, TSchema> { | ||
dialect: SQLiteAsyncDialect; | ||
|
||
private logger: Logger; | ||
|
||
constructor( | ||
private db: Database, | ||
dialect: SQLiteAsyncDialect, | ||
private schema: RelationalSchemaConfig<TSchema> | undefined, | ||
private options: DB0SessionOptions = {}, | ||
) { | ||
super(dialect); | ||
this.logger = options.logger ?? new NoopLogger(); | ||
} | ||
|
||
prepareQuery( | ||
query: Query, | ||
fields: SelectedFieldsOrdered | undefined, | ||
executeMethod: SQLiteExecuteMethod, | ||
customResultMapper?: (rows: unknown[][]) => unknown, | ||
): DB0PreparedQuery { | ||
const stmt = this.db.prepare(query.sql); | ||
return new DB0PreparedQuery( | ||
stmt, | ||
query, | ||
this.logger, | ||
fields, | ||
executeMethod, | ||
customResultMapper, | ||
); | ||
} | ||
|
||
// TODO: Implement batch | ||
|
||
// TODO: Implement transaction | ||
override transaction<T>( | ||
transaction: (tx: any) => T | Promise<T>, | ||
config?: SQLiteTransactionConfig, | ||
): Promise<T> { | ||
throw new Error("transaction is not implemented!"); | ||
// const tx = new D1Transaction('async', this.dialect, this, this.schema); | ||
// await this.run(sql.raw(`begin${config?.behavior ? ' ' + config.behavior : ''}`)); | ||
// try { | ||
// const result = await transaction(tx); | ||
// await this.run(sql`commit`); | ||
// return result; | ||
// } catch (err) { | ||
// await this.run(sql`rollback`); | ||
// throw err; | ||
// } | ||
} | ||
} | ||
|
||
export class DB0PreparedQuery< | ||
T extends PreparedQueryConfig = PreparedQueryConfig, | ||
> extends SQLitePreparedQuery<{ | ||
type: "async"; | ||
run: Awaited<ReturnType<Statement["run"]>>; | ||
all: T["all"]; | ||
get: T["get"]; | ||
values: T["values"]; | ||
execute: T["execute"]; | ||
}> { | ||
static readonly [entityKind]: string = "DB0PreparedQuery"; | ||
|
||
constructor( | ||
private stmt: Statement, | ||
query: Query, | ||
private logger: Logger, | ||
fields: SelectedFieldsOrdered | undefined, | ||
executeMethod: SQLiteExecuteMethod, | ||
customResultMapper?: (rows: unknown[][]) => unknown, | ||
) { | ||
super("async", executeMethod, query); | ||
} | ||
|
||
run() { | ||
return this.stmt.run(...(this.query.params as any[])); | ||
} | ||
|
||
all() { | ||
return this.stmt.all(...(this.query.params as any[])); | ||
} | ||
|
||
get() { | ||
return this.stmt.get(...(this.query.params as any[])); | ||
} | ||
|
||
values() { | ||
return Promise.reject(new Error("values is not implemented!")); | ||
} | ||
} |
Oops, something went wrong.