driver.cjs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var driver_exports = {};
  30. __export(driver_exports, {
  31. NodePgDatabase: () => NodePgDatabase,
  32. NodePgDriver: () => NodePgDriver,
  33. drizzle: () => drizzle
  34. });
  35. module.exports = __toCommonJS(driver_exports);
  36. var import_pg = __toESM(require("pg"), 1);
  37. var import_entity = require("../entity.cjs");
  38. var import_logger = require("../logger.cjs");
  39. var import_db = require("../pg-core/db.cjs");
  40. var import_dialect = require("../pg-core/dialect.cjs");
  41. var import_relations = require("../relations.cjs");
  42. var import_utils = require("../utils.cjs");
  43. var import_session = require("./session.cjs");
  44. class NodePgDriver {
  45. constructor(client, dialect, options = {}) {
  46. this.client = client;
  47. this.dialect = dialect;
  48. this.options = options;
  49. }
  50. static [import_entity.entityKind] = "NodePgDriver";
  51. createSession(schema) {
  52. return new import_session.NodePgSession(this.client, this.dialect, schema, {
  53. logger: this.options.logger,
  54. cache: this.options.cache
  55. });
  56. }
  57. }
  58. class NodePgDatabase extends import_db.PgDatabase {
  59. static [import_entity.entityKind] = "NodePgDatabase";
  60. }
  61. function construct(client, config = {}) {
  62. const dialect = new import_dialect.PgDialect({ casing: config.casing });
  63. let logger;
  64. if (config.logger === true) {
  65. logger = new import_logger.DefaultLogger();
  66. } else if (config.logger !== false) {
  67. logger = config.logger;
  68. }
  69. let schema;
  70. if (config.schema) {
  71. const tablesConfig = (0, import_relations.extractTablesRelationalConfig)(
  72. config.schema,
  73. import_relations.createTableRelationsHelpers
  74. );
  75. schema = {
  76. fullSchema: config.schema,
  77. schema: tablesConfig.tables,
  78. tableNamesMap: tablesConfig.tableNamesMap
  79. };
  80. }
  81. const driver = new NodePgDriver(client, dialect, { logger, cache: config.cache });
  82. const session = driver.createSession(schema);
  83. const db = new NodePgDatabase(dialect, session, schema);
  84. db.$client = client;
  85. db.$cache = config.cache;
  86. if (db.$cache) {
  87. db.$cache["invalidate"] = config.cache?.onMutate;
  88. }
  89. return db;
  90. }
  91. function drizzle(...params) {
  92. if (typeof params[0] === "string") {
  93. const instance = new import_pg.default.Pool({
  94. connectionString: params[0]
  95. });
  96. return construct(instance, params[1]);
  97. }
  98. if ((0, import_utils.isConfig)(params[0])) {
  99. const { connection, client, ...drizzleConfig } = params[0];
  100. if (client) return construct(client, drizzleConfig);
  101. const instance = typeof connection === "string" ? new import_pg.default.Pool({
  102. connectionString: connection
  103. }) : new import_pg.default.Pool(connection);
  104. return construct(instance, drizzleConfig);
  105. }
  106. return construct(params[0], params[1]);
  107. }
  108. ((drizzle2) => {
  109. function mock(config) {
  110. return construct({}, config);
  111. }
  112. drizzle2.mock = mock;
  113. })(drizzle || (drizzle = {}));
  114. // Annotate the CommonJS export names for ESM import in node:
  115. 0 && (module.exports = {
  116. NodePgDatabase,
  117. NodePgDriver,
  118. drizzle
  119. });
  120. //# sourceMappingURL=driver.cjs.map