driver.cjs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. NeonDatabase: () => NeonDatabase,
  22. NeonDriver: () => NeonDriver,
  23. drizzle: () => drizzle
  24. });
  25. module.exports = __toCommonJS(driver_exports);
  26. var import_serverless = require("@neondatabase/serverless");
  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_relations = require("../relations.cjs");
  32. var import_utils = require("../utils.cjs");
  33. var import_session = require("./session.cjs");
  34. class NeonDriver {
  35. constructor(client, dialect, options = {}) {
  36. this.client = client;
  37. this.dialect = dialect;
  38. this.options = options;
  39. }
  40. static [import_entity.entityKind] = "NeonDriver";
  41. createSession(schema) {
  42. return new import_session.NeonSession(this.client, this.dialect, schema, {
  43. logger: this.options.logger,
  44. cache: this.options.cache
  45. });
  46. }
  47. }
  48. class NeonDatabase extends import_db.PgDatabase {
  49. static [import_entity.entityKind] = "NeonServerlessDatabase";
  50. }
  51. function construct(client, config = {}) {
  52. const dialect = new import_dialect.PgDialect({ casing: config.casing });
  53. let logger;
  54. if (config.logger === true) {
  55. logger = new import_logger.DefaultLogger();
  56. } else if (config.logger !== false) {
  57. logger = config.logger;
  58. }
  59. let schema;
  60. if (config.schema) {
  61. const tablesConfig = (0, import_relations.extractTablesRelationalConfig)(
  62. config.schema,
  63. import_relations.createTableRelationsHelpers
  64. );
  65. schema = {
  66. fullSchema: config.schema,
  67. schema: tablesConfig.tables,
  68. tableNamesMap: tablesConfig.tableNamesMap
  69. };
  70. }
  71. const driver = new NeonDriver(client, dialect, { logger, cache: config.cache });
  72. const session = driver.createSession(schema);
  73. const db = new NeonDatabase(dialect, session, schema);
  74. db.$client = client;
  75. db.$cache = config.cache;
  76. if (db.$cache) {
  77. db.$cache["invalidate"] = config.cache?.onMutate;
  78. }
  79. return db;
  80. }
  81. function drizzle(...params) {
  82. if (typeof params[0] === "string") {
  83. const instance = new import_serverless.Pool({
  84. connectionString: params[0]
  85. });
  86. return construct(instance, params[1]);
  87. }
  88. if ((0, import_utils.isConfig)(params[0])) {
  89. const { connection, client, ws, ...drizzleConfig } = params[0];
  90. if (ws) {
  91. import_serverless.neonConfig.webSocketConstructor = ws;
  92. }
  93. if (client) return construct(client, drizzleConfig);
  94. const instance = typeof connection === "string" ? new import_serverless.Pool({
  95. connectionString: connection
  96. }) : new import_serverless.Pool(connection);
  97. return construct(instance, drizzleConfig);
  98. }
  99. return construct(params[0], params[1]);
  100. }
  101. ((drizzle2) => {
  102. function mock(config) {
  103. return construct({}, config);
  104. }
  105. drizzle2.mock = mock;
  106. })(drizzle || (drizzle = {}));
  107. // Annotate the CommonJS export names for ESM import in node:
  108. 0 && (module.exports = {
  109. NeonDatabase,
  110. NeonDriver,
  111. drizzle
  112. });
  113. //# sourceMappingURL=driver.cjs.map