import { CollectionConfiguration } from './' ; import { IDGenerator } from '../exposed' ; import { defaultIDGenerator } from '../defaults' ; import { Logger } from '@mu-ts/logger' ; import { CollectionRegistry } from './CollectionRegistry' ; import { CollectionConfigurationOptions } from './CollectionConfigurationOptions' ; import { S3DB } from '../db' ; const collectionRegistry = CollectionRegistry . instance ( ) ; const collectionLogger = S3DB . getRootLogger ( ) . child ( '@collection' ) ; const idLogger = S3DB . getRootLogger ( ) . child ( '@id' ) ; collection param configuration export function collection ( configuration ? ) { return ( target ) : typeof Function | void => { const metadata = { ... new CollectionConfiguration ( ) , ... ( configuration || { } ) , } ; collectionLogger . info ( { configuration , metadata } , 'collection configuration' ) ; if ( ! metadata . name ) metadata . name = target . name . toLowerCase ( ) ; if ( ! metadata . id ) metadata . id = target . name . toLowerCase ( ) ; collectionRegistry . register ( metadata ) ; collectionLogger . info ( { metadata } , 'collection setting metadata to' ) ; return target ; } ; } param generator export function id ( generator = defaultIDGenerator ) { return ( target , propertyKey , descriptor ) : PropertyDescriptor => { const name = target . constructor . name . toLowerCase ( ) ; const collectionConfiguration = collectionRegistry . resolve ( name ) ; if ( collectionConfiguration ) { collectionConfiguration . keyName = propertyKey ; collectionRegistry . register ( collectionConfiguration ) ; } else { collectionRegistry . register ( { keyName : propertyKey , name , idGenerator : generator } ) ; } idLogger . debug ( { keyName : propertyKey , generator , name } , 'id decorator' ) ; return descriptor ; } ; }