roccho
roccho
DTDrizzle Team
Created by roccho on 12/23/2023 in #help
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?
3 replies
DTDrizzle Team
Created by roccho on 11/23/2023 in #help
In memory feature, drissle has?
import { drizzle } from 'drizzle-orm/better-sqlite3';
import Database from 'better-sqlite3';

const sqlite = new Database('sqlite.db');
const db = drizzle(sqlite);
import { drizzle } from 'drizzle-orm/better-sqlite3';
import Database from 'better-sqlite3';

const sqlite = new Database('sqlite.db');
const db = drizzle(sqlite);
Basically, drizzle wrap each driver like above. OTOH, Drizzle can manage db object in memory? like this:
import { drizzle } from 'drizzle-orm'

const db = drizzle() // <- in memory
import { drizzle } from 'drizzle-orm'

const db = drizzle() // <- in memory
5 replies