import * as ReduxDB from "redux-db" ; import errors from "./errors" ; import { ExtendedTable } from "./types" ; export default class ExtendedTableModel < R extends ReduxDB . TableRecord > extends ReduxDB . TableModel < R > implements ExtendedTable < R > { getMany ( ids ) { return ids . map ( id => this . db . factory . newRecordModel ( ReduxDB . utils . asID ( id ) , this ) as R ) ; } getByFk ( fieldName , id ) { ReduxDB . utils . ensureParam ( "fieldName" , fieldName ) ; const pk = ReduxDB . utils . ensureID ( id ) ; const [ field ] = this . schema . fields . filter ( f => f . isForeignKey && f . name === fieldName ) ; if ( ! field ) throw new Error ( errors . fkUndefined ( this . schema . name , fieldName ) ) ; return this . db . factory . newRecordSetModel ( this , field , { id : pk } ) as ReduxDB . TableRecordSet < R > ; } getFieldValue ( id , field ) { const record = this . get ( id ) ; if ( record ) return record . value [ field as string ] ; else return undefined ; } }