Error while providing Schema to drizzle client.
Getting error while providing schema to drizzle client. Here are my files, do let me know what am I doing wrong.
Type 'typeof import("d:/workspace/template/src/database/schema/index")' is not assignable to type 'Record<string, never>'.
Property 'users' is incompatible with index signature.
Type 'MySqlTableWithColumns<{ name: "users"; schema: undefined; columns: { id: MySqlColumn<{ name: "id"; tableName: "users"; dataType: "number"; columnType: "MySqlInt"; data: number; driverParam: string | number; notNull: true; hasDefault: true; ... 5 more ...; generated: undefined; }, object>; name: MySqlColumn<...>; ema...' is not assignable to type 'never'
Type 'typeof import("d:/workspace/template/src/database/schema/index")' is not assignable to type 'Record<string, never>'.
Property 'users' is incompatible with index signature.
Type 'MySqlTableWithColumns<{ name: "users"; schema: undefined; columns: { id: MySqlColumn<{ name: "id"; tableName: "users"; dataType: "number"; columnType: "MySqlInt"; data: number; driverParam: string | number; notNull: true; hasDefault: true; ... 5 more ...; generated: undefined; }, object>; name: MySqlColumn<...>; ema...' is not assignable to type 'never'
// file: schema.ts
import { sql } from "drizzle-orm";
import {datetime,int,mysqlTable,text,varchar } from "drizzle-orm/mysql-core";
export const users = mysqlTable("users", {
id: int("id", { unsigned: true }).primaryKey().autoincrement(),
name: varchar("name", { length: 255 }),
email: varchar("email", { length: 255 }).unique().notNull(),
password: text("password").notNull(),
createdAt: datetime("created_at", { mode: "string" }).default(
sql`CURRENT_TIMESTAMP`,
),
});
// file: schema.ts
import { sql } from "drizzle-orm";
import {datetime,int,mysqlTable,text,varchar } from "drizzle-orm/mysql-core";
export const users = mysqlTable("users", {
id: int("id", { unsigned: true }).primaryKey().autoincrement(),
name: varchar("name", { length: 255 }),
email: varchar("email", { length: 255 }).unique().notNull(),
password: text("password").notNull(),
createdAt: datetime("created_at", { mode: "string" }).default(
sql`CURRENT_TIMESTAMP`,
),
});
// file: index.ts
import ...
import * as schema from "./schema";
let client: Connection;
class QueryLogger implements DrizzleLogger {
logQuery(query: string, params: unknown[]): void {
logger.debug(JSON.stringify({ query, params }));
}
}
function getDatabaseInstance(client: Connection) {
return drizzle(client, {
schema,
logger: new QueryLogger(),
});
}
export async function getConnection() {
if (!client) {
client = await createConnection({
host: ENV.DB_HOST,
port: ENV.DB_PORT,
user: ENV.DB_USER,
password: ENV.DB_PASS,
database: ENV.DB_NAME,
});
return getDatabaseInstance(client);
}
return getDatabaseInstance(client);
}
// file: index.ts
import ...
import * as schema from "./schema";
let client: Connection;
class QueryLogger implements DrizzleLogger {
logQuery(query: string, params: unknown[]): void {
logger.debug(JSON.stringify({ query, params }));
}
}
function getDatabaseInstance(client: Connection) {
return drizzle(client, {
schema,
logger: new QueryLogger(),
});
}
export async function getConnection() {
if (!client) {
client = await createConnection({
host: ENV.DB_HOST,
port: ENV.DB_PORT,
user: ENV.DB_USER,
password: ENV.DB_PASS,
database: ENV.DB_NAME,
});
return getDatabaseInstance(client);
}
return getDatabaseInstance(client);
}
1 Reply
package.json
"drizzle-orm": "^0.34.1",
"drizzle-zod": "^0.5.1",
"mysql2": "^3.11.3",
"drizzle-orm": "^0.34.1",
"drizzle-zod": "^0.5.1",
"mysql2": "^3.11.3",