database
parent
a9b8cb574a
commit
468c5bbff6
|
@ -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()),
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
import {MigrationCallback} from 'realm';
|
||||
import {databaseConf} from './types';
|
||||
import DBSchemas from './schemas';
|
||||
import {databaseConf, databaseNames} from './types';
|
||||
|
||||
export const usersDBMigration = (Schema: databaseConf) => {
|
||||
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) {
|
||||
|
@ -17,9 +19,8 @@ export const usersDBMigration = (Schema: databaseConf) => {
|
|||
};
|
||||
|
||||
return callback;
|
||||
};
|
||||
|
||||
export const chatDBMigration = (Schema: databaseConf) => {
|
||||
},
|
||||
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) {
|
||||
|
@ -35,4 +36,5 @@ export const chatDBMigration = (Schema: databaseConf) => {
|
|||
};
|
||||
|
||||
return callback;
|
||||
},
|
||||
};
|
||||
|
|
|
@ -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,7 +20,8 @@ type upgradeFunc = (
|
|||
event: IDBVersionChangeEvent,
|
||||
) => void;
|
||||
|
||||
export const usersDBMigration = (Schema: databaseConf) => {
|
||||
export const DBMigration: {[key in databaseNames]: any} = {
|
||||
users: (Schema: databaseConf<typeof DBSchemas.users.defaultProps>) => {
|
||||
const callback: upgradeFunc = (
|
||||
db,
|
||||
oldVersion,
|
||||
|
@ -44,9 +46,8 @@ export const usersDBMigration = (Schema: databaseConf) => {
|
|||
};
|
||||
|
||||
return callback;
|
||||
};
|
||||
|
||||
export const chatDBMigration = (Schema: databaseConf) => {
|
||||
},
|
||||
chat: (Schema: databaseConf<typeof DBSchemas.chat.defaultProps>) => {
|
||||
const callback: upgradeFunc = (
|
||||
db,
|
||||
oldVersion,
|
||||
|
@ -71,4 +72,5 @@ export const chatDBMigration = (Schema: databaseConf) => {
|
|||
};
|
||||
|
||||
return callback;
|
||||
},
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -32,21 +32,24 @@ async function getUser(
|
|||
}
|
||||
|
||||
if (user === undefined) {
|
||||
const usr = await BigDataManager.getEntry('users', UserId);
|
||||
const usr = await BigDataManager.databases.users.getEntry(UserId);
|
||||
|
||||
if (usr !== null) {
|
||||
user = {
|
||||
AccountName: usr.AccountName,
|
||||
Description: usr.Description,
|
||||
FollowersCount: usr.FollowersCount,
|
||||
FollowingCount: usr.FollowingCount,
|
||||
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: usr.ProfilePicture,
|
||||
ProfilePicture: createUserProp(SourceProp.offline, usr.ProfilePicture),
|
||||
UserId,
|
||||
Username: usr.Username,
|
||||
XpLevel: usr.XpLevel,
|
||||
XpPoints: usr.XpPoints,
|
||||
Username: createUserProp(SourceProp.offline, usr.Username),
|
||||
XpLevel: createUserProp(SourceProp.offline, usr.XpLevel),
|
||||
XpPoints: createUserProp(SourceProp.offline, usr.XpPoints),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (user === undefined) {
|
||||
try {
|
||||
|
|
Loading…
Reference in New Issue