driver.cjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. PlanetScaleDatabase: () => PlanetScaleDatabase,
  22. drizzle: () => drizzle
  23. });
  24. module.exports = __toCommonJS(driver_exports);
  25. var import_database = require("@planetscale/database");
  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 PlanetScaleDatabase extends import_db.MySqlDatabase {
  34. static [import_entity.entityKind] = "PlanetScaleDatabase";
  35. }
  36. function construct(client, config = {}) {
  37. if (!(client instanceof import_database.Client)) {
  38. throw new Error(`Warning: You need to pass an instance of Client:
  39. import { Client } from "@planetscale/database";
  40. const client = new Client({
  41. host: process.env["DATABASE_HOST"],
  42. username: process.env["DATABASE_USERNAME"],
  43. password: process.env["DATABASE_PASSWORD"],
  44. });
  45. const db = drizzle(client);
  46. `);
  47. }
  48. const dialect = new import_dialect.MySqlDialect({ casing: config.casing });
  49. let logger;
  50. if (config.logger === true) {
  51. logger = new import_logger.DefaultLogger();
  52. } else if (config.logger !== false) {
  53. logger = config.logger;
  54. }
  55. let schema;
  56. if (config.schema) {
  57. const tablesConfig = (0, import_relations.extractTablesRelationalConfig)(
  58. config.schema,
  59. import_relations.createTableRelationsHelpers
  60. );
  61. schema = {
  62. fullSchema: config.schema,
  63. schema: tablesConfig.tables,
  64. tableNamesMap: tablesConfig.tableNamesMap
  65. };
  66. }
  67. const session = new import_session.PlanetscaleSession(client, dialect, void 0, schema, { logger, cache: config.cache });
  68. const db = new PlanetScaleDatabase(dialect, session, schema, "planetscale");
  69. db.$client = client;
  70. db.$cache = config.cache;
  71. if (db.$cache) {
  72. db.$cache["invalidate"] = config.cache?.onMutate;
  73. }
  74. return db;
  75. }
  76. function drizzle(...params) {
  77. if (typeof params[0] === "string") {
  78. const instance = new import_database.Client({
  79. url: params[0]
  80. });
  81. return construct(instance, params[1]);
  82. }
  83. if ((0, import_utils.isConfig)(params[0])) {
  84. const { connection, client, ...drizzleConfig } = params[0];
  85. if (client) return construct(client, drizzleConfig);
  86. const instance = typeof connection === "string" ? new import_database.Client({
  87. url: connection
  88. }) : new import_database.Client(
  89. connection
  90. );
  91. return construct(instance, drizzleConfig);
  92. }
  93. return construct(params[0], params[1]);
  94. }
  95. ((drizzle2) => {
  96. function mock(config) {
  97. return construct({}, config);
  98. }
  99. drizzle2.mock = mock;
  100. })(drizzle || (drizzle = {}));
  101. // Annotate the CommonJS export names for ESM import in node:
  102. 0 && (module.exports = {
  103. PlanetScaleDatabase,
  104. drizzle
  105. });
  106. //# sourceMappingURL=driver.cjs.map