query.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { entityKind } from "../../entity.js";
  2. import { QueryPromise } from "../../query-promise.js";
  3. import {
  4. mapRelationalRow
  5. } from "../../relations.js";
  6. import { tracer } from "../../tracing.js";
  7. class RelationalQueryBuilder {
  8. constructor(fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session) {
  9. this.fullSchema = fullSchema;
  10. this.schema = schema;
  11. this.tableNamesMap = tableNamesMap;
  12. this.table = table;
  13. this.tableConfig = tableConfig;
  14. this.dialect = dialect;
  15. this.session = session;
  16. }
  17. static [entityKind] = "GelRelationalQueryBuilder";
  18. findMany(config) {
  19. return new GelRelationalQuery(
  20. this.fullSchema,
  21. this.schema,
  22. this.tableNamesMap,
  23. this.table,
  24. this.tableConfig,
  25. this.dialect,
  26. this.session,
  27. config ? config : {},
  28. "many"
  29. );
  30. }
  31. findFirst(config) {
  32. return new GelRelationalQuery(
  33. this.fullSchema,
  34. this.schema,
  35. this.tableNamesMap,
  36. this.table,
  37. this.tableConfig,
  38. this.dialect,
  39. this.session,
  40. config ? { ...config, limit: 1 } : { limit: 1 },
  41. "first"
  42. );
  43. }
  44. }
  45. class GelRelationalQuery extends QueryPromise {
  46. constructor(fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session, config, mode) {
  47. super();
  48. this.fullSchema = fullSchema;
  49. this.schema = schema;
  50. this.tableNamesMap = tableNamesMap;
  51. this.table = table;
  52. this.tableConfig = tableConfig;
  53. this.dialect = dialect;
  54. this.session = session;
  55. this.config = config;
  56. this.mode = mode;
  57. }
  58. static [entityKind] = "GelRelationalQuery";
  59. /** @internal */
  60. _prepare(name) {
  61. return tracer.startActiveSpan("drizzle.prepareQuery", () => {
  62. const { query, builtQuery } = this._toSQL();
  63. return this.session.prepareQuery(
  64. builtQuery,
  65. void 0,
  66. name,
  67. true,
  68. (rawRows, mapColumnValue) => {
  69. const rows = rawRows.map(
  70. (row) => mapRelationalRow(this.schema, this.tableConfig, row, query.selection, mapColumnValue)
  71. );
  72. if (this.mode === "first") {
  73. return rows[0];
  74. }
  75. return rows;
  76. }
  77. );
  78. });
  79. }
  80. prepare(name) {
  81. return this._prepare(name);
  82. }
  83. _getQuery() {
  84. return this.dialect.buildRelationalQueryWithoutPK({
  85. fullSchema: this.fullSchema,
  86. schema: this.schema,
  87. tableNamesMap: this.tableNamesMap,
  88. table: this.table,
  89. tableConfig: this.tableConfig,
  90. queryConfig: this.config,
  91. tableAlias: this.tableConfig.tsName
  92. });
  93. }
  94. /** @internal */
  95. getSQL() {
  96. return this._getQuery().sql;
  97. }
  98. _toSQL() {
  99. const query = this._getQuery();
  100. const builtQuery = this.dialect.sqlToQuery(query.sql);
  101. return { query, builtQuery };
  102. }
  103. toSQL() {
  104. return this._toSQL().builtQuery;
  105. }
  106. execute() {
  107. return tracer.startActiveSpan("drizzle.operation", () => {
  108. return this._prepare().execute(void 0);
  109. });
  110. }
  111. }
  112. export {
  113. GelRelationalQuery,
  114. RelationalQueryBuilder
  115. };
  116. //# sourceMappingURL=query.js.map