Files
2026-07-13 12:10:05 +08:00

154 lines
3.6 KiB
JavaScript

import 'dotenv/config';
import { drizzle } from 'drizzle-orm/mysql2';
import { Connection, createConnection, createPool } from 'mysql2';
import { describe, expect } from 'vitest';
import { mysql as schema } from './schema.mjs';
if (!process.env['MYSQL_CONNECTION_STRING']) {
throw new Error('MYSQL_CONNECTION_STRING is not defined');
}
describe('mysql2', async (it) => {
it('drizzle(string)', async () => {
const db = drizzle(
process.env['MYSQL_CONNECTION_STRING'],
);
await db.$client.execute(`SELECT 1`);
expect(db.$client.getConnection).not.toStrictEqual(undefined);
});
it('drizzle(string, config)', async () => {
const db = drizzle(
process.env['MYSQL_CONNECTION_STRING'],
{
schema,
mode: 'default',
},
);
await db.$client.execute('SELECT 1;');
expect(db.query.User).not.toStrictEqual(undefined);
expect(db.$client.getConnection).not.toStrictEqual(undefined);
});
it('drizzle({connection: string, ...config})', async () => {
const db = drizzle({
connection: process.env['MYSQL_CONNECTION_STRING'],
schema,
mode: 'default',
});
await db.$client.execute('SELECT 1;');
expect(db.query.User).not.toStrictEqual(undefined);
expect(db.$client.getConnection).not.toStrictEqual(undefined);
});
it('drizzle({connection: params, ...config})', async () => {
const db = drizzle({
connection: {
uri: process.env['MYSQL_CONNECTION_STRING'],
},
schema,
mode: 'default',
});
await db.$client.execute('SELECT 1;');
expect(db.query.User).not.toStrictEqual(undefined);
expect(db.$client.getConnection).not.toStrictEqual(undefined);
});
it('drizzle(client)', async () => {
const client = createPool({
uri: process.env['MYSQL_CONNECTION_STRING'],
});
const db = drizzle(client);
await db.$client.execute('SELECT 1;');
expect(db.$client.getConnection).not.toStrictEqual(undefined);
});
it('drizzle(client, config)', async () => {
const client = createPool({
uri: process.env['MYSQL_CONNECTION_STRING'],
});
const db = drizzle(client, {
schema,
mode: 'default',
});
await db.$client.execute('SELECT 1;');
expect(db.query.User).not.toStrictEqual(undefined);
expect(db.$client.getConnection).not.toStrictEqual(undefined);
});
it('drizzle({client, ...config})', async () => {
const client = createPool({
uri: process.env['MYSQL_CONNECTION_STRING'],
});
const db = drizzle({
client,
schema,
mode: 'default',
});
await db.$client.execute('SELECT 1;');
expect(db.query.User).not.toStrictEqual(undefined);
expect(db.$client.getConnection).not.toStrictEqual(undefined);
});
});
describe('mysql2:connection', async (it) => {
it('drizzle(client)', async () => {
const client = createConnection({
uri: process.env['MYSQL_CONNECTION_STRING'],
});
const db = drizzle(client);
await db.$client.execute('SELECT 1;');
expect(db.$client.getConnection).toStrictEqual(undefined);
});
it('drizzle(client, config)', async () => {
const client = createConnection({
uri: process.env['MYSQL_CONNECTION_STRING'],
});
const db = drizzle(client, {
schema,
mode: 'default',
});
await db.$client.execute('SELECT 1;');
expect(db.query.User).not.toStrictEqual(undefined);
expect(db.$client.getConnection).toStrictEqual(undefined);
});
it('drizzle({client, ...config})', async () => {
const client = createConnection({
uri: process.env['MYSQL_CONNECTION_STRING'],
});
const db = drizzle({
client,
schema,
mode: 'default',
});
await db.$client.execute('SELECT 1;');
expect(db.query.User).not.toStrictEqual(undefined);
expect(db.$client.getConnection).toStrictEqual(undefined);
});
});