driver.cjs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. MySql2Database: () => MySql2Database,
  22. MySql2Driver: () => MySql2Driver,
  23. MySqlDatabase: () => import_db2.MySqlDatabase,
  24. drizzle: () => drizzle
  25. });
  26. module.exports = __toCommonJS(driver_exports);
  27. var import_mysql2 = require("mysql2");
  28. var import_entity = require("../entity.cjs");
  29. var import_logger = require("../logger.cjs");
  30. var import_db = require("../mysql-core/db.cjs");
  31. var import_dialect = require("../mysql-core/dialect.cjs");
  32. var import_relations = require("../relations.cjs");
  33. var import_utils = require("../utils.cjs");
  34. var import_errors = require("../errors.cjs");
  35. var import_session = require("./session.cjs");
  36. var import_db2 = require("../mysql-core/db.cjs");
  37. class MySql2Driver {
  38. constructor(client, dialect, options = {}) {
  39. this.client = client;
  40. this.dialect = dialect;
  41. this.options = options;
  42. }
  43. static [import_entity.entityKind] = "MySql2Driver";
  44. createSession(schema, mode) {
  45. return new import_session.MySql2Session(this.client, this.dialect, schema, {
  46. logger: this.options.logger,
  47. mode,
  48. cache: this.options.cache
  49. });
  50. }
  51. }
  52. class MySql2Database extends import_db.MySqlDatabase {
  53. static [import_entity.entityKind] = "MySql2Database";
  54. }
  55. function construct(client, config = {}) {
  56. const dialect = new import_dialect.MySqlDialect({ casing: config.casing });
  57. let logger;
  58. if (config.logger === true) {
  59. logger = new import_logger.DefaultLogger();
  60. } else if (config.logger !== false) {
  61. logger = config.logger;
  62. }
  63. const clientForInstance = isCallbackClient(client) ? client.promise() : client;
  64. let schema;
  65. if (config.schema) {
  66. if (config.mode === void 0) {
  67. throw new import_errors.DrizzleError({
  68. message: 'You need to specify "mode": "planetscale" or "default" when providing a schema. Read more: https://orm.drizzle.team/docs/rqb#modes'
  69. });
  70. }
  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 mode = config.mode ?? "default";
  82. const driver = new MySql2Driver(clientForInstance, dialect, { logger, cache: config.cache });
  83. const session = driver.createSession(schema, mode);
  84. const db = new MySql2Database(dialect, session, schema, mode);
  85. db.$client = client;
  86. db.$cache = config.cache;
  87. if (db.$cache) {
  88. db.$cache["invalidate"] = config.cache?.onMutate;
  89. }
  90. return db;
  91. }
  92. function isCallbackClient(client) {
  93. return typeof client.promise === "function";
  94. }
  95. function drizzle(...params) {
  96. if (typeof params[0] === "string") {
  97. const connectionString = params[0];
  98. const instance = (0, import_mysql2.createPool)({
  99. uri: connectionString
  100. });
  101. return construct(instance, params[1]);
  102. }
  103. if ((0, import_utils.isConfig)(params[0])) {
  104. const { connection, client, ...drizzleConfig } = params[0];
  105. if (client) return construct(client, drizzleConfig);
  106. const instance = typeof connection === "string" ? (0, import_mysql2.createPool)({
  107. uri: connection,
  108. supportBigNumbers: true
  109. }) : (0, import_mysql2.createPool)(connection);
  110. const db = construct(instance, drizzleConfig);
  111. return db;
  112. }
  113. return construct(params[0], params[1]);
  114. }
  115. ((drizzle2) => {
  116. function mock(config) {
  117. return construct({}, config);
  118. }
  119. drizzle2.mock = mock;
  120. })(drizzle || (drizzle = {}));
  121. // Annotate the CommonJS export names for ESM import in node:
  122. 0 && (module.exports = {
  123. MySql2Database,
  124. MySql2Driver,
  125. MySqlDatabase,
  126. drizzle
  127. });
  128. //# sourceMappingURL=driver.cjs.map