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 {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');

View File

@ -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 (

View File

@ -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,

View File

@ -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 <T2 extends databaseConf<T, any>, T>(
schema: T2,
key: possibleDBKeys,
suffix?: string,
): Promise<null | T> => {
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<typeof schema.details.properties>(
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>(
schema: T2,
filter?: filterParam,
suffix?: string,
): Promise<null | T[]> => {
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<typeof schema.details.properties>(dbName);

View File

@ -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 <T2 extends databaseConf<T, any>, T>(
schema: T2,
key: possibleDBKeys,
suffix?: string,
): Promise<null | T> => {
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 <T2 extends databaseConf<T, any>, T>(
export const getAllEntries = async <T2 extends databaseConf<T, any>, T>(
schema: T2,
filter?: filterParam,
suffix?: string,
): Promise<null | T[]> => {
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);

View File

@ -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<DBType> {
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<DBType> {
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;

View File

@ -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<DBType> {
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<DBType> {
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;

View File

@ -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<void> => {
export const initDatabases = (): Promise<void> => {
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<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';
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;

View File

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

View File

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

View File

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

View File

@ -1,12 +1,15 @@
import {getDatabase} from './getDB';
import {databaseConf} from './types';
import {databaseConf, mergeDBName} from './types';
export const setEntry = async <T2 extends databaseConf<T, any>, 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);

View File

@ -1,12 +1,15 @@
import {getDatabase} from './getDB.web';
import {databaseConf} from './types';
import {databaseConf, mergeDBName} from './types';
export const setEntry = async <T2 extends databaseConf<T, any>, 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);

View File

@ -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<props, enums> {
version: number;
migration?: any;
keys: enums;
getEntry: (key: possibleDBKeys) => Promise<props | null>;
getAllEntries: (filter?: filterParam) => Promise<props[] | null>;
setEntry: (val: props) => Promise<void>;
getEntry: (key: possibleDBKeys, suffix?: string) => Promise<props | null>;
getAllEntries: (
filter?: filterParam,
suffix?: string,
) => Promise<props[] | null>;
setEntry: (val: props, suffix?: string) => Promise<void>;
defaultProps: props;
details: {
name: databaseNames;
@ -18,3 +22,23 @@ export interface databaseConf<props, enums> {
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) {
if (getUserList[UserId] === undefined) {