refactor: move project to frontend directory
This commit is contained in:
101
frontend/src/data/db/AniListDb.ts
Normal file
101
frontend/src/data/db/AniListDb.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import {type IDBPDatabase, openDB} from 'idb';
|
||||
import type {AniListUser} from '@/data/models/anilist/AniListUser';
|
||||
import type {AniListMangaListEntry} from '@/data/models/anilist/AniListMangaListEntry';
|
||||
import type {AniListMedia} from '@/data/models/anilist/AniListMedia';
|
||||
import type {AniListMangaList} from '@/data/models/anilist/AniListMangaList';
|
||||
|
||||
export type AlDb = IDBPDatabase<AniListDBSchema>;
|
||||
|
||||
export default class AniListDb {
|
||||
private intDb: IDBPDatabase<AniListDBSchema> | null = null;
|
||||
|
||||
get db(): IDBPDatabase<AniListDBSchema> | null {
|
||||
return this.intDb;
|
||||
}
|
||||
|
||||
static async withDb<T>(fn: (db: AlDb) => Promise<T> | T): Promise<T> {
|
||||
const db = new AniListDb();
|
||||
const idb = await db.open();
|
||||
try {
|
||||
return await fn(idb);
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
||||
async open(): Promise<IDBPDatabase<AniListDBSchema>> {
|
||||
this.intDb = await openDB('anilist', 1, {
|
||||
upgrade(db, oldVersion, newVersion, transaction, event) {
|
||||
switch (oldVersion) {
|
||||
case 0:
|
||||
const user = db.createObjectStore('user', {keyPath: 'id'});
|
||||
user.createIndex('id', 'id', {unique: true});
|
||||
user.createIndex('name', 'name', {unique: true});
|
||||
|
||||
const list = db.createObjectStore('list', {keyPath: '_userId_listName'});
|
||||
list.createIndex('_userId', '_userId', {multiEntry: true});
|
||||
list.createIndex('_userId_listName', '_userId_listName', {unique: true}); // TODO multiprop indexes
|
||||
|
||||
const manga = db.createObjectStore('manga', {keyPath: 'id'});
|
||||
manga.createIndex('id', 'id', {unique: true});
|
||||
manga.createIndex('_userId', '_userId', {multiEntry: true});
|
||||
manga.createIndex('_listName', '_listName', {multiEntry: true});
|
||||
manga.createIndex('_userId_listName', '_userId_listName', {multiEntry: true});
|
||||
|
||||
const media = db.createObjectStore('media', {keyPath: 'id'});
|
||||
media.createIndex('id', 'id', {unique: true});
|
||||
//fall through
|
||||
default:
|
||||
break;
|
||||
}
|
||||
},
|
||||
});
|
||||
return this.intDb;
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.intDb?.close();
|
||||
}
|
||||
}
|
||||
|
||||
export type AniListDBSchema = {
|
||||
user: {
|
||||
key: 'id',
|
||||
value: AniListUser,
|
||||
indexes: {
|
||||
id: number,
|
||||
name: string
|
||||
}
|
||||
},
|
||||
list: {
|
||||
value: AniListMangaList & {
|
||||
_userId: number,
|
||||
_userId_listName: string
|
||||
},
|
||||
indexes: {
|
||||
_userId: number,
|
||||
_userId_listName: string
|
||||
}
|
||||
},
|
||||
manga: {
|
||||
key: 'id';
|
||||
value: AniListMangaListEntry & {
|
||||
_userId: number,
|
||||
_listName: string,
|
||||
_userId_listName: string
|
||||
},
|
||||
indexes: {
|
||||
id: number,
|
||||
_userId: number,
|
||||
_listName: string,
|
||||
_userId_listName: string
|
||||
}
|
||||
},
|
||||
media: {
|
||||
key: 'id';
|
||||
value: AniListMedia;
|
||||
indexes: {
|
||||
id: number
|
||||
}
|
||||
},
|
||||
}
|
||||
4
frontend/src/data/db/DbUtil.ts
Normal file
4
frontend/src/data/db/DbUtil.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export function strip_<T extends {} | object>(obj: T): T {
|
||||
Object.keys(obj).filter(e => e.startsWith('_')).forEach(k => delete (obj as any)[k]);
|
||||
return obj;
|
||||
}
|
||||
73
frontend/src/data/db/MangaUpdatesDb.ts
Normal file
73
frontend/src/data/db/MangaUpdatesDb.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import {type IDBPDatabase, openDB} from 'idb';
|
||||
import type {MangaUpdatesSeries} from '@/data/models/mangaupdates/MangaUpdatesSeries';
|
||||
import type {MangaUpdatesChapter} from '@/data/models/mangaupdates/MangaUpdatesChapter';
|
||||
|
||||
export type MuDb = IDBPDatabase<MangaUpdatesDBSchema>;
|
||||
|
||||
export default class MangaUpdatesDb {
|
||||
private intDb: IDBPDatabase<MangaUpdatesDBSchema> | null = null;
|
||||
|
||||
get db(): IDBPDatabase<MangaUpdatesDBSchema> | null {
|
||||
return this.intDb;
|
||||
}
|
||||
|
||||
static async withDb<T>(fn: (db: MuDb) => Promise<T> | T): Promise<T> {
|
||||
const db = new MangaUpdatesDb();
|
||||
const idb = await db.open();
|
||||
try {
|
||||
return await fn(idb);
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
||||
async open(): Promise<IDBPDatabase<MangaUpdatesDBSchema>> {
|
||||
this.intDb = await openDB('mangaupdates', 1, {
|
||||
upgrade(db, oldVersion, newVersion, transaction, event) {
|
||||
switch (oldVersion) {
|
||||
case 0:
|
||||
const relation = db.createObjectStore('relation', {autoIncrement: true});
|
||||
relation.createIndex('unique', ['aniListMediaId', 'mangaUpdatesSeriesId'], {unique: true});
|
||||
|
||||
const media = db.createObjectStore('series', {keyPath: 'series_id'});
|
||||
media.createIndex('series_id', 'series_id', {unique: true});
|
||||
|
||||
const chapter = db.createObjectStore('chapter', {autoIncrement: true});
|
||||
chapter.createIndex('series_id', 'series_id', {multiEntry: true});
|
||||
//fall through
|
||||
default:
|
||||
break;
|
||||
}
|
||||
},
|
||||
});
|
||||
return this.intDb;
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.intDb?.close();
|
||||
}
|
||||
}
|
||||
|
||||
export type MangaUpdatesDBSchema = {
|
||||
relation: {
|
||||
key: 'id',
|
||||
value: MangaUpdatesRelation,
|
||||
indexes: {
|
||||
unique: [number, number]
|
||||
}
|
||||
},
|
||||
series: {
|
||||
key: 'series_id';
|
||||
value: MangaUpdatesSeries;
|
||||
indexes: {
|
||||
series_id: number
|
||||
}
|
||||
},
|
||||
chapter: {
|
||||
key: 'id';
|
||||
value: MangaUpdatesChapter;
|
||||
indexes: {
|
||||
series_id: number
|
||||
}
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user