Jan Umbach 2023-02-06 16:28:01 +01:00
parent b8f15fdf26
commit a9b8cb574a
6 changed files with 28 additions and 18 deletions

View File

@ -37,7 +37,9 @@ function onAppStart() {
setTimeout(async () => { setTimeout(async () => {
console.log('get'); console.log('get');
console.log(await BigDataManager.getEntry('users', 'test')); const obj = (await BigDataManager.getEntry('users', 'test'));
console.log("obj",obj);
console.log("objAccName",obj.AccountName);
console.log( console.log(
'user', 'user',
await UserManager.getUser(MyUserManager.getSelectedUserId()), await UserManager.getUser(MyUserManager.getSelectedUserId()),

View File

@ -9,5 +9,7 @@ export const getEntry = async (dbName: databaseNames, key: possibleDBKeys) => {
const schema = DBSchemas[dbName].details.properties; const schema = DBSchemas[dbName].details.properties;
const val = realm.objectForPrimaryKey<typeof schema>('users', key); const val = realm.objectForPrimaryKey<typeof schema>('users', key);
console.log('val', val);
return val; return val;
}; };

View File

@ -12,7 +12,7 @@ type DBType = Realm;
export interface DBObject { export interface DBObject {
name: databaseNames; name: databaseNames;
schema: databaseConf; schema: databaseConf<{[key: string]: string}>;
db: DBType | undefined; db: DBType | undefined;
lastUsedTimestamp?: timestamp; // when timeout is undefined then db is closed lastUsedTimestamp?: timestamp; // when timeout is undefined then db is closed
} }
@ -52,7 +52,9 @@ export function closeAllDatabases() {
} }
} }
export async function openMyDatabase(schema: databaseConf): Promise<DBType> { export async function openMyDatabase(
schema: databaseConf<{[key: string]: string}>,
): Promise<DBType> {
const folderPath = MyUserManager.getSelectedUserId(); const folderPath = MyUserManager.getSelectedUserId();
const path = folderPath + '/' + schema.filePath; const path = folderPath + '/' + schema.filePath;

View File

@ -9,7 +9,7 @@ type DBType = IDBPDatabase<unknown>;
export interface DBObject { export interface DBObject {
name: databaseNames; name: databaseNames;
schema: databaseConf; schema: databaseConf<{[key: string]: string}>;
db: DBType | undefined; db: DBType | undefined;
lastUsedTimestamp?: timestamp; // when timeout is undefined then db is closed lastUsedTimestamp?: timestamp; // when timeout is undefined then db is closed
} }
@ -50,7 +50,7 @@ export function closeAllDatabases() {
} }
export async function openMyDatabase( export async function openMyDatabase(
schema: databaseConf, schema: databaseConf<{[key: string]: string}>,
init?: boolean, init?: boolean,
): Promise<DBType> { ): Promise<DBType> {
const db = await openDB( const db = await openDB(

View File

@ -2,7 +2,13 @@ import {usersDBMigration} from './migration';
import {chatDBMigration} from './migration'; import {chatDBMigration} from './migration';
import {databaseConf} from './types'; import {databaseConf} from './types';
const users: databaseConf = { const usersProps = {
UserId: 'string',
AccountName: 'string',
Username: 'string',
};
const users: databaseConf<typeof usersProps> = {
filePath: 'users', filePath: 'users',
version: 1, version: 1,
migration: () => { migration: () => {
@ -10,16 +16,17 @@ const users: databaseConf = {
}, },
details: { details: {
name: 'users', name: 'users',
properties: { properties: usersProps,
UserId: 'string',
AccountName: 'string',
Username: 'string',
},
primaryKey: 'UserId', primaryKey: 'UserId',
}, },
}; };
const chat: databaseConf = { const chatProps = {
UserId: 'string',
msg: 'string',
};
const chat: databaseConf<typeof chatProps> = {
filePath: 'chat', filePath: 'chat',
version: 1, version: 1,
migration: () => { migration: () => {
@ -27,10 +34,7 @@ const chat: databaseConf = {
}, },
details: { details: {
name: 'chat', name: 'chat',
properties: { properties: chatProps,
UserId: 'string',
msg: 'string',
},
primaryKey: 'UserId', primaryKey: 'UserId',
}, },
}; };

View File

@ -1,13 +1,13 @@
export type databaseNames = 'users' | 'chat'; export type databaseNames = 'users' | 'chat';
export type possibleDBKeys = string; export type possibleDBKeys = string;
export interface databaseConf { export interface databaseConf<props> {
filePath: string; filePath: string;
version: number; version: number;
migration?: any; migration?: any;
details: { details: {
name: databaseNames; name: databaseNames;
properties: any; properties: props;
primaryKey: string; primaryKey: string;
}; };
} }