2017-08-03 18:23:43 +00:00
|
|
|
import Realm from 'realm';
|
2017-12-27 15:22:06 +00:00
|
|
|
|
2017-09-21 17:08:00 +00:00
|
|
|
// import { AsyncStorage } from 'react-native';
|
2017-12-27 15:22:06 +00:00
|
|
|
// Realm.clearTestState();
|
|
|
|
// AsyncStorage.clear();
|
2017-08-03 18:23:43 +00:00
|
|
|
|
|
|
|
const serversSchema = {
|
|
|
|
name: 'servers',
|
|
|
|
primaryKey: 'id',
|
|
|
|
properties: {
|
|
|
|
id: 'string',
|
2018-08-31 16:46:33 +00:00
|
|
|
name: { type: 'string', optional: true },
|
2019-02-27 20:29:37 +00:00
|
|
|
iconURL: { type: 'string', optional: true },
|
2019-04-17 17:01:03 +00:00
|
|
|
roomsUpdatedAt: { type: 'date', optional: true },
|
|
|
|
version: 'string?'
|
2017-08-03 18:23:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const settingsSchema = {
|
|
|
|
name: 'settings',
|
|
|
|
primaryKey: '_id',
|
|
|
|
properties: {
|
|
|
|
_id: 'string',
|
2017-08-13 23:02:46 +00:00
|
|
|
valueAsString: { type: 'string', optional: true },
|
|
|
|
valueAsBoolean: { type: 'bool', optional: true },
|
2017-11-19 04:10:29 +00:00
|
|
|
valueAsNumber: { type: 'int', optional: true },
|
|
|
|
_updatedAt: { type: 'date', optional: true }
|
2017-08-03 18:23:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-24 20:44:52 +00:00
|
|
|
const permissionsSchema = {
|
|
|
|
name: 'permissions',
|
|
|
|
primaryKey: '_id',
|
|
|
|
properties: {
|
|
|
|
_id: 'string',
|
2019-04-26 20:54:58 +00:00
|
|
|
roles: 'string[]',
|
2017-11-24 20:44:52 +00:00
|
|
|
_updatedAt: { type: 'date', optional: true }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-08 20:23:46 +00:00
|
|
|
const roomsSchema = {
|
|
|
|
name: 'rooms',
|
|
|
|
primaryKey: '_id',
|
|
|
|
properties: {
|
|
|
|
_id: 'string',
|
2019-05-20 20:43:50 +00:00
|
|
|
name: 'string?',
|
2018-05-24 20:17:45 +00:00
|
|
|
broadcast: { type: 'bool', optional: true }
|
2017-11-08 20:23:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-03 18:23:43 +00:00
|
|
|
const subscriptionSchema = {
|
|
|
|
name: 'subscriptions',
|
|
|
|
primaryKey: '_id',
|
|
|
|
properties: {
|
|
|
|
_id: 'string',
|
2017-11-22 16:40:59 +00:00
|
|
|
f: { type: 'bool', optional: true },
|
2017-08-03 18:23:43 +00:00
|
|
|
t: 'string',
|
2017-08-07 00:34:35 +00:00
|
|
|
ts: { type: 'date', optional: true },
|
|
|
|
ls: { type: 'date', optional: true },
|
2018-03-02 15:11:34 +00:00
|
|
|
name: { type: 'string', indexed: true },
|
2017-08-05 18:16:32 +00:00
|
|
|
fname: { type: 'string', optional: true },
|
2018-03-02 15:11:34 +00:00
|
|
|
rid: { type: 'string', indexed: true },
|
2017-08-07 00:34:35 +00:00
|
|
|
open: { type: 'bool', optional: true },
|
|
|
|
alert: { type: 'bool', optional: true },
|
2019-04-26 20:54:58 +00:00
|
|
|
roles: 'string[]',
|
2017-08-11 18:18:09 +00:00
|
|
|
unread: { type: 'int', optional: true },
|
2017-11-22 16:40:59 +00:00
|
|
|
userMentions: { type: 'int', optional: true },
|
2018-01-17 16:42:30 +00:00
|
|
|
roomUpdatedAt: { type: 'date', optional: true },
|
2018-02-16 18:34:25 +00:00
|
|
|
ro: { type: 'bool', optional: true },
|
2018-02-19 21:19:39 +00:00
|
|
|
lastOpen: { type: 'date', optional: true },
|
2018-03-02 15:11:34 +00:00
|
|
|
lastMessage: { type: 'messages', optional: true },
|
|
|
|
description: { type: 'string', optional: true },
|
|
|
|
announcement: { type: 'string', optional: true },
|
2018-03-23 16:49:51 +00:00
|
|
|
topic: { type: 'string', optional: true },
|
2018-03-29 17:55:37 +00:00
|
|
|
blocked: { type: 'bool', optional: true },
|
2018-05-24 20:17:45 +00:00
|
|
|
blocker: { type: 'bool', optional: true },
|
2018-03-29 17:55:37 +00:00
|
|
|
reactWhenReadOnly: { type: 'bool', optional: true },
|
|
|
|
archived: { type: 'bool', optional: true },
|
2018-04-24 19:34:03 +00:00
|
|
|
joinCodeRequired: { type: 'bool', optional: true },
|
|
|
|
notifications: { type: 'bool', optional: true },
|
2019-05-20 20:43:50 +00:00
|
|
|
muted: 'string[]',
|
2019-04-01 14:45:17 +00:00
|
|
|
broadcast: { type: 'bool', optional: true },
|
2019-04-08 12:35:28 +00:00
|
|
|
prid: { type: 'string', optional: true },
|
2019-04-24 18:36:29 +00:00
|
|
|
draftMessage: { type: 'string', optional: true },
|
|
|
|
lastThreadSync: 'date?'
|
2017-08-03 18:23:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-04 00:34:37 +00:00
|
|
|
const usersSchema = {
|
|
|
|
name: 'users',
|
2018-06-18 13:30:36 +00:00
|
|
|
primaryKey: '_id',
|
2017-08-04 00:34:37 +00:00
|
|
|
properties: {
|
2018-06-18 13:30:36 +00:00
|
|
|
_id: 'string',
|
2017-08-04 00:34:37 +00:00
|
|
|
username: 'string',
|
2019-05-20 20:43:50 +00:00
|
|
|
name: { type: 'string', optional: true }
|
2017-08-04 00:34:37 +00:00
|
|
|
}
|
2017-08-05 18:16:32 +00:00
|
|
|
};
|
2017-08-04 00:34:37 +00:00
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
const attachmentFields = {
|
|
|
|
name: 'attachmentFields',
|
|
|
|
properties: {
|
|
|
|
title: { type: 'string', optional: true },
|
|
|
|
value: { type: 'string', optional: true },
|
|
|
|
short: { type: 'bool', optional: true }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-15 19:28:46 +00:00
|
|
|
const attachment = {
|
|
|
|
name: 'attachment',
|
|
|
|
properties: {
|
|
|
|
description: { type: 'string', optional: true },
|
|
|
|
image_size: { type: 'int', optional: true },
|
|
|
|
image_type: { type: 'string', optional: true },
|
|
|
|
image_url: { type: 'string', optional: true },
|
2017-12-02 13:19:58 +00:00
|
|
|
audio_size: { type: 'int', optional: true },
|
|
|
|
audio_type: { type: 'string', optional: true },
|
|
|
|
audio_url: { type: 'string', optional: true },
|
|
|
|
video_size: { type: 'int', optional: true },
|
|
|
|
video_type: { type: 'string', optional: true },
|
|
|
|
video_url: { type: 'string', optional: true },
|
2017-08-15 19:28:46 +00:00
|
|
|
title: { type: 'string', optional: true },
|
|
|
|
title_link: { type: 'string', optional: true },
|
2018-07-10 13:40:32 +00:00
|
|
|
// title_link_download: { type: 'bool', optional: true },
|
2017-12-02 13:19:58 +00:00
|
|
|
type: { type: 'string', optional: true },
|
|
|
|
author_icon: { type: 'string', optional: true },
|
|
|
|
author_name: { type: 'string', optional: true },
|
|
|
|
author_link: { type: 'string', optional: true },
|
|
|
|
text: { type: 'string', optional: true },
|
|
|
|
color: { type: 'string', optional: true },
|
|
|
|
ts: { type: 'date', optional: true },
|
|
|
|
attachments: { type: 'list', objectType: 'attachment' },
|
2018-04-24 19:34:03 +00:00
|
|
|
fields: {
|
|
|
|
type: 'list', objectType: 'attachmentFields', default: []
|
|
|
|
}
|
2017-08-15 19:28:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-02 13:19:58 +00:00
|
|
|
const url = {
|
|
|
|
name: 'url',
|
2018-03-02 15:11:34 +00:00
|
|
|
primaryKey: 'url',
|
2017-12-02 13:19:58 +00:00
|
|
|
properties: {
|
2018-02-16 18:34:25 +00:00
|
|
|
// _id: { type: 'int', optional: true },
|
2017-12-02 13:19:58 +00:00
|
|
|
url: { type: 'string', optional: true },
|
|
|
|
title: { type: 'string', optional: true },
|
|
|
|
description: { type: 'string', optional: true },
|
|
|
|
image: { type: 'string', optional: true }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-30 19:48:26 +00:00
|
|
|
const messagesReactionsSchema = {
|
|
|
|
name: 'messagesReactions',
|
2018-10-15 20:22:42 +00:00
|
|
|
primaryKey: '_id',
|
2018-01-30 19:48:26 +00:00
|
|
|
properties: {
|
2018-10-15 20:22:42 +00:00
|
|
|
_id: 'string',
|
2018-01-30 19:48:26 +00:00
|
|
|
emoji: 'string',
|
2019-05-20 20:43:50 +00:00
|
|
|
usernames: 'string[]'
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
|
|
|
};
|
2017-12-02 13:19:58 +00:00
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
const messagesEditedBySchema = {
|
|
|
|
name: 'messagesEditedBy',
|
2018-03-02 15:11:34 +00:00
|
|
|
primaryKey: '_id',
|
2017-11-21 14:55:50 +00:00
|
|
|
properties: {
|
|
|
|
_id: { type: 'string', optional: true },
|
|
|
|
username: { type: 'string', optional: true }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-04 00:34:37 +00:00
|
|
|
const messagesSchema = {
|
|
|
|
name: 'messages',
|
|
|
|
primaryKey: '_id',
|
|
|
|
properties: {
|
|
|
|
_id: 'string',
|
2017-08-05 18:16:32 +00:00
|
|
|
msg: { type: 'string', optional: true },
|
2017-12-02 13:19:58 +00:00
|
|
|
t: { type: 'string', optional: true },
|
2018-03-02 15:11:34 +00:00
|
|
|
rid: { type: 'string', indexed: true },
|
2017-08-04 00:34:37 +00:00
|
|
|
ts: 'date',
|
|
|
|
u: 'users',
|
2017-08-13 23:45:47 +00:00
|
|
|
alias: { type: 'string', optional: true },
|
|
|
|
parseUrls: { type: 'bool', optional: true },
|
|
|
|
groupable: { type: 'bool', optional: true },
|
|
|
|
avatar: { type: 'string', optional: true },
|
2017-08-15 19:28:46 +00:00
|
|
|
attachments: { type: 'list', objectType: 'attachment' },
|
2018-02-16 18:34:25 +00:00
|
|
|
urls: { type: 'list', objectType: 'url', default: [] },
|
2017-08-09 20:08:50 +00:00
|
|
|
_updatedAt: { type: 'date', optional: true },
|
2017-12-13 15:00:26 +00:00
|
|
|
status: { type: 'int', optional: true },
|
2017-11-21 14:55:50 +00:00
|
|
|
pinned: { type: 'bool', optional: true },
|
|
|
|
starred: { type: 'bool', optional: true },
|
2018-01-30 19:48:26 +00:00
|
|
|
editedBy: 'messagesEditedBy',
|
2018-03-23 16:49:51 +00:00
|
|
|
reactions: { type: 'list', objectType: 'messagesReactions' },
|
2019-04-08 12:35:28 +00:00
|
|
|
role: { type: 'string', optional: true },
|
|
|
|
drid: { type: 'string', optional: true },
|
|
|
|
dcount: { type: 'int', optional: true },
|
2019-04-17 17:01:03 +00:00
|
|
|
dlm: { type: 'date', optional: true },
|
|
|
|
tmid: { type: 'string', optional: true },
|
|
|
|
tcount: { type: 'int', optional: true },
|
|
|
|
tlm: { type: 'date', optional: true },
|
2019-05-20 20:43:50 +00:00
|
|
|
replies: 'string[]',
|
|
|
|
mentions: { type: 'list', objectType: 'users' },
|
|
|
|
channels: { type: 'list', objectType: 'rooms' }
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const threadsSchema = {
|
|
|
|
name: 'threads',
|
|
|
|
primaryKey: '_id',
|
|
|
|
properties: {
|
|
|
|
_id: 'string',
|
|
|
|
msg: { type: 'string', optional: true },
|
|
|
|
t: { type: 'string', optional: true },
|
|
|
|
rid: { type: 'string', indexed: true },
|
|
|
|
ts: 'date',
|
|
|
|
u: 'users',
|
|
|
|
alias: { type: 'string', optional: true },
|
|
|
|
parseUrls: { type: 'bool', optional: true },
|
|
|
|
groupable: { type: 'bool', optional: true },
|
|
|
|
avatar: { type: 'string', optional: true },
|
|
|
|
attachments: { type: 'list', objectType: 'attachment' },
|
|
|
|
urls: { type: 'list', objectType: 'url', default: [] },
|
|
|
|
_updatedAt: { type: 'date', optional: true },
|
|
|
|
status: { type: 'int', optional: true },
|
|
|
|
pinned: { type: 'bool', optional: true },
|
|
|
|
starred: { type: 'bool', optional: true },
|
|
|
|
editedBy: 'messagesEditedBy',
|
|
|
|
reactions: { type: 'list', objectType: 'messagesReactions' },
|
|
|
|
role: { type: 'string', optional: true },
|
|
|
|
drid: { type: 'string', optional: true },
|
|
|
|
dcount: { type: 'int', optional: true },
|
|
|
|
dlm: { type: 'date', optional: true },
|
|
|
|
tmid: { type: 'string', optional: true },
|
|
|
|
tcount: { type: 'int', optional: true },
|
|
|
|
tlm: { type: 'date', optional: true },
|
2019-04-24 18:36:29 +00:00
|
|
|
replies: 'string[]',
|
|
|
|
draftMessage: 'string?'
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const threadMessagesSchema = {
|
|
|
|
name: 'threadMessages',
|
|
|
|
primaryKey: '_id',
|
|
|
|
properties: {
|
|
|
|
_id: 'string',
|
|
|
|
msg: { type: 'string', optional: true },
|
|
|
|
t: { type: 'string', optional: true },
|
|
|
|
rid: { type: 'string', indexed: true },
|
|
|
|
ts: 'date',
|
|
|
|
u: 'users',
|
|
|
|
alias: { type: 'string', optional: true },
|
|
|
|
parseUrls: { type: 'bool', optional: true },
|
|
|
|
groupable: { type: 'bool', optional: true },
|
|
|
|
avatar: { type: 'string', optional: true },
|
|
|
|
attachments: { type: 'list', objectType: 'attachment' },
|
|
|
|
urls: { type: 'list', objectType: 'url', default: [] },
|
|
|
|
_updatedAt: { type: 'date', optional: true },
|
|
|
|
status: { type: 'int', optional: true },
|
|
|
|
pinned: { type: 'bool', optional: true },
|
|
|
|
starred: { type: 'bool', optional: true },
|
|
|
|
editedBy: 'messagesEditedBy',
|
|
|
|
reactions: { type: 'list', objectType: 'messagesReactions' },
|
|
|
|
role: { type: 'string', optional: true }
|
2017-08-04 00:34:37 +00:00
|
|
|
}
|
|
|
|
};
|
2018-01-16 18:48:05 +00:00
|
|
|
|
|
|
|
const frequentlyUsedEmojiSchema = {
|
|
|
|
name: 'frequentlyUsedEmoji',
|
|
|
|
primaryKey: 'content',
|
|
|
|
properties: {
|
|
|
|
content: { type: 'string', optional: true },
|
|
|
|
extension: { type: 'string', optional: true },
|
|
|
|
isCustom: 'bool',
|
|
|
|
count: 'int'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const customEmojisSchema = {
|
|
|
|
name: 'customEmojis',
|
|
|
|
primaryKey: '_id',
|
|
|
|
properties: {
|
|
|
|
_id: 'string',
|
|
|
|
name: 'string',
|
2019-04-26 21:13:07 +00:00
|
|
|
aliases: 'string[]',
|
2018-01-16 18:48:05 +00:00
|
|
|
extension: 'string',
|
|
|
|
_updatedAt: { type: 'date', optional: true }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
const rolesSchema = {
|
|
|
|
name: 'roles',
|
|
|
|
primaryKey: '_id',
|
|
|
|
properties: {
|
|
|
|
_id: 'string',
|
|
|
|
description: { type: 'string', optional: true }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-17 19:10:27 +00:00
|
|
|
const uploadsSchema = {
|
|
|
|
name: 'uploads',
|
|
|
|
primaryKey: 'path',
|
|
|
|
properties: {
|
|
|
|
path: 'string',
|
|
|
|
rid: 'string',
|
|
|
|
name: { type: 'string', optional: true },
|
|
|
|
description: { type: 'string', optional: true },
|
|
|
|
size: { type: 'int', optional: true },
|
|
|
|
type: { type: 'string', optional: true },
|
|
|
|
store: { type: 'string', optional: true },
|
|
|
|
progress: { type: 'int', default: 1 },
|
|
|
|
error: { type: 'bool', default: false }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-08 12:35:28 +00:00
|
|
|
const usersTypingSchema = {
|
|
|
|
name: 'usersTyping',
|
|
|
|
properties: {
|
|
|
|
rid: { type: 'string', indexed: true },
|
|
|
|
username: { type: 'string', optional: true }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-29 16:03:52 +00:00
|
|
|
const activeUsersSchema = {
|
|
|
|
name: 'activeUsers',
|
|
|
|
primaryKey: 'id',
|
|
|
|
properties: {
|
|
|
|
id: 'string',
|
|
|
|
name: 'string?',
|
|
|
|
username: 'string?',
|
|
|
|
status: 'string?',
|
|
|
|
utcOffset: 'double?'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-27 15:22:06 +00:00
|
|
|
const schema = [
|
|
|
|
settingsSchema,
|
|
|
|
subscriptionSchema,
|
|
|
|
messagesSchema,
|
2019-04-17 17:01:03 +00:00
|
|
|
threadsSchema,
|
|
|
|
threadMessagesSchema,
|
2017-12-27 15:22:06 +00:00
|
|
|
usersSchema,
|
|
|
|
roomsSchema,
|
|
|
|
attachment,
|
|
|
|
attachmentFields,
|
|
|
|
messagesEditedBySchema,
|
|
|
|
permissionsSchema,
|
2018-01-16 18:48:05 +00:00
|
|
|
url,
|
|
|
|
frequentlyUsedEmojiSchema,
|
2018-01-30 19:48:26 +00:00
|
|
|
customEmojisSchema,
|
|
|
|
messagesReactionsSchema,
|
2018-04-24 19:34:03 +00:00
|
|
|
rolesSchema,
|
2018-07-17 19:10:27 +00:00
|
|
|
uploadsSchema
|
2017-12-27 15:22:06 +00:00
|
|
|
];
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-04-29 16:03:52 +00:00
|
|
|
const inMemorySchema = [usersTypingSchema, activeUsersSchema];
|
2019-04-08 12:35:28 +00:00
|
|
|
|
2017-12-27 15:22:06 +00:00
|
|
|
class DB {
|
|
|
|
databases = {
|
|
|
|
serversDB: new Realm({
|
|
|
|
path: 'default.realm',
|
|
|
|
schema: [
|
|
|
|
serversSchema
|
|
|
|
],
|
2019-05-20 20:43:50 +00:00
|
|
|
schemaVersion: 8,
|
2019-04-08 12:35:28 +00:00
|
|
|
migration: (oldRealm, newRealm) => {
|
2019-05-20 20:43:50 +00:00
|
|
|
if (oldRealm.schemaVersion >= 1 && newRealm.schemaVersion <= 8) {
|
2019-04-08 12:35:28 +00:00
|
|
|
const newServers = newRealm.objects('servers');
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-plusplus
|
|
|
|
for (let i = 0; i < newServers.length; i++) {
|
|
|
|
newServers[i].roomsUpdatedAt = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
inMemoryDB: new Realm({
|
|
|
|
path: 'memory.realm',
|
|
|
|
schema: inMemorySchema,
|
2019-04-29 16:03:52 +00:00
|
|
|
schemaVersion: 2,
|
2019-04-08 12:35:28 +00:00
|
|
|
inMemory: true
|
2017-12-27 15:22:06 +00:00
|
|
|
})
|
2018-09-25 19:28:42 +00:00
|
|
|
}
|
|
|
|
|
2017-12-27 15:22:06 +00:00
|
|
|
deleteAll(...args) {
|
|
|
|
return this.database.write(() => this.database.deleteAll(...args));
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-03-07 00:17:20 +00:00
|
|
|
delete(...args) {
|
|
|
|
return this.database.delete(...args);
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2017-12-27 15:22:06 +00:00
|
|
|
write(...args) {
|
|
|
|
return this.database.write(...args);
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2017-12-27 15:22:06 +00:00
|
|
|
create(...args) {
|
|
|
|
return this.database.create(...args);
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2017-12-27 15:22:06 +00:00
|
|
|
objects(...args) {
|
|
|
|
return this.database.objects(...args);
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
objectForPrimaryKey(...args) {
|
|
|
|
return this.database.objectForPrimaryKey(...args);
|
|
|
|
}
|
|
|
|
|
2017-12-27 15:22:06 +00:00
|
|
|
get database() {
|
|
|
|
return this.databases.activeDB;
|
|
|
|
}
|
|
|
|
|
2019-04-08 12:35:28 +00:00
|
|
|
get memoryDatabase() {
|
|
|
|
return this.databases.inMemoryDB;
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
setActiveDB(database = '') {
|
2017-12-27 15:22:06 +00:00
|
|
|
const path = database.replace(/(^\w+:|^)\/\//, '');
|
|
|
|
return this.databases.activeDB = new Realm({
|
|
|
|
path: `${ path }.realm`,
|
|
|
|
schema,
|
2019-05-20 20:43:50 +00:00
|
|
|
schemaVersion: 11,
|
2019-04-08 12:35:28 +00:00
|
|
|
migration: (oldRealm, newRealm) => {
|
2019-05-20 20:43:50 +00:00
|
|
|
if (oldRealm.schemaVersion >= 3 && newRealm.schemaVersion <= 11) {
|
2019-04-08 12:35:28 +00:00
|
|
|
const newSubs = newRealm.objects('subscriptions');
|
2019-05-20 20:43:50 +00:00
|
|
|
newRealm.delete(newSubs);
|
2019-04-08 12:35:28 +00:00
|
|
|
const newMessages = newRealm.objects('messages');
|
|
|
|
newRealm.delete(newMessages);
|
2019-04-24 18:36:29 +00:00
|
|
|
const newThreads = newRealm.objects('threads');
|
|
|
|
newRealm.delete(newThreads);
|
|
|
|
const newThreadMessages = newRealm.objects('threadMessages');
|
|
|
|
newRealm.delete(newThreadMessages);
|
2019-04-08 12:35:28 +00:00
|
|
|
}
|
2019-04-26 21:13:07 +00:00
|
|
|
if (newRealm.schemaVersion === 9) {
|
|
|
|
const newEmojis = newRealm.objects('customEmojis');
|
|
|
|
newRealm.delete(newEmojis);
|
|
|
|
const newSettings = newRealm.objects('settings');
|
|
|
|
newRealm.delete(newSettings);
|
|
|
|
}
|
2019-04-08 12:35:28 +00:00
|
|
|
}
|
2017-12-27 15:22:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 18:08:40 +00:00
|
|
|
const db = new DB();
|
|
|
|
export default db;
|
|
|
|
|
|
|
|
// Realm workaround for "Cannot create asynchronous query while in a write transaction"
|
|
|
|
// inpired from https://github.com/realm/realm-js/issues/1188#issuecomment-359223918
|
|
|
|
export function safeAddListener(results, callback, database = db) {
|
|
|
|
if (!results || !results.addListener) {
|
|
|
|
console.log('⚠️ safeAddListener called for non addListener-compliant object');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (database.isInTransaction) {
|
|
|
|
setTimeout(() => {
|
|
|
|
safeAddListener(results, callback);
|
|
|
|
}, 50);
|
|
|
|
} else {
|
|
|
|
results.addListener(callback);
|
|
|
|
}
|
|
|
|
}
|