Created
October 30, 2024 12:31
-
-
Save arihantverma/f08382fd874a37cdb29fe9dbdad6cdf5 to your computer and use it in GitHub Desktop.
Discord Link Type Debugging InstantDB: Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
EntitiesDef, | |
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(), | |
}; | |
} | |
function createGraph< | |
EntitiesWithoutLinks extends EntitiesDef, | |
const Links extends LinksDef<EntitiesWithoutLinks>, | |
>(entities: EntitiesWithoutLinks, links: Links) { | |
return [entities, links] as const; | |
} | |
const $users = i.entity({ | |
email: i.string().unique().indexed(), | |
}) | |
const profiles = i.entity( | |
addEssentialAttrs( | |
{ | |
username: i.string().unique().indexed(), | |
name: i.string(), | |
dailyCalorieGoal: i.number(), | |
dailyProteinGoal: i.number(), | |
dailyCarbsGoal: i.number(), | |
dailyFatsGoal: i.number(), | |
dailyFiberGoal: i.number(), | |
healthGoalWhat: i.string(), | |
healthGoalWhy: i.string(), | |
}) | |
) | |
const healthConditions = i.entity( | |
addEssentialAttrs({ | |
name: i.string().indexed(), | |
description: i.string(), | |
}) | |
) | |
const houses = i.entity( | |
addEssentialAttrs({ | |
name: i.string().indexed(), | |
}) | |
) | |
const ingredients = i.entity( | |
addEssentialAttrs({ | |
name: i.string().unique(), | |
unitOfMeasurement: i.string(), | |
nutrients: i.json(), | |
nutrientsQuantity: i.number(), | |
}) | |
) | |
const ingredientLabels = i.entity( | |
addEssentialAttrs({ | |
label: i.string().unique().indexed(), | |
type: i.string().optional(), | |
}) | |
) | |
const stockIngredients = i.entity( | |
addEssentialAttrs({ | |
quantity: i.number(), | |
unit: i.string(), | |
}) | |
) | |
const recipeIngredients = i.entity( | |
addEssentialAttrs({ | |
quantity: i.number(), | |
unit: i.string(), | |
}) | |
) | |
const recipes = i.entity( | |
addEssentialAttrs({ | |
name: i.string().unique(), | |
description: i.string().optional(), | |
isAddableByOtherHouses: i.boolean(), | |
}) | |
) | |
const roles = i.entity( | |
addEssentialAttrs({ | |
name: i.string().unique(), | |
description: i.string(), | |
}) | |
) | |
const permissions = i.entity( | |
addEssentialAttrs({ | |
action: i.string().unique(), | |
entity: i.string().unique(), | |
access: i.string().unique(), | |
description: i.string() | |
}) | |
) | |
const entitiesMap = { | |
$users, | |
profiles, | |
healthConditions, | |
houses, | |
ingredients, | |
ingredientLabels, | |
stockIngredients, | |
recipeIngredients, | |
recipes, | |
roles, | |
permissions | |
} as const | |
const linksMap = { | |
userProfiles: { | |
reverse: { | |
on: "$users", | |
has: "one", | |
label: "profile", | |
}, | |
forward: { | |
on: "profiles", | |
has: "one", | |
label: "user", | |
}, | |
}, | |
userRoles: { | |
reverse: { | |
on: "$users", | |
has: "many", | |
label: "roles", | |
}, | |
forward: { | |
on: "roles", | |
has: "many", | |
label: "users", | |
}, | |
}, | |
rolePermissions: { | |
forward: { | |
on: "roles", | |
has: "many", | |
label: "permissions", | |
}, | |
reverse: { | |
on: "permissions", | |
has: "many", | |
label: "roles", | |
} | |
}, | |
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( | |
...createGraph(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