Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export database instances #132

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/connectors/better-sqlite3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ export default function sqliteConnector(opts: ConnectorOptions) {
return _db;
};

return <Connector>{
return <Connector<Database.Database>>{
name: "sqlite",
dialect: "sqlite",
getInstance: async () => getDB(),
exec(sql: string) {
return getDB().exec(sql);
},
Expand Down
3 changes: 2 additions & 1 deletion src/connectors/bun-sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
return _db;
};

return <Connector>{
return <Connector<Database>>{

Check warning on line 32 in src/connectors/bun-sqlite.ts

View check run for this annotation

Codecov / codecov/patch

src/connectors/bun-sqlite.ts#L32

Added line #L32 was not covered by tests
name: "sqlite",
dialect: "sqlite",
getInstance: async () => getDB(),

Check warning on line 35 in src/connectors/bun-sqlite.ts

View check run for this annotation

Codecov / codecov/patch

src/connectors/bun-sqlite.ts#L35

Added line #L35 was not covered by tests
exec(sql: string) {
return getDB().exec(sql);
},
Expand Down
1 change: 1 addition & 0 deletions src/connectors/cloudflare-d1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
return <Connector>{
name: "cloudflare-d1",
dialect: "sqlite",
getInstance: async () => getDB(),

Check warning on line 20 in src/connectors/cloudflare-d1.ts

View check run for this annotation

Codecov / codecov/patch

src/connectors/cloudflare-d1.ts#L20

Added line #L20 was not covered by tests
exec: (sql: string) => getDB().exec(sql),
prepare: (sql: string) => {
const _stmt = getDB().prepare(sql);
Expand Down
3 changes: 2 additions & 1 deletion src/connectors/libsql/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export default function libSqlCoreConnector(opts: ConnectorOptions) {
return client.execute(sql);
}

return <Connector>{
return <Connector<Client>>{
name: opts.name || "libsql-core",
dialect: "libsql",
getInstance: async () => opts.getClient(),
exec(sql: string) {
return query(sql);
},
Expand Down
3 changes: 2 additions & 1 deletion src/connectors/mysql2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ export default function mysqlConnector(opts: mysql.ConnectionOptions) {
return _connection;
};

return <Connector>{
return <Connector<mysql.Connection>>{
name: "mysql",
dialect: "mysql",
getInstance: async () => getConnection(),
exec(sql: string) {
return getConnection().then((c) => c.query(sql).then((res) => res[0]));
},
Expand Down
3 changes: 2 additions & 1 deletion src/connectors/pglite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ export default function pgliteConnector(opts: ConnectorOptions = {}) {
return result;
}

return <Connector>{
return <Connector<PGlite>>{
name: "pglite",
dialect: "postgresql",
getInstance: async () => getClient(),
exec(sql: string) {
return query(sql);
},
Expand Down
3 changes: 2 additions & 1 deletion src/connectors/planetscale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ export default function planetscaleConnector(opts: Config) {
return client.execute(sql, params);
}

return <Connector>{
return <Connector<Client>>{
name: "planetscale",
dialect: "mysql",
getInstance: async () => getClient(),
exec(sql: string) {
return query(sql);
},
Expand Down
3 changes: 2 additions & 1 deletion src/connectors/postgresql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ export default function postgresqlConnector(opts: ConnectorOptions) {
return client.query(normalizeParams(sql), params);
}

return <Connector>{
return <Connector<pg.Client>>{
name: "postgresql",
dialect: "postgresql",
getInstance: async () => getClient(),
exec(sql: string) {
return query(sql);
},
Expand Down
6 changes: 4 additions & 2 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ const SQL_WITH_RES_RE = /^select/i;
* @param {Connector} connector - The database connector used to execute and prepare SQL statements. See {@link Connector}.
* @returns {Database} The database interface that allows SQL operations. See {@link Database}.
*/
export function createDatabase(connector: Connector): Database {
return <Database>{
export function createDatabase<TConnector extends Connector = Connector>(
connector: TConnector,
): Database<TConnector> {
return <Database<TConnector>>{
get dialect() {
return connector.dialect;
},
Expand Down
15 changes: 13 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export type ExecResult = unknown;
/**
* Defines a database connector for executing SQL queries and preparing statements.
*/
export type Connector = {
export type Connector<TInstance = any> = {
/**
* The name of the connector.
*/
Expand All @@ -54,6 +54,11 @@ export type Connector = {
*/
dialect: SQLDialect;

/**
* The client instance used internally.
*/
getInstance: () => Promise<TInstance>;

/**
* Executes an SQL query directly and returns the result.
* @param {string} sql - The SQL string to execute.
Expand All @@ -79,9 +84,15 @@ type DefaultSQLResult = {
rows?: { id?: string | number; [key: string]: unknown }[];
};

export interface Database {
export interface Database<TConnector extends Connector = Connector> {
readonly dialect: SQLDialect;

/**
* The client instance used internally.
* @returns {Promise<TInstance>} A promise that resolves with the client instance.
*/
getInstance: () => Promise<TConnector["getInstance"]>;

/**
* Executes a raw SQL string.
* @param {string} sql - The SQL string to execute.
Expand Down
4 changes: 2 additions & 2 deletions test/connectors/_tests.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { beforeAll, expect, it } from "vitest";
import { Connector, Database, createDatabase, type SQLDialect } from "../../src";

export function testConnector(opts: { connector: Connector, dialect: SQLDialect }) {
let db: Database;
export function testConnector<TConnector extends Connector = Connector>(opts: { connector: TConnector, dialect: SQLDialect }) {
let db: Database<TConnector>;
beforeAll(() => {
db = createDatabase(opts.connector);
});
Expand Down
4 changes: 2 additions & 2 deletions test/integrations/drizzle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("integrations: drizzle: better-sqlite3", () => {
});

let drizzleDb: DrizzleDatabase;
let db: Database;
let db: Database<ReturnType<typeof sqliteConnector>>;

beforeAll(async () => {
db = createDatabase(sqliteConnector({}));
Expand Down Expand Up @@ -57,7 +57,7 @@ describe.runIf(process.env.POSTGRESQL_URL)("integrations: drizzle: postgres", ()
});

let drizzleDb: DrizzleDatabase;
let db: Database;
let db: Database<ReturnType<typeof pgConnector>>;

beforeAll(async () => {
db = createDatabase(pgConnector({
Expand Down
Loading