database
parent
17399c518c
commit
014246a78b
|
@ -26,75 +26,72 @@ import {initDatabase} from '@caj/helper/storage/bdm/init';
|
|||
|
||||
const AnimationView = animated(View);
|
||||
|
||||
function onAppStart() {
|
||||
initAppData().then(async () => {
|
||||
await initKey();
|
||||
export async function onAppStart() {
|
||||
await initAppData();
|
||||
|
||||
BigDataManager.initDatabase()
|
||||
.then(async () => {
|
||||
const keys = BigDataManager.databases.chatRoomInfos.keys;
|
||||
const entries =
|
||||
(await BigDataManager.databases.chatRoomInfos.getAllEntries()) || [];
|
||||
await initKey();
|
||||
|
||||
entries.sort((a, b) =>
|
||||
a[keys.timestamp] > b[keys.timestamp]
|
||||
? 1
|
||||
: b[keys.timestamp] > a[keys.timestamp]
|
||||
? -1
|
||||
: 0,
|
||||
);
|
||||
BigDataManager.initDatabase()
|
||||
.then(async () => {
|
||||
const keys = BigDataManager.databases.chatRoomInfos.keys;
|
||||
const entries =
|
||||
(await BigDataManager.databases.chatRoomInfos.getAllEntries()) || [];
|
||||
|
||||
for (let i = 0; i < entries.length; i++) {
|
||||
console.log(entries[i]);
|
||||
const chat = convertDatabaseChat(entries[i]);
|
||||
if (chat === undefined) continue;
|
||||
entries.sort((a, b) =>
|
||||
a[keys.timestamp] > b[keys.timestamp]
|
||||
? 1
|
||||
: b[keys.timestamp] > a[keys.timestamp]
|
||||
? -1
|
||||
: 0,
|
||||
);
|
||||
|
||||
await initDatabase(DBSchemas.chat, chat.roomId);
|
||||
for (let i = 0; i < entries.length; i++) {
|
||||
console.log(entries[i]);
|
||||
const chat = convertDatabaseChat(entries[i]);
|
||||
if (chat === undefined) continue;
|
||||
|
||||
addChatEntity(chat);
|
||||
await initDatabase(DBSchemas.chat, chat.roomId);
|
||||
|
||||
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,
|
||||
);
|
||||
}
|
||||
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.data]: 'heyho',
|
||||
},
|
||||
chat.roomId,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log('finish');
|
||||
const usrDBKeys = BigDataManager.databases.users.keys;
|
||||
await BigDataManager.databases.users.setEntry({
|
||||
[usrDBKeys.UserId]: 'test',
|
||||
[usrDBKeys.AccountName]: '#845613',
|
||||
[usrDBKeys.Username]: 'TestGroupVirtual',
|
||||
[usrDBKeys.Description]:
|
||||
'This is a test account that is not real. ^^',
|
||||
[usrDBKeys.FollowersCount]: 2,
|
||||
[usrDBKeys.FollowingCount]: 24,
|
||||
[usrDBKeys.lastUpdateTimestamp]: 412341234,
|
||||
[usrDBKeys.ProfilePicture]: '',
|
||||
[usrDBKeys.ProfilePictureBinaryHQ]: new ArrayBuffer(0),
|
||||
[usrDBKeys.ProfilePictureBinaryLQ]: new ArrayBuffer(0),
|
||||
[usrDBKeys.XpLevel]: 0,
|
||||
[usrDBKeys.XpPoints]: 0,
|
||||
});
|
||||
|
||||
store.dispatch(
|
||||
appNonSaveVarActions.setAppStatus(appStatus.APP_RUNNING),
|
||||
);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error("Database Error! Can't start App :(", err);
|
||||
console.log('finish');
|
||||
const usrDBKeys = BigDataManager.databases.users.keys;
|
||||
await BigDataManager.databases.users.setEntry({
|
||||
[usrDBKeys.UserId]: 'test',
|
||||
[usrDBKeys.AccountName]: '#845613',
|
||||
[usrDBKeys.Username]: 'TestGroupVirtual',
|
||||
[usrDBKeys.Description]: 'This is a test account that is not real. ^^',
|
||||
[usrDBKeys.FollowersCount]: 2,
|
||||
[usrDBKeys.FollowingCount]: 24,
|
||||
[usrDBKeys.lastUpdateTimestamp]: 412341234,
|
||||
[usrDBKeys.ProfilePicture]: '',
|
||||
[usrDBKeys.ProfilePictureBinaryHQ]: new ArrayBuffer(0),
|
||||
[usrDBKeys.ProfilePictureBinaryLQ]: new ArrayBuffer(0),
|
||||
[usrDBKeys.XpLevel]: 0,
|
||||
[usrDBKeys.XpPoints]: 0,
|
||||
});
|
||||
|
||||
//store.dispatch(actions.loadPreferences(appVar));
|
||||
});
|
||||
store.dispatch(appNonSaveVarActions.setAppStatus(appStatus.APP_RUNNING));
|
||||
})
|
||||
.catch(err => {
|
||||
console.error("Database Error! Can't start App :(", err);
|
||||
});
|
||||
|
||||
//store.dispatch(actions.loadPreferences(appVar));
|
||||
}
|
||||
|
||||
function StartHelper() {
|
||||
|
@ -136,7 +133,11 @@ function StartHelper() {
|
|||
});
|
||||
}, []);
|
||||
|
||||
useEffect(onAppStart, []);
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
await onAppStart();
|
||||
})();
|
||||
}, []);
|
||||
|
||||
if (currentAppStatus === appStatus.APP_RUNNING) return null;
|
||||
|
||||
|
|
|
@ -54,6 +54,7 @@ import MostPopularPasswords from '@caj/helper/password-quality-calculator/MostPo
|
|||
import MyUserManager from '@caj/user/MyUserManager';
|
||||
|
||||
import {Buffer} from 'buffer';
|
||||
import {onAppStart} from '@caj/../appStart/StartHelper';
|
||||
|
||||
// Load the popular passwords list
|
||||
PasswordQualityCalculator.PopularPasswords.load(MostPopularPasswords);
|
||||
|
@ -1129,7 +1130,7 @@ function StepFinal() {
|
|||
WebSocketSessionId: '',
|
||||
},
|
||||
})
|
||||
.then(resp => {
|
||||
.then(async resp => {
|
||||
if (
|
||||
resp.response.XAuthorization !== '' &&
|
||||
resp.response.UserId !== '' &&
|
||||
|
@ -1151,6 +1152,7 @@ function StepFinal() {
|
|||
resp.response.XAuthorization,
|
||||
resp.response.WebSocketSessionId,
|
||||
);
|
||||
await onAppStart();
|
||||
navigation.popToTop();
|
||||
navigation.goBack();
|
||||
}
|
||||
|
@ -1284,7 +1286,7 @@ function Login() {
|
|||
resp.response.XAuthorization,
|
||||
resp.response.WebSocketSessionId,
|
||||
)
|
||||
.then(() => {
|
||||
.then(async () => {
|
||||
showToast(toast, {
|
||||
title: lang.account.login.success,
|
||||
variant: 'solid',
|
||||
|
@ -1295,6 +1297,7 @@ function Login() {
|
|||
});
|
||||
|
||||
setLoading(false);
|
||||
await onAppStart();
|
||||
navigation.goBack();
|
||||
})
|
||||
.catch(() => {
|
||||
|
|
|
@ -1,10 +1,15 @@
|
|||
import {appNonSaveVarActions} from '@caj/configs/appNonSaveVarReducer';
|
||||
import {initDatabase} from '@caj/helper/storage/bdm/init';
|
||||
import DBSchemas from '@caj/helper/storage/bdm/schemas';
|
||||
import BigDataManager from '@caj/helper/storage/BigDataManager';
|
||||
import {store} from '@caj/redux/store';
|
||||
import {chatEntity} from './types';
|
||||
|
||||
async function initChatDatabase(chat: chatEntity) {
|
||||
const keys = BigDataManager.databases.chatRoomInfos.keys;
|
||||
|
||||
await initDatabase(DBSchemas.chat, chat.roomId);
|
||||
|
||||
await BigDataManager.databases.chatRoomInfos.setEntry({
|
||||
[keys.RoomId]: chat.roomId,
|
||||
[keys.initSyncId]: chat.initSyncId,
|
||||
|
|
|
@ -16,7 +16,7 @@ export const getEntry = async <T2 extends databaseConf<T, any>, T>(
|
|||
const realm = await getDatabase(nameObj);
|
||||
|
||||
const val = realm.objectForPrimaryKey<typeof schema.details.properties>(
|
||||
dbName,
|
||||
nameObj.name,
|
||||
key,
|
||||
);
|
||||
|
||||
|
@ -33,7 +33,7 @@ export const getAllEntries = async <T2 extends databaseConf<T, any>, T>(
|
|||
|
||||
const realm = await getDatabase(nameObj);
|
||||
|
||||
const val = realm.objects<typeof schema.details.properties>(dbName);
|
||||
const val = realm.objects<typeof schema.details.properties>(nameObj.name);
|
||||
|
||||
return [...val] as T[];
|
||||
};
|
||||
|
|
|
@ -77,12 +77,17 @@ export async function openMyDatabase(
|
|||
await RNFS.mkdir(RNFS.DocumentDirectoryPath + '/' + folderPath);
|
||||
}
|
||||
|
||||
return await Realm.open({
|
||||
console.log('schema.details', schema.details);
|
||||
|
||||
const realm = await Realm.open({
|
||||
schema: [schema.details as any],
|
||||
schemaVersion: schema.version,
|
||||
path,
|
||||
encryptionKey: getKey(),
|
||||
});
|
||||
|
||||
console.log('path', path);
|
||||
return realm;
|
||||
}
|
||||
|
||||
export async function getDatabase(
|
||||
|
|
|
@ -27,6 +27,7 @@ export const initDatabase = async (
|
|||
name: schema.details.name,
|
||||
suffix: fileSuffix,
|
||||
};
|
||||
console.log('initDatabase', _nameObj);
|
||||
const name = mergeDBName(_nameObj);
|
||||
|
||||
let dbObj: DBObject = {
|
||||
|
|
|
@ -5,21 +5,46 @@ import {setEntry} from '../set';
|
|||
import {databaseConf, possibleDBKeys} from '../types';
|
||||
|
||||
enum keys {
|
||||
UserId = 'a',
|
||||
msg = 'b',
|
||||
syncID = "a",
|
||||
UserId = 'b',
|
||||
data = 'c',
|
||||
massageType = "d",
|
||||
created_at = "e",
|
||||
received_by_server = "f",
|
||||
received_by_target_clients = "g",
|
||||
reply = "h",
|
||||
reactions = "i",
|
||||
hasRead = "j",
|
||||
}
|
||||
|
||||
const name = 'chat';
|
||||
const primaryKey: keyof typeof propsDefault = keys.UserId;
|
||||
|
||||
const propsType: {[key in keyof typeof propsDefault]: string} = {
|
||||
|
||||
[keys.syncID]: "int",
|
||||
[keys.UserId]: 'string',
|
||||
[keys.msg]: 'string',
|
||||
[keys.data]: 'data',
|
||||
[keys.massageType]: "int",
|
||||
[keys.created_at]: "int",
|
||||
[keys.received_by_server]: "int",
|
||||
[keys.received_by_target_clients]: "int",
|
||||
[keys.reply]: "int",
|
||||
[keys.reactions]: "i",
|
||||
[keys.hasRead]: "j",
|
||||
};
|
||||
|
||||
const propsDefault = {
|
||||
[keys.UserId]: 'test',
|
||||
[keys.msg]: 'test',
|
||||
[keys.syncID]: "a",
|
||||
[keys.UserId]: 'b',
|
||||
[keys.data]: 'c',
|
||||
[keys.massageType]: "d",
|
||||
[keys.created_at]: "e",
|
||||
[keys.received_by_server]: "f",
|
||||
[keys.received_by_target_clients]: "g",
|
||||
[keys.reply]: "h",
|
||||
[keys.reactions]: "i",
|
||||
[keys.hasRead]: "j",
|
||||
};
|
||||
|
||||
const thisSchema: databaseConf<typeof propsDefault, typeof keys> = {
|
||||
|
|
|
@ -12,6 +12,6 @@ export const setEntry = async <T2 extends databaseConf<T, any>, T>(
|
|||
const realm = await getDatabase(nameObj);
|
||||
|
||||
realm.write(() => {
|
||||
realm.create(dbName, value as any, Realm.UpdateMode.Modified);
|
||||
realm.create(nameObj.name, value as any, Realm.UpdateMode.Modified);
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue