2019-10-07 20:56:30 +00:00
|
|
|
import { Database } from '@nozbe/watermelondb';
|
|
|
|
import SQLiteAdapter from '@nozbe/watermelondb/adapters/sqlite';
|
|
|
|
import logger from '@nozbe/watermelondb/utils/common/logger';
|
|
|
|
import RNFetchBlob from 'rn-fetch-blob';
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
import migrations from './model/migrations';
|
|
|
|
|
|
|
|
import { isIOS } from '../../utils/deviceInfo';
|
|
|
|
|
|
|
|
const appGroupPath = isIOS ? `${ RNFetchBlob.fs.syncPathAppGroup('group.ios.chat.rocket') }/` : '';
|
|
|
|
|
|
|
|
if (__DEV__ && isIOS) {
|
|
|
|
console.log(appGroupPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
class DB {
|
|
|
|
databases = {
|
|
|
|
serversDB: new Database({
|
|
|
|
adapter: new SQLiteAdapter({
|
|
|
|
dbName: `${ appGroupPath }default.db`,
|
|
|
|
schema: serversSchema
|
|
|
|
}),
|
|
|
|
modelClasses: [Server, User],
|
|
|
|
actionsEnabled: true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
get active() {
|
2019-12-11 23:01:12 +00:00
|
|
|
return this.databases.shareDB || this.databases.activeDB;
|
|
|
|
}
|
|
|
|
|
|
|
|
get share() {
|
|
|
|
return this.databases.shareDB;
|
|
|
|
}
|
|
|
|
|
|
|
|
set share(db) {
|
|
|
|
this.databases.shareDB = db;
|
2019-10-07 20:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get servers() {
|
|
|
|
return this.databases.serversDB;
|
|
|
|
}
|
|
|
|
|
2019-12-11 23:01:12 +00:00
|
|
|
setShareDB(database = '') {
|
|
|
|
const path = database.replace(/(^\w+:|^)\/\//, '').replace(/\//, '.');
|
|
|
|
const dbName = `${ appGroupPath }${ path }.db`;
|
|
|
|
|
|
|
|
const adapter = new SQLiteAdapter({
|
|
|
|
dbName,
|
|
|
|
schema: appSchema,
|
|
|
|
migrations
|
|
|
|
});
|
|
|
|
|
|
|
|
this.databases.shareDB = new Database({
|
|
|
|
adapter,
|
|
|
|
modelClasses: [
|
|
|
|
Subscription,
|
|
|
|
Message,
|
|
|
|
Thread,
|
|
|
|
ThreadMessage,
|
|
|
|
Upload
|
|
|
|
],
|
|
|
|
actionsEnabled: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:56:30 +00:00
|
|
|
setActiveDB(database = '') {
|
2019-12-11 23:01:12 +00:00
|
|
|
const path = database.replace(/(^\w+:|^)\/\//, '').replace(/\//, '.');
|
2019-10-07 20:56:30 +00:00
|
|
|
const dbName = `${ appGroupPath }${ path }.db`;
|
|
|
|
|
|
|
|
const adapter = new SQLiteAdapter({
|
|
|
|
dbName,
|
|
|
|
schema: appSchema,
|
|
|
|
migrations
|
|
|
|
});
|
|
|
|
|
|
|
|
this.databases.activeDB = new Database({
|
|
|
|
adapter,
|
|
|
|
modelClasses: [
|
|
|
|
Subscription,
|
|
|
|
Room,
|
|
|
|
Message,
|
|
|
|
Thread,
|
|
|
|
ThreadMessage,
|
|
|
|
CustomEmoji,
|
|
|
|
FrequentlyUsedEmoji,
|
|
|
|
Upload,
|
|
|
|
Setting,
|
|
|
|
Role,
|
|
|
|
Permission,
|
|
|
|
SlashCommand
|
|
|
|
],
|
|
|
|
actionsEnabled: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const db = new DB();
|
|
|
|
export default db;
|
|
|
|
|
|
|
|
if (!__DEV__) {
|
|
|
|
logger.silence();
|
|
|
|
}
|