Files

95 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2023-08-29 20:42:45 +05:00
import "./logger";
2023-06-10 12:17:23 +05:00
import { database } from "@notesnook/common";
2023-09-06 20:04:46 +05:00
import { logger as dbLogger } from "@notesnook/core/dist/logger";
import { Platform } from "react-native";
2023-09-06 20:04:46 +05:00
import * as Gzip from "react-native-gzip";
import EventSource from "../../utils/sse/even-source-ios";
import AndroidEventSource from "../../utils/sse/event-source";
2023-11-16 08:54:37 +05:00
import { SqliteAdapter, SqliteIntrospector, SqliteQueryCompiler } from "kysely";
2023-09-06 20:04:46 +05:00
import filesystem from "../filesystem";
import Storage from "./storage";
2023-11-16 08:54:37 +05:00
import { RNSqliteDriver } from "./sqlite.kysely";
2023-12-28 14:35:15 +05:00
import { getDatabaseKey } from "./encryption";
2020-10-13 17:02:14 +05:00
database.host(
__DEV__
? {
2024-02-06 12:08:41 +05:00
API_HOST: "https://api.notesnook.com",
AUTH_HOST: "https://auth.streetwriters.co",
SSE_HOST: "https://events.streetwriters.co",
SUBSCRIPTIONS_HOST: "https://subscriptions.streetwriters.co",
ISSUES_HOST: "https://issues.streetwriters.co"
// API_HOST: "http://192.168.43.5:5264",
// AUTH_HOST: "http://192.168.43.5:8264",
// SSE_HOST: "http://192.168.43.5:7264",
// SUBSCRIPTIONS_HOST: "http://192.168.43.5:9264",
// ISSUES_HOST: "http://192.168.43.5:2624"
}
: {
API_HOST: "https://api.notesnook.com",
AUTH_HOST: "https://auth.streetwriters.co",
SSE_HOST: "https://events.streetwriters.co",
SUBSCRIPTIONS_HOST: "https://subscriptions.streetwriters.co",
ISSUES_HOST: "https://issues.streetwriters.co"
}
);
2022-07-08 18:24:31 +05:00
2023-12-28 14:35:15 +05:00
export async function setupDatabase(password) {
const key = await getDatabaseKey(password);
if (!key)
throw new Error("Database setup failed, could not get database key");
2024-02-06 12:08:41 +05:00
console.log("Opening database with key:", !!key);
2023-12-28 14:35:15 +05:00
database.setup({
storage: Storage,
eventsource: Platform.OS === "ios" ? EventSource : AndroidEventSource,
fs: filesystem,
compressor: {
compress: Gzip.deflate,
decompress: Gzip.inflate
},
batchSize: 100,
sqliteOptions: {
dialect: (name) => ({
createDriver: () => {
return new RNSqliteDriver({ async: true, dbName: name });
},
createAdapter: () => new SqliteAdapter(),
createIntrospector: (db) => new SqliteIntrospector(db),
createQueryCompiler: () => new SqliteQueryCompiler()
}),
tempStore: "memory",
2024-02-12 19:22:03 +05:00
journalMode: Platform.OS === "ios" ? "DELETE" : "WAL",
2023-12-28 14:35:15 +05:00
password: key
}
});
}
2023-06-10 12:17:23 +05:00
export const db = database;
2024-05-20 15:03:57 +05:00
let DatabaseLogger = dbLogger.scope(Platform.OS);
const setLogger = () => {
DatabaseLogger = dbLogger.scope(Platform.OS);
};
export { DatabaseLogger, setLogger };