returning insert values undefined with drizzle http sqlite proxy

Hey, I just ran into an issue while using drizzle-orm/sqlite-proxy when used with the returning() statement. This is what my db client looks like: `
export const getDB = (identifier: string) => {
const db = drizzle(async (sql, params, method) => {
const doId = env.DATABASE_DO.idFromName("test");
const durableObject = env.DATABASE_DO.get(doId);
const result = await durableObject.query(sql, params, method);
console.log(result);
return { rows: result.rows };
});

return new Proxy(db, {
get(target, prop, receiver) {
const original = Reflect.get(target, prop, receiver);
if (typeof original !== 'function') {
return original;
}
return (...args: any[]) => {
const result = original.apply(target, args);
if (result && typeof result.catch === 'function') {
return result.catch((err: Error) => {
throw new DatabaseError(err.message);
});
}
return result;
};
},
});
};

export type DB = ReturnType<typeof getDB>;
export const getDB = (identifier: string) => {
const db = drizzle(async (sql, params, method) => {
const doId = env.DATABASE_DO.idFromName("test");
const durableObject = env.DATABASE_DO.get(doId);
const result = await durableObject.query(sql, params, method);
console.log(result);
return { rows: result.rows };
});

return new Proxy(db, {
get(target, prop, receiver) {
const original = Reflect.get(target, prop, receiver);
if (typeof original !== 'function') {
return original;
}
return (...args: any[]) => {
const result = original.apply(target, args);
if (result && typeof result.catch === 'function') {
return result.catch((err: Error) => {
throw new DatabaseError(err.message);
});
}
return result;
};
},
});
};

export type DB = ReturnType<typeof getDB>;
And this is what my durable object method looks like:
async query(sqlBody: string, params: any[] = [], method: 'all' | 'first' | 'run' | 'values' | 'get' = 'all') {
const cursor = this.sql.exec(sqlBody, ...params);

if (method === 'all' || method === 'run' || method === 'values') {
const rows = cursor.toArray();
return { rows };
} else {
const row = cursor.one();
return { rows: row ? [row] : [] };
}
}
async query(sqlBody: string, params: any[] = [], method: 'all' | 'first' | 'run' | 'values' | 'get' = 'all') {
const cursor = this.sql.exec(sqlBody, ...params);

if (method === 'all' || method === 'run' || method === 'values') {
const rows = cursor.toArray();
return { rows };
} else {
const row = cursor.one();
return { rows: row ? [row] : [] };
}
}
What am I doing wrong here? The ID field of the "returning" insert query is undefined for some reason even though my durable object method returns it correctly
1 Reply
Zetax
ZetaxOP2mo ago
This is my database query:
const [account] = await db
.insert(ACCOUNTS)
.values({
email,
displayName: user.global_name ?? 'Unset',
discordId: user.id,
passwordHash: '',
settings: defaultSettings
})
.returning({ id: ACCOUNTS.id });
const [account] = await db
.insert(ACCOUNTS)
.values({
email,
displayName: user.global_name ?? 'Unset',
discordId: user.id,
passwordHash: '',
settings: defaultSettings
})
.returning({ id: ACCOUNTS.id });
but logging it just shows { id: undefined } even though the result from the query is { rows: [ { id: 'Du27aJydA1CgmKMxaxTXS' } ] }

Did you find this page helpful?