alpha
Jan Umbach 2023-02-08 00:04:56 +01:00
parent a9b8cb574a
commit 468c5bbff6
10 changed files with 219 additions and 145 deletions

View File

@ -19,6 +19,7 @@ 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';
import DBSchemas from '@caj/helper/storage/bdm/schemas';
const AnimationView = animated(View);
@ -37,9 +38,9 @@ function onAppStart() {
setTimeout(async () => {
console.log('get');
const obj = (await BigDataManager.getEntry('users', 'test'));
console.log("obj",obj);
console.log("objAccName",obj.AccountName);
const obj = await BigDataManager.databases.users.getEntry('test');
console.log('obj', obj);
console.log('objAccName', obj?.AccountName);
console.log(
'user',
await UserManager.getUser(MyUserManager.getSelectedUserId()),

View File

@ -1,6 +1,13 @@
import {initDatabase} 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, setEntry,getEntry};
const BigDataManager = {
initDatabase,
setEntry,
getEntry,
databases: DBSchemas,
};
export default BigDataManager;

View File

@ -2,14 +2,17 @@ import DBSchemas from './schemas';
import {getDatabase} from './getDB';
import {databaseConf, databaseNames, possibleDBKeys} from './types';
export const getEntry = async (dbName: databaseNames, key: possibleDBKeys) => {
export const getEntry = async <T2 extends databaseConf<T>, T>(
db: T2,
key: possibleDBKeys,
): Promise<null | T> => {
const dbName = db.details.name;
const realm = await getDatabase(dbName);
console.log(realm.path);
const schema = DBSchemas[dbName].details.properties;
const val = realm.objectForPrimaryKey<typeof db.details.properties>(
dbName,
key,
);
const val = realm.objectForPrimaryKey<typeof schema>('users', key);
console.log('val', val);
return val;
return val as T;
};

View File

@ -1,38 +1,40 @@
import {MigrationCallback} from 'realm';
import {databaseConf} from './types';
import DBSchemas from './schemas';
import {databaseConf, databaseNames} from './types';
export const usersDBMigration = (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}`;
}
}*/
};
export const DBMigration: {[key in databaseNames]: any} = {
users: (Schema: databaseConf<typeof DBSchemas.users.defaultProps>) => {
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;
};
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;
return callback;
},
chat: (Schema: databaseConf<typeof DBSchemas.chat.defaultProps>) => {
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;
},
};

View File

@ -1,7 +1,8 @@
import MyUserManager from '@caj/user/MyUserManager';
import {IDBPDatabase, IDBPTransaction, StoreNames} from 'idb';
import DBSchemas from './schemas';
import {databaseConf} from './types';
import {databaseConf, databaseNames} from './types';
interface migrationArguments {
db: IDBPDatabase<unknown>;
@ -19,56 +20,57 @@ type upgradeFunc = (
event: IDBVersionChangeEvent,
) => void;
export const usersDBMigration = (Schema: databaseConf) => {
const callback: upgradeFunc = (
db,
oldVersion,
newVersion,
transaction,
event,
) => {
if (oldVersion == 0) {
// perform the initialization
db.createObjectStore(Schema.details.name, {
keyPath: Schema.details.primaryKey,
});
} else if (newVersion !== null) {
let ver = oldVersion;
export const DBMigration: {[key in databaseNames]: any} = {
users: (Schema: databaseConf<typeof DBSchemas.users.defaultProps>) => {
const callback: upgradeFunc = (
db,
oldVersion,
newVersion,
transaction,
event,
) => {
if (oldVersion == 0) {
// perform the initialization
db.createObjectStore(Schema.details.name, {
keyPath: Schema.details.primaryKey,
});
} else if (newVersion !== null) {
let ver = oldVersion;
while (ver < newVersion) {
console.log('upgrade from v', ver, ' to v', ver + 1);
while (ver < newVersion) {
console.log('upgrade from v', ver, ' to v', ver + 1);
ver++;
ver++;
}
}
}
};
};
return callback;
};
return callback;
},
chat: (Schema: databaseConf<typeof DBSchemas.chat.defaultProps>) => {
const callback: upgradeFunc = (
db,
oldVersion,
newVersion,
transaction,
event,
) => {
if (oldVersion == 0) {
// perform the initialization
db.createObjectStore(Schema.details.name, {
keyPath: Schema.details.primaryKey,
});
} else if (newVersion !== null) {
let ver = oldVersion;
export const chatDBMigration = (Schema: databaseConf) => {
const callback: upgradeFunc = (
db,
oldVersion,
newVersion,
transaction,
event,
) => {
if (oldVersion == 0) {
// perform the initialization
db.createObjectStore(Schema.details.name, {
keyPath: Schema.details.primaryKey,
});
} else if (newVersion !== null) {
let ver = oldVersion;
while (ver < newVersion) {
console.log('upgrade from v', ver, ' to v', ver + 1);
while (ver < newVersion) {
console.log('upgrade from v', ver, ' to v', ver + 1);
ver++;
ver++;
}
}
}
};
};
return callback;
return callback;
},
};

View File

@ -1,43 +1,5 @@
import {usersDBMigration} from './migration';
import {chatDBMigration} from './migration';
import {databaseConf} from './types';
const usersProps = {
UserId: 'string',
AccountName: 'string',
Username: 'string',
};
const users: databaseConf<typeof usersProps> = {
filePath: 'users',
version: 1,
migration: () => {
return usersDBMigration(users);
},
details: {
name: 'users',
properties: usersProps,
primaryKey: 'UserId',
},
};
const chatProps = {
UserId: 'string',
msg: 'string',
};
const chat: databaseConf<typeof chatProps> = {
filePath: 'chat',
version: 1,
migration: () => {
return chatDBMigration(chat);
},
details: {
name: 'chat',
properties: chatProps,
primaryKey: 'UserId',
},
};
import chat from './schemas/chat';
import users from './schemas/users';
const DBSchemas = {users, chat};
export default DBSchemas;

View File

@ -0,0 +1,37 @@
import {getEntry} from '../get';
import {DBMigration} from '../migration';
import {databaseConf, possibleDBKeys} from '../types';
const name = 'chat';
const primaryKey: keyof typeof propsDefault = 'UserId';
const propsType: {[key in keyof typeof propsDefault]: string} = {
UserId: 'string',
msg: 'string',
};
const propsDefault = {
UserId: 'test',
msg: 'test',
};
const thisSchema: databaseConf<typeof propsDefault> = {
filePath: name,
version: 1,
migration: () => {
return DBMigration[name](thisSchema);
},
getEntry: (key: possibleDBKeys) => {
const thisObj = thisSchema;
return getEntry<typeof thisObj, typeof thisObj.defaultProps>(thisObj, key);
},
defaultProps: propsDefault,
details: {
name,
properties: propsType,
primaryKey,
},
};
export default thisSchema;

View File

@ -0,0 +1,55 @@
import {getEntry} from '../get';
import {DBMigration} from '../migration';
import {databaseConf, possibleDBKeys} from '../types';
const name = 'users';
const primaryKey: keyof typeof propsDefault = 'UserId';
const propsType: {[key in keyof typeof propsDefault]: string} = {
UserId: 'string',
AccountName: 'string',
Username: 'string',
Description: 'string',
FollowersCount: 'int',
FollowingCount: 'int',
lastUpdateTimestamp: 'int',
ProfilePicture: 'string', //URL
ProfilePictureBinary: 'data',
XpLevel: 'int',
XpPoints: 'int',
};
const propsDefault = {
UserId: '',
AccountName: '',
Username: '',
Description: '',
FollowersCount: 0,
FollowingCount: 0,
lastUpdateTimestamp: 0,
ProfilePicture: '', //URL
ProfilePictureBinary: [],
XpLevel: 0,
XpPoints: 0,
};
const thisSchema: databaseConf<typeof propsDefault> = {
filePath: name,
version: 1,
migration: () => {
return DBMigration[name](thisSchema);
},
getEntry: (key: possibleDBKeys) => {
const thisObj = thisSchema;
return getEntry<typeof thisObj, typeof thisObj.defaultProps>(thisObj, key);
},
defaultProps: propsDefault,
details: {
name,
properties: propsType,
primaryKey,
},
};
export default thisSchema;

View File

@ -5,9 +5,11 @@ export interface databaseConf<props> {
filePath: string;
version: number;
migration?: any;
getEntry: (key: possibleDBKeys) => Promise<props | null>;
defaultProps: props;
details: {
name: databaseNames;
properties: props;
primaryKey: string;
properties: {[key in keyof props]: string};
primaryKey: keyof props;
};
}

View File

@ -32,20 +32,23 @@ async function getUser(
}
if (user === undefined) {
const usr = await BigDataManager.getEntry('users', UserId);
const usr = await BigDataManager.databases.users.getEntry(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 (usr !== null) {
user = {
AccountName: createUserProp(SourceProp.offline, usr.AccountName),
Description: createUserProp(SourceProp.offline, usr.Description),
FollowersCount: createUserProp(SourceProp.offline, usr.FollowersCount),
FollowingCount: createUserProp(SourceProp.offline, usr.FollowingCount),
lastUpdateTimestamp: usr.lastUpdateTimestamp,
ProfilePicture: createUserProp(SourceProp.offline, usr.ProfilePicture),
UserId,
Username: createUserProp(SourceProp.offline, usr.Username),
XpLevel: createUserProp(SourceProp.offline, usr.XpLevel),
XpPoints: createUserProp(SourceProp.offline, usr.XpPoints),
};
}
}
if (user === undefined) {