Skip to content

Relations

jsorm uses explicit relation builders instead of generic relation metadata.

  • t.belongsTo(Model)
  • t.hasOne(Model)
  • t.hasMany(Model)
  • t.manyToMany(Model)
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),
});
await db.update(User, {
data: {
role: { connect: 1 },
profile: {
create: { bio: 'Builder' },
},
tags: {
connect: [1, 2],
},
},
where: {
id: 1,
},
});
  1. Use relation builders that match the real data shape.
  2. Configure onUpdate and onDelete intentionally.
  3. Keep relation mutations close to write operations instead of scattering pivot logic manually.