query.cjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 query_exports = {};
  20. __export(query_exports, {
  21. GelRelationalQuery: () => GelRelationalQuery,
  22. RelationalQueryBuilder: () => RelationalQueryBuilder
  23. });
  24. module.exports = __toCommonJS(query_exports);
  25. var import_entity = require("../../entity.cjs");
  26. var import_query_promise = require("../../query-promise.cjs");
  27. var import_relations = require("../../relations.cjs");
  28. var import_tracing = require("../../tracing.cjs");
  29. class RelationalQueryBuilder {
  30. constructor(fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session) {
  31. this.fullSchema = fullSchema;
  32. this.schema = schema;
  33. this.tableNamesMap = tableNamesMap;
  34. this.table = table;
  35. this.tableConfig = tableConfig;
  36. this.dialect = dialect;
  37. this.session = session;
  38. }
  39. static [import_entity.entityKind] = "GelRelationalQueryBuilder";
  40. findMany(config) {
  41. return new GelRelationalQuery(
  42. this.fullSchema,
  43. this.schema,
  44. this.tableNamesMap,
  45. this.table,
  46. this.tableConfig,
  47. this.dialect,
  48. this.session,
  49. config ? config : {},
  50. "many"
  51. );
  52. }
  53. findFirst(config) {
  54. return new GelRelationalQuery(
  55. this.fullSchema,
  56. this.schema,
  57. this.tableNamesMap,
  58. this.table,
  59. this.tableConfig,
  60. this.dialect,
  61. this.session,
  62. config ? { ...config, limit: 1 } : { limit: 1 },
  63. "first"
  64. );
  65. }
  66. }
  67. class GelRelationalQuery extends import_query_promise.QueryPromise {
  68. constructor(fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session, config, mode) {
  69. super();
  70. this.fullSchema = fullSchema;
  71. this.schema = schema;
  72. this.tableNamesMap = tableNamesMap;
  73. this.table = table;
  74. this.tableConfig = tableConfig;
  75. this.dialect = dialect;
  76. this.session = session;
  77. this.config = config;
  78. this.mode = mode;
  79. }
  80. static [import_entity.entityKind] = "GelRelationalQuery";
  81. /** @internal */
  82. _prepare(name) {
  83. return import_tracing.tracer.startActiveSpan("drizzle.prepareQuery", () => {
  84. const { query, builtQuery } = this._toSQL();
  85. return this.session.prepareQuery(
  86. builtQuery,
  87. void 0,
  88. name,
  89. true,
  90. (rawRows, mapColumnValue) => {
  91. const rows = rawRows.map(
  92. (row) => (0, import_relations.mapRelationalRow)(this.schema, this.tableConfig, row, query.selection, mapColumnValue)
  93. );
  94. if (this.mode === "first") {
  95. return rows[0];
  96. }
  97. return rows;
  98. }
  99. );
  100. });
  101. }
  102. prepare(name) {
  103. return this._prepare(name);
  104. }
  105. _getQuery() {
  106. return this.dialect.buildRelationalQueryWithoutPK({
  107. fullSchema: this.fullSchema,
  108. schema: this.schema,
  109. tableNamesMap: this.tableNamesMap,
  110. table: this.table,
  111. tableConfig: this.tableConfig,
  112. queryConfig: this.config,
  113. tableAlias: this.tableConfig.tsName
  114. });
  115. }
  116. /** @internal */
  117. getSQL() {
  118. return this._getQuery().sql;
  119. }
  120. _toSQL() {
  121. const query = this._getQuery();
  122. const builtQuery = this.dialect.sqlToQuery(query.sql);
  123. return { query, builtQuery };
  124. }
  125. toSQL() {
  126. return this._toSQL().builtQuery;
  127. }
  128. execute() {
  129. return import_tracing.tracer.startActiveSpan("drizzle.operation", () => {
  130. return this._prepare().execute(void 0);
  131. });
  132. }
  133. }
  134. // Annotate the CommonJS export names for ESM import in node:
  135. 0 && (module.exports = {
  136. GelRelationalQuery,
  137. RelationalQueryBuilder
  138. });
  139. //# sourceMappingURL=query.cjs.map