mirror of
https://github.com/zoriya/Kyoo.git
synced 2026-06-08 07:15:15 -04:00
Type value lists
This commit is contained in:
+12
-5
@@ -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})`;
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user