• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vzakharchenko / forge-sql-orm / 19837660918

01 Dec 2025 09:10PM UTC coverage: 84.777% (-0.2%) from 84.933%
19837660918

push

github

vzakharchenko
fix sonar

653 of 832 branches covered (78.49%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

1246 of 1408 relevant lines covered (88.49%)

20.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.49
/src/core/ForgeSQLSelectOperations.ts
1
import { sql, UpdateQueryResponse } from "@forge/sql";
2
import { ForgeSqlOrmOptions, SchemaSqlForgeSql } from "./ForgeSQLQueryBuilder";
3
import {
4
  AnyMySqlSelectQueryBuilder,
5
  MySqlSelectDynamic,
6
} from "drizzle-orm/mysql-core/query-builders/select.types";
7

8
/**
9
 * Class implementing SQL select operations for ForgeSQL ORM.
10
 * Provides methods for executing queries and mapping results to entity types.
11
 */
12
export class ForgeSQLSelectOperations implements SchemaSqlForgeSql {
13
  private readonly options: ForgeSqlOrmOptions;
14

15
  /**
16
   * Creates a new instance of ForgeSQLSelectOperations.
17
   * @param {ForgeSqlOrmOptions} options - Configuration options for the ORM
18
   */
19
  constructor(options: ForgeSqlOrmOptions) {
20
    this.options = options;
3✔
21
  }
22

23
  /**
24
   * Executes a Drizzle query and returns a single result.
25
   * Throws an error if more than one record is returned.
26
   *
27
   * @template T - The type of the query builder
28
   * @param {T} query - The Drizzle query to execute
29
   * @returns {Promise<Awaited<T> extends Array<any> ? Awaited<T>[number] | undefined : Awaited<T> | undefined>} A single result object or undefined
30
   * @throws {Error} If more than one record is returned
31
   */
32
  async executeQueryOnlyOne<T extends MySqlSelectDynamic<AnyMySqlSelectQueryBuilder>>(
33
    query: T,
34
  ): Promise<
35
    Awaited<T> extends Array<any> ? Awaited<T>[number] | undefined : Awaited<T> | undefined
36
  > {
37
    const results: Awaited<T> = await query;
4✔
38
    const datas = results as unknown[];
4✔
39
    if (!datas.length) {
4✔
40
      return undefined;
1✔
41
    }
42
    if (datas.length > 1) {
3✔
43
      throw new Error(`Expected 1 record but returned ${datas.length}`);
1✔
44
    }
45

46
    return datas[0] as Awaited<T> extends Array<any> ? Awaited<T>[number] : Awaited<T>;
2✔
47
  }
48

49
  /**
50
   * Executes a raw SQL query and returns the results.
51
   * Logs the query if logging is enabled.
52
   *
53
   * @template T - The type of the result objects
54
   * @param {string} query - The raw SQL query to execute
55
   * @param {SqlParameters[]} [params] - Optional SQL parameters
56
   * @returns {Promise<T[]>} A list of results as objects
57
   */
58
  async executeRawSQL<T extends object | unknown>(query: string, params?: unknown[]): Promise<T[]> {
59
    if (this.options.logRawSqlQuery) {
1!
60
      const paramsStr = params ? `, with params: ${JSON.stringify(params)}` : "";
1!
61
      // eslint-disable-next-line no-console
62
      console.debug(`Executing with SQL ${query}${paramsStr}`);
1✔
63
    }
64
    const sqlStatement = sql.prepare<T>(query);
1✔
65
    if (params) {
1!
UNCOV
66
      sqlStatement.bindParams(...params);
×
67
    }
68
    const result = await sqlStatement.execute();
1✔
69
    return result.rows as T[];
1✔
70
  }
71

72
  /**
73
   * Executes a raw SQL update query.
74
   * @param {string} query - The raw SQL update query
75
   * @param {SqlParameters[]} [params] - Optional SQL parameters
76
   * @returns {Promise<UpdateQueryResponse>} The update response containing affected rows
77
   */
78
  async executeRawUpdateSQL(query: string, params?: unknown[]): Promise<UpdateQueryResponse> {
79
    const sqlStatement = sql.prepare<UpdateQueryResponse>(query);
1✔
80
    if (params) {
1!
81
      sqlStatement.bindParams(...params);
×
82
    }
83
    if (this.options.logRawSqlQuery) {
1!
84
      // eslint-disable-next-line no-console
85
      console.debug(
1✔
86
        `Executing Update with SQL ${query}` +
87
          (params ? `, with params: ${JSON.stringify(params)}` : ""),
1!
88
      );
89
    }
90
    const updateQueryResponseResults = await sqlStatement.execute();
1✔
91
    return updateQueryResponseResults.rows;
1✔
92
  }
93
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc