feat(core): custom field definitions and value validation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d392a65aa7
commit
56b86e27da
3 files changed
+344
No files matched your search
@@ -0,0 +1,65 @@
|
||||
import type { Db } from "@sttracker/db/db";
|
||||
import { newId, nowIso } from "@sttracker/db/ids";
|
||||
import { toJson } from "@sttracker/db/json";
|
||||
|
||||
/** Insert a user and return its id. */
|
||||
export async function makeUser(db: Db, username = "ada", preferences: Record<string, unknown> = {}): Promise<string> {
|
||||
const id = newId();
|
||||
const now = nowIso();
|
||||
await db.k
|
||||
.insertInto("users")
|
||||
.values({
|
||||
id,
|
||||
username,
|
||||
display_name: username,
|
||||
password_hash: "x",
|
||||
role: "member",
|
||||
preferences: toJson(preferences),
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
})
|
||||
.execute();
|
||||
return id;
|
||||
}
|
||||
|
||||
/** Insert a work (shared unless `ownerUserId` is set) and return its id. */
|
||||
export async function makeWork(
|
||||
db: Db,
|
||||
opts: {
|
||||
title?: string;
|
||||
mediaType?: string;
|
||||
year?: number | null;
|
||||
parentId?: string | null;
|
||||
ownerUserId?: string | null;
|
||||
externalIds?: Record<string, string>;
|
||||
} = {},
|
||||
): Promise<string> {
|
||||
const id = newId();
|
||||
const now = nowIso();
|
||||
await db.k
|
||||
.insertInto("works")
|
||||
.values({
|
||||
id,
|
||||
media_type: opts.mediaType ?? "book",
|
||||
title: opts.title ?? "Dune",
|
||||
original_title: "",
|
||||
year: opts.year ?? null,
|
||||
release_date: null,
|
||||
creators: toJson([]),
|
||||
description: "",
|
||||
cover_url: "",
|
||||
cover_attachment_id: null,
|
||||
metadata: toJson({}),
|
||||
parent_id: opts.parentId ?? null,
|
||||
position: null,
|
||||
owner_user_id: opts.ownerUserId ?? null,
|
||||
merged_into_id: null,
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
})
|
||||
.execute();
|
||||
for (const [namespace, value] of Object.entries(opts.externalIds ?? {})) {
|
||||
await db.k.insertInto("external_ids").values({ id: newId(), work_id: id, namespace, value }).execute();
|
||||
}
|
||||
return id;
|
||||
}
|
||||
Reference in new issue
Block a user