integer.cjs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var integer_exports = {};
  20. __export(integer_exports, {
  21. SQLiteBaseInteger: () => SQLiteBaseInteger,
  22. SQLiteBaseIntegerBuilder: () => SQLiteBaseIntegerBuilder,
  23. SQLiteBoolean: () => SQLiteBoolean,
  24. SQLiteBooleanBuilder: () => SQLiteBooleanBuilder,
  25. SQLiteInteger: () => SQLiteInteger,
  26. SQLiteIntegerBuilder: () => SQLiteIntegerBuilder,
  27. SQLiteTimestamp: () => SQLiteTimestamp,
  28. SQLiteTimestampBuilder: () => SQLiteTimestampBuilder,
  29. int: () => int,
  30. integer: () => integer
  31. });
  32. module.exports = __toCommonJS(integer_exports);
  33. var import_entity = require("../../entity.cjs");
  34. var import_sql = require("../../sql/sql.cjs");
  35. var import_utils = require("../../utils.cjs");
  36. var import_common = require("./common.cjs");
  37. class SQLiteBaseIntegerBuilder extends import_common.SQLiteColumnBuilder {
  38. static [import_entity.entityKind] = "SQLiteBaseIntegerBuilder";
  39. constructor(name, dataType, columnType) {
  40. super(name, dataType, columnType);
  41. this.config.autoIncrement = false;
  42. }
  43. primaryKey(config) {
  44. if (config?.autoIncrement) {
  45. this.config.autoIncrement = true;
  46. }
  47. this.config.hasDefault = true;
  48. return super.primaryKey();
  49. }
  50. }
  51. class SQLiteBaseInteger extends import_common.SQLiteColumn {
  52. static [import_entity.entityKind] = "SQLiteBaseInteger";
  53. autoIncrement = this.config.autoIncrement;
  54. getSQLType() {
  55. return "integer";
  56. }
  57. }
  58. class SQLiteIntegerBuilder extends SQLiteBaseIntegerBuilder {
  59. static [import_entity.entityKind] = "SQLiteIntegerBuilder";
  60. constructor(name) {
  61. super(name, "number", "SQLiteInteger");
  62. }
  63. build(table) {
  64. return new SQLiteInteger(
  65. table,
  66. this.config
  67. );
  68. }
  69. }
  70. class SQLiteInteger extends SQLiteBaseInteger {
  71. static [import_entity.entityKind] = "SQLiteInteger";
  72. }
  73. class SQLiteTimestampBuilder extends SQLiteBaseIntegerBuilder {
  74. static [import_entity.entityKind] = "SQLiteTimestampBuilder";
  75. constructor(name, mode) {
  76. super(name, "date", "SQLiteTimestamp");
  77. this.config.mode = mode;
  78. }
  79. /**
  80. * @deprecated Use `default()` with your own expression instead.
  81. *
  82. * Adds `DEFAULT (cast((julianday('now') - 2440587.5)*86400000 as integer))` to the column, which is the current epoch timestamp in milliseconds.
  83. */
  84. defaultNow() {
  85. return this.default(import_sql.sql`(cast((julianday('now') - 2440587.5)*86400000 as integer))`);
  86. }
  87. build(table) {
  88. return new SQLiteTimestamp(
  89. table,
  90. this.config
  91. );
  92. }
  93. }
  94. class SQLiteTimestamp extends SQLiteBaseInteger {
  95. static [import_entity.entityKind] = "SQLiteTimestamp";
  96. mode = this.config.mode;
  97. mapFromDriverValue(value) {
  98. if (this.config.mode === "timestamp") {
  99. return new Date(value * 1e3);
  100. }
  101. return new Date(value);
  102. }
  103. mapToDriverValue(value) {
  104. const unix = value.getTime();
  105. if (this.config.mode === "timestamp") {
  106. return Math.floor(unix / 1e3);
  107. }
  108. return unix;
  109. }
  110. }
  111. class SQLiteBooleanBuilder extends SQLiteBaseIntegerBuilder {
  112. static [import_entity.entityKind] = "SQLiteBooleanBuilder";
  113. constructor(name, mode) {
  114. super(name, "boolean", "SQLiteBoolean");
  115. this.config.mode = mode;
  116. }
  117. build(table) {
  118. return new SQLiteBoolean(
  119. table,
  120. this.config
  121. );
  122. }
  123. }
  124. class SQLiteBoolean extends SQLiteBaseInteger {
  125. static [import_entity.entityKind] = "SQLiteBoolean";
  126. mode = this.config.mode;
  127. mapFromDriverValue(value) {
  128. return Number(value) === 1;
  129. }
  130. mapToDriverValue(value) {
  131. return value ? 1 : 0;
  132. }
  133. }
  134. function integer(a, b) {
  135. const { name, config } = (0, import_utils.getColumnNameAndConfig)(a, b);
  136. if (config?.mode === "timestamp" || config?.mode === "timestamp_ms") {
  137. return new SQLiteTimestampBuilder(name, config.mode);
  138. }
  139. if (config?.mode === "boolean") {
  140. return new SQLiteBooleanBuilder(name, config.mode);
  141. }
  142. return new SQLiteIntegerBuilder(name);
  143. }
  144. const int = integer;
  145. // Annotate the CommonJS export names for ESM import in node:
  146. 0 && (module.exports = {
  147. SQLiteBaseInteger,
  148. SQLiteBaseIntegerBuilder,
  149. SQLiteBoolean,
  150. SQLiteBooleanBuilder,
  151. SQLiteInteger,
  152. SQLiteIntegerBuilder,
  153. SQLiteTimestamp,
  154. SQLiteTimestampBuilder,
  155. int,
  156. integer
  157. });
  158. //# sourceMappingURL=integer.cjs.map