|
| 1 | +import type { WhereOptions } from '../abstract-dialect/where-sql-builder-types.js'; |
| 2 | +import type { FindAttributeOptions, Model, ModelStatic } from '../model.d.ts'; |
| 3 | +import { BaseSqlExpression, SQL_IDENTIFIER } from './base-sql-expression.js'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Do not use me directly. Use Model.select() instead. |
| 7 | + */ |
| 8 | +export class QueryBuilder<M extends Model = Model> extends BaseSqlExpression { |
| 9 | + declare protected readonly [SQL_IDENTIFIER]: 'queryBuilder'; |
| 10 | + |
| 11 | + private readonly _model: ModelStatic<M>; |
| 12 | + private _attributes?: FindAttributeOptions; |
| 13 | + private _where?: WhereOptions; |
| 14 | + private _isSelect: boolean = false; |
| 15 | + |
| 16 | + constructor(model: ModelStatic<M>) { |
| 17 | + super(); |
| 18 | + this._model = model; |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Initialize a SELECT query |
| 23 | + * |
| 24 | + * @returns The query builder instance for chaining |
| 25 | + */ |
| 26 | + select(): QueryBuilder<M> { |
| 27 | + const newBuilder = new QueryBuilder(this._model); |
| 28 | + newBuilder._isSelect = true; |
| 29 | + if (this._attributes !== undefined) { |
| 30 | + newBuilder._attributes = this._attributes; |
| 31 | + } |
| 32 | + |
| 33 | + if (this._where !== undefined) { |
| 34 | + newBuilder._where = this._where; |
| 35 | + } |
| 36 | + |
| 37 | + return newBuilder; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Specify which attributes to select |
| 42 | + * |
| 43 | + * @param attributes - Array of attribute names or attribute options |
| 44 | + * @returns The query builder instance for chaining |
| 45 | + */ |
| 46 | + attributes(attributes: FindAttributeOptions): QueryBuilder<M> { |
| 47 | + const newBuilder = new QueryBuilder(this._model); |
| 48 | + newBuilder._isSelect = this._isSelect; |
| 49 | + newBuilder._attributes = attributes; |
| 50 | + newBuilder._where = this._where; |
| 51 | + |
| 52 | + return newBuilder; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Add WHERE conditions to the query |
| 57 | + * |
| 58 | + * @param conditions - Where conditions object |
| 59 | + * @returns The query builder instance for chaining |
| 60 | + */ |
| 61 | + where(conditions: WhereOptions): QueryBuilder<M> { |
| 62 | + const newBuilder = new QueryBuilder(this._model); |
| 63 | + newBuilder._isSelect = this._isSelect; |
| 64 | + if (this._attributes !== undefined) { |
| 65 | + newBuilder._attributes = this._attributes; |
| 66 | + } |
| 67 | + |
| 68 | + newBuilder._where = conditions; |
| 69 | + |
| 70 | + return newBuilder; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Generate the SQL query string |
| 75 | + * |
| 76 | + * @returns The SQL query |
| 77 | + */ |
| 78 | + getQuery(): string { |
| 79 | + if (!this._isSelect) { |
| 80 | + throw new Error('Query builder requires select() to be called first'); |
| 81 | + } |
| 82 | + |
| 83 | + const queryGenerator = this._model.queryGenerator; |
| 84 | + const tableName = this._model.tableName; |
| 85 | + |
| 86 | + // Build the options object that matches Sequelize's FindOptions pattern |
| 87 | + const options: any = { |
| 88 | + attributes: this._attributes, |
| 89 | + where: this._where, |
| 90 | + raw: true, |
| 91 | + plain: false, |
| 92 | + }; |
| 93 | + |
| 94 | + // Generate the SQL using the existing query generator |
| 95 | + const sql = queryGenerator.selectQuery(tableName, options, this._model); |
| 96 | + |
| 97 | + return sql; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Get the table name for this query |
| 102 | + * |
| 103 | + * @returns The table name |
| 104 | + */ |
| 105 | + get tableName(): string { |
| 106 | + return this._model.tableName; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Get the model class |
| 111 | + * |
| 112 | + * @returns The model class |
| 113 | + */ |
| 114 | + get model(): ModelStatic<M> { |
| 115 | + return this._model; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Creates a new QueryBuilder instance for the given model |
| 121 | + * |
| 122 | + * @param model - The model class |
| 123 | + * @returns A new query builder instance |
| 124 | + */ |
| 125 | +export function createQueryBuilder<M extends Model>(model: ModelStatic<M>): QueryBuilder<M> { |
| 126 | + return new QueryBuilder(model); |
| 127 | +} |
0 commit comments