db structure abstraction; async collection; update/insert queries; model saving
This commit is contained in:
43
lib/src/lifecycle/Application.ts
Normal file
43
lib/src/lifecycle/Application.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { Service } from '../../../di/src/decorator/Service.ts'
|
||||
import { Logging } from '../service/logging/Logging.ts'
|
||||
import Unit from './Unit.ts'
|
||||
import { container, make } from '../../../di/src/global.ts'
|
||||
import { DependencyKey } from '../../../di/src/type/DependencyKey.ts'
|
||||
import RunLevelErrorHandler from '../error/RunLevelErrorHandler.ts'
|
||||
|
||||
@Service()
|
||||
export default class Application {
|
||||
constructor(
|
||||
protected logger: Logging,
|
||||
protected rleh: RunLevelErrorHandler,
|
||||
protected units: Unit[],
|
||||
) {}
|
||||
|
||||
make(token: DependencyKey) {
|
||||
return make(token)
|
||||
}
|
||||
|
||||
container() {
|
||||
return container
|
||||
}
|
||||
|
||||
async up() {
|
||||
|
||||
}
|
||||
|
||||
async down() {
|
||||
|
||||
}
|
||||
|
||||
async run() {
|
||||
try {
|
||||
this.logger.info('Starting Daton...')
|
||||
} catch (e) {
|
||||
await this.app_error(e)
|
||||
}
|
||||
}
|
||||
|
||||
async app_error(e: Error) {
|
||||
this.rleh.handle(e)
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,42 @@
|
||||
import { STATUS_STOPPED, isStatus } from '../const/status.ts'
|
||||
import { Status, isStatus } from '../const/status.ts'
|
||||
import { Unit } from './decorators.ts'
|
||||
import { Collection } from '../collection/Collection.ts'
|
||||
import {container, make} from '../../../di/src/global.ts'
|
||||
import {DependencyKey} from "../../../di/src/type/DependencyKey.ts";
|
||||
import Instantiable, {isInstantiable} from "../../../di/src/type/Instantiable.ts";
|
||||
|
||||
const isLifecycleUnit = (something: any): something is (typeof LifecycleUnit) => {
|
||||
return isInstantiable(something) && something.prototype instanceof LifecycleUnit
|
||||
}
|
||||
|
||||
export default abstract class LifecycleUnit {
|
||||
private _status = STATUS_STOPPED
|
||||
private _status = Status.Stopped
|
||||
|
||||
public get status() {
|
||||
return this._status
|
||||
}
|
||||
|
||||
public set status(status) {
|
||||
if ( !isStatus(status) )
|
||||
throw new TypeError('Invalid unit status: '+status.description)
|
||||
|
||||
this._status = status
|
||||
}
|
||||
|
||||
public async up(): Promise<void> {};
|
||||
public async down(): Promise<void> {};
|
||||
|
||||
public static get_dependencies(): Collection<typeof LifecycleUnit> {
|
||||
if ( isInstantiable(this) ) {
|
||||
const deps = new Collection<typeof LifecycleUnit>()
|
||||
for ( const dependency of container.get_dependencies(this) ) {
|
||||
if ( isLifecycleUnit(dependency) )
|
||||
deps.push(dependency)
|
||||
}
|
||||
|
||||
return deps
|
||||
}
|
||||
return new Collection<typeof LifecycleUnit>()
|
||||
}
|
||||
|
||||
protected make<T>(target: Instantiable<T>|DependencyKey, ...parameters: any[]) {
|
||||
return make(target, ...parameters)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user