2019-09-16 20:26:32 +00:00
|
|
|
import { Database } from '@nozbe/watermelondb';
|
|
|
|
import SQLiteAdapter from '@nozbe/watermelondb/adapters/sqlite';
|
2019-09-26 14:59:27 +00:00
|
|
|
import logger from '@nozbe/watermelondb/utils/common/logger';
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2022-06-06 14:17:51 +00:00
|
|
|
import { isIOS } from '../methods/helpers';
|
|
|
|
import appGroup from './appGroup';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { isOfficial } from '../constants';
|
2019-09-16 20:26:32 +00:00
|
|
|
import Subscription from './model/Subscription';
|
|
|
|
import Room from './model/Room';
|
|
|
|
import Message from './model/Message';
|
|
|
|
import Thread from './model/Thread';
|
|
|
|
import ThreadMessage from './model/ThreadMessage';
|
|
|
|
import CustomEmoji from './model/CustomEmoji';
|
|
|
|
import FrequentlyUsedEmoji from './model/FrequentlyUsedEmoji';
|
|
|
|
import Upload from './model/Upload';
|
|
|
|
import Setting from './model/Setting';
|
|
|
|
import Role from './model/Role';
|
|
|
|
import Permission from './model/Permission';
|
|
|
|
import SlashCommand from './model/SlashCommand';
|
|
|
|
import User from './model/User';
|
2020-10-30 13:12:02 +00:00
|
|
|
import LoggedUser from './model/servers/User';
|
|
|
|
import Server from './model/servers/Server';
|
2020-09-11 17:10:16 +00:00
|
|
|
import ServersHistory from './model/ServersHistory';
|
2019-09-16 20:26:32 +00:00
|
|
|
import serversSchema from './schema/servers';
|
|
|
|
import appSchema from './schema/app';
|
2019-09-18 17:32:12 +00:00
|
|
|
import migrations from './model/migrations';
|
2020-10-30 13:12:02 +00:00
|
|
|
import serversMigrations from './model/servers/migrations';
|
2022-02-01 13:39:09 +00:00
|
|
|
import { TAppDatabase, TServerDatabase } from './interfaces';
|
2020-03-30 20:19:01 +00:00
|
|
|
|
2020-08-31 20:29:44 +00:00
|
|
|
const appGroupPath = isIOS ? appGroup.path : '';
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
if (__DEV__ && isIOS) {
|
|
|
|
console.log(appGroupPath);
|
|
|
|
}
|
|
|
|
|
2022-02-01 13:39:09 +00:00
|
|
|
const getDatabasePath = (name: string) => `${appGroupPath}${name}${isOfficial ? '' : '-experimental'}.db`;
|
2020-12-14 16:20:32 +00:00
|
|
|
|
2022-02-01 13:39:09 +00:00
|
|
|
export const getDatabase = (database = ''): Database => {
|
2020-05-04 20:20:45 +00:00
|
|
|
const path = database.replace(/(^\w+:|^)\/\//, '').replace(/\//g, '.');
|
2020-12-14 16:20:32 +00:00
|
|
|
const dbName = getDatabasePath(path);
|
2020-05-04 20:20:45 +00:00
|
|
|
|
|
|
|
const adapter = new SQLiteAdapter({
|
|
|
|
dbName,
|
|
|
|
schema: appSchema,
|
|
|
|
migrations
|
|
|
|
});
|
|
|
|
|
|
|
|
return new Database({
|
|
|
|
adapter,
|
|
|
|
modelClasses: [
|
|
|
|
Subscription,
|
|
|
|
Room,
|
|
|
|
Message,
|
|
|
|
Thread,
|
|
|
|
ThreadMessage,
|
|
|
|
CustomEmoji,
|
|
|
|
FrequentlyUsedEmoji,
|
|
|
|
Upload,
|
|
|
|
Setting,
|
|
|
|
Role,
|
|
|
|
Permission,
|
2020-10-30 13:12:02 +00:00
|
|
|
SlashCommand,
|
|
|
|
User
|
2021-10-01 20:35:13 +00:00
|
|
|
]
|
2020-05-04 20:20:45 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-02-01 13:39:09 +00:00
|
|
|
interface IDatabases {
|
2022-03-16 20:40:32 +00:00
|
|
|
shareDB?: TAppDatabase | null;
|
2022-02-01 13:39:09 +00:00
|
|
|
serversDB: TServerDatabase;
|
|
|
|
activeDB?: TAppDatabase;
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
class DB {
|
2022-02-01 13:39:09 +00:00
|
|
|
databases: IDatabases = {
|
2019-09-16 20:26:32 +00:00
|
|
|
serversDB: new Database({
|
|
|
|
adapter: new SQLiteAdapter({
|
2020-12-14 16:20:32 +00:00
|
|
|
dbName: getDatabasePath('default'),
|
2020-03-30 20:19:01 +00:00
|
|
|
schema: serversSchema,
|
|
|
|
migrations: serversMigrations
|
2019-09-16 20:26:32 +00:00
|
|
|
}),
|
2021-10-01 20:35:13 +00:00
|
|
|
modelClasses: [Server, LoggedUser, ServersHistory]
|
2022-02-01 13:39:09 +00:00
|
|
|
}) as TServerDatabase
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2022-02-01 13:39:09 +00:00
|
|
|
// Expected at least one database
|
|
|
|
get active(): TAppDatabase {
|
|
|
|
return this.databases.shareDB || this.databases.activeDB!;
|
2019-12-04 16:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get share() {
|
|
|
|
return this.databases.shareDB;
|
|
|
|
}
|
|
|
|
|
|
|
|
set share(db) {
|
|
|
|
this.databases.shareDB = db;
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get servers() {
|
|
|
|
return this.databases.serversDB;
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:41:37 +00:00
|
|
|
setShareDB(database = '') {
|
2020-02-05 14:01:01 +00:00
|
|
|
const path = database.replace(/(^\w+:|^)\/\//, '').replace(/\//g, '.');
|
2020-12-14 16:20:32 +00:00
|
|
|
const dbName = getDatabasePath(path);
|
2019-12-04 16:41:37 +00:00
|
|
|
|
|
|
|
const adapter = new SQLiteAdapter({
|
|
|
|
dbName,
|
|
|
|
schema: appSchema,
|
|
|
|
migrations
|
|
|
|
});
|
|
|
|
|
|
|
|
this.databases.shareDB = new Database({
|
|
|
|
adapter,
|
|
|
|
modelClasses: [
|
|
|
|
Subscription,
|
|
|
|
Message,
|
|
|
|
Thread,
|
|
|
|
ThreadMessage,
|
2020-04-13 12:51:16 +00:00
|
|
|
Upload,
|
2020-07-08 16:19:41 +00:00
|
|
|
Permission,
|
|
|
|
CustomEmoji,
|
2020-10-30 13:12:02 +00:00
|
|
|
FrequentlyUsedEmoji,
|
2020-10-30 15:54:02 +00:00
|
|
|
Setting,
|
2020-10-30 13:12:02 +00:00
|
|
|
User
|
2021-10-01 20:35:13 +00:00
|
|
|
]
|
2022-02-01 13:39:09 +00:00
|
|
|
}) as TAppDatabase;
|
2019-12-04 16:41:37 +00:00
|
|
|
}
|
|
|
|
|
2022-02-01 13:39:09 +00:00
|
|
|
setActiveDB(database: string) {
|
|
|
|
this.databases.activeDB = getDatabase(database) as TAppDatabase;
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const db = new DB();
|
|
|
|
export default db;
|
2019-09-26 14:59:27 +00:00
|
|
|
|
|
|
|
if (!__DEV__) {
|
|
|
|
logger.silence();
|
|
|
|
}
|