All files / src/persistence Sequence.ts

61.53% Statements 8/13
37.5% Branches 3/8
75% Functions 3/4
66.66% Lines 8/12

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  9x   9x 9x   9x               39x             17x             792x   792x                        
import { Constructor, Model } from "@decaf-ts/decorator-validation";
import { sequenceNameForModel } from "../identity/utils";
import { SequenceOptions } from "../interfaces/SequenceOptions";
import { InternalError } from "@decaf-ts/db-decorators";
import { Logger, Logging } from "@decaf-ts/logging";
 
export abstract class Sequence {
  private logger!: Logger;
 
  protected get log() {
    Iif (!this.logger) this.logger = Logging.for(this as any);
    return this.logger;
  }
 
  protected constructor(protected readonly options: SequenceOptions) {}
 
  abstract next(): Promise<string | number | bigint>;
  abstract current(): Promise<string | number | bigint>;
  abstract range(count: number): Promise<(number | string | bigint)[]>;
 
  static pk<M extends Model>(model: M | Constructor<M>) {
    return sequenceNameForModel(model, "pk");
  }
 
  static parseValue(
    type: "Number" | "BigInt" | undefined,
    value: string | number | bigint
  ): string | number | bigint {
    switch (type) {
      case "Number":
        return typeof value === "string"
          ? parseInt(value)
          : typeof value === "number"
            ? value
            : BigInt(value);
      case "BigInt":
        return BigInt(value);
      default:
        throw new InternalError("Should never happen");
    }
  }
}