Skip to content

Commit

Permalink
Make database type schema agnostic (#3)
Browse files Browse the repository at this point in the history
* Make database type schema agnostic

* Connect with pool

* Make `connect` function synchronous
  • Loading branch information
lilac authored Dec 24, 2024
1 parent e33e376 commit a7f8f38
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "mysql-queue",
"description": "A lite job queue for Node.js",
"author": "lilac <[email protected]>",
"version": "0.1.3",
"version": "0.1.4",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
8 changes: 4 additions & 4 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import mysql from "mysql2/promise";
import path from "node:path";
import * as schema from "./schema";

export type Database = MySql2Database<typeof schema>;
export type Database = MySql2Database<Record<string, unknown>>;

export const affectedRows = (rawResult: MySqlRawQueryResult) => {
return rawResult[0].affectedRows
};

export async function connect(url: string) {
const connection = await mysql.createConnection(url);
export function connect(url: string) {
const connection = mysql.createPool(url);
const db = drizzle(connection, { schema, mode: 'default' });
return db;
}

export function migrateDB(db: MySql2Database<Record<string, unknown>>) {
export function migrateDB(db: Database) {
return migrate(db, {
migrationsFolder: path.join(import.meta.dirname, '../drizzle')
});
Expand Down
2 changes: 1 addition & 1 deletion src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const defaultUrl = env['DATABASE_URL'] ?? 'mysql://root:root@localhost:3306/queu


async function prepareDB(url?: string) {
const db = await connect(url ?? defaultUrl);
const db = connect(url ?? defaultUrl);
await migrateDB(db);
return db;
}
Expand Down

0 comments on commit a7f8f38

Please sign in to comment.