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 {initKey} from '@caj/helper/storage/bdm/encryption';
|
||||||
import UserManager from '@caj/user/UserManager';
|
import UserManager from '@caj/user/UserManager';
|
||||||
import MyUserManager from '@caj/user/MyUserManager';
|
import MyUserManager from '@caj/user/MyUserManager';
|
||||||
|
import DBSchemas from '@caj/helper/storage/bdm/schemas';
|
||||||
|
|
||||||
const AnimationView = animated(View);
|
const AnimationView = animated(View);
|
||||||
|
|
||||||
|
@ -37,9 +38,9 @@ function onAppStart() {
|
||||||
|
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
console.log('get');
|
console.log('get');
|
||||||
const obj = (await BigDataManager.getEntry('users', 'test'));
|
const obj = await BigDataManager.databases.users.getEntry('test');
|
||||||
console.log("obj",obj);
|
console.log('obj', obj);
|
||||||
console.log("objAccName",obj.AccountName);
|
console.log('objAccName', obj?.AccountName);
|
||||||
console.log(
|
console.log(
|
||||||
'user',
|
'user',
|
||||||
await UserManager.getUser(MyUserManager.getSelectedUserId()),
|
await UserManager.getUser(MyUserManager.getSelectedUserId()),
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
import {initDatabase} from './bdm/init';
|
import {initDatabase} from './bdm/init';
|
||||||
import {setEntry} from './bdm/set';
|
import {setEntry} from './bdm/set';
|
||||||
import {getEntry} from './bdm/get';
|
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;
|
export default BigDataManager;
|
||||||
|
|
|
@ -2,14 +2,17 @@ import DBSchemas from './schemas';
|
||||||
import {getDatabase} from './getDB';
|
import {getDatabase} from './getDB';
|
||||||
import {databaseConf, databaseNames, possibleDBKeys} from './types';
|
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);
|
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);
|
return val as T;
|
||||||
console.log('val', val);
|
|
||||||
|
|
||||||
return val;
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,38 +1,40 @@
|
||||||
import {MigrationCallback} from 'realm';
|
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} = {
|
||||||
const callback: MigrationCallback = (oldRealm, newRealm) => {
|
users: (Schema: databaseConf<typeof DBSchemas.users.defaultProps>) => {
|
||||||
/*// only apply this change if upgrading to schemaVersion 2
|
const callback: MigrationCallback = (oldRealm, newRealm) => {
|
||||||
if (oldRealm.schemaVersion < 2) {
|
/*// only apply this change if upgrading to schemaVersion 2
|
||||||
const oldObjects = oldRealm.objects('Person');
|
if (oldRealm.schemaVersion < 2) {
|
||||||
const newObjects = newRealm.objects('Person');
|
const oldObjects = oldRealm.objects('Person');
|
||||||
// loop through all objects and set the fullName property in the new schema
|
const newObjects = newRealm.objects('Person');
|
||||||
for (const objectIndex in oldObjects) {
|
// loop through all objects and set the fullName property in the new schema
|
||||||
const oldObject = oldObjects[objectIndex];
|
for (const objectIndex in oldObjects) {
|
||||||
const newObject = newObjects[objectIndex];
|
const oldObject = oldObjects[objectIndex];
|
||||||
newObject.fullName = `${oldObject.firstName} ${oldObject.lastName}`;
|
const newObject = newObjects[objectIndex];
|
||||||
}
|
newObject.fullName = `${oldObject.firstName} ${oldObject.lastName}`;
|
||||||
}*/
|
}
|
||||||
};
|
}*/
|
||||||
|
};
|
||||||
|
|
||||||
return callback;
|
return callback;
|
||||||
};
|
},
|
||||||
|
chat: (Schema: databaseConf<typeof DBSchemas.chat.defaultProps>) => {
|
||||||
export const chatDBMigration = (Schema: databaseConf) => {
|
const callback: MigrationCallback = (oldRealm, newRealm) => {
|
||||||
const callback: MigrationCallback = (oldRealm, newRealm) => {
|
/*// only apply this change if upgrading to schemaVersion 2
|
||||||
/*// only apply this change if upgrading to schemaVersion 2
|
if (oldRealm.schemaVersion < 2) {
|
||||||
if (oldRealm.schemaVersion < 2) {
|
const oldObjects = oldRealm.objects('Person');
|
||||||
const oldObjects = oldRealm.objects('Person');
|
const newObjects = newRealm.objects('Person');
|
||||||
const newObjects = newRealm.objects('Person');
|
// loop through all objects and set the fullName property in the new schema
|
||||||
// loop through all objects and set the fullName property in the new schema
|
for (const objectIndex in oldObjects) {
|
||||||
for (const objectIndex in oldObjects) {
|
const oldObject = oldObjects[objectIndex];
|
||||||
const oldObject = oldObjects[objectIndex];
|
const newObject = newObjects[objectIndex];
|
||||||
const newObject = newObjects[objectIndex];
|
newObject.fullName = `${oldObject.firstName} ${oldObject.lastName}`;
|
||||||
newObject.fullName = `${oldObject.firstName} ${oldObject.lastName}`;
|
}
|
||||||
}
|
}*/
|
||||||
}*/
|
};
|
||||||
};
|
|
||||||
|
return callback;
|
||||||
return callback;
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
import MyUserManager from '@caj/user/MyUserManager';
|
import MyUserManager from '@caj/user/MyUserManager';
|
||||||
import {IDBPDatabase, IDBPTransaction, StoreNames} from 'idb';
|
import {IDBPDatabase, IDBPTransaction, StoreNames} from 'idb';
|
||||||
|
import DBSchemas from './schemas';
|
||||||
|
|
||||||
import {databaseConf} from './types';
|
import {databaseConf, databaseNames} from './types';
|
||||||
|
|
||||||
interface migrationArguments {
|
interface migrationArguments {
|
||||||
db: IDBPDatabase<unknown>;
|
db: IDBPDatabase<unknown>;
|
||||||
|
@ -19,56 +20,57 @@ type upgradeFunc = (
|
||||||
event: IDBVersionChangeEvent,
|
event: IDBVersionChangeEvent,
|
||||||
) => void;
|
) => void;
|
||||||
|
|
||||||
export const usersDBMigration = (Schema: databaseConf) => {
|
export const DBMigration: {[key in databaseNames]: any} = {
|
||||||
const callback: upgradeFunc = (
|
users: (Schema: databaseConf<typeof DBSchemas.users.defaultProps>) => {
|
||||||
db,
|
const callback: upgradeFunc = (
|
||||||
oldVersion,
|
db,
|
||||||
newVersion,
|
oldVersion,
|
||||||
transaction,
|
newVersion,
|
||||||
event,
|
transaction,
|
||||||
) => {
|
event,
|
||||||
if (oldVersion == 0) {
|
) => {
|
||||||
// perform the initialization
|
if (oldVersion == 0) {
|
||||||
db.createObjectStore(Schema.details.name, {
|
// perform the initialization
|
||||||
keyPath: Schema.details.primaryKey,
|
db.createObjectStore(Schema.details.name, {
|
||||||
});
|
keyPath: Schema.details.primaryKey,
|
||||||
} else if (newVersion !== null) {
|
});
|
||||||
let ver = oldVersion;
|
} else if (newVersion !== null) {
|
||||||
|
let ver = oldVersion;
|
||||||
|
|
||||||
while (ver < newVersion) {
|
while (ver < newVersion) {
|
||||||
console.log('upgrade from v', ver, ' to v', ver + 1);
|
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) => {
|
while (ver < newVersion) {
|
||||||
const callback: upgradeFunc = (
|
console.log('upgrade from v', ver, ' to v', ver + 1);
|
||||||
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) {
|
ver++;
|
||||||
console.log('upgrade from v', ver, ' to v', ver + 1);
|
}
|
||||||
|
|
||||||
ver++;
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
return callback;
|
return callback;
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,43 +1,5 @@
|
||||||
import {usersDBMigration} from './migration';
|
import chat from './schemas/chat';
|
||||||
import {chatDBMigration} from './migration';
|
import users from './schemas/users';
|
||||||
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',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const DBSchemas = {users, chat};
|
const DBSchemas = {users, chat};
|
||||||
export default DBSchemas;
|
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;
|
filePath: string;
|
||||||
version: number;
|
version: number;
|
||||||
migration?: any;
|
migration?: any;
|
||||||
|
getEntry: (key: possibleDBKeys) => Promise<props | null>;
|
||||||
|
defaultProps: props;
|
||||||
details: {
|
details: {
|
||||||
name: databaseNames;
|
name: databaseNames;
|
||||||
properties: props;
|
properties: {[key in keyof props]: string};
|
||||||
primaryKey: string;
|
primaryKey: keyof props;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,20 +32,23 @@ async function getUser(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (user === undefined) {
|
if (user === undefined) {
|
||||||
const usr = await BigDataManager.getEntry('users', UserId);
|
const usr = await BigDataManager.databases.users.getEntry(UserId);
|
||||||
|
|
||||||
user = {
|
if (usr !== null) {
|
||||||
AccountName: usr.AccountName,
|
user = {
|
||||||
Description: usr.Description,
|
AccountName: createUserProp(SourceProp.offline, usr.AccountName),
|
||||||
FollowersCount: usr.FollowersCount,
|
Description: createUserProp(SourceProp.offline, usr.Description),
|
||||||
FollowingCount: usr.FollowingCount,
|
|
||||||
lastUpdateTimestamp: usr.lastUpdateTimestamp,
|
FollowersCount: createUserProp(SourceProp.offline, usr.FollowersCount),
|
||||||
ProfilePicture: usr.ProfilePicture,
|
FollowingCount: createUserProp(SourceProp.offline, usr.FollowingCount),
|
||||||
UserId,
|
lastUpdateTimestamp: usr.lastUpdateTimestamp,
|
||||||
Username: usr.Username,
|
ProfilePicture: createUserProp(SourceProp.offline, usr.ProfilePicture),
|
||||||
XpLevel: usr.XpLevel,
|
UserId,
|
||||||
XpPoints: usr.XpPoints,
|
Username: createUserProp(SourceProp.offline, usr.Username),
|
||||||
};
|
XpLevel: createUserProp(SourceProp.offline, usr.XpLevel),
|
||||||
|
XpPoints: createUserProp(SourceProp.offline, usr.XpPoints),
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (user === undefined) {
|
if (user === undefined) {
|
||||||
|
|
Loading…
Reference in New Issue