import { AUTO_GENERATE_ATTRIBUTE_STRATEGY , EntityTarget } from '@typedorm/common' ; import { Table } from '../table' ; import { AttributeOptionsUniqueType } from '../decorators/attribute.decorator' ; import { ScalarType } from '../helpers/scalar-type' ; import { IndexOptionsWithAlias } from '../index-options' ; export const IsAutoGenerateAttributeRawMetadataOptions = ( attr ) : attr is AutoGenerateAttributeRawMetadataOptions => ! ! attr . strategy ; export type PrimaryKey = SimplePrimaryKey | CompositePrimaryKey ; export type SimplePrimaryKey = { partitionKey : string ; } ; export type CompositePrimaryKey = { partitionKey : string ; sortKey : string ; } ; export type Indexes < Entity = any > = { [ key ] : IndexOptionsWithAlias < Entity > ; } ; export interface EntityRawMetadataOptions < Entity = any > { name : string ; target : EntityTarget < Entity > ; primaryKey : PrimaryKey ; indexes ? : Indexes < Entity > ; table ? : Table ; } interface BaseAttributeRawMetadataOptions { name : string ; type : any ; hidden ? : boolean ; } export interface AttributeRawMetadataOptions extends BaseAttributeRawMetadataOptions { unique ? : AttributeOptionsUniqueType ; default ? : ScalarType | ( ( ) => ScalarType ) ; } export interface AutoGenerateAttributeRawMetadataOptions extends BaseAttributeRawMetadataOptions { strategy : AUTO_GENERATE_ATTRIBUTE_STRATEGY ; autoUpdate ? : boolean ; } export class MetadataStorage { private _entities : Map < EntityTarget < any > , EntityRawMetadataOptions > ; private _attributes : Map < EntityTarget < any > , Map < string , AttributeRawMetadataOptions | AutoGenerateAttributeRawMetadataOptions > > ; constructor ( ) { this . _entities = new Map ( ) ; this . _attributes = new Map ( ) ; } param name getEntityByName ( name ) { return this . entities . find ( en => en . name === name ) ; } hasKnownEntity < Entity > ( entityClass ) { return this . _entities . has ( entityClass ) ; } getRawAttributesForEntity < Entity > ( entityClass ) { const attributes = this . _attributes . get ( entityClass ) ?. values ( ) ; if ( ! attributes ) { return [ ] ; } return Array . from ( attributes ) ; } getRawEntityByTarget < Entity > ( entityClass ) { const entity = this . _entities . get ( entityClass ) ; if ( ! entity ) { throw new Error ( `No entity with name "${ entityClass . name }" could be resolved, 
      make sure they have been declared at the connection creation time.` ) ; } return entity ; } addRawAttribute < Entity > ( entityClass , attribute ) { let attributesForEntity = this . _attributes . get ( entityClass ) ; if ( ! attributesForEntity ) { attributesForEntity = new Map ( ) ; } attributesForEntity . set ( attribute . name , attribute ) ; this . _attributes . set ( entityClass , attributesForEntity ) ; } addRawEntity ( entity ) { this . _entities . set ( entity . target , entity ) ; } get entities ( ) { return Array . from ( this . _entities . values ( ) ) ; } get attributes ( ) { return Array . from ( this . _attributes . values ( ) ) . map ( attr => Array . from ( attr . values ( ) ) ) ; } }