CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
migrator.cjs90 linesDownload Raw Back to expo-sqlite
1"use strict";2var __defProp = Object.defineProperty;3var __getOwnPropDesc = Object.getOwnPropertyDescriptor;4var __getOwnPropNames = Object.getOwnPropertyNames;5var __hasOwnProp = Object.prototype.hasOwnProperty;6var __export = (target, all) => {7  for (var name in all)8    __defProp(target, name, { get: all[name], enumerable: true });9};10var __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};18var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);19var migrator_exports = {};20__export(migrator_exports, {21  migrate: () => migrate,22  useMigrations: () => useMigrations23});24module.exports = __toCommonJS(migrator_exports);25var import_react = require("react");26async 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}49async function migrate(db, config) {50  const migrations = await readMigrationFiles(config);51  return db.dialect.migrate(migrations, db.session);52}53const useMigrations = (db, migrations) => {54  const initialState = {55    success: false,56    error: void 057  };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:860 && (module.exports = {87  migrate,88  useMigrations89});90//# sourceMappingURL=migrator.cjs.map