Cambaru
Cambaru
DTDrizzle Team
Created by Cambaru on 6/9/2024 in #help
Creating DB with schema
I am pretty new to drizzle and try to follow the documentation. I found that you can initiallize drizzle with a schema like so:
import {drizzle} from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
import * as schema from './schema';

export const connection = await mysql.createConnection(
process.env.DATABASE_CONNECTION as string
);

export const db = drizzle(connection,{schema});
import {drizzle} from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
import * as schema from './schema';

export const connection = await mysql.createConnection(
process.env.DATABASE_CONNECTION as string
);

export const db = drizzle(connection,{schema});
./schema is my MySQL Database schema like so:
import {mysqlTable, varchar, int, boolean, timestamp} from 'drizzle-orm/mysql-core';

export const answers = mysqlTable('answers', {
id: int('id').primaryKey().autoincrement(),
text: varchar('text', { length: 2048 }).notNull(),
correct: boolean('correct').notNull(),
questionId: int('questionId').notNull()
.references(() => questions.id),
});
... many more
import {mysqlTable, varchar, int, boolean, timestamp} from 'drizzle-orm/mysql-core';

export const answers = mysqlTable('answers', {
id: int('id').primaryKey().autoincrement(),
text: varchar('text', { length: 2048 }).notNull(),
correct: boolean('correct').notNull(),
questionId: int('questionId').notNull()
.references(() => questions.id),
});
... many more
But i get this error wich i cannot explain:
TS2345: Argument of type '{ schema: typeof schema; }' is not assignable to parameter of type 'MySql2DrizzleConfig<typeof import("/home/...../lib/db/schema")> | undefined'.
Type '{ schema: typeof schema; }' is not assignable to type 'Omit<DrizzleConfig<typeof import("/home/...../lib/db/schema")>, "schema"> & { schema: typeof import("/home/...../lib/db/schema"); mode: Mode; }'.
Property 'mode' is missing in type '{ schema: typeof schema; }' but required in type '{ schema: typeof import("/home/...../lib/db/schema"); mode: Mode; }'.
TS2345: Argument of type '{ schema: typeof schema; }' is not assignable to parameter of type 'MySql2DrizzleConfig<typeof import("/home/...../lib/db/schema")> | undefined'.
Type '{ schema: typeof schema; }' is not assignable to type 'Omit<DrizzleConfig<typeof import("/home/...../lib/db/schema")>, "schema"> & { schema: typeof import("/home/...../lib/db/schema"); mode: Mode; }'.
Property 'mode' is missing in type '{ schema: typeof schema; }' but required in type '{ schema: typeof import("/home/...../lib/db/schema"); mode: Mode; }'.
I hope someone can help me 🙂
2 replies
DTDrizzle Team
Created by Cambaru on 8/25/2023 in #help
Correct way to deal with MySqlRawQueryResult
What is best practice to check if the data was created and how to get the newly added data?
db.insert(user).values({
firstname: data.data.firstname,
lastname: data.data.lastname,
username: data.data.username,
email: data.data.email,
password: hash
}).then((newUser) => {
//How do i check if the user was created?
}).catch((err) => {
return res.status(500).json(err)
})
db.insert(user).values({
firstname: data.data.firstname,
lastname: data.data.lastname,
username: data.data.username,
email: data.data.email,
password: hash
}).then((newUser) => {
//How do i check if the user was created?
}).catch((err) => {
return res.status(500).json(err)
})
7 replies