From b48f669af4fdf03f4940afaf1ad61370b5e218e9 Mon Sep 17 00:00:00 2001 From: hiperman Date: Wed, 25 Feb 2026 01:02:29 -0500 Subject: [PATCH] add triplt db schema --- src/lib/triplit/schema.ts | 80 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/lib/triplit/schema.ts diff --git a/src/lib/triplit/schema.ts b/src/lib/triplit/schema.ts new file mode 100644 index 0000000..e70e7ba --- /dev/null +++ b/src/lib/triplit/schema.ts @@ -0,0 +1,80 @@ +import { Schema as S, type Entity } from "@triplit/client"; + +/** + * Define your schema here. After: + * - Pass your schema to your Triplit client + * - Push your schema to your Triplit server with 'triplit schema push' + * + * For more information about schemas, see the docs: https://www.triplit.dev/docs/schemas + */ +export const schema = S.Collections({ + habits: { + schema: S.Schema({ + id: S.Id(), + userId: S.String(), + name: S.String(), + duration: S.Number(), + target: S.Number(), + increment: S.Number(), + unit: S.String({ nullable: true }), // "minutes", "reps", "pages" + active: S.Boolean({ default: true }), + startDate: S.Date({ default: S.Default.now() }), + endDate: S.Date({ nullable: true }), + createdAt: S.Date({ default: S.Default.now() }), + updatedAt: S.Date({ default: S.Default.now() }), + }), + relationships: { + completions: S.RelationMany('habit_completions', { + where: [['habitId', '=', '$id']], + }), + }, + permissions: { + owner: { + read: { filter: [['userId', '=', '$userId']] }, + insert: { filter: [['userId', '=', '$userId']] }, + update: { filter: [['userId', '=', '$userId']] }, + delete: { filter: [['userId', '=', '$userId']] } + } + } + }, + habit_completions: { + schema: S.Schema({ + id: S.Id(), + habitId: S.String(), + completedAt: S.Date({ default: S.Default.now() }), + value: S.Number(), + failed: S.Boolean({ default: false }), + }), + relationships: { + habit: S.RelationById('habits', '$habitId'), + }, + permissions: { + owner: { + read: { + filter: [ + ['habitId', 'in', { collectionName: 'habits', where: [['userId', '=', '$userId']] }] + ] + }, + insert: { + filter: [ + ['habitId', 'in', { collectionName: 'habits', where: [['userId', '=', '$userId']] }] + ] + }, + update: { + filter: [ + ['habitId', 'in', { collectionName: 'habits', where: [['userId', '=', '$userId']] }] + ] + }, + delete: { + filter: [ + ['habitId', 'in', { collectionName: 'habits', where: [['userId', '=', '$userId']] }] + ] + } + } + } + } +}); + +// Use the `Entity` type to extract clean types for your collections +export type Habit = Entity; +export type HabitCompletion = Entity; \ No newline at end of file