driver.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { entityKind } from "../entity.js";
  2. import { DefaultLogger } from "../logger.js";
  3. import { PgDatabase } from "../pg-core/db.js";
  4. import { PgDialect } from "../pg-core/dialect.js";
  5. import { createTableRelationsHelpers, extractTablesRelationalConfig } from "../relations.js";
  6. import { XataHttpSession } from "./session.js";
  7. class XataHttpDriver {
  8. constructor(client, dialect, options = {}) {
  9. this.client = client;
  10. this.dialect = dialect;
  11. this.options = options;
  12. this.initMappers();
  13. }
  14. static [entityKind] = "XataDriver";
  15. createSession(schema) {
  16. return new XataHttpSession(this.client, this.dialect, schema, {
  17. logger: this.options.logger,
  18. cache: this.options.cache
  19. });
  20. }
  21. initMappers() {
  22. }
  23. }
  24. class XataHttpDatabase extends PgDatabase {
  25. static [entityKind] = "XataHttpDatabase";
  26. }
  27. function drizzle(client, config = {}) {
  28. const dialect = new PgDialect({ casing: config.casing });
  29. let logger;
  30. if (config.logger === true) {
  31. logger = new DefaultLogger();
  32. } else if (config.logger !== false) {
  33. logger = config.logger;
  34. }
  35. let schema;
  36. if (config.schema) {
  37. const tablesConfig = extractTablesRelationalConfig(config.schema, createTableRelationsHelpers);
  38. schema = {
  39. fullSchema: config.schema,
  40. schema: tablesConfig.tables,
  41. tableNamesMap: tablesConfig.tableNamesMap
  42. };
  43. }
  44. const driver = new XataHttpDriver(client, dialect, { logger, cache: config.cache });
  45. const session = driver.createSession(schema);
  46. const db = new XataHttpDatabase(
  47. dialect,
  48. session,
  49. schema
  50. );
  51. db.$client = client;
  52. db.$cache = config.cache;
  53. if (db.$cache) {
  54. db.$cache["invalidate"] = config.cache?.onMutate;
  55. }
  56. return db;
  57. }
  58. export {
  59. XataHttpDatabase,
  60. XataHttpDriver,
  61. drizzle
  62. };
  63. //# sourceMappingURL=driver.js.map