Building the chat / Keeping it
Keeping it
The chat's whole state is one directory, so keeping it safe is one job: back it up every night while it runs, restore it on another machine when you need to, and test the parts that depend on time without waiting for time to pass. This chapter uses the store's backup, a schedule from the Jobs engine, and a store that runs on a clock your test moves.
Back up while it runs
const backup = await store.backup('backups/chat-2026-10-19.zip')
// { files: 7, bytes: 1834221, keyLeftOut: false }backup = await store.backup("backups/chat-2026-10-19.zip")
# Backup(files=7, bytes=1834221, key_left_out=False)backup, err := store.Backup(ctx, "backups/chat-2026-10-19.zip")
// tinystore.Backup{Files: 7, Bytes: 1834221, KeyLeftOut: false}tinystore backup ./data backups/chat-2026-10-19.zipThe backup is one zip of every engine's files as they stood at one moment,
with a manifest.json of each file's size and SHA-256, and the chat keeps
answering while it is made. A backup that fails leaves no half zip behind.
The folder it goes into must exist. The zip is not encrypted, so keep
backups as safely as the store's directory: What a backup
holds has the rest.
Every night
A schedule is a repeating job that the code owns. The chat backs up at three in the morning, Berlin time:
store.schedule('backup', { cron: '0 3 * * *', timeZone: 'Europe/Berlin' }, async run => {
const day = run.at.toISOString().slice(0, 10)
await store.backup(`backups/chat-${day}.zip`)
})async def nightly_backup(run: tinystore.Run) -> None:
await store.backup(f"backups/chat-{run.at.date()}.zip")
store.schedule("backup", nightly_backup, cron="0 3 * * *", time_zone="Europe/Berlin")nightly, err := jobs.OpenSchedule(store, "backup", jobs.Cron("0 3 * * *", "Europe/Berlin"))
err = nightly.Work(ctx, func(ctx context.Context, run *jobs.Run) error {
_, err := store.Backup(ctx, fmt.Sprintf("backups/chat-%s.zip", run.At.Format(time.DateOnly)))
return err
})let backups = store.clone();
let nightly = store.schedule("backup").cron("0 3 * * *", "Europe/Berlin").work(move |run| -> Result<(), Box<dyn Error>> {
let day = jiff::Timestamp::try_from(run.at())?.strftime("%Y-%m-%d"); // the jiff crate
backups.backup().write(format!("backups/chat-{day}.zip"))?;
Ok(())
})?;run.at is the time the run was due, not the time it started, so a backup
that starts late is still named after its night. If the chat is down at
three, the backup runs once when it comes back, and a run that fails is
retried (How a repeat
behaves). A cron needs
a time zone, because "three in the morning" means nothing without one, and a
default of UTC would be a guess.
Restore
tinystore restore backups/chat-2026-10-19.zip ./datatinystore::backup::restore("backups/chat-2026-10-19.zip", "data")?; // before Store::openA restore writes into an empty or missing directory, never over a store, and
restores everything or leaves the directory empty (What a restore
checks). Then the
chat opens the directory as it opens any store. A program in TypeScript,
Python or Go restores through the tinystore command, because a restore
happens before any store is open.
Test on a clock you move
Sign-in codes expire after 15 minutes, a reminder runs in an hour, sessions end after 30 idle days. A test should not wait for any of that. A store for a test runs on a clock that the test moves, in a directory of its own that its close removes:
import { openTemp } from '@tinyshed/tinystore/testing'
await using store = await openTemp({ clock: new Date('2026-10-09T09:00:00Z') })
const codes = store.bucket<number>('sign-in-codes', { ttl: '15m' })
const reminders = store.queue<Reminder>('reminders')
await codes.set('k7q2', 42)
await reminders.add({ to: 'ada@example.com' }, { delay: '1h' })
await reminders.runDue(remind) // 0: nothing is due yet
await store.clock.advance('1h')
await reminders.runDue(remind) // 1: the reminder ran
await codes.get('k7q2') // undefined: the code expiredfrom tinystore.testing import open_temp
store = await open_temp(clock=datetime(2026, 10, 9, 9, tzinfo=UTC)) # or async with open_temp(...) as store
codes = store.bucket("sign-in-codes", int, ttl="15m")
reminders = store.queue("reminders", Reminder)
await codes.set("k7q2", 42)
await reminders.add(Reminder(to="ada@example.com"), delay="1h")
await reminders.run_due(remind) # 0: nothing is due yet
await store.clock.advance("1h")
await reminders.run_due(remind) # 1: the reminder ran
await codes.get("k7q2") # None: the code expiredstore := tinystoretest.Open(t, tinystore.Clock(time.Date(2026, 10, 9, 9, 0, 0, 0, time.UTC))) // closed and removed with t
codes, err := kv.OpenBucket[int64](store, "sign-in-codes", kv.TTL(15*time.Minute))
reminders, err := jobs.OpenQueue[Reminder](store, "reminders")
err = codes.Set(ctx, "k7q2", 42)
_, err = reminders.Add(ctx, Reminder{To: "ada@example.com"}, jobs.Delay(time.Hour))
err = reminders.RunDue(ctx, remind) // nothing is due yet
err = store.Clock().Advance(ctx, time.Hour)
err = reminders.RunDue(ctx, remind) // the reminder ran
_, found, err := codes.Get(ctx, "k7q2") // found is false: the code expiredlet clock = Arc::new(TestClock::new(start));
let store = tinystore::testing::open_temp(Options { clock: Some(clock.clone()), background: false, ..Options::default() })?;
let codes = store.bucket::<i64>("sign-in-codes").ttl(Duration::from_mins(15)).open()?;
let reminders = store.queue::<Reminder>("reminders").open()?;
codes.set("k7q2", &42)?;
reminders.delay(Duration::from_hours(1)).add(&Reminder { to: "ada@example.com".into() })?;
reminders.run_due(remind)?; // 0: nothing is due yet
clock.advance(Duration::from_hours(1));
reminders.run_due(remind)?; // 1: the reminder ran
codes.get("k7q2")?; // None: the code expiredMoving the clock moves every engine together: expiry, jobs, a quota's
windows and the time of a log line. runDue runs what is due and returns,
so a test checks the result right after. Each test's store is its own, so
tests run in parallel (How it
behaves).
Why this design?
Why not copy the directory. A file that SQLite is writing has a write-ahead log beside it, and a copy taken mid-write can hold half a commit. A backup instead begins a read of every file at one moment and copies each from its read: SQLite's own backup of the pages, and a link for each stored file, so a large attachment costs the backup a directory entry and no copy until the zip is written. Engines that hold numbers in memory, such as counters and the logger's buffer, write them first, so the backup has them.
Why one moment for every file. No write spans two files, but a program's rows often name what another file holds: a message's row names its attachment, a cursor says how far the samples it collected go. Copied at two moments, a restore could hold the name without the thing. So the backup holds every file's commits back for the instant it takes to begin a read of each, and a restore is the store as it stood then.
Why a restore checks every byte. A backup is the copy you use on your worst day, and a restore that stopped halfway would leave a store that opens and is wrong. So a restore is either the backup or nothing (contract BAK-02).
Why a clock instead of sleeping. A test that waits 15 minutes for a code to expire is never run, and one that waits 50 milliseconds for a job is flaky. A clock the test moves makes time a value the test controls: the same test passes the same way on a laptop and on a slow CI machine.
See also
- One directory, many engines: what is in the directory a backup copies.
- Replies in the background: schedules are queues' jobs.
- Backup API reference: every option of a backup.