driver.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { neon, types } 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 { createTableRelationsHelpers, extractTablesRelationalConfig } from "../relations.js";
  7. import { isConfig } from "../utils.js";
  8. import { NeonHttpSession } from "./session.js";
  9. class NeonHttpDriver {
  10. constructor(client, dialect, options = {}) {
  11. this.client = client;
  12. this.dialect = dialect;
  13. this.options = options;
  14. this.initMappers();
  15. }
  16. static [entityKind] = "NeonHttpDriver";
  17. createSession(schema) {
  18. return new NeonHttpSession(this.client, this.dialect, schema, {
  19. logger: this.options.logger,
  20. cache: this.options.cache
  21. });
  22. }
  23. initMappers() {
  24. types.setTypeParser(types.builtins.TIMESTAMPTZ, (val) => val);
  25. types.setTypeParser(types.builtins.TIMESTAMP, (val) => val);
  26. types.setTypeParser(types.builtins.DATE, (val) => val);
  27. types.setTypeParser(types.builtins.INTERVAL, (val) => val);
  28. types.setTypeParser(1231, (val) => val);
  29. types.setTypeParser(1115, (val) => val);
  30. types.setTypeParser(1185, (val) => val);
  31. types.setTypeParser(1187, (val) => val);
  32. types.setTypeParser(1182, (val) => val);
  33. }
  34. }
  35. function wrap(target, token, cb, deep) {
  36. return new Proxy(target, {
  37. get(target2, p) {
  38. const element = target2[p];
  39. if (typeof element !== "function" && (typeof element !== "object" || element === null)) return element;
  40. if (deep) return wrap(element, token, cb);
  41. if (p === "query") return wrap(element, token, cb, true);
  42. return new Proxy(element, {
  43. apply(target3, thisArg, argArray) {
  44. const res = target3.call(thisArg, ...argArray);
  45. if (typeof res === "object" && res !== null && "setToken" in res && typeof res.setToken === "function") {
  46. res.setToken(token);
  47. }
  48. return cb(target3, p, res);
  49. }
  50. });
  51. }
  52. });
  53. }
  54. class NeonHttpDatabase extends PgDatabase {
  55. static [entityKind] = "NeonHttpDatabase";
  56. $withAuth(token) {
  57. this.authToken = token;
  58. return wrap(this, token, (target, p, res) => {
  59. if (p === "with") {
  60. return wrap(res, token, (_, __, res2) => res2);
  61. }
  62. return res;
  63. });
  64. }
  65. async batch(batch) {
  66. return this.session.batch(batch);
  67. }
  68. }
  69. function construct(client, config = {}) {
  70. const dialect = new PgDialect({ casing: config.casing });
  71. let logger;
  72. if (config.logger === true) {
  73. logger = new DefaultLogger();
  74. } else if (config.logger !== false) {
  75. logger = config.logger;
  76. }
  77. let schema;
  78. if (config.schema) {
  79. const tablesConfig = extractTablesRelationalConfig(
  80. config.schema,
  81. createTableRelationsHelpers
  82. );
  83. schema = {
  84. fullSchema: config.schema,
  85. schema: tablesConfig.tables,
  86. tableNamesMap: tablesConfig.tableNamesMap
  87. };
  88. }
  89. const driver = new NeonHttpDriver(client, dialect, { logger, cache: config.cache });
  90. const session = driver.createSession(schema);
  91. const db = new NeonHttpDatabase(
  92. dialect,
  93. session,
  94. schema
  95. );
  96. db.$client = client;
  97. db.$cache = config.cache;
  98. if (db.$cache) {
  99. db.$cache["invalidate"] = config.cache?.onMutate;
  100. }
  101. return db;
  102. }
  103. function drizzle(...params) {
  104. if (typeof params[0] === "string") {
  105. const instance = neon(params[0]);
  106. return construct(instance, params[1]);
  107. }
  108. if (isConfig(params[0])) {
  109. const { connection, client, ...drizzleConfig } = params[0];
  110. if (client) return construct(client, drizzleConfig);
  111. if (typeof connection === "object") {
  112. const { connectionString, ...options } = connection;
  113. const instance2 = neon(connectionString, options);
  114. return construct(instance2, drizzleConfig);
  115. }
  116. const instance = neon(connection);
  117. return construct(instance, drizzleConfig);
  118. }
  119. return construct(params[0], params[1]);
  120. }
  121. ((drizzle2) => {
  122. function mock(config) {
  123. return construct({}, config);
  124. }
  125. drizzle2.mock = mock;
  126. })(drizzle || (drizzle = {}));
  127. export {
  128. NeonHttpDatabase,
  129. NeonHttpDriver,
  130. drizzle
  131. };
  132. //# sourceMappingURL=driver.js.map