common.cjs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 common_exports = {};
  20. __export(common_exports, {
  21. GelArray: () => GelArray,
  22. GelArrayBuilder: () => GelArrayBuilder,
  23. GelColumn: () => GelColumn,
  24. GelColumnBuilder: () => GelColumnBuilder,
  25. GelExtraConfigColumn: () => GelExtraConfigColumn,
  26. IndexedColumn: () => IndexedColumn
  27. });
  28. module.exports = __toCommonJS(common_exports);
  29. var import_column_builder = require("../../column-builder.cjs");
  30. var import_column = require("../../column.cjs");
  31. var import_entity = require("../../entity.cjs");
  32. var import_foreign_keys = require("../foreign-keys.cjs");
  33. var import_tracing_utils = require("../../tracing-utils.cjs");
  34. var import_unique_constraint = require("../unique-constraint.cjs");
  35. class GelColumnBuilder extends import_column_builder.ColumnBuilder {
  36. foreignKeyConfigs = [];
  37. static [import_entity.entityKind] = "GelColumnBuilder";
  38. array(size) {
  39. return new GelArrayBuilder(this.config.name, this, size);
  40. }
  41. references(ref, actions = {}) {
  42. this.foreignKeyConfigs.push({ ref, actions });
  43. return this;
  44. }
  45. unique(name, config) {
  46. this.config.isUnique = true;
  47. this.config.uniqueName = name;
  48. this.config.uniqueType = config?.nulls;
  49. return this;
  50. }
  51. generatedAlwaysAs(as) {
  52. this.config.generated = {
  53. as,
  54. type: "always",
  55. mode: "stored"
  56. };
  57. return this;
  58. }
  59. /** @internal */
  60. buildForeignKeys(column, table) {
  61. return this.foreignKeyConfigs.map(({ ref, actions }) => {
  62. return (0, import_tracing_utils.iife)(
  63. (ref2, actions2) => {
  64. const builder = new import_foreign_keys.ForeignKeyBuilder(() => {
  65. const foreignColumn = ref2();
  66. return { columns: [column], foreignColumns: [foreignColumn] };
  67. });
  68. if (actions2.onUpdate) {
  69. builder.onUpdate(actions2.onUpdate);
  70. }
  71. if (actions2.onDelete) {
  72. builder.onDelete(actions2.onDelete);
  73. }
  74. return builder.build(table);
  75. },
  76. ref,
  77. actions
  78. );
  79. });
  80. }
  81. /** @internal */
  82. buildExtraConfigColumn(table) {
  83. return new GelExtraConfigColumn(table, this.config);
  84. }
  85. }
  86. class GelColumn extends import_column.Column {
  87. constructor(table, config) {
  88. if (!config.uniqueName) {
  89. config.uniqueName = (0, import_unique_constraint.uniqueKeyName)(table, [config.name]);
  90. }
  91. super(table, config);
  92. this.table = table;
  93. }
  94. static [import_entity.entityKind] = "GelColumn";
  95. }
  96. class GelExtraConfigColumn extends GelColumn {
  97. static [import_entity.entityKind] = "GelExtraConfigColumn";
  98. getSQLType() {
  99. return this.getSQLType();
  100. }
  101. indexConfig = {
  102. order: this.config.order ?? "asc",
  103. nulls: this.config.nulls ?? "last",
  104. opClass: this.config.opClass
  105. };
  106. defaultConfig = {
  107. order: "asc",
  108. nulls: "last",
  109. opClass: void 0
  110. };
  111. asc() {
  112. this.indexConfig.order = "asc";
  113. return this;
  114. }
  115. desc() {
  116. this.indexConfig.order = "desc";
  117. return this;
  118. }
  119. nullsFirst() {
  120. this.indexConfig.nulls = "first";
  121. return this;
  122. }
  123. nullsLast() {
  124. this.indexConfig.nulls = "last";
  125. return this;
  126. }
  127. /**
  128. * ### PostgreSQL documentation quote
  129. *
  130. * > An operator class with optional parameters can be specified for each column of an index.
  131. * The operator class identifies the operators to be used by the index for that column.
  132. * For example, a B-tree index on four-byte integers would use the int4_ops class;
  133. * this operator class includes comparison functions for four-byte integers.
  134. * In practice the default operator class for the column's data type is usually sufficient.
  135. * The main point of having operator classes is that for some data types, there could be more than one meaningful ordering.
  136. * For example, we might want to sort a complex-number data type either by absolute value or by real part.
  137. * We could do this by defining two operator classes for the data type and then selecting the proper class when creating an index.
  138. * More information about operator classes check:
  139. *
  140. * ### Useful links
  141. * https://www.postgresql.org/docs/current/sql-createindex.html
  142. *
  143. * https://www.postgresql.org/docs/current/indexes-opclass.html
  144. *
  145. * https://www.postgresql.org/docs/current/xindex.html
  146. *
  147. * ### Additional types
  148. * If you have the `Gel_vector` extension installed in your database, you can use the
  149. * `vector_l2_ops`, `vector_ip_ops`, `vector_cosine_ops`, `vector_l1_ops`, `bit_hamming_ops`, `bit_jaccard_ops`, `halfvec_l2_ops`, `sparsevec_l2_ops` options, which are predefined types.
  150. *
  151. * **You can always specify any string you want in the operator class, in case Drizzle doesn't have it natively in its types**
  152. *
  153. * @param opClass
  154. * @returns
  155. */
  156. op(opClass) {
  157. this.indexConfig.opClass = opClass;
  158. return this;
  159. }
  160. }
  161. class IndexedColumn {
  162. static [import_entity.entityKind] = "IndexedColumn";
  163. constructor(name, keyAsName, type, indexConfig) {
  164. this.name = name;
  165. this.keyAsName = keyAsName;
  166. this.type = type;
  167. this.indexConfig = indexConfig;
  168. }
  169. name;
  170. keyAsName;
  171. type;
  172. indexConfig;
  173. }
  174. class GelArrayBuilder extends GelColumnBuilder {
  175. static [import_entity.entityKind] = "GelArrayBuilder";
  176. constructor(name, baseBuilder, size) {
  177. super(name, "array", "GelArray");
  178. this.config.baseBuilder = baseBuilder;
  179. this.config.size = size;
  180. }
  181. /** @internal */
  182. build(table) {
  183. const baseColumn = this.config.baseBuilder.build(table);
  184. return new GelArray(
  185. table,
  186. this.config,
  187. baseColumn
  188. );
  189. }
  190. }
  191. class GelArray extends GelColumn {
  192. constructor(table, config, baseColumn, range) {
  193. super(table, config);
  194. this.baseColumn = baseColumn;
  195. this.range = range;
  196. this.size = config.size;
  197. }
  198. size;
  199. static [import_entity.entityKind] = "GelArray";
  200. getSQLType() {
  201. return `${this.baseColumn.getSQLType()}[${typeof this.size === "number" ? this.size : ""}]`;
  202. }
  203. }
  204. // Annotate the CommonJS export names for ESM import in node:
  205. 0 && (module.exports = {
  206. GelArray,
  207. GelArrayBuilder,
  208. GelColumn,
  209. GelColumnBuilder,
  210. GelExtraConfigColumn,
  211. IndexedColumn
  212. });
  213. //# sourceMappingURL=common.cjs.map