query.cjs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. RelationalQueryBuilder: () => RelationalQueryBuilder,
  22. SQLiteRelationalQuery: () => SQLiteRelationalQuery,
  23. SQLiteSyncRelationalQuery: () => SQLiteSyncRelationalQuery
  24. });
  25. module.exports = __toCommonJS(query_exports);
  26. var import_entity = require("../../entity.cjs");
  27. var import_query_promise = require("../../query-promise.cjs");
  28. var import_relations = require("../../relations.cjs");
  29. class RelationalQueryBuilder {
  30. constructor(mode, fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session) {
  31. this.mode = mode;
  32. this.fullSchema = fullSchema;
  33. this.schema = schema;
  34. this.tableNamesMap = tableNamesMap;
  35. this.table = table;
  36. this.tableConfig = tableConfig;
  37. this.dialect = dialect;
  38. this.session = session;
  39. }
  40. static [import_entity.entityKind] = "SQLiteAsyncRelationalQueryBuilder";
  41. findMany(config) {
  42. return this.mode === "sync" ? new SQLiteSyncRelationalQuery(
  43. this.fullSchema,
  44. this.schema,
  45. this.tableNamesMap,
  46. this.table,
  47. this.tableConfig,
  48. this.dialect,
  49. this.session,
  50. config ? config : {},
  51. "many"
  52. ) : new SQLiteRelationalQuery(
  53. this.fullSchema,
  54. this.schema,
  55. this.tableNamesMap,
  56. this.table,
  57. this.tableConfig,
  58. this.dialect,
  59. this.session,
  60. config ? config : {},
  61. "many"
  62. );
  63. }
  64. findFirst(config) {
  65. return this.mode === "sync" ? new SQLiteSyncRelationalQuery(
  66. this.fullSchema,
  67. this.schema,
  68. this.tableNamesMap,
  69. this.table,
  70. this.tableConfig,
  71. this.dialect,
  72. this.session,
  73. config ? { ...config, limit: 1 } : { limit: 1 },
  74. "first"
  75. ) : new SQLiteRelationalQuery(
  76. this.fullSchema,
  77. this.schema,
  78. this.tableNamesMap,
  79. this.table,
  80. this.tableConfig,
  81. this.dialect,
  82. this.session,
  83. config ? { ...config, limit: 1 } : { limit: 1 },
  84. "first"
  85. );
  86. }
  87. }
  88. class SQLiteRelationalQuery extends import_query_promise.QueryPromise {
  89. constructor(fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session, config, mode) {
  90. super();
  91. this.fullSchema = fullSchema;
  92. this.schema = schema;
  93. this.tableNamesMap = tableNamesMap;
  94. this.table = table;
  95. this.tableConfig = tableConfig;
  96. this.dialect = dialect;
  97. this.session = session;
  98. this.config = config;
  99. this.mode = mode;
  100. }
  101. static [import_entity.entityKind] = "SQLiteAsyncRelationalQuery";
  102. /** @internal */
  103. mode;
  104. /** @internal */
  105. getSQL() {
  106. return this.dialect.buildRelationalQuery({
  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. }).sql;
  115. }
  116. /** @internal */
  117. _prepare(isOneTimeQuery = false) {
  118. const { query, builtQuery } = this._toSQL();
  119. return this.session[isOneTimeQuery ? "prepareOneTimeQuery" : "prepareQuery"](
  120. builtQuery,
  121. void 0,
  122. this.mode === "first" ? "get" : "all",
  123. true,
  124. (rawRows, mapColumnValue) => {
  125. const rows = rawRows.map(
  126. (row) => (0, import_relations.mapRelationalRow)(this.schema, this.tableConfig, row, query.selection, mapColumnValue)
  127. );
  128. if (this.mode === "first") {
  129. return rows[0];
  130. }
  131. return rows;
  132. }
  133. );
  134. }
  135. prepare() {
  136. return this._prepare(false);
  137. }
  138. _toSQL() {
  139. const query = this.dialect.buildRelationalQuery({
  140. fullSchema: this.fullSchema,
  141. schema: this.schema,
  142. tableNamesMap: this.tableNamesMap,
  143. table: this.table,
  144. tableConfig: this.tableConfig,
  145. queryConfig: this.config,
  146. tableAlias: this.tableConfig.tsName
  147. });
  148. const builtQuery = this.dialect.sqlToQuery(query.sql);
  149. return { query, builtQuery };
  150. }
  151. toSQL() {
  152. return this._toSQL().builtQuery;
  153. }
  154. /** @internal */
  155. executeRaw() {
  156. if (this.mode === "first") {
  157. return this._prepare(false).get();
  158. }
  159. return this._prepare(false).all();
  160. }
  161. async execute() {
  162. return this.executeRaw();
  163. }
  164. }
  165. class SQLiteSyncRelationalQuery extends SQLiteRelationalQuery {
  166. static [import_entity.entityKind] = "SQLiteSyncRelationalQuery";
  167. sync() {
  168. return this.executeRaw();
  169. }
  170. }
  171. // Annotate the CommonJS export names for ESM import in node:
  172. 0 && (module.exports = {
  173. RelationalQueryBuilder,
  174. SQLiteRelationalQuery,
  175. SQLiteSyncRelationalQuery
  176. });
  177. //# sourceMappingURL=query.cjs.map