diff --git a/src/appStart/StartHelper.tsx b/src/appStart/StartHelper.tsx index ce3ce5a..b0b33ee 100644 --- a/src/appStart/StartHelper.tsx +++ b/src/appStart/StartHelper.tsx @@ -22,6 +22,7 @@ import MyUserManager from '@caj/user/MyUserManager'; import DBSchemas from '@caj/helper/storage/bdm/schemas'; import {chatTags, convertDatabaseChat} from '@caj/components/chat/types'; import {addChatEntity} from '@caj/components/chat/initChatDatabase'; +import {initDatabase} from '@caj/helper/storage/bdm/init'; const AnimationView = animated(View); @@ -48,9 +49,22 @@ function onAppStart() { const chat = convertDatabaseChat(entries[i]); if (chat === undefined) continue; - console.log(chat); + await initDatabase(DBSchemas.chat, chat.roomId); addChatEntity(chat); + + if (chat.roomId === 'test') { + const chatDBKeys = BigDataManager.databases.chat.keys; + for (let i = 0; i < 10; i++) { + await BigDataManager.databases.chat.setEntry( + { + [chatDBKeys.UserId]: MyUserManager.getSelectedUserId(), + [chatDBKeys.msg]: 'heyho', + }, + chat.roomId, + ); + } + } } console.log('finish'); diff --git a/src/caj/components/ProfilePicture.tsx b/src/caj/components/ProfilePicture.tsx index 4a870ac..021cae9 100644 --- a/src/caj/components/ProfilePicture.tsx +++ b/src/caj/components/ProfilePicture.tsx @@ -9,6 +9,7 @@ export function ProfilePicture(props: {size: number; UserId: UserId}) { const UserId = props.UserId; const ProfilePicture = UserManager.getUserSelectorPicture(UserId); const accountName = UserManager.getUserSelectorAccountName(UserId).data; + if (accountName === undefined) { return ( diff --git a/src/caj/helper/storage/BigDataManager.ts b/src/caj/helper/storage/BigDataManager.ts index 459d5af..d41d805 100644 --- a/src/caj/helper/storage/BigDataManager.ts +++ b/src/caj/helper/storage/BigDataManager.ts @@ -1,11 +1,11 @@ -import {initDatabase} from './bdm/init'; +import {initDatabases} from './bdm/init'; import {setEntry} from './bdm/set'; import {getEntry} from './bdm/get'; import DBSchemas from './bdm/schemas'; import {databaseNames, possibleDBKeys} from './bdm/types'; const BigDataManager = { - initDatabase, + initDatabase: initDatabases, setEntry, getEntry, databases: DBSchemas, diff --git a/src/caj/helper/storage/bdm/get.ts b/src/caj/helper/storage/bdm/get.ts index b592a76..0b2e6ff 100644 --- a/src/caj/helper/storage/bdm/get.ts +++ b/src/caj/helper/storage/bdm/get.ts @@ -1,5 +1,5 @@ import {getDatabase} from './getDB'; -import {databaseConf, possibleDBKeys} from './types'; +import {databaseConf, mergeDBName, possibleDBKeys} from './types'; export interface filterParam { type: 'name'; @@ -8,9 +8,12 @@ export interface filterParam { export const getEntry = async , T>( schema: T2, key: possibleDBKeys, + suffix?: string, ): Promise => { - const dbName = schema.details.name; - const realm = await getDatabase(dbName); + const nameObj = {name: schema.details.name, suffix}; + const dbName = mergeDBName(nameObj); + + const realm = await getDatabase(nameObj); const val = realm.objectForPrimaryKey( dbName, @@ -23,9 +26,12 @@ export const getEntry = async , T>( export const getAllEntries = async , T>( schema: T2, filter?: filterParam, + suffix?: string, ): Promise => { - const dbName = schema.details.name; - const realm = await getDatabase(dbName); + const nameObj = {name: schema.details.name, suffix}; + const dbName = mergeDBName(nameObj); + + const realm = await getDatabase(nameObj); const val = realm.objects(dbName); diff --git a/src/caj/helper/storage/bdm/get.web.ts b/src/caj/helper/storage/bdm/get.web.ts index 4fff298..6f57d32 100644 --- a/src/caj/helper/storage/bdm/get.web.ts +++ b/src/caj/helper/storage/bdm/get.web.ts @@ -1,13 +1,16 @@ import {filterParam} from './get'; import {getDatabase} from './getDB.web'; -import {databaseConf, possibleDBKeys} from './types'; +import {databaseConf, mergeDBName, possibleDBKeys} from './types'; export const getEntry = async , T>( schema: T2, key: possibleDBKeys, + suffix?: string, ): Promise => { - const dbName = schema.details.name; - const db = await getDatabase(dbName); + const nameObj = {name: schema.details.name, suffix}; + const dbName = nameObj.name; + + const db = await getDatabase(nameObj); const tx = db.transaction(dbName, 'readonly'); const store = tx.objectStore(dbName); @@ -20,9 +23,14 @@ export const getEntry = async , T>( export const getAllEntries = async , T>( schema: T2, filter?: filterParam, + suffix?: string, ): Promise => { - const dbName = schema.details.name; - const db = await getDatabase(dbName); + const nameObj = {name: schema.details.name, suffix}; + const dbName = nameObj.name; + + console.log(dbName); + + const db = await getDatabase(nameObj); const tx = db.transaction(dbName, 'readonly'); const store = tx.objectStore(dbName); diff --git a/src/caj/helper/storage/bdm/getDB.ts b/src/caj/helper/storage/bdm/getDB.ts index ed3b0ac..84684c3 100644 --- a/src/caj/helper/storage/bdm/getDB.ts +++ b/src/caj/helper/storage/bdm/getDB.ts @@ -2,7 +2,12 @@ import {timestamp} from '@caj/configs/types'; import MyUserManager from '@caj/user/MyUserManager'; import Realm from 'realm'; import {getKey} from './encryption'; -import {databaseConf, databaseNames} from './types'; +import { + databaseConf, + databaseNames, + databaseNameSuffix, + mergeDBName, +} from './types'; import RNFS from 'react-native-fs'; import {databaseConfType} from './schemas'; @@ -12,7 +17,7 @@ const closeTimeout = 5; // in seconds // when DB read/writes is too long in idle type DBType = Realm; export interface DBObject { - name: databaseNames; + name: string; schema: databaseConfType; db: DBType | undefined; lastUsedTimestamp?: timestamp; // when timeout is undefined then db is closed @@ -55,9 +60,14 @@ export function closeAllDatabases() { export async function openMyDatabase( schema: databaseConfType, + nameObj: databaseNameSuffix, ): Promise { const folderPath = MyUserManager.getSelectedUserId(); - const path = folderPath + '/' + schema.filePath; + const path = + folderPath + + '/' + + schema.filePath + + (nameObj.suffix !== undefined ? '_' + nameObj.suffix : ''); const folderExists = await RNFS.exists( RNFS.DocumentDirectoryPath + '/' + folderPath, @@ -76,17 +86,19 @@ export async function openMyDatabase( } export async function getDatabase( - name: databaseNames, + _name: databaseNameSuffix, init?: boolean, ): Promise { + const name = mergeDBName(_name); let dbObj = databases[name]; + if (dbObj !== undefined) { if (dbObj.lastUsedTimestamp !== undefined && dbObj.db !== undefined) { dbObj.lastUsedTimestamp = getTime(); return dbObj.db; } else { dbObj.lastUsedTimestamp = undefined; - const db = await openMyDatabase(dbObj.schema); + const db = await openMyDatabase(dbObj.schema, _name); dbObj.lastUsedTimestamp = getTime(); dbObj.db = db; return db; diff --git a/src/caj/helper/storage/bdm/getDB.web.ts b/src/caj/helper/storage/bdm/getDB.web.ts index 8337d93..5ef7a08 100644 --- a/src/caj/helper/storage/bdm/getDB.web.ts +++ b/src/caj/helper/storage/bdm/getDB.web.ts @@ -2,7 +2,12 @@ import {timestamp} from '@caj/configs/types'; import MyUserManager from '@caj/user/MyUserManager'; import {IDBPDatabase, openDB} from 'idb'; import {databaseConfType} from './schemas'; -import {databaseConf, databaseNames} from './types'; +import { + databaseConf, + databaseNames, + databaseNameSuffix, + mergeDBName, +} from './types'; const closeTimeout = 5; // in seconds // when DB read/writes is too long in idle it will be closed @@ -52,15 +57,11 @@ export function closeAllDatabases() { export async function openMyDatabase( schema: databaseConfType, - init?: boolean, + nameObj: databaseNameSuffix, ): Promise { - const db = await openDB( - schema.details.name + '-' + MyUserManager.getSelectedUserId(), - schema.version, - { - upgrade: schema.migration(), - }, - ); + const db = await openDB(mergeDBName(nameObj, 'web'), schema.version, { + upgrade: schema.migration(), + }); /*if (init === true) { const UserId = MyUserManager.getSelectedUserId(); @@ -73,10 +74,12 @@ export async function openMyDatabase( } export async function getDatabase( - name: databaseNames, + _name: databaseNameSuffix, init?: boolean, ): Promise { + const name = mergeDBName(_name); let dbObj = databases[name]; + if (dbObj !== undefined) { if (dbObj.lastUsedTimestamp !== undefined && dbObj.db !== undefined) { dbObj.lastUsedTimestamp = getTime(); @@ -85,7 +88,7 @@ export async function getDatabase( dbObj.db.close(); dbObj.lastUsedTimestamp = undefined; - const db = await openMyDatabase(dbObj.schema, init); + const db = await openMyDatabase(dbObj.schema /*, init*/, _name); dbObj.lastUsedTimestamp = getTime(); dbObj.db = db; @@ -96,7 +99,7 @@ export async function getDatabase( } } else { dbObj.lastUsedTimestamp = undefined; - const db = await openMyDatabase(dbObj.schema, init); + const db = await openMyDatabase(dbObj.schema /*, init*/, _name); dbObj.lastUsedTimestamp = getTime(); dbObj.db = db; return db; diff --git a/src/caj/helper/storage/bdm/init.ts b/src/caj/helper/storage/bdm/init.ts index 0c2f3d3..4b1a6b4 100644 --- a/src/caj/helper/storage/bdm/init.ts +++ b/src/caj/helper/storage/bdm/init.ts @@ -1,25 +1,42 @@ import {openDB, deleteDB, wrap, unwrap} from 'idb'; import {closeAllDatabases, databases, DBObject, getDatabase} from './getDB'; -import DBSchemas from './schemas'; -import {databaseConf} from './types'; +import DBSchemas, {SkipDBSchemas} from './schemas'; +import {databaseConf, databaseNameSuffix, mergeDBName} from './types'; -export const initDatabase = (): Promise => { +export const initDatabases = (): Promise => { return new Promise(async (resolve, reject) => { closeAllDatabases(); for (const key in DBSchemas) { const schema = DBSchemas[key as keyof typeof DBSchemas]; + const name = schema.details.name; - let dbObj: DBObject = { - schema: schema, - name: schema.details.name, - db: undefined, - }; + if (SkipDBSchemas.includes(name)) continue; - databases[dbObj.name] = dbObj; - - await getDatabase(dbObj.name, true); // init Database + await initDatabase(schema); } resolve(); }); }; + +export const initDatabase = async ( + schema: typeof DBSchemas[keyof typeof DBSchemas], + fileSuffix?: string, +): Promise => { + const _nameObj: databaseNameSuffix = { + name: schema.details.name, + suffix: fileSuffix, + }; + const name = mergeDBName(_nameObj); + + let dbObj: DBObject = { + schema: schema, + name, + db: undefined, + }; + + databases[name] = dbObj; + + await getDatabase(_nameObj, true); // init Database + return name; +}; diff --git a/src/caj/helper/storage/bdm/schemas.ts b/src/caj/helper/storage/bdm/schemas.ts index f8a1bc1..d836b2d 100644 --- a/src/caj/helper/storage/bdm/schemas.ts +++ b/src/caj/helper/storage/bdm/schemas.ts @@ -3,5 +3,7 @@ import users from './schemas/users'; import chatRoomInfos from './schemas/chatRoomInfos'; const DBSchemas = {users, chat, chatRoomInfos}; -export default DBSchemas; +export const SkipDBSchemas = [chat.details.name]; + export type databaseConfType = typeof DBSchemas[keyof typeof DBSchemas]; +export default DBSchemas; diff --git a/src/caj/helper/storage/bdm/schemas/chat.ts b/src/caj/helper/storage/bdm/schemas/chat.ts index cc855ca..0d2161a 100644 --- a/src/caj/helper/storage/bdm/schemas/chat.ts +++ b/src/caj/helper/storage/bdm/schemas/chat.ts @@ -29,22 +29,25 @@ const thisSchema: databaseConf = { migration: () => { return DBMigration[name](thisSchema); }, - setEntry: (val: typeof thisSchema.defaultProps) => { + setEntry: (val: typeof thisSchema.defaultProps, suffix?: string) => { return setEntry( thisSchema, val, + suffix, ); }, - getEntry: (key: possibleDBKeys) => { + getEntry: (key: possibleDBKeys, suffix?: string) => { return getEntry( thisSchema, key, + suffix, ); }, - getAllEntries: (filter?: filterParam) => { + getAllEntries: (filter?: filterParam, suffix?: string) => { return getAllEntries( thisSchema, filter, + suffix, ); }, defaultProps: propsDefault, diff --git a/src/caj/helper/storage/bdm/schemas/chatRoomInfos.ts b/src/caj/helper/storage/bdm/schemas/chatRoomInfos.ts index f45f912..b7b2b19 100644 --- a/src/caj/helper/storage/bdm/schemas/chatRoomInfos.ts +++ b/src/caj/helper/storage/bdm/schemas/chatRoomInfos.ts @@ -41,22 +41,25 @@ const thisSchema: databaseConf = { migration: () => { return DBMigration[name](thisSchema); }, - setEntry: (val: typeof thisSchema.defaultProps) => { + setEntry: (val: typeof thisSchema.defaultProps, suffix?: string) => { return setEntry( thisSchema, val, + suffix, ); }, - getEntry: (key: possibleDBKeys) => { + getEntry: (key: possibleDBKeys, suffix?: string) => { return getEntry( thisSchema, key, + suffix, ); }, - getAllEntries: (filter?: filterParam) => { + getAllEntries: (filter?: filterParam, suffix?: string) => { return getAllEntries( thisSchema, filter, + suffix, ); }, defaultProps: propsDefault, diff --git a/src/caj/helper/storage/bdm/schemas/users.ts b/src/caj/helper/storage/bdm/schemas/users.ts index a81f708..e1a49a0 100644 --- a/src/caj/helper/storage/bdm/schemas/users.ts +++ b/src/caj/helper/storage/bdm/schemas/users.ts @@ -61,22 +61,25 @@ const thisSchema: databaseConf = { migration: () => { return DBMigration[name](thisSchema); }, - setEntry: (val: typeof thisSchema.defaultProps) => { + setEntry: (val: typeof thisSchema.defaultProps, suffix?: string) => { return setEntry( thisSchema, val, + suffix, ); }, - getEntry: (key: possibleDBKeys) => { + getEntry: (key: possibleDBKeys, suffix?: string) => { return getEntry( thisSchema, key, + suffix, ); }, - getAllEntries: (filter?: filterParam) => { + getAllEntries: (filter?: filterParam, suffix?: string) => { return getAllEntries( thisSchema, filter, + suffix, ); }, defaultProps: propsDefault, diff --git a/src/caj/helper/storage/bdm/set.ts b/src/caj/helper/storage/bdm/set.ts index f1ba8dd..b9e0319 100644 --- a/src/caj/helper/storage/bdm/set.ts +++ b/src/caj/helper/storage/bdm/set.ts @@ -1,12 +1,15 @@ import {getDatabase} from './getDB'; -import {databaseConf} from './types'; +import {databaseConf, mergeDBName} from './types'; export const setEntry = async , T>( schema: T2, value: T, + suffix?: string, ) => { - const dbName = schema.details.name; - const realm = await getDatabase(dbName); + const nameObj = {name: schema.details.name, suffix}; + const dbName = mergeDBName(nameObj); + + const realm = await getDatabase(nameObj); realm.write(() => { realm.create(dbName, value as any, Realm.UpdateMode.Modified); diff --git a/src/caj/helper/storage/bdm/set.web.ts b/src/caj/helper/storage/bdm/set.web.ts index b0c9373..cdce4eb 100644 --- a/src/caj/helper/storage/bdm/set.web.ts +++ b/src/caj/helper/storage/bdm/set.web.ts @@ -1,12 +1,15 @@ import {getDatabase} from './getDB.web'; -import {databaseConf} from './types'; +import {databaseConf, mergeDBName} from './types'; export const setEntry = async , T>( schema: T2, value: T, + suffix?: string, ) => { - const dbName = schema.details.name; - const db = await getDatabase(dbName); + const nameObj = {name: schema.details.name, suffix}; + const dbName = nameObj.name; + + const db = await getDatabase(nameObj); const tx = db.transaction(dbName, 'readwrite'); const store = tx.objectStore(dbName); diff --git a/src/caj/helper/storage/bdm/types.ts b/src/caj/helper/storage/bdm/types.ts index 7a0eb49..aff0136 100644 --- a/src/caj/helper/storage/bdm/types.ts +++ b/src/caj/helper/storage/bdm/types.ts @@ -1,3 +1,4 @@ +import MyUserManager from '@caj/user/MyUserManager'; import {filterParam} from './get'; export type databaseNames = 'users' | 'chat' | 'chatRoomInfos'; @@ -8,9 +9,12 @@ export interface databaseConf { version: number; migration?: any; keys: enums; - getEntry: (key: possibleDBKeys) => Promise; - getAllEntries: (filter?: filterParam) => Promise; - setEntry: (val: props) => Promise; + getEntry: (key: possibleDBKeys, suffix?: string) => Promise; + getAllEntries: ( + filter?: filterParam, + suffix?: string, + ) => Promise; + setEntry: (val: props, suffix?: string) => Promise; defaultProps: props; details: { name: databaseNames; @@ -18,3 +22,23 @@ export interface databaseConf { primaryKey: keyof props; }; } + +export interface databaseNameSuffix { + name: databaseNames; + suffix?: string; +} + +export function mergeDBName(nameObj: databaseNameSuffix, web?: 'web'): string { + if (web === 'web') { + return nameObj.suffix === undefined + ? nameObj.name + '-' + MyUserManager.getSelectedUserId() + : nameObj.name + + '-' + + MyUserManager.getSelectedUserId() + + ('_' + nameObj.suffix); + } + + return nameObj.suffix === undefined + ? nameObj.name + : nameObj.name + nameObj.suffix; +} diff --git a/src/caj/user/UserManager.ts b/src/caj/user/UserManager.ts index 6137442..2bd3fe5 100644 --- a/src/caj/user/UserManager.ts +++ b/src/caj/user/UserManager.ts @@ -200,7 +200,7 @@ async function refreshUsers() { } } -setInterval(refreshUsers, 1300); +setInterval(refreshUsers, 500); function addUserToGetQueue(UserId: UserId, param: GetParam) { if (getUserList[UserId] === undefined) {