Type value lists

This commit is contained in:
Zoe Roux
2025-05-01 19:13:13 +02:00
parent 07a41bb175
commit 060c4d74b4
4 changed files with 53 additions and 28 deletions
+12 -5
View File
@@ -74,15 +74,22 @@ export function sqlarr(array: unknown[]) {
}
// See https://github.com/drizzle-team/drizzle-orm/issues/4044
// TODO: type values (everything is a `text` for now)
export function values(items: Record<string, unknown>[]) {
if (items[0] === undefined) throw new Error("Invalid values, expecting at least one items")
const [firstProp, ...props] = Object.keys(items[0]);
export function values<K extends string>(
items: Record<K, unknown>[],
typeInfo: Partial<Record<K, string>> = {},
) {
if (items[0] === undefined)
throw new Error("Invalid values, expecting at least one items");
const [firstProp, ...props] = Object.keys(items[0]) as K[];
const values = items
.map((x) => {
.map((x, i) => {
let ret = sql`(${x[firstProp]}`;
if (i === 0 && typeInfo[firstProp])
ret = sql`${ret}::${sql.raw(typeInfo[firstProp])}`;
for (const val of props) {
ret = sql`${ret}, ${x[val]}`;
if (i === 0 && typeInfo[val])
ret = sql`${ret}::${sql.raw(typeInfo[val])}`;
}
return sql`${ret})`;
})