Files
notesnook/apps/web/src/common/db.js

59 lines
1.8 KiB
JavaScript
Raw Normal View History

import { EventSourcePolyfill as EventSource } from "event-source-polyfill";
import { EVENTS } from "@streetwriters/notesnook-core/common";
2022-07-07 13:29:52 +05:00
import { TaskManager } from "./task-manager";
2021-02-17 01:24:20 +05:00
/**
* @type {import("@streetwriters/notesnook-core/api").default}
2021-02-17 01:24:20 +05:00
*/
var db;
2022-04-01 19:01:06 +05:00
async function initializeDatabase(persistence) {
const { default: Database } = await import(
"@streetwriters/notesnook-core/api"
);
2022-04-01 19:01:06 +05:00
const { NNStorage } = await import("../interfaces/storage");
const { default: FS } = await import("../interfaces/fs");
2022-04-01 19:01:06 +05:00
db = new Database(new NNStorage(persistence), EventSource, FS);
// if (isTesting()) {
2022-07-07 13:29:52 +05:00
db.host({
API_HOST: "https://api.notesnook.com",
AUTH_HOST: "https://auth.streetwriters.co",
SSE_HOST: "https://events.streetwriters.co",
});
// } else {
// db.host({
// API_HOST: "http://localhost:5264",
// AUTH_HOST: "http://localhost:8264",
// SSE_HOST: "http://localhost:7264",
// });
2022-07-07 13:29:52 +05:00
// const base = `http://${process.env.REACT_APP_LOCALHOST}`;
// db.host({
// API_HOST: `${base}:5264`,
// AUTH_HOST: `${base}:8264`,
// SSE_HOST: `${base}:7264`,
// ISSUES_HOST: `${base}:2624`,
// SUBSCRIPTIONS_HOST: `${base}:9264`,
// });
// }
2021-04-06 11:55:23 +05:00
2022-07-07 13:29:52 +05:00
db.eventManager.subscribe(EVENTS.databaseMigrating, async ({ from, to }) => {
await TaskManager.startTask({
type: "modal",
title: `Migrating your database`,
subtitle:
"Please do not close your browser/app before the migration is done.",
action: (task) => {
task({ text: `Migrating database from v${from} to v${to}` });
return new Promise((resolve) => {
db.eventManager.subscribe(EVENTS.databaseMigrated, resolve);
});
},
});
});
await db.init();
return db;
2021-02-17 01:24:20 +05:00
}
export { db, initializeDatabase };