baronnoraz
baronnoraz
DTDrizzle Team
Created by baronnoraz on 2/1/2024 in #help
Programmatic Database Migration without migrations folder
I'm curious if there's a way to programmatically run the migration without the need for the migrations folder. I have a mono repo with an api library that uses my db package. I'm creating tests for the api and was going to recreate the db before each test run. I was going to run the migrate function from drizzle-orm/mysql2/migrator but it requires the migrationsFolder path to be supplied as an argument. Since I'm in a mono repo, this path will vary based on the relative paths of my tests and the db package. I can't put those paths in my test files. Since the tables are defined in typescript, is there a way to directly in code to create the table without first doing a generate and then a migrate?
1 replies
DTDrizzle Team
Created by baronnoraz on 12/29/2023 in #help
Unit Testing with Transactions
Typically in our Unit Tests, we have something like...
describe('test suite', () => {
let tx: Transaction;
beforeEach(() => {
tx = db.getConnection();
})
afterEach(() => {
tx.rollback();
})
it('test', async () => {
const result = await tx.makeSomeDbChanges();
expect(result).toBe(something);
})
describe('test suite', () => {
let tx: Transaction;
beforeEach(() => {
tx = db.getConnection();
})
afterEach(() => {
tx.rollback();
})
it('test', async () => {
const result = await tx.makeSomeDbChanges();
expect(result).toBe(something);
})
Does Drizzle have a mechanism for getting access to the transaction without the anonymous function route? I'd prefer not to do the following in every test...
describe('test suite', () => {
it('test', async () => {
const db = drizzle(conn);
db.transaction( async (tx) => {
const result = await tx.makeSomeDbChanges();
expect(result).toBe(something);
tx.rollback();
});
})
}
describe('test suite', () => {
it('test', async () => {
const db = drizzle(conn);
db.transaction( async (tx) => {
const result = await tx.makeSomeDbChanges();
expect(result).toBe(something);
tx.rollback();
});
})
}
Especially when I have to throw in error handling because tx.rollback() throws an Error and doesn't simply rollback. What are others doing for unit testing?
12 replies
DTDrizzle Team
Created by baronnoraz on 11/29/2023 in #help
create exportable types for frontend services
I feel like I'm missing something fundamental. I can create my Drizzle schema user = mysqlTable('user', {id: ....}); and use them in my queries to get data back. I want to return my result set to the frontend as {id: 1, ...} and strongly type the API return value. I thought that doing export type UserModel = InferModeltype<typeof user> would do that, but I get a type that looks like MySQLTableWithColumns.... instead of simply {id: number....}. What am I missing? I see how Drizzle keeps my queries type safe, but how can I leverage it to make sure consumers of my services use proper types? I feel like everyone is doing this, that it's super easy, and I'm just dumb right now.
16 replies