driver.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { neonConfig, Pool } from "@neondatabase/serverless";
  2. import { entityKind } from "../entity.js";
  3. import { DefaultLogger } from "../logger.js";
  4. import { PgDatabase } from "../pg-core/db.js";
  5. import { PgDialect } from "../pg-core/dialect.js";
  6. import {
  7. createTableRelationsHelpers,
  8. extractTablesRelationalConfig
  9. } from "../relations.js";
  10. import { isConfig } from "../utils.js";
  11. import { NeonSession } from "./session.js";
  12. class NeonDriver {
  13. constructor(client, dialect, options = {}) {
  14. this.client = client;
  15. this.dialect = dialect;
  16. this.options = options;
  17. }
  18. static [entityKind] = "NeonDriver";
  19. createSession(schema) {
  20. return new NeonSession(this.client, this.dialect, schema, {
  21. logger: this.options.logger,
  22. cache: this.options.cache
  23. });
  24. }
  25. }
  26. class NeonDatabase extends PgDatabase {
  27. static [entityKind] = "NeonServerlessDatabase";
  28. }
  29. function construct(client, config = {}) {
  30. const dialect = new PgDialect({ casing: config.casing });
  31. let logger;
  32. if (config.logger === true) {
  33. logger = new DefaultLogger();
  34. } else if (config.logger !== false) {
  35. logger = config.logger;
  36. }
  37. let schema;
  38. if (config.schema) {
  39. const tablesConfig = extractTablesRelationalConfig(
  40. config.schema,
  41. createTableRelationsHelpers
  42. );
  43. schema = {
  44. fullSchema: config.schema,
  45. schema: tablesConfig.tables,
  46. tableNamesMap: tablesConfig.tableNamesMap
  47. };
  48. }
  49. const driver = new NeonDriver(client, dialect, { logger, cache: config.cache });
  50. const session = driver.createSession(schema);
  51. const db = new NeonDatabase(dialect, session, schema);
  52. db.$client = client;
  53. db.$cache = config.cache;
  54. if (db.$cache) {
  55. db.$cache["invalidate"] = config.cache?.onMutate;
  56. }
  57. return db;
  58. }
  59. function drizzle(...params) {
  60. if (typeof params[0] === "string") {
  61. const instance = new Pool({
  62. connectionString: params[0]
  63. });
  64. return construct(instance, params[1]);
  65. }
  66. if (isConfig(params[0])) {
  67. const { connection, client, ws, ...drizzleConfig } = params[0];
  68. if (ws) {
  69. neonConfig.webSocketConstructor = ws;
  70. }
  71. if (client) return construct(client, drizzleConfig);
  72. const instance = typeof connection === "string" ? new Pool({
  73. connectionString: connection
  74. }) : new Pool(connection);
  75. return construct(instance, drizzleConfig);
  76. }
  77. return construct(params[0], params[1]);
  78. }
  79. ((drizzle2) => {
  80. function mock(config) {
  81. return construct({}, config);
  82. }
  83. drizzle2.mock = mock;
  84. })(drizzle || (drizzle = {}));
  85. export {
  86. NeonDatabase,
  87. NeonDriver,
  88. drizzle
  89. };
  90. //# sourceMappingURL=driver.js.map