All files / src/associations include.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140                                                                                                                                                                                                                                                                                       
import type { ModelDefinition } from '../model/model-definition.ts';
import type { AnyModel, ModelStatic } from '../model/options.ts';
import type { FindAttributeOptions } from '../model/select-query-builder.ts';
import type { Association } from './types.ts';
 
type Model = ModelStatic<AnyModel>;
 
/**
 * Options of the junction table of an included belongsToMany association.
 *
 * @example
 * ```ts
 * const through: IncludeThroughOptions = { attributes: ['role'], where: { role: 'owner' } };
 * const rows = await Project.findAll({ include: [{ model: User, through }] });
 * ```
 */
export type IncludeThroughOptions = Readonly<{
  /** Junction attributes to select (`[]` for none). Default: all. */
  attributes?: FindAttributeOptions;
  /** Filter on junction rows; see {@link WhereOptions}. */
  where?: unknown;
}>;
 
/**
 * One `include` entry (Sequelize v6 `IncludeOptions`): which association to load and how.
 *
 * Name the association with `model`, `as` or `association`. Eager loading uses JOINs (see the guide), except `separate`, which runs another query.
 *
 * @see {@link FindOptions}
 *
 * @example
 * ```ts
 * const opts: IncludeOptions = {
 *   model: Post,
 *   as: 'posts',
 *   where: { published: true },
 *   required: false,
 *   attributes: ['id', 'title'],
 *   include: [{ model: Tag }],
 * };
 * const users = await User.findAll({ include: [opts] });
 * ```
 */
export type IncludeOptions = {
  /** Associated model; the association is looked up by model (and `as`). */
  model?: Model;
  /** Alias of the association, as given when it was defined. */
  as?: string;
  /** Association object or alias. */
  association?: Association | string;
  /** Filter on the included rows; makes the include required unless `required: false`. See {@link WhereOptions}. */
  where?: unknown;
  /**
   * Custom `ON` condition of the join; replaces the condition derived from the association keys.
   * Not used by belongsToMany includes (the junction joins keep their key conditions). See {@link WhereOptions}.
   */
  on?: unknown;
  /** `INNER JOIN` (true) or `LEFT OUTER JOIN` (false). */
  required?: boolean;
  /** Use `RIGHT OUTER JOIN`.
   * @defaultValue false */
  right?: boolean;
  /** Selected attributes. */
  attributes?: FindAttributeOptions;
  /** Nested includes. */
  include?: IncludeOption;
  /** Load a hasMany association with a separate query instead of a JOIN.
   * @defaultValue false */
  separate?: boolean;
  /** Junction options for belongsToMany. */
  through?: IncludeThroughOptions;
  /**
   * Fetch only the first `limit` rows per parent in the database (`JOIN LATERAL` over the parent
   * keys) instead of loading every child and trimming in memory. Needs `separate` and `limit`.
   * PostgreSQL extension.
   *
   * @postgrelize
   * @defaultValue false
   */
  lateral?: boolean;
  /** Order of a separate include. */
  order?: unknown;
  /** Maximum rows per include of a separate include. */
  limit?: number;
  /** For paranoid targets: `false` includes soft-deleted rows.
   * @defaultValue true */
  paranoid?: boolean;
  /** Include every association of the model (`{ all: true }`).
   * @defaultValue false */
  all?: boolean;
};
 
/**
 * One entry of the `include` option (Sequelize v6 `Includeable`): a model, an association (or its
 * alias), include options, or `{ all: true }` for every association of the model.
 *
 * @see {@link IncludeOptions}
 *
 * @example
 * ```ts
 * const entries: Includeable[] = [Post, 'profile', { model: Tag, required: false }, { all: true }];
 * const users = await User.findAll({ include: entries });
 * ```
 */
export type Includeable = Model | Association | string | IncludeOptions;
 
/** `include` option: one entry or a list of entries (Sequelize v6 `Includeable | Includeable[]`). */
export type IncludeOption = Includeable | readonly Includeable[];
 
/** Normalized include. */
export type IncludeNode = Readonly<{
  association: Association;
  model: Model;
  definition: ModelDefinition;
  as: string;
  alias: string;
  path: string;
  where: unknown;
  on: unknown;
  required: boolean;
  right: boolean;
  attributes: FindAttributeOptions | undefined;
  separate: boolean;
  lateral: boolean;
  paranoid: boolean;
  order: unknown;
  limit: number | undefined;
  through:
    | Readonly<{
        model: Model;
        definition: ModelDefinition;
        alias: string;
        path: string;
        attributes: FindAttributeOptions | undefined;
        where: unknown;
      }>
    | undefined;
  children: readonly IncludeNode[];
}>;