database
parent
536291a76f
commit
b8f15fdf26
|
@ -17,6 +17,8 @@ import {appStatus} from '@caj/configs/appNonSaveVar';
|
|||
import {appNonSaveVarActions} from '@caj/configs/appNonSaveVarReducer';
|
||||
import BigDataManager from '@caj/helper/storage/BigDataManager';
|
||||
import {initKey} from '@caj/helper/storage/bdm/encryption';
|
||||
import UserManager from '@caj/user/UserManager';
|
||||
import MyUserManager from '@caj/user/MyUserManager';
|
||||
|
||||
const AnimationView = animated(View);
|
||||
|
||||
|
@ -27,15 +29,23 @@ function onAppStart() {
|
|||
BigDataManager.initDatabase()
|
||||
.then(async () => {
|
||||
console.log('finish');
|
||||
/*await BigDataManager.setEntry('users', {
|
||||
await BigDataManager.setEntry('users', {
|
||||
UserId: 'test',
|
||||
AccountName: 'test222222222',
|
||||
Username: 'us',
|
||||
});*/
|
||||
});
|
||||
|
||||
setTimeout(async () => {
|
||||
console.log('get');
|
||||
console.log(await BigDataManager.getEntry('users', 'test'));
|
||||
console.log(
|
||||
'user',
|
||||
await UserManager.getUser(MyUserManager.getSelectedUserId()),
|
||||
);
|
||||
console.log(
|
||||
'user2',
|
||||
await UserManager.getUser('ce97da21-c5f1-46f8-947b-ce9288b74ed1'),
|
||||
);
|
||||
}, 1100);
|
||||
|
||||
store.dispatch(
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
import DBSchemas from './schemas';
|
||||
import {getDatabase} from './getDB';
|
||||
import {databaseNames, possibleDBKeys} from './types';
|
||||
import {databaseConf, databaseNames, possibleDBKeys} from './types';
|
||||
|
||||
export const getEntry = async (dbName: databaseNames, key: possibleDBKeys) => {
|
||||
const realm = await getDatabase(dbName);
|
||||
console.log(realm.path);
|
||||
|
||||
const val = realm.objectForPrimaryKey(dbName, key);
|
||||
const schema = DBSchemas[dbName].details.properties;
|
||||
|
||||
const val = realm.objectForPrimaryKey<typeof schema>('users', key);
|
||||
return val;
|
||||
};
|
||||
|
|
|
@ -7,8 +7,8 @@ export const getEntry = async (dbName: databaseNames, key: possibleDBKeys) => {
|
|||
|
||||
const UserId = MyUserManager.getSelectedUserId();
|
||||
|
||||
const tx = db.transaction(UserId, 'readonly');
|
||||
const store = tx.objectStore(UserId);
|
||||
const tx = db.transaction(dbName, 'readonly');
|
||||
const store = tx.objectStore(dbName);
|
||||
|
||||
return await store.get(key);
|
||||
//await store.put(value);
|
||||
|
|
|
@ -36,11 +36,22 @@ setInterval(() => {
|
|||
) {
|
||||
dbObj.db?.close();
|
||||
dbObj.lastUsedTimestamp = undefined;
|
||||
console.log('Database not used. Closing ' + dbObj.name);
|
||||
//console.log('Database not used. Closing ' + dbObj.name);
|
||||
}
|
||||
}
|
||||
}, 5000);
|
||||
|
||||
export function closeAllDatabases() {
|
||||
for (let name in databases) {
|
||||
let dbObj = databases[name];
|
||||
|
||||
if (dbObj.lastUsedTimestamp !== undefined) {
|
||||
dbObj.db?.close();
|
||||
dbObj.lastUsedTimestamp = undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function openMyDatabase(schema: databaseConf): Promise<DBType> {
|
||||
const folderPath = MyUserManager.getSelectedUserId();
|
||||
const path = folderPath + '/' + schema.filePath;
|
||||
|
|
|
@ -38,6 +38,17 @@ setInterval(() => {
|
|||
}
|
||||
}, 5000);
|
||||
|
||||
export function closeAllDatabases() {
|
||||
for (let name in databases) {
|
||||
let dbObj = databases[name];
|
||||
|
||||
if (dbObj.lastUsedTimestamp !== undefined) {
|
||||
dbObj.db?.close();
|
||||
dbObj.lastUsedTimestamp = undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function openMyDatabase(
|
||||
schema: databaseConf,
|
||||
init?: boolean,
|
||||
|
@ -50,12 +61,12 @@ export async function openMyDatabase(
|
|||
},
|
||||
);
|
||||
|
||||
if (init === true) {
|
||||
/*if (init === true) {
|
||||
const UserId = MyUserManager.getSelectedUserId();
|
||||
console.log(db.objectStoreNames.contains(UserId));
|
||||
if (db.objectStoreNames.contains(UserId) === false) {
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
return db;
|
||||
}
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import {openDB, deleteDB, wrap, unwrap} from 'idb';
|
||||
import {databases, DBObject, getDatabase} from './getDB';
|
||||
import {closeAllDatabases, databases, DBObject, getDatabase} from './getDB';
|
||||
import DBSchemas from './schemas';
|
||||
|
||||
export const initDatabase = (): Promise<void> => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
setTimeout(async () => {
|
||||
closeAllDatabases();
|
||||
|
||||
for (const key in DBSchemas) {
|
||||
const schema = DBSchemas[key as keyof typeof DBSchemas];
|
||||
|
||||
|
@ -19,6 +20,5 @@ export const initDatabase = (): Promise<void> => {
|
|||
await getDatabase(dbObj.name, true); // init Database
|
||||
}
|
||||
resolve();
|
||||
}, 1000);
|
||||
});
|
||||
};
|
||||
|
|
|
@ -18,3 +18,21 @@ export const usersDBMigration = (Schema: databaseConf) => {
|
|||
|
||||
return callback;
|
||||
};
|
||||
|
||||
export const chatDBMigration = (Schema: databaseConf) => {
|
||||
const callback: MigrationCallback = (oldRealm, newRealm) => {
|
||||
/*// only apply this change if upgrading to schemaVersion 2
|
||||
if (oldRealm.schemaVersion < 2) {
|
||||
const oldObjects = oldRealm.objects('Person');
|
||||
const newObjects = newRealm.objects('Person');
|
||||
// loop through all objects and set the fullName property in the new schema
|
||||
for (const objectIndex in oldObjects) {
|
||||
const oldObject = oldObjects[objectIndex];
|
||||
const newObject = newObjects[objectIndex];
|
||||
newObject.fullName = `${oldObject.firstName} ${oldObject.lastName}`;
|
||||
}
|
||||
}*/
|
||||
};
|
||||
|
||||
return callback;
|
||||
};
|
||||
|
|
|
@ -29,7 +29,7 @@ export const usersDBMigration = (Schema: databaseConf) => {
|
|||
) => {
|
||||
if (oldVersion == 0) {
|
||||
// perform the initialization
|
||||
db.createObjectStore(MyUserManager.getSelectedUserId(), {
|
||||
db.createObjectStore(Schema.details.name, {
|
||||
keyPath: Schema.details.primaryKey,
|
||||
});
|
||||
} else if (newVersion !== null) {
|
||||
|
@ -56,7 +56,7 @@ export const chatDBMigration = (Schema: databaseConf) => {
|
|||
) => {
|
||||
if (oldVersion == 0) {
|
||||
// perform the initialization
|
||||
db.createObjectStore(MyUserManager.getSelectedUserId(), {
|
||||
db.createObjectStore(Schema.details.name, {
|
||||
keyPath: Schema.details.primaryKey,
|
||||
});
|
||||
} else if (newVersion !== null) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {usersDBMigration} from './migration';
|
||||
import {chatDBMigration} from './migration.web';
|
||||
import {chatDBMigration} from './migration';
|
||||
import {databaseConf} from './types';
|
||||
|
||||
const users: databaseConf = {
|
||||
|
|
|
@ -7,8 +7,8 @@ export const setEntry = async (dbName: databaseNames, value: any) => {
|
|||
|
||||
const UserId = MyUserManager.getSelectedUserId();
|
||||
|
||||
const tx = db.transaction(UserId, 'readwrite');
|
||||
const store = tx.objectStore(UserId);
|
||||
const tx = db.transaction(dbName, 'readwrite');
|
||||
const store = tx.objectStore(dbName);
|
||||
//const val = (await store.get('counter')) || 0;
|
||||
await store.put(value);
|
||||
await tx.done;
|
||||
|
|
|
@ -9,6 +9,7 @@ import {
|
|||
} from '@caj/configs/types';
|
||||
import {saveVarChanges} from '@caj/helper/appData';
|
||||
import {apiBackendRequest, makeRequest} from '@caj/helper/request';
|
||||
import BigDataManager from '@caj/helper/storage/BigDataManager';
|
||||
import {store} from '@caj/redux/store';
|
||||
import {MyUserAccount, createUserProp, SourceProp} from './types';
|
||||
|
||||
|
@ -33,13 +34,10 @@ function createNewMyUser(
|
|||
EMail,
|
||||
SessionId,
|
||||
WebSocketSessionId,
|
||||
lastUpdateTimestamp: createUserProp(
|
||||
SourceProp.offline,
|
||||
Math.floor(new Date().getTime() / 1000),
|
||||
),
|
||||
lastUpdateTimestamp: Math.floor(new Date().getTime() / 1000),
|
||||
ProfilePicture: {
|
||||
hq: createUserProp(SourceProp.offline),
|
||||
lq: createUserProp(SourceProp.offline),
|
||||
hq: createUserProp(SourceProp.online),
|
||||
lq: createUserProp(SourceProp.online),
|
||||
},
|
||||
userSettings: {
|
||||
lang: store.getState().appVariables.lang.details.langCode,
|
||||
|
@ -48,6 +46,7 @@ function createNewMyUser(
|
|||
};
|
||||
|
||||
createMyUser(user);
|
||||
BigDataManager.initDatabase();
|
||||
|
||||
makeRequest({
|
||||
path: apiBackendRequest.APP_START,
|
||||
|
|
|
@ -1,5 +1,106 @@
|
|||
import {UserId, XAuthorization} from '@caj/configs/types';
|
||||
import {makeRequest, apiBackendRequest} from '@caj/helper/request';
|
||||
import BigDataManager from '@caj/helper/storage/BigDataManager';
|
||||
import {store} from '@caj/redux/store';
|
||||
import {createUserProp, SourceProp, User} from './types';
|
||||
|
||||
const UserManager = {};
|
||||
async function getUser(
|
||||
UserId: UserId,
|
||||
save?: boolean,
|
||||
): Promise<User | undefined> {
|
||||
let user: User | undefined;
|
||||
|
||||
let state = store.getState();
|
||||
|
||||
if (UserId === state.appVariables.preferences.selectedAccount) {
|
||||
const usr = state.appVariables.preferences.accounts[UserId];
|
||||
|
||||
if (usr !== undefined) {
|
||||
user = {
|
||||
AccountName: usr.AccountName,
|
||||
Description: usr.Description,
|
||||
FollowersCount: usr.FollowersCount,
|
||||
FollowingCount: usr.FollowingCount,
|
||||
lastUpdateTimestamp: usr.lastUpdateTimestamp,
|
||||
ProfilePicture: usr.ProfilePicture,
|
||||
UserId,
|
||||
Username: usr.Username,
|
||||
XpLevel: usr.XpLevel,
|
||||
XpPoints: usr.XpPoints,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (user === undefined) {
|
||||
const usr = await BigDataManager.getEntry('users', UserId);
|
||||
|
||||
user = {
|
||||
AccountName: usr.AccountName,
|
||||
Description: usr.Description,
|
||||
FollowersCount: usr.FollowersCount,
|
||||
FollowingCount: usr.FollowingCount,
|
||||
lastUpdateTimestamp: usr.lastUpdateTimestamp,
|
||||
ProfilePicture: usr.ProfilePicture,
|
||||
UserId,
|
||||
Username: usr.Username,
|
||||
XpLevel: usr.XpLevel,
|
||||
XpPoints: usr.XpPoints,
|
||||
};
|
||||
}
|
||||
|
||||
if (user === undefined) {
|
||||
try {
|
||||
const resp = await makeRequest({
|
||||
path: apiBackendRequest.GET_USER_PROFILE,
|
||||
requestGET: {':userId': UserId},
|
||||
response: {
|
||||
AccountName: '',
|
||||
Description: '',
|
||||
FollowersCount: 0,
|
||||
FollowingCount: 0,
|
||||
Username: '',
|
||||
XpLevel: 0,
|
||||
XpPoints: 0,
|
||||
AvatarUrl: '',
|
||||
},
|
||||
});
|
||||
|
||||
user = {
|
||||
AccountName: createUserProp(
|
||||
SourceProp.cached,
|
||||
resp.response.AccountName,
|
||||
),
|
||||
Description: createUserProp(
|
||||
SourceProp.cached,
|
||||
resp.response.Description,
|
||||
),
|
||||
FollowersCount: createUserProp(
|
||||
SourceProp.cached,
|
||||
resp.response.FollowersCount,
|
||||
),
|
||||
FollowingCount: createUserProp(
|
||||
SourceProp.cached,
|
||||
resp.response.FollowingCount,
|
||||
),
|
||||
lastUpdateTimestamp: Math.floor(new Date().getTime() / 1000),
|
||||
ProfilePicture: {
|
||||
hq: createUserProp(SourceProp.online, resp.response.AvatarUrl),
|
||||
lq: createUserProp(SourceProp.online, resp.response.AvatarUrl),
|
||||
},
|
||||
UserId,
|
||||
Username: createUserProp(SourceProp.offline, resp.response.Username),
|
||||
XpLevel: createUserProp(SourceProp.cached, resp.response.XpLevel),
|
||||
XpPoints: createUserProp(SourceProp.cached, resp.response.XpPoints),
|
||||
};
|
||||
|
||||
BigDataManager.setEntry('users', user);
|
||||
} catch (error: any) {
|
||||
console.error(error.status);
|
||||
}
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
const UserManager = {getUser};
|
||||
export default UserManager;
|
||||
|
|
|
@ -31,7 +31,7 @@ export interface User {
|
|||
UserId: UserId;
|
||||
|
||||
ProfilePicture: ProfilePicture;
|
||||
lastUpdateTimestamp: BasicUserProp<timestamp>;
|
||||
lastUpdateTimestamp: timestamp;
|
||||
AccountName: BasicUserProp<AccountName>;
|
||||
Username: BasicUserProp<Username>;
|
||||
Description: BasicUserProp<string>;
|
||||
|
|
Loading…
Reference in New Issue