You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.2 KiB
102 lines
3.2 KiB
import { Injectable } from '@angular/core';
|
|
import Dexie from 'dexie';
|
|
import {IMigration, Migration} from './Migration';
|
|
import {IMenuItem, MenuItem} from './MenuItem';
|
|
import {KeyValue, IKeyValue} from './KeyValue';
|
|
import {Codium, ICodium} from './Codium';
|
|
import {Database, IDatabase} from './Database';
|
|
import {DatabaseColumn, IDatabaseColumn} from './DatabaseColumn';
|
|
import {DatabaseEntry, IDatabaseEntry} from './DatabaseEntry';
|
|
import {FileGroup, IFileGroup} from './FileGroup';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class DatabaseService extends Dexie {
|
|
protected static registeredModels = [Migration, MenuItem, KeyValue, Codium, Database, DatabaseColumn, DatabaseEntry, FileGroup];
|
|
protected initialized = false;
|
|
|
|
migrations!: Dexie.Table<IMigration, number>;
|
|
menuItems!: Dexie.Table<IMenuItem, number>;
|
|
keyValues!: Dexie.Table<IKeyValue, number>;
|
|
codiums!: Dexie.Table<ICodium, number>;
|
|
databases!: Dexie.Table<IDatabase, number>;
|
|
databaseColumns!: Dexie.Table<IDatabaseColumn, number>;
|
|
databaseEntries!: Dexie.Table<IDatabaseEntry, number>;
|
|
fileGroups!: Dexie.Table<IFileGroup, number>;
|
|
|
|
constructor(
|
|
) {
|
|
super('NodedLocalDatabase');
|
|
}
|
|
|
|
public async getKeyValue(key: string): Promise<KeyValue> {
|
|
const matches = await this.keyValues.where({ key }).toArray();
|
|
|
|
if ( matches.length > 0 ) {
|
|
return matches[0] as KeyValue;
|
|
}
|
|
|
|
return new KeyValue(key, '', false);
|
|
}
|
|
|
|
public async createSchemata() {
|
|
if ( this.initialized ) {
|
|
return;
|
|
}
|
|
this.initialized = true;
|
|
|
|
console.log('db', this);
|
|
|
|
const staticClass = this.constructor as typeof DatabaseService;
|
|
const schema: any = {};
|
|
|
|
for ( const ModelClass of staticClass.registeredModels ) {
|
|
ModelClass.dbService = this;
|
|
schema[ModelClass.getTableName()] = ModelClass.getSchema();
|
|
}
|
|
|
|
await this.version(11).stores(schema);
|
|
await this.open();
|
|
|
|
this.migrations = this.table('migrations');
|
|
this.migrations.mapToClass(Migration);
|
|
|
|
this.menuItems = this.table('menuItems');
|
|
this.menuItems.mapToClass(MenuItem);
|
|
|
|
this.keyValues = this.table('keyValues');
|
|
this.keyValues.mapToClass(KeyValue);
|
|
|
|
this.codiums = this.table('codiums');
|
|
this.codiums.mapToClass(Codium);
|
|
|
|
this.databases = this.table('databases');
|
|
this.databases.mapToClass(Database);
|
|
|
|
this.databaseColumns = this.table('databaseColumns');
|
|
this.databaseColumns.mapToClass(DatabaseColumn);
|
|
|
|
this.databaseEntries = this.table('databaseEntries');
|
|
this.databaseEntries.mapToClass(DatabaseEntry);
|
|
|
|
this.fileGroups = this.table('fileGroups');
|
|
this.fileGroups.mapToClass(FileGroup);
|
|
}
|
|
|
|
public async purge() {
|
|
console.warn('Purging all local data!');
|
|
|
|
await Promise.all([
|
|
this.migrations.clear(),
|
|
this.menuItems.clear(),
|
|
this.keyValues.clear(),
|
|
this.codiums.clear(),
|
|
this.databases.clear(),
|
|
this.databaseColumns.clear(),
|
|
this.databaseEntries.clear(),
|
|
this.fileGroups.clear(),
|
|
]);
|
|
}
|
|
}
|