Skip to content

Class: HasMany<OM, TM, PKT, FM, PKF>

Represents a one-to-many relationship between models where the ID of the originating model is stored as a property of the foreign model.

Description

For example, a user has many posts:

In your configuration you would have:

typescript
{
  ...
  models: {
      users: {
        schema: '++id, email, createdAt, updatedAt',
        properties: ['id', 'email', 'password'],
        primaryKey: 'id',
        relationships: {
          posts: [HasMany, 'posts', 'user_id'], 
        }
      },
      posts: {
        schema: '++id, user_id, title, body',
        properties: ['id', 'user_id', 'title', 'body'],
      }
  },
  ...
}

Which will then allow you to access all of the user's related posts using the posts accessor on the user model: user.posts.

Remarks

The inverse of this relationship is the BelongsTo relationship. Configured using the HasManyConfiguration tuple.

Extends

Type Parameters

Type ParameterDescription
OM extends Record<string, PlainObject>the map of all models in the database
TM extends StringKeyOf<OM>the table of the originating model
PKT extends StringKeyOf<OM[TM]>the property used as the primary key in the originating model
FM extends StringKeyOf<OM>the table of the foreign model
PKF extends StringKeyOf<OM[FM]>the property used as the primary key in the foreign model