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

83 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-02-17 01:24:20 +05:00
import StorageInterface from "../interfaces/storage";
2021-09-15 02:16:27 +05:00
import FS from "../interfaces/fs";
2021-04-06 11:55:23 +05:00
import Config from "../utils/config";
import http from "notes-core/utils/http";
import { EV, EVENTS } from "notes-core/common";
import { getCurrentHash, hashNavigate } from "../navigation";
2021-08-20 11:50:29 +05:00
import { isTesting } from "../utils/platform";
2021-02-17 01:24:20 +05:00
2021-09-23 15:18:11 +05:00
global.HTMLParser = new DOMParser().parseFromString(
"<body></body>",
"text/html"
);
2021-02-17 01:24:20 +05:00
/**
* @type {import("notes-core/api").default}
*/
var db;
async function initializeDatabase() {
const { default: Database } = await import("notes-core/api");
2021-09-15 02:16:27 +05:00
db = new Database(StorageInterface, EventSource, FS);
2021-08-20 11:50:29 +05:00
if (isTesting()) {
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",
2021-08-20 11:50:29 +05:00
});
// db.host({
// API_HOST: "http://192.168.10.29:5264",
// AUTH_HOST: "http://192.168.10.29:8264",
// SSE_HOST: "http://192.168.10.29:7264",
// });
2021-08-20 11:50:29 +05:00
}
2021-04-06 11:55:23 +05:00
await db.init();
2021-08-20 11:50:29 +05:00
if (!isAppHydrated() && !isTesting()) {
try {
loadDefaultNotes(db);
} catch (e) {}
}
return db;
2021-02-17 01:24:20 +05:00
}
export { db, initializeDatabase };
2021-04-06 11:55:23 +05:00
function isAppHydrated() {
return Config.get("hydrated", false);
}
2021-02-17 01:24:20 +05:00
2021-04-06 11:55:23 +05:00
function setAppHydrated() {
return Config.set("hydrated", true);
}
async function loadDefaultNotes(db) {
2021-07-03 12:42:37 +05:00
const notes = await http.get("/notes/index_v14.json");
2021-04-06 11:55:23 +05:00
if (!notes) return;
let autoOpenId;
const hash = getCurrentHash().replaceAll("#", "");
2021-04-06 11:55:23 +05:00
for (let note of notes) {
const content = await http.get(note.webContent);
let id = await db.notes.add({
title: note.title,
headline: note.headline,
localOnly: true,
content: { type: "tiny", data: content },
});
if (note.autoOpen) autoOpenId = id;
}
if (autoOpenId) {
hashNavigate(`/notes/${autoOpenId}/edit`);
if (hash) setTimeout(() => hashNavigate(hash), 100);
}
setAppHydrated();
EV.publish(EVENTS.appRefreshRequested);
2021-04-06 11:55:23 +05:00
}