Sakib
Sakib
Explore posts from servers
DTDrizzle Team
Created by Sakib on 3/6/2024 in #help
TypeErr: BatchItem<sqlite>[] not assignable to readonly [BatchItem<sqlite>, ...BatchItem<sqlite>[]]
Hi, I'm trying to use drizzle/d1 (cloudflare worker) for batch. But I'm getting type error.
export const insert_logs_from_batch = async (
batch: MessageBatch<logSchema>,
env: Bindings
): Promise<InsertBatchLogsResponse> => {
const db = drizzle(env.DB);

const refine_batch = batch.messages.map((log) => {
return db.insert(logs).values({
...log.body
}) as BatchItem<"sqlite">; // <--casting as BatchItem cause original type doesn't match as batchItem
});

try {
const res = await db.batch([...refine_batch]); // --> Getting the type error here. it works on runtime

return { success: true, data: null };
} catch (err) {
return { success: false, error: err };
}
};
export const insert_logs_from_batch = async (
batch: MessageBatch<logSchema>,
env: Bindings
): Promise<InsertBatchLogsResponse> => {
const db = drizzle(env.DB);

const refine_batch = batch.messages.map((log) => {
return db.insert(logs).values({
...log.body
}) as BatchItem<"sqlite">; // <--casting as BatchItem cause original type doesn't match as batchItem
});

try {
const res = await db.batch([...refine_batch]); // --> Getting the type error here. it works on runtime

return { success: true, data: null };
} catch (err) {
return { success: false, error: err };
}
};
1 replies
DTDrizzle Team
Created by Sakib on 12/30/2023 in #help
Typescript not showing where field in query. Query working but ts throwing error
Hi, I have a query like below
const weight = await drizzle(c.env.DB, { schema }).query.weights.findFirst({
with: {
type: {
// @ts-expect-error where is not defined in the type
where: and(eq(types.type, parse.type), eq(types.id, parse.type_id)),
// Only showing columns, extras, with field
},
},
where: and(eq(weights.type_id, parse.type_id), eq(weights.weight, parse.weight)),
});
const weight = await drizzle(c.env.DB, { schema }).query.weights.findFirst({
with: {
type: {
// @ts-expect-error where is not defined in the type
where: and(eq(types.type, parse.type), eq(types.id, parse.type_id)),
// Only showing columns, extras, with field
},
},
where: and(eq(weights.type_id, parse.type_id), eq(weights.weight, parse.weight)),
});
// relations.ts
export const typesRelations = relations(types, ({ many }) => ({
weights: many(weights),
}));

export const weightsRelations = relations(weights, ({ one }) => ({
type: one(types, {
fields: [weights.type_id],
references: [types.id],
}),
}));
export const typesRelations = relations(types, ({ many }) => ({
weights: many(weights),
}));

export const weightsRelations = relations(weights, ({ one }) => ({
type: one(types, {
fields: [weights.type_id],
references: [types.id],
}),
}));
I'm using drizzle-orm: ^0.29.1
4 replies
DTDrizzle Team
Created by Sakib on 9/20/2023 in #help
Which typescript type should I use for select?
Hi, this question might be confusing. Let me explain the problem, I'm building a common function for drizzle().select() somethine like
const data = await selectQuery(ctx, tableName, {id:tableName.id, data:tableName.data}); // should return {id:number,data:string}

// select
export const selectQuery = async <T extends SQLiteTable<TableConfig>>(ctx: TRPContextUser, table: T, select: SelectedFields) => {
return await drizzle(ctx.c.env.DB).select(select).from(table).all(); // returning {[x:string]:never}
};
const data = await selectQuery(ctx, tableName, {id:tableName.id, data:tableName.data}); // should return {id:number,data:string}

// select
export const selectQuery = async <T extends SQLiteTable<TableConfig>>(ctx: TRPContextUser, table: T, select: SelectedFields) => {
return await drizzle(ctx.c.env.DB).select(select).from(table).all(); // returning {[x:string]:never}
};
you can see I'm getting {[x:string]:never} when I pass {id:number;data:string} on select param.
5 replies