common.js 5.9 KB

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