Journal
Dated entries in a collection, and preferences in a document. The same layout fits any log: workouts, expenses, reading notes.
// store.ts
import { Store } from '@civility/cloud'
import { IDBStorage } from '@civility/cloud/storage/idb'
type Entry = { title: string; body: string; date: string }
type Prefs = { defaultMood: string }
const store = new Store({
id: 'app-k3m7q2x4p6w5z7n3b2v4c6h5', // from `civ id`
storage: { db: new IDBStorage() },
documents: ['prefs'],
collections: ['entries'],
versions: [{
version: '1.0.0',
schema: {
type: 'object',
properties: {
prefs: { type: 'object' },
entries: { type: 'object' },
},
},
}],
})
export const prefs = store.document<Prefs>('prefs', { defaultMood: 'neutral' })
export const entries = store.collection<Entry>('entries')
export function write(title: string, body: string) {
const date = new Date().toISOString()
// The id starts with the date, so sorting ids sorts by date.
const id = `${date}_${crypto.randomUUID().slice(0, 8)}`
return entries.set(id, { title, body, date })
}
export async function recent(limit = 20) {
const all = await entries.getAll()
return [...all.keys()].sort().reverse().slice(0, limit)
.map((id) => ({ id, entry: all.get(id)! }))
}
To show how one entry changed over time, use entries.getHistory(id). It returns the entry's changes, newest first.
Next: Gallery