migrator.cjs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 migrator_exports = {};
  20. __export(migrator_exports, {
  21. migrate: () => migrate,
  22. useMigrations: () => useMigrations
  23. });
  24. module.exports = __toCommonJS(migrator_exports);
  25. var import_react = require("react");
  26. async function readMigrationFiles({ journal, migrations }) {
  27. const migrationQueries = [];
  28. for await (const journalEntry of journal.entries) {
  29. const query = migrations[`m${journalEntry.idx.toString().padStart(4, "0")}`];
  30. if (!query) {
  31. throw new Error(`Missing migration: ${journalEntry.tag}`);
  32. }
  33. try {
  34. const result = query.split("--> statement-breakpoint").map((it) => {
  35. return it;
  36. });
  37. migrationQueries.push({
  38. sql: result,
  39. bps: journalEntry.breakpoints,
  40. folderMillis: journalEntry.when,
  41. hash: ""
  42. });
  43. } catch {
  44. throw new Error(`Failed to parse migration: ${journalEntry.tag}`);
  45. }
  46. }
  47. return migrationQueries;
  48. }
  49. async function migrate(db, config) {
  50. const migrations = await readMigrationFiles(config);
  51. return db.dialect.migrate(migrations, db.session);
  52. }
  53. const useMigrations = (db, migrations) => {
  54. const initialState = {
  55. success: false,
  56. error: void 0
  57. };
  58. const fetchReducer = (state2, action) => {
  59. switch (action.type) {
  60. case "migrating": {
  61. return { ...initialState };
  62. }
  63. case "migrated": {
  64. return { ...initialState, success: action.payload };
  65. }
  66. case "error": {
  67. return { ...initialState, error: action.payload };
  68. }
  69. default: {
  70. return state2;
  71. }
  72. }
  73. };
  74. const [state, dispatch] = (0, import_react.useReducer)(fetchReducer, initialState);
  75. (0, import_react.useEffect)(() => {
  76. dispatch({ type: "migrating" });
  77. migrate(db, migrations).then(() => {
  78. dispatch({ type: "migrated", payload: true });
  79. }).catch((error) => {
  80. dispatch({ type: "error", payload: error });
  81. });
  82. }, []);
  83. return state;
  84. };
  85. // Annotate the CommonJS export names for ESM import in node:
  86. 0 && (module.exports = {
  87. migrate,
  88. useMigrations
  89. });
  90. //# sourceMappingURL=migrator.cjs.map