CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
integer.js125 linesDownload Raw Back to columns
1import { entityKind } from "../../entity.js";2import { sql } from "../../sql/sql.js";3import { getColumnNameAndConfig } from "../../utils.js";4import { SQLiteColumn, SQLiteColumnBuilder } from "./common.js";5class SQLiteBaseIntegerBuilder extends SQLiteColumnBuilder {6  static [entityKind] = "SQLiteBaseIntegerBuilder";7  constructor(name, dataType, columnType) {8    super(name, dataType, columnType);9    this.config.autoIncrement = false;10  }11  primaryKey(config) {12    if (config?.autoIncrement) {13      this.config.autoIncrement = true;14    }15    this.config.hasDefault = true;16    return super.primaryKey();17  }18}19class SQLiteBaseInteger extends SQLiteColumn {20  static [entityKind] = "SQLiteBaseInteger";21  autoIncrement = this.config.autoIncrement;22  getSQLType() {23    return "integer";24  }25}26class SQLiteIntegerBuilder extends SQLiteBaseIntegerBuilder {27  static [entityKind] = "SQLiteIntegerBuilder";28  constructor(name) {29    super(name, "number", "SQLiteInteger");30  }31  build(table) {32    return new SQLiteInteger(33      table,34      this.config35    );36  }37}38class SQLiteInteger extends SQLiteBaseInteger {39  static [entityKind] = "SQLiteInteger";40}41class SQLiteTimestampBuilder extends SQLiteBaseIntegerBuilder {42  static [entityKind] = "SQLiteTimestampBuilder";43  constructor(name, mode) {44    super(name, "date", "SQLiteTimestamp");45    this.config.mode = mode;46  }47  /**48   * @deprecated Use `default()` with your own expression instead.49   *50   * Adds `DEFAULT (cast((julianday('now') - 2440587.5)*86400000 as integer))` to the column, which is the current epoch timestamp in milliseconds.51   */52  defaultNow() {53    return this.default(sql`(cast((julianday('now') - 2440587.5)*86400000 as integer))`);54  }55  build(table) {56    return new SQLiteTimestamp(57      table,58      this.config59    );60  }61}62class SQLiteTimestamp extends SQLiteBaseInteger {63  static [entityKind] = "SQLiteTimestamp";64  mode = this.config.mode;65  mapFromDriverValue(value) {66    if (this.config.mode === "timestamp") {67      return new Date(value * 1e3);68    }69    return new Date(value);70  }71  mapToDriverValue(value) {72    const unix = value.getTime();73    if (this.config.mode === "timestamp") {74      return Math.floor(unix / 1e3);75    }76    return unix;77  }78}79class SQLiteBooleanBuilder extends SQLiteBaseIntegerBuilder {80  static [entityKind] = "SQLiteBooleanBuilder";81  constructor(name, mode) {82    super(name, "boolean", "SQLiteBoolean");83    this.config.mode = mode;84  }85  build(table) {86    return new SQLiteBoolean(87      table,88      this.config89    );90  }91}92class SQLiteBoolean extends SQLiteBaseInteger {93  static [entityKind] = "SQLiteBoolean";94  mode = this.config.mode;95  mapFromDriverValue(value) {96    return Number(value) === 1;97  }98  mapToDriverValue(value) {99    return value ? 1 : 0;100  }101}102function integer(a, b) {103  const { name, config } = getColumnNameAndConfig(a, b);104  if (config?.mode === "timestamp" || config?.mode === "timestamp_ms") {105    return new SQLiteTimestampBuilder(name, config.mode);106  }107  if (config?.mode === "boolean") {108    return new SQLiteBooleanBuilder(name, config.mode);109  }110  return new SQLiteIntegerBuilder(name);111}112const int = integer;113export {114  SQLiteBaseInteger,115  SQLiteBaseIntegerBuilder,116  SQLiteBoolean,117  SQLiteBooleanBuilder,118  SQLiteInteger,119  SQLiteIntegerBuilder,120  SQLiteTimestamp,121  SQLiteTimestampBuilder,122  int,123  integer124};125//# sourceMappingURL=integer.js.map