driver.cjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. TiDBServerlessDatabase: () => TiDBServerlessDatabase,
  22. drizzle: () => drizzle
  23. });
  24. module.exports = __toCommonJS(driver_exports);
  25. var import_serverless = require("@tidbcloud/serverless");
  26. var import_entity = require("../entity.cjs");
  27. var import_logger = require("../logger.cjs");
  28. var import_db = require("../mysql-core/db.cjs");
  29. var import_dialect = require("../mysql-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 TiDBServerlessDatabase extends import_db.MySqlDatabase {
  34. static [import_entity.entityKind] = "TiDBServerlessDatabase";
  35. }
  36. function construct(client, config = {}) {
  37. const dialect = new import_dialect.MySqlDialect({ 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.TiDBServerlessSession(client, dialect, void 0, schema, { logger, cache: config.cache });
  57. const db = new TiDBServerlessDatabase(dialect, session, schema, "default");
  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 = (0, import_serverless.connect)({
  68. url: params[0]
  69. });
  70. return construct(instance, params[1]);
  71. }
  72. if ((0, import_utils.isConfig)(params[0])) {
  73. const { connection, client, ...drizzleConfig } = params[0];
  74. if (client) return construct(client, drizzleConfig);
  75. const instance = typeof connection === "string" ? (0, import_serverless.connect)({
  76. url: connection
  77. }) : (0, import_serverless.connect)(connection);
  78. return construct(instance, drizzleConfig);
  79. }
  80. return construct(params[0], params[1]);
  81. }
  82. ((drizzle2) => {
  83. function mock(config) {
  84. return construct({}, config);
  85. }
  86. drizzle2.mock = mock;
  87. })(drizzle || (drizzle = {}));
  88. // Annotate the CommonJS export names for ESM import in node:
  89. 0 && (module.exports = {
  90. TiDBServerlessDatabase,
  91. drizzle
  92. });
  93. //# sourceMappingURL=driver.cjs.map