common.cjs 7.3 KB

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