db structure abstraction; async collection; update/insert queries; model saving
This commit is contained in:
@@ -45,7 +45,11 @@ class Logging {
|
||||
const message = this.build_message(level, output)
|
||||
if ( this._level >= level || force ) {
|
||||
for ( const logger of this._loggers ) {
|
||||
logger.write(message)
|
||||
try {
|
||||
logger.write(message)
|
||||
} catch (e) {
|
||||
console.error('logging error', e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
5
lib/src/service/logging/global.ts
Normal file
5
lib/src/service/logging/global.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { make } from '../../../../di/src/global.ts'
|
||||
import { Logging } from './Logging.ts'
|
||||
|
||||
const logger = make(Logging)
|
||||
export { logger }
|
||||
@@ -1,11 +1,11 @@
|
||||
enum LoggingLevel {
|
||||
Silent = 0,
|
||||
Success = 1,
|
||||
Error = 1,
|
||||
Warning = 2,
|
||||
Info = 3,
|
||||
Debug = 4,
|
||||
Verbose = 5,
|
||||
Error = 2,
|
||||
Warning = 3,
|
||||
Info = 4,
|
||||
Debug = 5,
|
||||
Verbose = 6,
|
||||
}
|
||||
|
||||
const isLoggingLevel = (something: any): something is LoggingLevel => {
|
||||
|
||||
55
lib/src/service/utility/Utility.ts
Normal file
55
lib/src/service/utility/Utility.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { Service } from '../../../../di/src/decorator/Service.ts'
|
||||
import { Logging } from '../logging/Logging.ts'
|
||||
|
||||
@Service()
|
||||
export default class Utility {
|
||||
constructor(
|
||||
protected logger: Logging
|
||||
) {}
|
||||
|
||||
deep_copy<T>(target: T): T {
|
||||
if ( target === null )
|
||||
return target
|
||||
|
||||
if ( target instanceof Date )
|
||||
return new Date(target.getTime()) as any
|
||||
|
||||
if ( target instanceof Array ) {
|
||||
const copy = [] as any[]
|
||||
(target as any[]).forEach(item => { copy.push(item) })
|
||||
return copy.map((item: any) => this.deep_copy<any>(item)) as any
|
||||
}
|
||||
|
||||
if ( typeof target === 'object' && target !== {} ) {
|
||||
const copy = { ...(target as {[key: string]: any }) } as { [key: string]: any }
|
||||
Object.keys(copy).forEach(key => {
|
||||
copy[key] = this.deep_copy<any>(copy[key])
|
||||
})
|
||||
}
|
||||
|
||||
return target
|
||||
}
|
||||
|
||||
// TODO deep_merge
|
||||
|
||||
infer(val: string): any {
|
||||
if ( !val ) return undefined
|
||||
else if ( val.toLowerCase() === 'true' ) return true
|
||||
else if ( val.toLowerCase() === 'false' ) return false
|
||||
else if ( !isNaN(Number(val)) ) return Number(val)
|
||||
else if ( this.is_json(val) ) return JSON.parse(val)
|
||||
else if ( val.toLowerCase() === 'null' ) return null
|
||||
else if ( val.toLowerCase() === 'undefined' ) return undefined
|
||||
else return val
|
||||
}
|
||||
|
||||
is_json(val: string): boolean {
|
||||
try {
|
||||
JSON.parse(val)
|
||||
return true
|
||||
} catch(e) {
|
||||
this.logger.verbose(`Error encountered while checking is_json. Might be invalid. Error: ${e.message}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user