alpha
Jan Umbach 2023-03-13 23:41:13 +01:00
parent 3696246455
commit 17399c518c
16 changed files with 163 additions and 61 deletions

View File

@ -22,6 +22,7 @@ import MyUserManager from '@caj/user/MyUserManager';
import DBSchemas from '@caj/helper/storage/bdm/schemas'; import DBSchemas from '@caj/helper/storage/bdm/schemas';
import {chatTags, convertDatabaseChat} from '@caj/components/chat/types'; import {chatTags, convertDatabaseChat} from '@caj/components/chat/types';
import {addChatEntity} from '@caj/components/chat/initChatDatabase'; import {addChatEntity} from '@caj/components/chat/initChatDatabase';
import {initDatabase} from '@caj/helper/storage/bdm/init';
const AnimationView = animated(View); const AnimationView = animated(View);
@ -48,9 +49,22 @@ function onAppStart() {
const chat = convertDatabaseChat(entries[i]); const chat = convertDatabaseChat(entries[i]);
if (chat === undefined) continue; if (chat === undefined) continue;
console.log(chat); await initDatabase(DBSchemas.chat, chat.roomId);
addChatEntity(chat); 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'); console.log('finish');

View File

@ -10,6 +10,7 @@ export function ProfilePicture(props: {size: number; UserId: UserId}) {
const ProfilePicture = UserManager.getUserSelectorPicture(UserId); const ProfilePicture = UserManager.getUserSelectorPicture(UserId);
const accountName = UserManager.getUserSelectorAccountName(UserId).data; const accountName = UserManager.getUserSelectorAccountName(UserId).data;
if (accountName === undefined) { if (accountName === undefined) {
return ( return (
<Skeleton <Skeleton

View File

@ -1,11 +1,11 @@
import {initDatabase} from './bdm/init'; import {initDatabases} from './bdm/init';
import {setEntry} from './bdm/set'; import {setEntry} from './bdm/set';
import {getEntry} from './bdm/get'; import {getEntry} from './bdm/get';
import DBSchemas from './bdm/schemas'; import DBSchemas from './bdm/schemas';
import {databaseNames, possibleDBKeys} from './bdm/types'; import {databaseNames, possibleDBKeys} from './bdm/types';
const BigDataManager = { const BigDataManager = {
initDatabase, initDatabase: initDatabases,
setEntry, setEntry,
getEntry, getEntry,
databases: DBSchemas, databases: DBSchemas,

View File

@ -1,5 +1,5 @@
import {getDatabase} from './getDB'; import {getDatabase} from './getDB';
import {databaseConf, possibleDBKeys} from './types'; import {databaseConf, mergeDBName, possibleDBKeys} from './types';
export interface filterParam { export interface filterParam {
type: 'name'; type: 'name';
@ -8,9 +8,12 @@ export interface filterParam {
export const getEntry = async <T2 extends databaseConf<T, any>, T>( export const getEntry = async <T2 extends databaseConf<T, any>, T>(
schema: T2, schema: T2,
key: possibleDBKeys, key: possibleDBKeys,
suffix?: string,
): Promise<null | T> => { ): Promise<null | T> => {
const dbName = schema.details.name; const nameObj = {name: schema.details.name, suffix};
const realm = await getDatabase(dbName); const dbName = mergeDBName(nameObj);
const realm = await getDatabase(nameObj);
const val = realm.objectForPrimaryKey<typeof schema.details.properties>( const val = realm.objectForPrimaryKey<typeof schema.details.properties>(
dbName, dbName,
@ -23,9 +26,12 @@ export const getEntry = async <T2 extends databaseConf<T, any>, T>(
export const getAllEntries = async <T2 extends databaseConf<T, any>, T>( export const getAllEntries = async <T2 extends databaseConf<T, any>, T>(
schema: T2, schema: T2,
filter?: filterParam, filter?: filterParam,
suffix?: string,
): Promise<null | T[]> => { ): Promise<null | T[]> => {
const dbName = schema.details.name; const nameObj = {name: schema.details.name, suffix};
const realm = await getDatabase(dbName); const dbName = mergeDBName(nameObj);
const realm = await getDatabase(nameObj);
const val = realm.objects<typeof schema.details.properties>(dbName); const val = realm.objects<typeof schema.details.properties>(dbName);

View File

@ -1,13 +1,16 @@
import {filterParam} from './get'; import {filterParam} from './get';
import {getDatabase} from './getDB.web'; import {getDatabase} from './getDB.web';
import {databaseConf, possibleDBKeys} from './types'; import {databaseConf, mergeDBName, possibleDBKeys} from './types';
export const getEntry = async <T2 extends databaseConf<T, any>, T>( export const getEntry = async <T2 extends databaseConf<T, any>, T>(
schema: T2, schema: T2,
key: possibleDBKeys, key: possibleDBKeys,
suffix?: string,
): Promise<null | T> => { ): Promise<null | T> => {
const dbName = schema.details.name; const nameObj = {name: schema.details.name, suffix};
const db = await getDatabase(dbName); const dbName = nameObj.name;
const db = await getDatabase(nameObj);
const tx = db.transaction(dbName, 'readonly'); const tx = db.transaction(dbName, 'readonly');
const store = tx.objectStore(dbName); const store = tx.objectStore(dbName);
@ -20,9 +23,14 @@ export const getEntry = async <T2 extends databaseConf<T, any>, T>(
export const getAllEntries = async <T2 extends databaseConf<T, any>, T>( export const getAllEntries = async <T2 extends databaseConf<T, any>, T>(
schema: T2, schema: T2,
filter?: filterParam, filter?: filterParam,
suffix?: string,
): Promise<null | T[]> => { ): Promise<null | T[]> => {
const dbName = schema.details.name; const nameObj = {name: schema.details.name, suffix};
const db = await getDatabase(dbName); const dbName = nameObj.name;
console.log(dbName);
const db = await getDatabase(nameObj);
const tx = db.transaction(dbName, 'readonly'); const tx = db.transaction(dbName, 'readonly');
const store = tx.objectStore(dbName); const store = tx.objectStore(dbName);

View File

@ -2,7 +2,12 @@ import {timestamp} from '@caj/configs/types';
import MyUserManager from '@caj/user/MyUserManager'; import MyUserManager from '@caj/user/MyUserManager';
import Realm from 'realm'; import Realm from 'realm';
import {getKey} from './encryption'; import {getKey} from './encryption';
import {databaseConf, databaseNames} from './types'; import {
databaseConf,
databaseNames,
databaseNameSuffix,
mergeDBName,
} from './types';
import RNFS from 'react-native-fs'; import RNFS from 'react-native-fs';
import {databaseConfType} from './schemas'; 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; type DBType = Realm;
export interface DBObject { export interface DBObject {
name: databaseNames; name: string;
schema: databaseConfType; schema: databaseConfType;
db: DBType | undefined; db: DBType | undefined;
lastUsedTimestamp?: timestamp; // when timeout is undefined then db is closed lastUsedTimestamp?: timestamp; // when timeout is undefined then db is closed
@ -55,9 +60,14 @@ export function closeAllDatabases() {
export async function openMyDatabase( export async function openMyDatabase(
schema: databaseConfType, schema: databaseConfType,
nameObj: databaseNameSuffix,
): Promise<DBType> { ): Promise<DBType> {
const folderPath = MyUserManager.getSelectedUserId(); const folderPath = MyUserManager.getSelectedUserId();
const path = folderPath + '/' + schema.filePath; const path =
folderPath +
'/' +
schema.filePath +
(nameObj.suffix !== undefined ? '_' + nameObj.suffix : '');
const folderExists = await RNFS.exists( const folderExists = await RNFS.exists(
RNFS.DocumentDirectoryPath + '/' + folderPath, RNFS.DocumentDirectoryPath + '/' + folderPath,
@ -76,17 +86,19 @@ export async function openMyDatabase(
} }
export async function getDatabase( export async function getDatabase(
name: databaseNames, _name: databaseNameSuffix,
init?: boolean, init?: boolean,
): Promise<DBType> { ): Promise<DBType> {
const name = mergeDBName(_name);
let dbObj = databases[name]; let dbObj = databases[name];
if (dbObj !== undefined) { if (dbObj !== undefined) {
if (dbObj.lastUsedTimestamp !== undefined && dbObj.db !== undefined) { if (dbObj.lastUsedTimestamp !== undefined && dbObj.db !== undefined) {
dbObj.lastUsedTimestamp = getTime(); dbObj.lastUsedTimestamp = getTime();
return dbObj.db; return dbObj.db;
} else { } else {
dbObj.lastUsedTimestamp = undefined; dbObj.lastUsedTimestamp = undefined;
const db = await openMyDatabase(dbObj.schema); const db = await openMyDatabase(dbObj.schema, _name);
dbObj.lastUsedTimestamp = getTime(); dbObj.lastUsedTimestamp = getTime();
dbObj.db = db; dbObj.db = db;
return db; return db;

View File

@ -2,7 +2,12 @@ import {timestamp} from '@caj/configs/types';
import MyUserManager from '@caj/user/MyUserManager'; import MyUserManager from '@caj/user/MyUserManager';
import {IDBPDatabase, openDB} from 'idb'; import {IDBPDatabase, openDB} from 'idb';
import {databaseConfType} from './schemas'; 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 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( export async function openMyDatabase(
schema: databaseConfType, schema: databaseConfType,
init?: boolean, nameObj: databaseNameSuffix,
): Promise<DBType> { ): Promise<DBType> {
const db = await openDB( const db = await openDB(mergeDBName(nameObj, 'web'), schema.version, {
schema.details.name + '-' + MyUserManager.getSelectedUserId(),
schema.version,
{
upgrade: schema.migration(), upgrade: schema.migration(),
}, });
);
/*if (init === true) { /*if (init === true) {
const UserId = MyUserManager.getSelectedUserId(); const UserId = MyUserManager.getSelectedUserId();
@ -73,10 +74,12 @@ export async function openMyDatabase(
} }
export async function getDatabase( export async function getDatabase(
name: databaseNames, _name: databaseNameSuffix,
init?: boolean, init?: boolean,
): Promise<DBType> { ): Promise<DBType> {
const name = mergeDBName(_name);
let dbObj = databases[name]; let dbObj = databases[name];
if (dbObj !== undefined) { if (dbObj !== undefined) {
if (dbObj.lastUsedTimestamp !== undefined && dbObj.db !== undefined) { if (dbObj.lastUsedTimestamp !== undefined && dbObj.db !== undefined) {
dbObj.lastUsedTimestamp = getTime(); dbObj.lastUsedTimestamp = getTime();
@ -85,7 +88,7 @@ export async function getDatabase(
dbObj.db.close(); dbObj.db.close();
dbObj.lastUsedTimestamp = undefined; dbObj.lastUsedTimestamp = undefined;
const db = await openMyDatabase(dbObj.schema, init); const db = await openMyDatabase(dbObj.schema /*, init*/, _name);
dbObj.lastUsedTimestamp = getTime(); dbObj.lastUsedTimestamp = getTime();
dbObj.db = db; dbObj.db = db;
@ -96,7 +99,7 @@ export async function getDatabase(
} }
} else { } else {
dbObj.lastUsedTimestamp = undefined; dbObj.lastUsedTimestamp = undefined;
const db = await openMyDatabase(dbObj.schema, init); const db = await openMyDatabase(dbObj.schema /*, init*/, _name);
dbObj.lastUsedTimestamp = getTime(); dbObj.lastUsedTimestamp = getTime();
dbObj.db = db; dbObj.db = db;
return db; return db;

View File

@ -1,25 +1,42 @@
import {openDB, deleteDB, wrap, unwrap} from 'idb'; import {openDB, deleteDB, wrap, unwrap} from 'idb';
import {closeAllDatabases, databases, DBObject, getDatabase} from './getDB'; import {closeAllDatabases, databases, DBObject, getDatabase} from './getDB';
import DBSchemas from './schemas'; import DBSchemas, {SkipDBSchemas} from './schemas';
import {databaseConf} from './types'; import {databaseConf, databaseNameSuffix, mergeDBName} from './types';
export const initDatabase = (): Promise<void> => { export const initDatabases = (): Promise<void> => {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
closeAllDatabases(); closeAllDatabases();
for (const key in DBSchemas) { for (const key in DBSchemas) {
const schema = DBSchemas[key as keyof typeof DBSchemas]; const schema = DBSchemas[key as keyof typeof DBSchemas];
const name = schema.details.name;
let dbObj: DBObject = { if (SkipDBSchemas.includes(name)) continue;
schema: schema,
name: schema.details.name,
db: undefined,
};
databases[dbObj.name] = dbObj; await initDatabase(schema);
await getDatabase(dbObj.name, true); // init Database
} }
resolve(); resolve();
}); });
}; };
export const initDatabase = async (
schema: typeof DBSchemas[keyof typeof DBSchemas],
fileSuffix?: string,
): Promise<string> => {
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;
};

View File

@ -3,5 +3,7 @@ import users from './schemas/users';
import chatRoomInfos from './schemas/chatRoomInfos'; import chatRoomInfos from './schemas/chatRoomInfos';
const DBSchemas = {users, chat, chatRoomInfos}; const DBSchemas = {users, chat, chatRoomInfos};
export default DBSchemas; export const SkipDBSchemas = [chat.details.name];
export type databaseConfType = typeof DBSchemas[keyof typeof DBSchemas]; export type databaseConfType = typeof DBSchemas[keyof typeof DBSchemas];
export default DBSchemas;

View File

@ -29,22 +29,25 @@ const thisSchema: databaseConf<typeof propsDefault, typeof keys> = {
migration: () => { migration: () => {
return DBMigration[name](thisSchema); return DBMigration[name](thisSchema);
}, },
setEntry: (val: typeof thisSchema.defaultProps) => { setEntry: (val: typeof thisSchema.defaultProps, suffix?: string) => {
return setEntry<typeof thisSchema, typeof thisSchema.defaultProps>( return setEntry<typeof thisSchema, typeof thisSchema.defaultProps>(
thisSchema, thisSchema,
val, val,
suffix,
); );
}, },
getEntry: (key: possibleDBKeys) => { getEntry: (key: possibleDBKeys, suffix?: string) => {
return getEntry<typeof thisSchema, typeof thisSchema.defaultProps>( return getEntry<typeof thisSchema, typeof thisSchema.defaultProps>(
thisSchema, thisSchema,
key, key,
suffix,
); );
}, },
getAllEntries: (filter?: filterParam) => { getAllEntries: (filter?: filterParam, suffix?: string) => {
return getAllEntries<typeof thisSchema, typeof thisSchema.defaultProps>( return getAllEntries<typeof thisSchema, typeof thisSchema.defaultProps>(
thisSchema, thisSchema,
filter, filter,
suffix,
); );
}, },
defaultProps: propsDefault, defaultProps: propsDefault,

View File

@ -41,22 +41,25 @@ const thisSchema: databaseConf<typeof propsDefault, typeof keys> = {
migration: () => { migration: () => {
return DBMigration[name](thisSchema); return DBMigration[name](thisSchema);
}, },
setEntry: (val: typeof thisSchema.defaultProps) => { setEntry: (val: typeof thisSchema.defaultProps, suffix?: string) => {
return setEntry<typeof thisSchema, typeof thisSchema.defaultProps>( return setEntry<typeof thisSchema, typeof thisSchema.defaultProps>(
thisSchema, thisSchema,
val, val,
suffix,
); );
}, },
getEntry: (key: possibleDBKeys) => { getEntry: (key: possibleDBKeys, suffix?: string) => {
return getEntry<typeof thisSchema, typeof thisSchema.defaultProps>( return getEntry<typeof thisSchema, typeof thisSchema.defaultProps>(
thisSchema, thisSchema,
key, key,
suffix,
); );
}, },
getAllEntries: (filter?: filterParam) => { getAllEntries: (filter?: filterParam, suffix?: string) => {
return getAllEntries<typeof thisSchema, typeof thisSchema.defaultProps>( return getAllEntries<typeof thisSchema, typeof thisSchema.defaultProps>(
thisSchema, thisSchema,
filter, filter,
suffix,
); );
}, },
defaultProps: propsDefault, defaultProps: propsDefault,

View File

@ -61,22 +61,25 @@ const thisSchema: databaseConf<typeof propsDefault, typeof keys> = {
migration: () => { migration: () => {
return DBMigration[name](thisSchema); return DBMigration[name](thisSchema);
}, },
setEntry: (val: typeof thisSchema.defaultProps) => { setEntry: (val: typeof thisSchema.defaultProps, suffix?: string) => {
return setEntry<typeof thisSchema, typeof thisSchema.defaultProps>( return setEntry<typeof thisSchema, typeof thisSchema.defaultProps>(
thisSchema, thisSchema,
val, val,
suffix,
); );
}, },
getEntry: (key: possibleDBKeys) => { getEntry: (key: possibleDBKeys, suffix?: string) => {
return getEntry<typeof thisSchema, typeof thisSchema.defaultProps>( return getEntry<typeof thisSchema, typeof thisSchema.defaultProps>(
thisSchema, thisSchema,
key, key,
suffix,
); );
}, },
getAllEntries: (filter?: filterParam) => { getAllEntries: (filter?: filterParam, suffix?: string) => {
return getAllEntries<typeof thisSchema, typeof thisSchema.defaultProps>( return getAllEntries<typeof thisSchema, typeof thisSchema.defaultProps>(
thisSchema, thisSchema,
filter, filter,
suffix,
); );
}, },
defaultProps: propsDefault, defaultProps: propsDefault,

View File

@ -1,12 +1,15 @@
import {getDatabase} from './getDB'; import {getDatabase} from './getDB';
import {databaseConf} from './types'; import {databaseConf, mergeDBName} from './types';
export const setEntry = async <T2 extends databaseConf<T, any>, T>( export const setEntry = async <T2 extends databaseConf<T, any>, T>(
schema: T2, schema: T2,
value: T, value: T,
suffix?: string,
) => { ) => {
const dbName = schema.details.name; const nameObj = {name: schema.details.name, suffix};
const realm = await getDatabase(dbName); const dbName = mergeDBName(nameObj);
const realm = await getDatabase(nameObj);
realm.write(() => { realm.write(() => {
realm.create(dbName, value as any, Realm.UpdateMode.Modified); realm.create(dbName, value as any, Realm.UpdateMode.Modified);

View File

@ -1,12 +1,15 @@
import {getDatabase} from './getDB.web'; import {getDatabase} from './getDB.web';
import {databaseConf} from './types'; import {databaseConf, mergeDBName} from './types';
export const setEntry = async <T2 extends databaseConf<T, any>, T>( export const setEntry = async <T2 extends databaseConf<T, any>, T>(
schema: T2, schema: T2,
value: T, value: T,
suffix?: string,
) => { ) => {
const dbName = schema.details.name; const nameObj = {name: schema.details.name, suffix};
const db = await getDatabase(dbName); const dbName = nameObj.name;
const db = await getDatabase(nameObj);
const tx = db.transaction(dbName, 'readwrite'); const tx = db.transaction(dbName, 'readwrite');
const store = tx.objectStore(dbName); const store = tx.objectStore(dbName);

View File

@ -1,3 +1,4 @@
import MyUserManager from '@caj/user/MyUserManager';
import {filterParam} from './get'; import {filterParam} from './get';
export type databaseNames = 'users' | 'chat' | 'chatRoomInfos'; export type databaseNames = 'users' | 'chat' | 'chatRoomInfos';
@ -8,9 +9,12 @@ export interface databaseConf<props, enums> {
version: number; version: number;
migration?: any; migration?: any;
keys: enums; keys: enums;
getEntry: (key: possibleDBKeys) => Promise<props | null>; getEntry: (key: possibleDBKeys, suffix?: string) => Promise<props | null>;
getAllEntries: (filter?: filterParam) => Promise<props[] | null>; getAllEntries: (
setEntry: (val: props) => Promise<void>; filter?: filterParam,
suffix?: string,
) => Promise<props[] | null>;
setEntry: (val: props, suffix?: string) => Promise<void>;
defaultProps: props; defaultProps: props;
details: { details: {
name: databaseNames; name: databaseNames;
@ -18,3 +22,23 @@ export interface databaseConf<props, enums> {
primaryKey: keyof props; 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;
}

View File

@ -200,7 +200,7 @@ async function refreshUsers() {
} }
} }
setInterval(refreshUsers, 1300); setInterval(refreshUsers, 500);
function addUserToGetQueue(UserId: UserId, param: GetParam) { function addUserToGetQueue(UserId: UserId, param: GetParam) {
if (getUserList[UserId] === undefined) { if (getUserList[UserId] === undefined) {