Skip to main content
Models in your application are rarely isolated. Esix ships with three ActiveRecord-style helpers that make it easy to traverse relationships between collections: hasMany, hasOne, and belongsTo. Each helper follows the same convention: by default, the foreign key is the camelCased class name with Id appended (e.g. AuthorauthorId), and the local/owner key is id. Both keys can be overridden when your schema differs.

hasMany

Use hasMany when a parent record can own many related records. It returns a QueryBuilder, so you can chain additional constraints before fetching:
author
allPosts recentPosts You can pass a custom foreign key, local key, or both:

hasOne

hasOne mirrors hasMany but returns a single record (or null). It’s the right choice for true one-to-one relationships such as a user and their profile:
user
profile
Like hasMany, you can override the foreign and local keys:

belongsTo

belongsTo is the inverse of hasOne / hasMany: it looks up the parent record from a foreign key stored on the current model.
post
author
By default belongsTo uses the parent’s id as the owner key and looks up the foreign key derived from the parent class name. Override either if your schema uses different fields:
If the foreign key is null or undefined on the current model, belongsTo returns null without hitting the database.