indexes.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { SQL } from "../sql/sql.js";
  2. import { entityKind, is } from "../entity.js";
  3. import { IndexedColumn } from "./columns/index.js";
  4. class IndexBuilderOn {
  5. constructor(unique, name) {
  6. this.unique = unique;
  7. this.name = name;
  8. }
  9. static [entityKind] = "GelIndexBuilderOn";
  10. on(...columns) {
  11. return new IndexBuilder(
  12. columns.map((it) => {
  13. if (is(it, SQL)) {
  14. return it;
  15. }
  16. it = it;
  17. const clonedIndexedColumn = new IndexedColumn(it.name, !!it.keyAsName, it.columnType, it.indexConfig);
  18. it.indexConfig = JSON.parse(JSON.stringify(it.defaultConfig));
  19. return clonedIndexedColumn;
  20. }),
  21. this.unique,
  22. false,
  23. this.name
  24. );
  25. }
  26. onOnly(...columns) {
  27. return new IndexBuilder(
  28. columns.map((it) => {
  29. if (is(it, SQL)) {
  30. return it;
  31. }
  32. it = it;
  33. const clonedIndexedColumn = new IndexedColumn(it.name, !!it.keyAsName, it.columnType, it.indexConfig);
  34. it.indexConfig = it.defaultConfig;
  35. return clonedIndexedColumn;
  36. }),
  37. this.unique,
  38. true,
  39. this.name
  40. );
  41. }
  42. /**
  43. * Specify what index method to use. Choices are `btree`, `hash`, `gist`, `sGelist`, `gin`, `brin`, or user-installed access methods like `bloom`. The default method is `btree.
  44. *
  45. * If you have the `Gel_vector` extension installed in your database, you can use the `hnsw` and `ivfflat` options, which are predefined types.
  46. *
  47. * **You can always specify any string you want in the method, in case Drizzle doesn't have it natively in its types**
  48. *
  49. * @param method The name of the index method to be used
  50. * @param columns
  51. * @returns
  52. */
  53. using(method, ...columns) {
  54. return new IndexBuilder(
  55. columns.map((it) => {
  56. if (is(it, SQL)) {
  57. return it;
  58. }
  59. it = it;
  60. const clonedIndexedColumn = new IndexedColumn(it.name, !!it.keyAsName, it.columnType, it.indexConfig);
  61. it.indexConfig = JSON.parse(JSON.stringify(it.defaultConfig));
  62. return clonedIndexedColumn;
  63. }),
  64. this.unique,
  65. true,
  66. this.name,
  67. method
  68. );
  69. }
  70. }
  71. class IndexBuilder {
  72. static [entityKind] = "GelIndexBuilder";
  73. /** @internal */
  74. config;
  75. constructor(columns, unique, only, name, method = "btree") {
  76. this.config = {
  77. name,
  78. columns,
  79. unique,
  80. only,
  81. method
  82. };
  83. }
  84. concurrently() {
  85. this.config.concurrently = true;
  86. return this;
  87. }
  88. with(obj) {
  89. this.config.with = obj;
  90. return this;
  91. }
  92. where(condition) {
  93. this.config.where = condition;
  94. return this;
  95. }
  96. /** @internal */
  97. build(table) {
  98. return new Index(this.config, table);
  99. }
  100. }
  101. class Index {
  102. static [entityKind] = "GelIndex";
  103. config;
  104. constructor(config, table) {
  105. this.config = { ...config, table };
  106. }
  107. }
  108. function index(name) {
  109. return new IndexBuilderOn(false, name);
  110. }
  111. function uniqueIndex(name) {
  112. return new IndexBuilderOn(true, name);
  113. }
  114. export {
  115. Index,
  116. IndexBuilder,
  117. IndexBuilderOn,
  118. index,
  119. uniqueIndex
  120. };
  121. //# sourceMappingURL=indexes.js.map