driver.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { createPool } from "mysql2";
  2. import { entityKind } from "../entity.js";
  3. import { DefaultLogger } from "../logger.js";
  4. import { MySqlDatabase } from "../mysql-core/db.js";
  5. import { MySqlDialect } from "../mysql-core/dialect.js";
  6. import {
  7. createTableRelationsHelpers,
  8. extractTablesRelationalConfig
  9. } from "../relations.js";
  10. import { isConfig } from "../utils.js";
  11. import { DrizzleError } from "../errors.js";
  12. import { MySql2Session } from "./session.js";
  13. class MySql2Driver {
  14. constructor(client, dialect, options = {}) {
  15. this.client = client;
  16. this.dialect = dialect;
  17. this.options = options;
  18. }
  19. static [entityKind] = "MySql2Driver";
  20. createSession(schema, mode) {
  21. return new MySql2Session(this.client, this.dialect, schema, {
  22. logger: this.options.logger,
  23. mode,
  24. cache: this.options.cache
  25. });
  26. }
  27. }
  28. import { MySqlDatabase as MySqlDatabase2 } from "../mysql-core/db.js";
  29. class MySql2Database extends MySqlDatabase {
  30. static [entityKind] = "MySql2Database";
  31. }
  32. function construct(client, config = {}) {
  33. const dialect = new MySqlDialect({ casing: config.casing });
  34. let logger;
  35. if (config.logger === true) {
  36. logger = new DefaultLogger();
  37. } else if (config.logger !== false) {
  38. logger = config.logger;
  39. }
  40. const clientForInstance = isCallbackClient(client) ? client.promise() : client;
  41. let schema;
  42. if (config.schema) {
  43. if (config.mode === void 0) {
  44. throw new DrizzleError({
  45. message: 'You need to specify "mode": "planetscale" or "default" when providing a schema. Read more: https://orm.drizzle.team/docs/rqb#modes'
  46. });
  47. }
  48. const tablesConfig = extractTablesRelationalConfig(
  49. config.schema,
  50. createTableRelationsHelpers
  51. );
  52. schema = {
  53. fullSchema: config.schema,
  54. schema: tablesConfig.tables,
  55. tableNamesMap: tablesConfig.tableNamesMap
  56. };
  57. }
  58. const mode = config.mode ?? "default";
  59. const driver = new MySql2Driver(clientForInstance, dialect, { logger, cache: config.cache });
  60. const session = driver.createSession(schema, mode);
  61. const db = new MySql2Database(dialect, session, schema, mode);
  62. db.$client = client;
  63. db.$cache = config.cache;
  64. if (db.$cache) {
  65. db.$cache["invalidate"] = config.cache?.onMutate;
  66. }
  67. return db;
  68. }
  69. function isCallbackClient(client) {
  70. return typeof client.promise === "function";
  71. }
  72. function drizzle(...params) {
  73. if (typeof params[0] === "string") {
  74. const connectionString = params[0];
  75. const instance = createPool({
  76. uri: connectionString
  77. });
  78. return construct(instance, params[1]);
  79. }
  80. if (isConfig(params[0])) {
  81. const { connection, client, ...drizzleConfig } = params[0];
  82. if (client) return construct(client, drizzleConfig);
  83. const instance = typeof connection === "string" ? createPool({
  84. uri: connection,
  85. supportBigNumbers: true
  86. }) : createPool(connection);
  87. const db = construct(instance, drizzleConfig);
  88. return db;
  89. }
  90. return construct(params[0], params[1]);
  91. }
  92. ((drizzle2) => {
  93. function mock(config) {
  94. return construct({}, config);
  95. }
  96. drizzle2.mock = mock;
  97. })(drizzle || (drizzle = {}));
  98. export {
  99. MySql2Database,
  100. MySql2Driver,
  101. MySqlDatabase2 as MySqlDatabase,
  102. drizzle
  103. };
  104. //# sourceMappingURL=driver.js.map