session.cjs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 session_exports = {};
  20. __export(session_exports, {
  21. PglitePreparedQuery: () => PglitePreparedQuery,
  22. PgliteSession: () => PgliteSession,
  23. PgliteTransaction: () => PgliteTransaction
  24. });
  25. module.exports = __toCommonJS(session_exports);
  26. var import_entity = require("../entity.cjs");
  27. var import_logger = require("../logger.cjs");
  28. var import_pg_core = require("../pg-core/index.cjs");
  29. var import_session = require("../pg-core/session.cjs");
  30. var import_sql = require("../sql/sql.cjs");
  31. var import_utils = require("../utils.cjs");
  32. var import_pglite = require("@electric-sql/pglite");
  33. var import_cache = require("../cache/core/cache.cjs");
  34. class PglitePreparedQuery extends import_session.PgPreparedQuery {
  35. constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, name, _isResponseInArrayMode, customResultMapper) {
  36. super({ sql: queryString, params }, cache, queryMetadata, cacheConfig);
  37. this.client = client;
  38. this.queryString = queryString;
  39. this.params = params;
  40. this.logger = logger;
  41. this.fields = fields;
  42. this._isResponseInArrayMode = _isResponseInArrayMode;
  43. this.customResultMapper = customResultMapper;
  44. this.rawQueryConfig = {
  45. rowMode: "object",
  46. parsers: {
  47. [import_pglite.types.TIMESTAMP]: (value) => value,
  48. [import_pglite.types.TIMESTAMPTZ]: (value) => value,
  49. [import_pglite.types.INTERVAL]: (value) => value,
  50. [import_pglite.types.DATE]: (value) => value,
  51. // numeric[]
  52. [1231]: (value) => value,
  53. // timestamp[]
  54. [1115]: (value) => value,
  55. // timestamp with timezone[]
  56. [1185]: (value) => value,
  57. // interval[]
  58. [1187]: (value) => value,
  59. // date[]
  60. [1182]: (value) => value
  61. }
  62. };
  63. this.queryConfig = {
  64. rowMode: "array",
  65. parsers: {
  66. [import_pglite.types.TIMESTAMP]: (value) => value,
  67. [import_pglite.types.TIMESTAMPTZ]: (value) => value,
  68. [import_pglite.types.INTERVAL]: (value) => value,
  69. [import_pglite.types.DATE]: (value) => value,
  70. // numeric[]
  71. [1231]: (value) => value,
  72. // timestamp[]
  73. [1115]: (value) => value,
  74. // timestamp with timezone[]
  75. [1185]: (value) => value,
  76. // interval[]
  77. [1187]: (value) => value,
  78. // date[]
  79. [1182]: (value) => value
  80. }
  81. };
  82. }
  83. static [import_entity.entityKind] = "PglitePreparedQuery";
  84. rawQueryConfig;
  85. queryConfig;
  86. async execute(placeholderValues = {}) {
  87. const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);
  88. this.logger.logQuery(this.queryString, params);
  89. const { fields, client, queryConfig, joinsNotNullableMap, customResultMapper, queryString, rawQueryConfig } = this;
  90. if (!fields && !customResultMapper) {
  91. return this.queryWithCache(queryString, params, async () => {
  92. return await client.query(queryString, params, rawQueryConfig);
  93. });
  94. }
  95. const result = await this.queryWithCache(queryString, params, async () => {
  96. return await client.query(queryString, params, queryConfig);
  97. });
  98. return customResultMapper ? customResultMapper(result.rows) : result.rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));
  99. }
  100. all(placeholderValues = {}) {
  101. const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);
  102. this.logger.logQuery(this.queryString, params);
  103. return this.queryWithCache(this.queryString, params, async () => {
  104. return await this.client.query(this.queryString, params, this.rawQueryConfig);
  105. }).then((result) => result.rows);
  106. }
  107. /** @internal */
  108. isResponseInArrayMode() {
  109. return this._isResponseInArrayMode;
  110. }
  111. }
  112. class PgliteSession extends import_session.PgSession {
  113. constructor(client, dialect, schema, options = {}) {
  114. super(dialect);
  115. this.client = client;
  116. this.schema = schema;
  117. this.options = options;
  118. this.logger = options.logger ?? new import_logger.NoopLogger();
  119. this.cache = options.cache ?? new import_cache.NoopCache();
  120. }
  121. static [import_entity.entityKind] = "PgliteSession";
  122. logger;
  123. cache;
  124. prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
  125. return new PglitePreparedQuery(
  126. this.client,
  127. query.sql,
  128. query.params,
  129. this.logger,
  130. this.cache,
  131. queryMetadata,
  132. cacheConfig,
  133. fields,
  134. name,
  135. isResponseInArrayMode,
  136. customResultMapper
  137. );
  138. }
  139. async transaction(transaction, config) {
  140. return this.client.transaction(async (client) => {
  141. const session = new PgliteSession(
  142. client,
  143. this.dialect,
  144. this.schema,
  145. this.options
  146. );
  147. const tx = new PgliteTransaction(this.dialect, session, this.schema);
  148. if (config) {
  149. await tx.setTransaction(config);
  150. }
  151. return transaction(tx);
  152. });
  153. }
  154. async count(sql2) {
  155. const res = await this.execute(sql2);
  156. return Number(
  157. res["rows"][0]["count"]
  158. );
  159. }
  160. }
  161. class PgliteTransaction extends import_pg_core.PgTransaction {
  162. static [import_entity.entityKind] = "PgliteTransaction";
  163. async transaction(transaction) {
  164. const savepointName = `sp${this.nestedIndex + 1}`;
  165. const tx = new PgliteTransaction(
  166. this.dialect,
  167. this.session,
  168. this.schema,
  169. this.nestedIndex + 1
  170. );
  171. await tx.execute(import_sql.sql.raw(`savepoint ${savepointName}`));
  172. try {
  173. const result = await transaction(tx);
  174. await tx.execute(import_sql.sql.raw(`release savepoint ${savepointName}`));
  175. return result;
  176. } catch (err) {
  177. await tx.execute(import_sql.sql.raw(`rollback to savepoint ${savepointName}`));
  178. throw err;
  179. }
  180. }
  181. }
  182. // Annotate the CommonJS export names for ESM import in node:
  183. 0 && (module.exports = {
  184. PglitePreparedQuery,
  185. PgliteSession,
  186. PgliteTransaction
  187. });
  188. //# sourceMappingURL=session.cjs.map