driver.cjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. BunSQLDatabase: () => BunSQLDatabase,
  22. drizzle: () => drizzle
  23. });
  24. module.exports = __toCommonJS(driver_exports);
  25. var import_bun = require("bun");
  26. var import_entity = require("../entity.cjs");
  27. var import_logger = require("../logger.cjs");
  28. var import_db = require("../pg-core/db.cjs");
  29. var import_dialect = require("../pg-core/dialect.cjs");
  30. var import_relations = require("../relations.cjs");
  31. var import_utils = require("../utils.cjs");
  32. var import_session = require("./session.cjs");
  33. class BunSQLDatabase extends import_db.PgDatabase {
  34. static [import_entity.entityKind] = "BunSQLDatabase";
  35. }
  36. function construct(client, config = {}) {
  37. const dialect = new import_dialect.PgDialect({ casing: config.casing });
  38. let logger;
  39. if (config.logger === true) {
  40. logger = new import_logger.DefaultLogger();
  41. } else if (config.logger !== false) {
  42. logger = config.logger;
  43. }
  44. let schema;
  45. if (config.schema) {
  46. const tablesConfig = (0, import_relations.extractTablesRelationalConfig)(
  47. config.schema,
  48. import_relations.createTableRelationsHelpers
  49. );
  50. schema = {
  51. fullSchema: config.schema,
  52. schema: tablesConfig.tables,
  53. tableNamesMap: tablesConfig.tableNamesMap
  54. };
  55. }
  56. const session = new import_session.BunSQLSession(client, dialect, schema, { logger, cache: config.cache });
  57. const db = new BunSQLDatabase(dialect, session, schema);
  58. db.$client = client;
  59. db.$cache = config.cache;
  60. if (db.$cache) {
  61. db.$cache["invalidate"] = config.cache?.onMutate;
  62. }
  63. return db;
  64. }
  65. function drizzle(...params) {
  66. if (typeof params[0] === "string") {
  67. const instance = new import_bun.SQL(params[0]);
  68. return construct(instance, params[1]);
  69. }
  70. if ((0, import_utils.isConfig)(params[0])) {
  71. const { connection, client, ...drizzleConfig } = params[0];
  72. if (client) return construct(client, drizzleConfig);
  73. if (typeof connection === "object" && connection.url !== void 0) {
  74. const { url, ...config } = connection;
  75. const instance2 = new import_bun.SQL({ url, ...config });
  76. return construct(instance2, drizzleConfig);
  77. }
  78. const instance = new import_bun.SQL(connection);
  79. return construct(instance, drizzleConfig);
  80. }
  81. return construct(params[0], params[1]);
  82. }
  83. ((drizzle2) => {
  84. function mock(config) {
  85. return construct({
  86. options: {
  87. parsers: {},
  88. serializers: {}
  89. }
  90. }, config);
  91. }
  92. drizzle2.mock = mock;
  93. })(drizzle || (drizzle = {}));
  94. // Annotate the CommonJS export names for ESM import in node:
  95. 0 && (module.exports = {
  96. BunSQLDatabase,
  97. drizzle
  98. });
  99. //# sourceMappingURL=driver.cjs.map