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