All files / src/query query-result-shaper.ts

100% Statements 17/17
87.09% Branches 27/31
100% Functions 6/6
100% Lines 17/17

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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244                131x                                                                                                                                                                                                                                                                                                                                                             131x                   2425x                               1153x 1153x   906x     3x   1x   2x     2x   239x         1153x 1153x 1152x 1x     1x 3x              
import type { QueryResult } from '../connection/connection-manager.ts';
import type { AnyModel, ModelStatic, Values } from '../model/options.ts';
import type { RowMapper } from '../model/row-mapper.ts';
import type { QueryOptionsBase } from '../model/runtime.ts';
import type { ColumnDescription } from '../query-interface/query-interface.ts';
import type { BindParameters, Replacements } from './raw-query.ts';
 
/** Query types of `sequelize.query` (Sequelize v6 `QueryTypes`). */
export const QueryTypes = {
  /** Returns rows. */
  SELECT: 'SELECT',
  /** Returns `[rows, rowCount]`. */
  INSERT: 'INSERT',
  /** Returns `[rows, rowCount]`. */
  UPDATE: 'UPDATE',
  /** Returns `[undefined, null]` like v6 on PostgreSQL (the statement runs; its rows are not returned). */
  UPSERT: 'UPSERT',
  /** Returns the rows of the statement (`[]` without `RETURNING`). */
  DELETE: 'DELETE',
  /** Returns the number of affected rows. */
  BULKUPDATE: 'BULKUPDATE',
  /** Returns the number of affected rows. */
  BULKDELETE: 'BULKDELETE',
  /** Returns `[rows, result]` (default). */
  RAW: 'RAW',
  /** Returns `[rows, result]`. */
  SHOWTABLES: 'SHOWTABLES',
  /** Returns `[rows, result]`. */
  DESCRIBE: 'DESCRIBE',
  /** Returns `[rows, result]`. */
  VERSION: 'VERSION',
  /** Returns `[rows, result]`. */
  FOREIGNKEYS: 'FOREIGNKEYS',
  /** Returns `[rows, result]`. */
  SHOWINDEXES: 'SHOWINDEXES',
  /** Returns `[rows, result]`. */
  SHOWCONSTRAINTS: 'SHOWCONSTRAINTS',
} as const;
 
/** One of {@link QueryTypes}. */
export type QueryType = (typeof QueryTypes)[keyof typeof QueryTypes];
 
/** `QueryTypes` is also a type, as the v6 enum is: the union of the query type names. */
export type QueryTypes = QueryType;
 
/** Member types of {@link QueryTypes}, so `QueryTypes.SELECT` works in type positions like the v6 enum members. */
// oxlint-disable-next-line typescript/no-namespace, typescript/no-redeclare -- merges the v6 enum's type members into the constant.
export declare namespace QueryTypes {
  /** Type of `QueryTypes.SELECT`. */
  type SELECT = typeof QueryTypes.SELECT;
  /** Type of `QueryTypes.INSERT`. */
  type INSERT = typeof QueryTypes.INSERT;
  /** Type of `QueryTypes.UPDATE`. */
  type UPDATE = typeof QueryTypes.UPDATE;
  /** Type of `QueryTypes.BULKUPDATE`. */
  type BULKUPDATE = typeof QueryTypes.BULKUPDATE;
  /** Type of `QueryTypes.BULKDELETE`. */
  type BULKDELETE = typeof QueryTypes.BULKDELETE;
  /** Type of `QueryTypes.DELETE`. */
  type DELETE = typeof QueryTypes.DELETE;
  /** Type of `QueryTypes.UPSERT`. */
  type UPSERT = typeof QueryTypes.UPSERT;
  /** Type of `QueryTypes.VERSION`. */
  type VERSION = typeof QueryTypes.VERSION;
  /** Type of `QueryTypes.SHOWTABLES`. */
  type SHOWTABLES = typeof QueryTypes.SHOWTABLES;
  /** Type of `QueryTypes.SHOWINDEXES`. */
  type SHOWINDEXES = typeof QueryTypes.SHOWINDEXES;
  /** Type of `QueryTypes.SHOWCONSTRAINTS`. */
  type SHOWCONSTRAINTS = typeof QueryTypes.SHOWCONSTRAINTS;
  /** Type of `QueryTypes.DESCRIBE`. */
  type DESCRIBE = typeof QueryTypes.DESCRIBE;
  /** Type of `QueryTypes.RAW`. */
  type RAW = typeof QueryTypes.RAW;
  /** Type of `QueryTypes.FOREIGNKEYS`. */
  type FOREIGNKEYS = typeof QueryTypes.FOREIGNKEYS;
}
 
/**
 * Options of `sequelize.query`, in addition to the common query options such as `transaction` and `logging`.
 *
 * @example
 * ```ts
 * const rows = await sequelize.query('SELECT * FROM users WHERE id = :id', {
 *   type: QueryTypes.SELECT,
 *   replacements: { id: 1 },
 *   model: User,
 *   mapToModel: true,
 * });
 * ```
 */
export type QueryOptions = QueryOptionsBase &
  Readonly<{
    /** Shapes the result; default `RAW`. */
    type?: QueryType;
    /** Values for `?` / `:name` placeholders; bound as parameters. */
    replacements?: Replacements;
    /** Values for `$1` / `$name` placeholders. */
    bind?: BindParameters;
    /** Return only the first row (or `null`). */
    plain?: boolean;
    /** Build instances of this model from SELECT rows. */
    model?: ModelStatic<AnyModel>;
    /** Map column names to attribute names of `model`. */
    mapToModel?: boolean;
    /** Return plain rows even with `model`. */
    raw?: boolean;
    /** Nests `a.b` keys of the rows into objects (`{ a: { b } }`); implies `type: SELECT` unless a type is given. */
    nest?: boolean;
  }>;
 
/**
 * Result types of `sequelize.query` per query `type`, as the Sequelize v6 typings declare them.
 *
 * @example
 * ```ts
 * type Updated = TypedQueryResults['UPDATE']; // [undefined, number]
 * ```
 */
export type TypedQueryResults = {
  /** `QueryTypes.UPDATE`. */
  UPDATE: [undefined, number];
  /** `QueryTypes.BULKUPDATE`: affected rows. */
  BULKUPDATE: number;
  /** `QueryTypes.INSERT`. */
  INSERT: [number, number];
  /** `QueryTypes.UPSERT`. */
  UPSERT: number;
  /** `QueryTypes.DELETE`. */
  DELETE: void;
  /** `QueryTypes.BULKDELETE`: affected rows. */
  BULKDELETE: number;
  /** `QueryTypes.SHOWTABLES`: table names. */
  SHOWTABLES: string[];
  /** `QueryTypes.DESCRIBE`: column descriptions by column name. */
  DESCRIBE: Record<string, ColumnDescription>;
};
 
/**
 * {@link QueryOptions} with a required query `type` (Sequelize v6 `QueryOptionsWithType`).
 *
 * @typeParam T - The query type, e.g. `QueryTypes.SELECT`.
 *
 * @example
 * ```ts
 * const options: QueryOptionsWithType<QueryTypes.SELECT> = { type: QueryTypes.SELECT };
 * const rows = await sequelize.query('SELECT 1 AS one', options);
 * ```
 */
export type QueryOptionsWithType<T extends QueryType> = QueryOptions &
  Readonly<{
    /** The query type. */
    type: T;
  }>;
 
/**
 * {@link QueryOptions} with a required `model` (Sequelize v6 `QueryOptionsWithModel`): the rows become instances of it.
 *
 * @typeParam M - The model instance type.
 *
 * @example
 * ```ts
 * const options: QueryOptionsWithModel<Model> = { model: User, mapToModel: true };
 * const users = await sequelize.query('SELECT * FROM users', options);
 * ```
 */
export type QueryOptionsWithModel<M extends AnyModel> = QueryOptions &
  Readonly<{
    /** Model the rows are built as. */
    model: ModelStatic<M>;
  }>;
 
/**
 * Shapes the result of `sequelize.query` like Sequelize v6: rows (optionally as model instances),
 * `[rows, rowCount]`, a row count or `[rows, result]` depending on the query type.
 */
export class QueryResultShaper {
  readonly #rows: RowMapper;
 
  /**
   * @param rows - Maps columns to attributes for `mapToModel`.
   */
  constructor(rows: RowMapper) {
    this.#rows = rows;
  }
 
  /**
   * Query type in effect: the `type` option, or `SELECT` with `model` / `plain`.
   *
   * @param options - Query options.
   * @returns Query type, or `undefined` for RAW.
   */
  type(options: QueryOptions): QueryType | undefined {
    return (
      options.type ??
      (options.model !== undefined || options.plain === true || options.nest === true
        ? QueryTypes.SELECT
        : undefined)
    );
  }
 
  /**
   * Shapes a driver result according to the query options.
   *
   * @param result - Driver result.
   * @param options - Query options.
   * @returns Shaped result.
   */
  shape(result: QueryResult, options: QueryOptions): unknown {
    const rows = this.#shapeRows(result, options);
    switch (this.type(options)) {
      case QueryTypes.SELECT:
        return options.plain === true ? (rows[0] ?? null) : rows;
      case QueryTypes.INSERT:
      case QueryTypes.UPDATE:
        return [rows, result.rowCount];
      case QueryTypes.UPSERT:
        return [undefined, null];
      case QueryTypes.DELETE:
        return rows;
      case QueryTypes.BULKDELETE:
      case QueryTypes.BULKUPDATE:
        return result.rowCount;
      default:
        return options.plain === true ? (rows[0] ?? null) : [rows, result];
    }
  }
 
  #shapeRows(result: QueryResult, options: QueryOptions): readonly unknown[] {
    const model = options.model;
    if (model === undefined || options.raw === true) {
      return options.nest === true
        ? result.rows.map((row) => this.#rows.nest(row as Values))
        : result.rows;
    }
    return result.rows.map((row) =>
      model.build(options.mapToModel === true ? this.#rows.parseModel(model, row) : row, {
        isNewRecord: false,
        raw: true,
      }),
    );
  }
}