All files / src/core/services D1Service.ts

23.07% Statements 3/13
0% Branches 0/2
0% Functions 0/7
27.27% Lines 3/11

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 10728x                             28x                                     28x                                                                                                                                                
import { D1Binding } from '@/core/bindings/D1Binding'
 
import type { DataStore } from '@bayudwiyansatria/core'
import type { D1Statement } from '@/types/d1/D1Statement'
import type { D1WriteResult } from '@/types/d1/D1WriteResult'
import type { CloudflareEnv } from '@/types/CloudflareEnv'
 
/**
 * D1 database binding.
 *
 * Every setting it needs — binding name and any defaults — arrives resolved
 * from the configuration layer, so nothing about them is decided here. The
 * binding itself is read from `env` per call, which is what makes one
 * module-scope instance safe to share.
 */
const db = new D1Binding()
 
/**
 * D1 capability.
 *
 * The relational-storage half of the core: queries, writes, and batches
 * expressed in SQL, with the binding and its configuration kept out of sight.
 *
 * @remarks
 * Callers pass values separately from the statement, so user input is always
 * parameterised. Nothing here knows what the rows mean — the schema belongs to
 * whichever business service owns it.
 *
 * @class
 *
 * @author Bayu Dwiyan Satria
 * @version 1.0.0
 * @since 1.0.0
 */
export class D1Service<E extends CloudflareEnv = CloudflareEnv> implements DataStore<E> {
  /**
   * Runs a query and returns every row.
   *
   * @param env The Worker environment.
   * @param sql SQL statement, using `?` placeholders for bind values.
   * @param values Values bound to the placeholders, in order.
   * @returns The matched rows, empty when the query matched nothing.
   */
  public async all<T = Record<string, unknown>>(env: E, sql: string, ...values: unknown[]): Promise<T[]> {
    return await db.all<T>(env, sql, ...values)
  }
 
  /**
   * Runs a query and returns its first row.
   *
   * @param env The Worker environment.
   * @param sql SQL statement, using `?` placeholders for bind values.
   * @param values Values bound to the placeholders, in order.
   * @returns The first matched row, or `null` when the query matched nothing.
   */
  public async first<T = Record<string, unknown>>(env: E, sql: string, ...values: unknown[]): Promise<T | null> {
    return await db.first<T>(env, sql, ...values)
  }
 
  /**
   * Runs a write and reports what it changed.
   *
   * @param env The Worker environment.
   * @param sql SQL statement, using `?` placeholders for bind values.
   * @param values Values bound to the placeholders, in order.
   * @returns The row count and the id assigned by an insert.
   */
  public async write(env: E, sql: string, ...values: unknown[]): Promise<D1WriteResult> {
    const result = await db.run(env, sql, ...values)
 
    return {
      changes: result.meta.changes,
      lastRowId: result.meta.last_row_id
    }
  }
 
  /**
   * Runs several statements as one batch.
   *
   * The batch shares an implicit transaction, so a failure part-way through
   * rolls the whole set back.
   *
   * @param env The Worker environment.
   * @param statements Statements and their bind values, in execution order.
   * @returns One result per statement, in the order supplied.
   */
  public async batch(env: E, statements: D1Statement[]): Promise<D1WriteResult[]> {
    const prepared = statements.map(statement => db.prepare(env, statement.sql, ...(statement.values || [])))
    const results = await db.batch(env, prepared)
 
    return results.map(result => ({
      changes: result.meta.changes,
      lastRowId: result.meta.last_row_id
    }))
  }
 
  /**
   * Reports whether the database is bound to this Worker.
   *
   * @param env The Worker environment.
   * @returns `true` when the binding is present.
   */
  public isAvailable(env: E): boolean {
    return db.isBound(env)
  }
}