add triplt db schema
This commit is contained in:
@@ -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<typeof schema, 'habits'>;
|
||||
export type HabitCompletion = Entity<typeof schema, 'habit_completions'>;
|
||||
Reference in New Issue
Block a user