Start offline support
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
a72fc72c83
commit
f6168b6b7c
@ -0,0 +1,65 @@
|
|||||||
|
import {Model} from './Model';
|
||||||
|
|
||||||
|
export interface IKeyValue {
|
||||||
|
id?: number;
|
||||||
|
key: string;
|
||||||
|
value: string;
|
||||||
|
json: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class KeyValue extends Model<IKeyValue> implements IKeyValue {
|
||||||
|
id?: number;
|
||||||
|
key: string;
|
||||||
|
value: string;
|
||||||
|
json: boolean;
|
||||||
|
|
||||||
|
public static getTableName() {
|
||||||
|
return 'keyValues';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static getSchema() {
|
||||||
|
return '++id, key, value, json';
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(key: string, value: string, json: boolean, id?: number) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
this.json = json;
|
||||||
|
if ( id ) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get data(): any {
|
||||||
|
if ( this.json ) {
|
||||||
|
return JSON.parse(this.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
set data(val: any) {
|
||||||
|
if ( typeof val === 'string' ) {
|
||||||
|
this.json = false;
|
||||||
|
this.value = val;
|
||||||
|
} else {
|
||||||
|
this.json = true;
|
||||||
|
this.value = JSON.stringify(val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getSaveRecord(): any {
|
||||||
|
return {
|
||||||
|
...(this.id ? { id: this.id } : {}),
|
||||||
|
key: this.key,
|
||||||
|
value: this.value,
|
||||||
|
json: this.json,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public getDatabase(): Dexie.Table<IKeyValue, number> {
|
||||||
|
return this.staticClass().dbService.table('keyValues') as Dexie.Table<IKeyValue, number>;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,171 @@
|
|||||||
|
import {Model} from './Model';
|
||||||
|
import Dexie from 'dexie';
|
||||||
|
|
||||||
|
export interface IMenuItem {
|
||||||
|
id?: number;
|
||||||
|
serverId: string;
|
||||||
|
name: string;
|
||||||
|
childIds?: string[];
|
||||||
|
noDelete?: boolean;
|
||||||
|
noChildren?: boolean;
|
||||||
|
virtual?: boolean;
|
||||||
|
type?: string;
|
||||||
|
shared?: boolean;
|
||||||
|
needsServerUpdate?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class MenuItem extends Model<IMenuItem> implements IMenuItem {
|
||||||
|
id?: number;
|
||||||
|
serverId: string;
|
||||||
|
name: string;
|
||||||
|
childIds?: string[];
|
||||||
|
noDelete?: boolean;
|
||||||
|
noChildren?: boolean;
|
||||||
|
virtual?: boolean;
|
||||||
|
type?: string;
|
||||||
|
shared?: boolean;
|
||||||
|
needsServerUpdate?: boolean;
|
||||||
|
|
||||||
|
public static getTableName() {
|
||||||
|
return 'menuItems';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static getSchema() {
|
||||||
|
return '++id, serverId, name, childIds, noDelete, noChildren, virtual, type, shared, needsServerUpdate';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static deflateTree(nodes: any[]): MenuItem[] {
|
||||||
|
let items = [];
|
||||||
|
|
||||||
|
for ( const node of nodes ) {
|
||||||
|
const childIds = node.children ? node.children.map(x => x.id) : [];
|
||||||
|
const item = new MenuItem(node.name, node.id, childIds, node.noDelete, node.noChildren, node.virtual, node.type, node.shared);
|
||||||
|
|
||||||
|
items.push(item);
|
||||||
|
if ( node.children ) {
|
||||||
|
items = items.concat(...this.deflateTree(node.children));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static inflateTree(items: MenuItem[]) {
|
||||||
|
const serverIdXItems: { [key: string]: MenuItem[] } = {};
|
||||||
|
|
||||||
|
for ( const item of items ) {
|
||||||
|
if ( !serverIdXItems[item.serverId] ) {
|
||||||
|
serverIdXItems[item.serverId] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
serverIdXItems[item.serverId].push(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
const inflateNode = (item, alreadyChildren = [], seen = []) => {
|
||||||
|
const node: any = item.getSaveRecord();
|
||||||
|
seen.push(item);
|
||||||
|
node.id = node.serverId;
|
||||||
|
|
||||||
|
node.children = [];
|
||||||
|
if ( item.childIds ) {
|
||||||
|
for ( const childId of item.childIds ) {
|
||||||
|
if ( serverIdXItems[childId] ) {
|
||||||
|
const children = serverIdXItems[childId].filter(x => {
|
||||||
|
if ( x.type !== 'page' && item.serverId !== x.serverId ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return x !== item && !alreadyChildren.includes(x) && !seen.includes(x);
|
||||||
|
});
|
||||||
|
|
||||||
|
node.children = node.children.concat(...children.map(x => inflateNode(x, children, seen)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const pageChildren = [];
|
||||||
|
const otherChildren = [];
|
||||||
|
for ( const child of node.children ) {
|
||||||
|
if ( child.type === 'page' ) {
|
||||||
|
pageChildren.push(child);
|
||||||
|
} else {
|
||||||
|
otherChildren.push(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
node.children = [...otherChildren, ...pageChildren];
|
||||||
|
return node;
|
||||||
|
};
|
||||||
|
|
||||||
|
const topLevelItems = items.filter(x => String(x.serverId) === '0');
|
||||||
|
return topLevelItems.map(x => inflateNode(x));
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
name: string,
|
||||||
|
serverId: string,
|
||||||
|
childIds?: string[],
|
||||||
|
noDelete?: boolean,
|
||||||
|
noChildren?: boolean,
|
||||||
|
virtual?: boolean,
|
||||||
|
type?: string,
|
||||||
|
shared?: boolean,
|
||||||
|
needsServerUpdate?: boolean,
|
||||||
|
id?: number
|
||||||
|
) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.name = name;
|
||||||
|
this.serverId = serverId;
|
||||||
|
if ( childIds ) {
|
||||||
|
this.childIds = childIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( typeof noDelete !== 'undefined' ) {
|
||||||
|
this.noDelete = noDelete;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( typeof noChildren !== 'undefined' ) {
|
||||||
|
this.noChildren = noChildren;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( typeof virtual !== 'undefined' ) {
|
||||||
|
this.virtual = virtual;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( type ) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( typeof shared !== 'undefined' ) {
|
||||||
|
this.shared = shared;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( typeof needsServerUpdate !== 'undefined' ) {
|
||||||
|
this.needsServerUpdate = needsServerUpdate;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( id ) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getDatabase(): Dexie.Table<IMenuItem, number> {
|
||||||
|
return this.staticClass().dbService.table('menuItems') as Dexie.Table<IMenuItem, number>;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getSaveRecord(): any {
|
||||||
|
return {
|
||||||
|
...(this.id ? { id: this.id } : {}),
|
||||||
|
serverId: this.serverId,
|
||||||
|
name: this.name,
|
||||||
|
...(typeof this.childIds !== 'undefined' ? { childIds: this.childIds } : {}),
|
||||||
|
...(typeof this.noDelete !== 'undefined' ? { noDelete: this.noDelete } : {}),
|
||||||
|
...(typeof this.noChildren !== 'undefined' ? { noChildren: this.noChildren } : {}),
|
||||||
|
...(typeof this.virtual !== 'undefined' ? { virtual: this.virtual } : {}),
|
||||||
|
...(typeof this.type !== 'undefined' ? { type: this.type } : {}),
|
||||||
|
...(typeof this.shared !== 'undefined' ? { shared: this.shared } : {}),
|
||||||
|
...(typeof this.needsServerUpdate !== 'undefined' ? { needsServerUpdate: this.needsServerUpdate } : {}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
import {Model} from './Model';
|
||||||
|
|
||||||
|
export interface IMigration {
|
||||||
|
id?: number;
|
||||||
|
uuid: string;
|
||||||
|
applied: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Migration extends Model<IMigration> implements IMigration {
|
||||||
|
id?: number;
|
||||||
|
uuid: string;
|
||||||
|
applied: boolean;
|
||||||
|
|
||||||
|
public static getTableName() {
|
||||||
|
return 'migrations';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static getSchema() {
|
||||||
|
return '++id, uuid, applied';
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(uuid: string, applied: boolean, id?: number) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.uuid = uuid;
|
||||||
|
this.applied = applied;
|
||||||
|
if ( id ) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getSaveRecord(): any {
|
||||||
|
return {
|
||||||
|
...(this.id ? { id: this.id } : {}),
|
||||||
|
uuid: this.uuid,
|
||||||
|
applied: this.applied,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public getDatabase(): Dexie.Table<IMigration, number> {
|
||||||
|
return this.staticClass().dbService.table('migrations') as Dexie.Table<IMigration, number>;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
import Dexie from 'dexie';
|
||||||
|
import {DatabaseService} from './database.service';
|
||||||
|
|
||||||
|
export abstract class Model<InterfaceType> {
|
||||||
|
public static dbService?: DatabaseService;
|
||||||
|
|
||||||
|
public id?: number;
|
||||||
|
|
||||||
|
public static getSchema(): string {
|
||||||
|
throw new TypeError('Child class must implement.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static getTableName(): string {
|
||||||
|
throw new TypeError('Child class must implement.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract getDatabase(): Dexie.Table<InterfaceType, number>;
|
||||||
|
public abstract getSaveRecord(): any;
|
||||||
|
|
||||||
|
public staticClass() {
|
||||||
|
return (this.constructor as typeof Model);
|
||||||
|
}
|
||||||
|
|
||||||
|
public exists() {
|
||||||
|
return !!this.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async save() {
|
||||||
|
this.id = await this.getDatabase().put(this.getSaveRecord());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import Dexie from 'dexie';
|
||||||
|
import {IMigration, Migration} from './Migration';
|
||||||
|
import {IMenuItem, MenuItem} from './MenuItem';
|
||||||
|
import {KeyValue, IKeyValue} from './KeyValue';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
|
export class DatabaseService extends Dexie {
|
||||||
|
protected static registeredModels = [Migration, MenuItem, KeyValue];
|
||||||
|
protected initialized = false;
|
||||||
|
|
||||||
|
migrations!: Dexie.Table<IMigration, number>;
|
||||||
|
menuItems!: Dexie.Table<IMenuItem, number>;
|
||||||
|
keyValues!: Dexie.Table<IKeyValue, 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(3).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);
|
||||||
|
|
||||||
|
// await new Promise(res => {
|
||||||
|
// setTimeout(() => {
|
||||||
|
// res();
|
||||||
|
// }, 1000);
|
||||||
|
// });
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue