Relaciones
Relaciones
Section titled “Relaciones”jsorm usa relation builders explícitos en lugar de metadata genérica.
Relation builders
Section titled “Relation builders”t.belongsTo(Model)t.hasOne(Model)t.hasMany(Model)t.manyToMany(Model)
Ejemplo
Section titled “Ejemplo”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),});Mutaciones de relaciones
Section titled “Mutaciones de relaciones”await db.update(User, { data: { role: { connect: 1 }, profile: { create: { bio: 'Builder' }, }, tags: { connect: [1, 2], }, }, where: { id: 1, },});Buenas prácticas
Section titled “Buenas prácticas”- Usa el relation builder que coincida con la forma real de los datos.
- Configura
onUpdateyonDeletede forma intencional. - Mantén las mutaciones de relaciones cerca de la operación de escritura.