Test CloufFlare D1 migration

https://orm.drizzle.team/docs/migrations
import { drizzle } from 'drizzle-orm/mysql2';
import mysql from 'mysql2/promise';
import * as schema from './schema';

export const connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
multipleStatements: true,
});

export const db = drizzle(connection, { schema });

import 'dotenv/config';
import { migrate } from 'drizzle-orm/mysql2/migrator';
import { db, connection } from './db';

// This will run migrations on the database, skipping the ones already applied
await migrate(db, { migrationsFolder: './drizzle' });

// Don't forget to close the connection, otherwise the script will hang
await connection.end();
import { drizzle } from 'drizzle-orm/mysql2';
import mysql from 'mysql2/promise';
import * as schema from './schema';

export const connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
multipleStatements: true,
});

export const db = drizzle(connection, { schema });

import 'dotenv/config';
import { migrate } from 'drizzle-orm/mysql2/migrator';
import { db, connection } from './db';

// This will run migrations on the database, skipping the ones already applied
await migrate(db, { migrationsFolder: './drizzle' });

// Don't forget to close the connection, otherwise the script will hang
await connection.end();
This is a great example of migrating to a mysql instance that can connect to any local remote instance. It is complete in Typescript and can be unit tested! I would like to do the same with D1 I could not find any documentation, is it possible? Or should I test by substituting a sqlite instance or an in-memory database?
Drizzle ORM - next gen TypeScript ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
2 Replies
roccho
rocchoOP13mo ago
I'm trying:
import { D1Database, D1DatabaseAPI } from "@miniflare/d1";
import { drizzle } from "drizzle-orm/d1";

const d1 = new D1Database(new D1DatabaseAPI(sqliteDb));
const db = drizzle(d1);
import { D1Database, D1DatabaseAPI } from "@miniflare/d1";
import { drizzle } from "drizzle-orm/d1";

const d1 = new D1Database(new D1DatabaseAPI(sqliteDb));
const db = drizzle(d1);
louis
louis12mo ago
@roccho did you find a solution?
Want results from more Discord servers?
Add your server