driver.cjs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 driver_exports = {};
  20. __export(driver_exports, {
  21. AwsDataApiPgDatabase: () => AwsDataApiPgDatabase,
  22. AwsPgDialect: () => AwsPgDialect,
  23. drizzle: () => drizzle
  24. });
  25. module.exports = __toCommonJS(driver_exports);
  26. var import_client_rds_data = require("@aws-sdk/client-rds-data");
  27. var import_entity = require("../../entity.cjs");
  28. var import_logger = require("../../logger.cjs");
  29. var import_db = require("../../pg-core/db.cjs");
  30. var import_dialect = require("../../pg-core/dialect.cjs");
  31. var import_pg_core = require("../../pg-core/index.cjs");
  32. var import_relations = require("../../relations.cjs");
  33. var import_sql = require("../../sql/sql.cjs");
  34. var import_table = require("../../table.cjs");
  35. var import_session = require("./session.cjs");
  36. class AwsDataApiPgDatabase extends import_db.PgDatabase {
  37. static [import_entity.entityKind] = "AwsDataApiPgDatabase";
  38. execute(query) {
  39. return super.execute(query);
  40. }
  41. }
  42. class AwsPgDialect extends import_dialect.PgDialect {
  43. static [import_entity.entityKind] = "AwsPgDialect";
  44. escapeParam(num) {
  45. return `:${num + 1}`;
  46. }
  47. buildInsertQuery({ table, values, onConflict, returning, select, withList }) {
  48. const columns = table[import_table.Table.Symbol.Columns];
  49. if (!select) {
  50. for (const value of values) {
  51. for (const fieldName of Object.keys(columns)) {
  52. const colValue = value[fieldName];
  53. if ((0, import_entity.is)(colValue, import_sql.Param) && colValue.value !== void 0 && (0, import_entity.is)(colValue.encoder, import_pg_core.PgArray) && Array.isArray(colValue.value)) {
  54. value[fieldName] = import_sql.sql`cast(${colValue} as ${import_sql.sql.raw(colValue.encoder.getSQLType())})`;
  55. }
  56. }
  57. }
  58. }
  59. return super.buildInsertQuery({ table, values, onConflict, returning, withList });
  60. }
  61. buildUpdateSet(table, set) {
  62. const columns = table[import_table.Table.Symbol.Columns];
  63. for (const [colName, colValue] of Object.entries(set)) {
  64. const currentColumn = columns[colName];
  65. if (currentColumn && (0, import_entity.is)(colValue, import_sql.Param) && colValue.value !== void 0 && (0, import_entity.is)(colValue.encoder, import_pg_core.PgArray) && Array.isArray(colValue.value)) {
  66. set[colName] = import_sql.sql`cast(${colValue} as ${import_sql.sql.raw(colValue.encoder.getSQLType())})`;
  67. }
  68. }
  69. return super.buildUpdateSet(table, set);
  70. }
  71. }
  72. function construct(client, config) {
  73. const dialect = new AwsPgDialect({ casing: config.casing });
  74. let logger;
  75. if (config.logger === true) {
  76. logger = new import_logger.DefaultLogger();
  77. } else if (config.logger !== false) {
  78. logger = config.logger;
  79. }
  80. let schema;
  81. if (config.schema) {
  82. const tablesConfig = (0, import_relations.extractTablesRelationalConfig)(
  83. config.schema,
  84. import_relations.createTableRelationsHelpers
  85. );
  86. schema = {
  87. fullSchema: config.schema,
  88. schema: tablesConfig.tables,
  89. tableNamesMap: tablesConfig.tableNamesMap
  90. };
  91. }
  92. const session = new import_session.AwsDataApiSession(client, dialect, schema, { ...config, logger, cache: config.cache }, void 0);
  93. const db = new AwsDataApiPgDatabase(dialect, session, schema);
  94. db.$client = client;
  95. db.$cache = config.cache;
  96. if (db.$cache) {
  97. db.$cache["invalidate"] = config.cache?.onMutate;
  98. }
  99. return db;
  100. }
  101. function drizzle(...params) {
  102. if (params[0] instanceof import_client_rds_data.RDSDataClient || params[0].constructor.name !== "Object") {
  103. return construct(params[0], params[1]);
  104. }
  105. if (params[0].client) {
  106. const { client, ...drizzleConfig2 } = params[0];
  107. return construct(client, drizzleConfig2);
  108. }
  109. const { connection, ...drizzleConfig } = params[0];
  110. const { resourceArn, database, secretArn, ...rdsConfig } = connection;
  111. const instance = new import_client_rds_data.RDSDataClient(rdsConfig);
  112. return construct(instance, { resourceArn, database, secretArn, ...drizzleConfig });
  113. }
  114. ((drizzle2) => {
  115. function mock(config) {
  116. return construct({}, config);
  117. }
  118. drizzle2.mock = mock;
  119. })(drizzle || (drizzle = {}));
  120. // Annotate the CommonJS export names for ESM import in node:
  121. 0 && (module.exports = {
  122. AwsDataApiPgDatabase,
  123. AwsPgDialect,
  124. drizzle
  125. });
  126. //# sourceMappingURL=driver.cjs.map