diff --git a/src/appStart/StartHelper.tsx b/src/appStart/StartHelper.tsx index b7adf71..f16e678 100644 --- a/src/appStart/StartHelper.tsx +++ b/src/appStart/StartHelper.tsx @@ -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()), diff --git a/src/caj/helper/storage/BigDataManager.ts b/src/caj/helper/storage/BigDataManager.ts index b959c4c..459d5af 100644 --- a/src/caj/helper/storage/BigDataManager.ts +++ b/src/caj/helper/storage/BigDataManager.ts @@ -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; diff --git a/src/caj/helper/storage/bdm/get.ts b/src/caj/helper/storage/bdm/get.ts index 8c2ccd2..a645906 100644 --- a/src/caj/helper/storage/bdm/get.ts +++ b/src/caj/helper/storage/bdm/get.ts @@ -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 , T>( + db: T2, + key: possibleDBKeys, +): Promise => { + const dbName = db.details.name; const realm = await getDatabase(dbName); - console.log(realm.path); - const schema = DBSchemas[dbName].details.properties; + const val = realm.objectForPrimaryKey( + dbName, + key, + ); - const val = realm.objectForPrimaryKey('users', key); - console.log('val', val); - - return val; + return val as T; }; diff --git a/src/caj/helper/storage/bdm/migration.ts b/src/caj/helper/storage/bdm/migration.ts index 0f10f2a..168f809 100644 --- a/src/caj/helper/storage/bdm/migration.ts +++ b/src/caj/helper/storage/bdm/migration.ts @@ -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) => { + 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) => { + 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; + }, }; diff --git a/src/caj/helper/storage/bdm/migration.web.ts b/src/caj/helper/storage/bdm/migration.web.ts index 80e6784..7c6a9d9 100644 --- a/src/caj/helper/storage/bdm/migration.web.ts +++ b/src/caj/helper/storage/bdm/migration.web.ts @@ -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; @@ -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) => { + 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) => { + 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; + }, }; diff --git a/src/caj/helper/storage/bdm/schemas.ts b/src/caj/helper/storage/bdm/schemas.ts index c642114..4032731 100644 --- a/src/caj/helper/storage/bdm/schemas.ts +++ b/src/caj/helper/storage/bdm/schemas.ts @@ -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 = { - filePath: 'users', - version: 1, - migration: () => { - return usersDBMigration(users); - }, - details: { - name: 'users', - properties: usersProps, - primaryKey: 'UserId', - }, -}; - -const chatProps = { - UserId: 'string', - msg: 'string', -}; - -const chat: databaseConf = { - 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; diff --git a/src/caj/helper/storage/bdm/schemas/chat.ts b/src/caj/helper/storage/bdm/schemas/chat.ts new file mode 100644 index 0000000..2c8dbfe --- /dev/null +++ b/src/caj/helper/storage/bdm/schemas/chat.ts @@ -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 = { + filePath: name, + version: 1, + migration: () => { + return DBMigration[name](thisSchema); + }, + getEntry: (key: possibleDBKeys) => { + const thisObj = thisSchema; + return getEntry(thisObj, key); + }, + defaultProps: propsDefault, + details: { + name, + properties: propsType, + primaryKey, + }, +}; + +export default thisSchema; diff --git a/src/caj/helper/storage/bdm/schemas/users.ts b/src/caj/helper/storage/bdm/schemas/users.ts new file mode 100644 index 0000000..46f5dbf --- /dev/null +++ b/src/caj/helper/storage/bdm/schemas/users.ts @@ -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 = { + filePath: name, + version: 1, + migration: () => { + return DBMigration[name](thisSchema); + }, + getEntry: (key: possibleDBKeys) => { + const thisObj = thisSchema; + return getEntry(thisObj, key); + }, + defaultProps: propsDefault, + details: { + name, + properties: propsType, + primaryKey, + }, +}; + +export default thisSchema; diff --git a/src/caj/helper/storage/bdm/types.ts b/src/caj/helper/storage/bdm/types.ts index a0def77..783b64b 100644 --- a/src/caj/helper/storage/bdm/types.ts +++ b/src/caj/helper/storage/bdm/types.ts @@ -5,9 +5,11 @@ export interface databaseConf { filePath: string; version: number; migration?: any; + getEntry: (key: possibleDBKeys) => Promise; + defaultProps: props; details: { name: databaseNames; - properties: props; - primaryKey: string; + properties: {[key in keyof props]: string}; + primaryKey: keyof props; }; } diff --git a/src/caj/user/UserManager.ts b/src/caj/user/UserManager.ts index decd1e2..36207c6 100644 --- a/src/caj/user/UserManager.ts +++ b/src/caj/user/UserManager.ts @@ -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) {