driver.cjs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. PostgresJsDatabase: () => PostgresJsDatabase,
  32. drizzle: () => drizzle
  33. });
  34. module.exports = __toCommonJS(driver_exports);
  35. var import_postgres = __toESM(require("postgres"), 1);
  36. var import_entity = require("../entity.cjs");
  37. var import_logger = require("../logger.cjs");
  38. var import_db = require("../pg-core/db.cjs");
  39. var import_dialect = require("../pg-core/dialect.cjs");
  40. var import_relations = require("../relations.cjs");
  41. var import_utils = require("../utils.cjs");
  42. var import_session = require("./session.cjs");
  43. class PostgresJsDatabase extends import_db.PgDatabase {
  44. static [import_entity.entityKind] = "PostgresJsDatabase";
  45. }
  46. function construct(client, config = {}) {
  47. const transparentParser = (val) => val;
  48. for (const type of ["1184", "1082", "1083", "1114", "1182", "1185", "1115", "1231"]) {
  49. client.options.parsers[type] = transparentParser;
  50. client.options.serializers[type] = transparentParser;
  51. }
  52. client.options.serializers["114"] = transparentParser;
  53. client.options.serializers["3802"] = transparentParser;
  54. const dialect = new import_dialect.PgDialect({ casing: config.casing });
  55. let logger;
  56. if (config.logger === true) {
  57. logger = new import_logger.DefaultLogger();
  58. } else if (config.logger !== false) {
  59. logger = config.logger;
  60. }
  61. let schema;
  62. if (config.schema) {
  63. const tablesConfig = (0, import_relations.extractTablesRelationalConfig)(
  64. config.schema,
  65. import_relations.createTableRelationsHelpers
  66. );
  67. schema = {
  68. fullSchema: config.schema,
  69. schema: tablesConfig.tables,
  70. tableNamesMap: tablesConfig.tableNamesMap
  71. };
  72. }
  73. const session = new import_session.PostgresJsSession(client, dialect, schema, { logger, cache: config.cache });
  74. const db = new PostgresJsDatabase(dialect, session, schema);
  75. db.$client = client;
  76. db.$cache = config.cache;
  77. if (db.$cache) {
  78. db.$cache["invalidate"] = config.cache?.onMutate;
  79. }
  80. return db;
  81. }
  82. function drizzle(...params) {
  83. if (typeof params[0] === "string") {
  84. const instance = (0, import_postgres.default)(params[0]);
  85. return construct(instance, params[1]);
  86. }
  87. if ((0, import_utils.isConfig)(params[0])) {
  88. const { connection, client, ...drizzleConfig } = params[0];
  89. if (client) return construct(client, drizzleConfig);
  90. if (typeof connection === "object" && connection.url !== void 0) {
  91. const { url, ...config } = connection;
  92. const instance2 = (0, import_postgres.default)(url, config);
  93. return construct(instance2, drizzleConfig);
  94. }
  95. const instance = (0, import_postgres.default)(connection);
  96. return construct(instance, drizzleConfig);
  97. }
  98. return construct(params[0], params[1]);
  99. }
  100. ((drizzle2) => {
  101. function mock(config) {
  102. return construct({
  103. options: {
  104. parsers: {},
  105. serializers: {}
  106. }
  107. }, config);
  108. }
  109. drizzle2.mock = mock;
  110. })(drizzle || (drizzle = {}));
  111. // Annotate the CommonJS export names for ESM import in node:
  112. 0 && (module.exports = {
  113. PostgresJsDatabase,
  114. drizzle
  115. });
  116. //# sourceMappingURL=driver.cjs.map