Skip to content

egon-orm-workspace / egon-orm / hasMany

Function: hasMany() ​

Call Signature ​

ts
function hasMany<T, FK>(target, options): Assoc<"hasMany", T, FK>;

Defined in: core/src/schema/assoc.ts:277

Declares that any number of rows of target point at this schema. foreignKey names the field on the target and is required.

With through, the rows are reached by following other associations instead; such an association can be joined and preloaded, but not written through.

Preloaded, it is an array of the target's rows.

Type Parameters ​

T ​

T

FK ​

FK extends | string | readonly [FieldNames<T>, FieldNames<T>]

Parameters ​

target ​

() => T

options ​

HasManyOptions<T, FK>

Returns ​

Assoc<"hasMany", T, FK>

Example ​

ts
class User extends Schema("users", { id: t.integer().primaryKey().dbGenerated() }) {
  static posts = hasMany(() => Post, { foreignKey: "authorId", orderBy: ["createdAt"] });
  static comments = hasMany(() => Comment, { through: ["posts", "comments"] });
}

Call Signature ​

ts
function hasMany<T>(target, options): Assoc<"hasMany", T, undefined>;

Defined in: core/src/schema/assoc.ts:281

Declares that any number of rows of target point at this schema. foreignKey names the field on the target and is required.

With through, the rows are reached by following other associations instead; such an association can be joined and preloaded, but not written through.

Preloaded, it is an array of the target's rows.

Type Parameters ​

T ​

T

Parameters ​

target ​

() => T

options ​

ThroughOptions

Returns ​

Assoc<"hasMany", T, undefined>

Example ​

ts
class User extends Schema("users", { id: t.integer().primaryKey().dbGenerated() }) {
  static posts = hasMany(() => Post, { foreignKey: "authorId", orderBy: ["createdAt"] });
  static comments = hasMany(() => Comment, { through: ["posts", "comments"] });
}