Skip to content
Source
Sign inSign up
fuko/trytanstackdb-v2
Open History to see the last commit

index.ts

38 lines1.4 KB

1import accountJson from "./account.json";
2import foldersJson from "./folders.json";
3import notesJson from "./notes.json";
4import { accountSchema, folderSchema, noteSchema } from "@/db/schema";
5import type { Account, Folder, Note } from "@/db/schema";
6​
7/**
8 * The fixtures were written as if "now" was this moment. When seeding, every
9 * timestamp is shifted by the same amount so "Today", "Yesterday" and
10 * "Previous 7 Days" still make sense whenever the app is opened.
11 */
12export const FIXTURES_WRITTEN_AT = "2026-11-03T19:40:00.000Z";
13​
14export interface SeedData {
15 folders: Folder[];
16 notes: Note[];
17 account: Account;
19​
20export function createSeedData(now: Date = new Date()): SeedData {
21 const offset = now.getTime() - Date.parse(FIXTURES_WRITTEN_AT);
22 const shift = (iso: string) => new Date(Date.parse(iso) + offset).toISOString();
23​
24 const folders = folderSchema.array().parse(foldersJson);
25 const notes = noteSchema.array().parse(notesJson);
26 const account = accountSchema.parse(accountJson);
27​
28 return {
29 folders: folders.map((folder) => ({ ...folder, createdAt: shift(folder.createdAt) })),
30 notes: notes.map((note) => ({
31 ...note,
32 createdAt: shift(note.createdAt),
33 updatedAt: shift(note.updatedAt),
34 deletedAt: note.deletedAt ? shift(note.deletedAt) : null,
35 })),
36 account: { ...account, createdAt: shift(account.createdAt) },
37 };