Relations
Relations
Section titled “Relations”jsorm uses explicit relation builders instead of generic relation metadata.
Relation builders
Section titled “Relation builders”t.belongsTo(Model)t.hasOne(Model)t.hasMany(Model)t.manyToMany(Model)
Example
Section titled “Example”const User = defineModel('users', { id: t.number().primary(), role: t.belongsTo(Role, { onUpdate: 'cascade', onDelete: 'restrict', }), profile: t.hasOne(Profile), posts: t.hasMany(Post), tags: t.manyToMany(Tag),});Relation mutations
Section titled “Relation mutations”await db.update(User, { data: { role: { connect: 1 }, profile: { create: { bio: 'Builder' }, }, tags: { connect: [1, 2], }, }, where: { id: 1, },});Best practices
Section titled “Best practices”- Use relation builders that match the real data shape.
- Configure
onUpdateandonDeleteintentionally. - Keep relation mutations close to write operations instead of scattering pivot logic manually.