Skip to content

Instantly share code, notes, and snippets.

@arihantverma
Last active October 29, 2024 20:00
Show Gist options
  • Save arihantverma/58299a8dc9c0ba94220da7f1c31d55fb to your computer and use it in GitHub Desktop.
Save arihantverma/58299a8dc9c0ba94220da7f1c31d55fb to your computer and use it in GitHub Desktop.
Discord Link Type Debugging InstantDB
import {
i,
LinksDef,
type AttrsDefs,
} from "@instantdb/react";
function addEssentialAttrs<Attrs extends AttrsDefs>(attrs: Attrs) {
return {
...attrs,
id: i.string(),
createdAt: i.string(),
updatedAt: i.string(),
} as const;
}
const $userTable = i.entity({
email: i.string().unique().indexed(),
});
const profileTable = i.entity(
addEssentialAttrs({
username: i.string().unique().indexed(),
name: i.string(),
createdAt: i.string(),
updatedAt: i.string(),
dailyCalorieGoal: i.number(),
dailyProteinGoal: i.number(),
dailyCarbsGoal: i.number(),
dailyFatsGoal: i.number(),
dailyFiberGoal: i.number(),
healthGoalWhat: i.string(),
healthGoalWhy: i.string(),
})
);
const healthConditionTable = i.entity(
addEssentialAttrs({
name: i.string().indexed(),
description: i.string(),
createdAt: i.string(),
updatedAt: i.string(),
})
);
const housesTable = i.entity(
addEssentialAttrs({
name: i.string().indexed(),
})
);
const ingredientTable = i.entity(
addEssentialAttrs({
name: i.string().unique(),
unitOfMeasurement: i.string(), // TODO define exhasutive list of available units of measurement from the client validated with zod
nutrients: i.json(), // TODO define the exact schema in zod, while taking user input.
nutrientsQuantity: i.number(),
})
);
const ingredientLabelTable = i.entity(
addEssentialAttrs({
label: i.string().unique().indexed(),
type: i.string().optional(), // TODO define exhasutive list of available types from the client validated with zod
})
);
const stockIngredientTable = i.entity(
addEssentialAttrs({
quantity: i.number(),
unit: i.string(),
})
);
const recipeIngredientTable = i.entity(
addEssentialAttrs({
quantity: i.number(),
unit: i.string(),
})
);
const recipeTable = i.entity(
addEssentialAttrs({
name: i.string().unique(),
description: i.string().optional(),
isAddableByOtherHouses: i.boolean(), // which means it's public and addable by other houses.
})
);
const entitiesMap = {
$users: $userTable,
profiles: profileTable,
healthConditions: healthConditionTable,
houses: housesTable,
ingredients: ingredientTable,
ingredientLabels: ingredientLabelTable,
stockIngredients: stockIngredientTable,
recipeIngredients: recipeIngredientTable,
recipes: recipeTable,
} as const;
const linksMap: LinksDef<typeof entitiesMap> = {
userProfiles: {
reverse: {
on: "$users",
has: "one",
label: "profile",
},
forward: {
on: "profiles",
has: "one",
label: "user",
},
},
userCreatedHouses: {
reverse: {
on: "$users",
has: "many",
label: "createdHouses",
},
forward: {
on: "houses",
has: "one",
label: "creator",
},
},
houseMembers: {
reverse: {
on: "$users",
has: "many",
label: "houses",
},
forward: {
on: "houses",
has: "many",
label: "members",
},
},
userHealthConditions: {
reverse: {
on: "$users",
has: "many",
label: "healthConditions",
},
forward: {
on: "healthConditions",
has: "many",
label: "users",
},
},
userCreatedRecipes: {
reverse: {
on: "$users",
has: "many",
label: "createdRecipes",
},
forward: {
on: "recipes",
has: "one",
label: "creator",
},
},
ingredientLabels: {
forward: {
on: "ingredients",
has: "many",
label: "labels",
},
reverse: {
on: "ingredientLabels",
has: "many",
label: "ingredients",
},
},
houseIngredients: {
forward: {
on: "houses",
has: "many",
label: "stockIngredients",
},
reverse: {
on: "stockIngredients",
has: "many",
label: "houses",
},
},
houseRecipes: {
forward: {
on: "houses",
has: "many",
label: "recipes",
},
reverse: {
on: "recipes",
has: "many",
label: "house",
},
},
recipeIngredientsToStockIngredients: {
forward: {
on: "recipeIngredients",
has: "one",
label: "stockIngredient",
},
reverse: {
on: "stockIngredients",
has: "one",
label: "recipeIngredient",
},
},
stockIngredientsToIngredients: {
forward: {
on: "stockIngredients",
has: "one",
label: "ingredient",
},
reverse: {
on: "ingredients",
has: "one",
label: "stockIngredient",
},
},
recipesToRecipesIngredients: {
forward: {
on: "recipes",
has: "many",
label: "recipeIngredients",
},
reverse: {
on: "recipeIngredients",
has: "many",
label: "recipes",
},
},
recipeHealthConditions: {
forward: {
on: "healthConditions",
has: "many",
label: "recipes",
},
reverse: {
on: "recipes",
has: "many",
label: "healthConditions",
},
},
} as const;
const graph = i.graph(entitiesMap, linksMap);
export default graph;
export type DBSchema = typeof graph;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment