common.js 5.4 KB

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