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
|
|
|
|
|
|
|
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';
|
|
|
|
import Server from './model/Server';
|
|
|
|
|
|
|
|
import serversSchema from './schema/servers';
|
|
|
|
import appSchema from './schema/app';
|
|
|
|
|
2019-09-18 17:32:12 +00:00
|
|
|
import migrations from './model/migrations';
|
|
|
|
|
2020-03-30 20:19:01 +00:00
|
|
|
import serversMigrations from './model/serversMigrations';
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
import { isIOS } from '../../utils/deviceInfo';
|
2020-08-31 20:29:44 +00:00
|
|
|
import appGroup from '../../utils/appGroup';
|
2019-09-16 20:26:32 +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);
|
|
|
|
}
|
|
|
|
|
2020-05-04 20:20:45 +00:00
|
|
|
export const getDatabase = (database = '') => {
|
|
|
|
const path = database.replace(/(^\w+:|^)\/\//, '').replace(/\//g, '.');
|
|
|
|
const dbName = `${ appGroupPath }${ path }.db`;
|
|
|
|
|
|
|
|
const adapter = new SQLiteAdapter({
|
|
|
|
dbName,
|
|
|
|
schema: appSchema,
|
|
|
|
migrations
|
|
|
|
});
|
|
|
|
|
|
|
|
return new Database({
|
|
|
|
adapter,
|
|
|
|
modelClasses: [
|
|
|
|
Subscription,
|
|
|
|
Room,
|
|
|
|
Message,
|
|
|
|
Thread,
|
|
|
|
ThreadMessage,
|
|
|
|
CustomEmoji,
|
|
|
|
FrequentlyUsedEmoji,
|
|
|
|
Upload,
|
|
|
|
Setting,
|
|
|
|
Role,
|
|
|
|
Permission,
|
|
|
|
SlashCommand
|
|
|
|
],
|
|
|
|
actionsEnabled: true
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
class DB {
|
|
|
|
databases = {
|
|
|
|
serversDB: new Database({
|
|
|
|
adapter: new SQLiteAdapter({
|
|
|
|
dbName: `${ appGroupPath }default.db`,
|
2020-03-30 20:19:01 +00:00
|
|
|
schema: serversSchema,
|
|
|
|
migrations: serversMigrations
|
2019-09-16 20:26:32 +00:00
|
|
|
}),
|
|
|
|
modelClasses: [Server, User],
|
|
|
|
actionsEnabled: true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
get active() {
|
2019-12-04 16:41:37 +00:00
|
|
|
return this.databases.shareDB || this.databases.activeDB;
|
|
|
|
}
|
|
|
|
|
|
|
|
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, '.');
|
2019-12-04 16:41:37 +00:00
|
|
|
const dbName = `${ appGroupPath }${ path }.db`;
|
|
|
|
|
|
|
|
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,
|
|
|
|
FrequentlyUsedEmoji
|
2019-12-04 16:41:37 +00:00
|
|
|
],
|
|
|
|
actionsEnabled: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-04 20:20:45 +00:00
|
|
|
setActiveDB(database) {
|
|
|
|
this.databases.activeDB = getDatabase(database);
|
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();
|
|
|
|
}
|