2020-10-21 18:54:18 +00:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import Dexie from 'dexie';
|
|
|
|
import {IMigration, Migration} from './Migration';
|
|
|
|
import {IMenuItem, MenuItem} from './MenuItem';
|
|
|
|
import {KeyValue, IKeyValue} from './KeyValue';
|
2020-10-22 02:12:04 +00:00
|
|
|
import {Codium, ICodium} from './Codium';
|
2020-10-22 03:40:20 +00:00
|
|
|
import {Database, IDatabase} from './Database';
|
|
|
|
import {DatabaseColumn, IDatabaseColumn} from './DatabaseColumn';
|
|
|
|
import {DatabaseEntry, IDatabaseEntry} from './DatabaseEntry';
|
2020-10-22 04:12:57 +00:00
|
|
|
import {FileGroup, IFileGroup} from './FileGroup';
|
2020-10-25 23:42:27 +00:00
|
|
|
import {Page, IPage} from './Page';
|
|
|
|
import {PageNode, IPageNode} from './PageNode';
|
2020-10-21 18:54:18 +00:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class DatabaseService extends Dexie {
|
2020-10-25 23:42:27 +00:00
|
|
|
protected static registeredModels = [
|
|
|
|
Migration, MenuItem, KeyValue, Codium, Database, DatabaseColumn, DatabaseEntry, FileGroup, Page, PageNode
|
|
|
|
];
|
|
|
|
|
2020-10-21 18:54:18 +00:00
|
|
|
protected initialized = false;
|
|
|
|
|
|
|
|
migrations!: Dexie.Table<IMigration, number>;
|
|
|
|
menuItems!: Dexie.Table<IMenuItem, number>;
|
|
|
|
keyValues!: Dexie.Table<IKeyValue, number>;
|
2020-10-22 02:12:04 +00:00
|
|
|
codiums!: Dexie.Table<ICodium, number>;
|
2020-10-22 03:40:20 +00:00
|
|
|
databases!: Dexie.Table<IDatabase, number>;
|
|
|
|
databaseColumns!: Dexie.Table<IDatabaseColumn, number>;
|
|
|
|
databaseEntries!: Dexie.Table<IDatabaseEntry, number>;
|
2020-10-22 04:12:57 +00:00
|
|
|
fileGroups!: Dexie.Table<IFileGroup, number>;
|
2020-10-25 23:42:27 +00:00
|
|
|
pages!: Dexie.Table<IPage, number>;
|
|
|
|
pageNodes!: Dexie.Table<IPageNode, number>;
|
2020-10-21 18:54:18 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-11-13 17:27:34 +00:00
|
|
|
await this.version(17).stores(schema);
|
2020-10-21 18:54:18 +00:00
|
|
|
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);
|
|
|
|
|
2020-10-22 02:12:04 +00:00
|
|
|
this.codiums = this.table('codiums');
|
|
|
|
this.codiums.mapToClass(Codium);
|
2020-10-22 03:40:20 +00:00
|
|
|
|
|
|
|
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);
|
2020-10-22 04:12:57 +00:00
|
|
|
|
|
|
|
this.fileGroups = this.table('fileGroups');
|
|
|
|
this.fileGroups.mapToClass(FileGroup);
|
2020-10-25 23:42:27 +00:00
|
|
|
|
|
|
|
this.pages = this.table('pages');
|
|
|
|
this.pages.mapToClass(Page);
|
|
|
|
|
|
|
|
this.pageNodes = this.table('pageNodes');
|
|
|
|
this.pageNodes.mapToClass(PageNode);
|
2020-10-22 04:12:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 04:48:46 +00:00
|
|
|
/** Return the local database records that need to be synced up with the server. */
|
|
|
|
public async getDirtyRecords() {
|
|
|
|
const codiums = await this.codiums.where({ needsServerUpdate: 1 }).toArray() as Codium[];
|
|
|
|
const databases = await this.databases.where({ needsServerUpdate: 1 }).toArray() as Database[];
|
|
|
|
const databaseColumns = await this.databaseColumns.where({ needsServerUpdate: 1 }).toArray() as DatabaseColumn[];
|
|
|
|
const databaseEntries = await this.databaseEntries.where({ needsServerUpdate: 1 }).toArray() as DatabaseEntry[];
|
|
|
|
const fileGroups = await this.fileGroups.where({ needsServerUpdate: 1 }).toArray() as FileGroup[];
|
|
|
|
const pages = await this.pages.where({ needsServerUpdate: 1 }).toArray() as Page[];
|
|
|
|
const pageNodes = await this.pageNodes.where({ needsServerUpdate: 1 }).toArray() as PageNode[];
|
|
|
|
|
|
|
|
return {
|
|
|
|
codiums: codiums.map(x => x.getSaveRecord()),
|
|
|
|
databases: databases.map(x => x.getSaveRecord()),
|
|
|
|
databaseColumns: databaseColumns.map(x => x.getSaveRecord()),
|
|
|
|
databaseEntries: databaseEntries.map(x => x.getSaveRecord()),
|
|
|
|
fileGroups: fileGroups.map(x => x.getSaveRecord()),
|
|
|
|
pages: pages.map(x => x.getSaveRecord()),
|
|
|
|
pageNodes: pageNodes.map(x => x.getSaveRecord()),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-22 04:12:57 +00:00
|
|
|
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(),
|
2020-10-25 23:42:27 +00:00
|
|
|
this.pages.clear(),
|
|
|
|
this.pageNodes.clear(),
|
2020-10-22 04:12:57 +00:00
|
|
|
]);
|
2020-10-21 18:54:18 +00:00
|
|
|
}
|
|
|
|
}
|