import { RecordModel } from './models/record-model' ; import { ListModel } from './models/list-model' ; import { BaseModel } from './models/base-model' ; import { Record , RecordRawData , RecordDirector } from './datatypes/record' ; import { Observable , Subject , ReplaySubject } from 'rxjs' ; import { QuerySet } from './query/queryset' ; import { Message } from './message' ; import { utilRecordModelFromRawData } from './utils' ; import { Query , DeleteQuery , UpdateQuery , InsertQuery } from './query/query' ; import { ListRowRawData } from './datatypes/list-row' ; import { RowDoesNotExistError , MultipleRowsReturnedError } from './exceptions' ; import { BaseField } from './fields/base' ; import { RecordLookupData } from './datatypes/record' ; export class RecordCreatedNotification { constructor ( public lookupData , public initiatorId ? ) { } } export class RecordLookupChangedNotification { constructor ( public oldLookupData , public newLookupData , public initiatorId ? ) { } } export class RecordSavedNotification { constructor ( public lookupData , public initiatorId ? ) { } } export class RecordDeletedNotification { constructor ( public lookupData , public initiatorId ? ) { } } export type RecordNotification = RecordCreatedNotification | RecordLookupChangedNotification | RecordSavedNotification | RecordDeletedNotification ; export interface SaveRecordSucces { query : Query ; fields : string [ ] ; } export function instanceOf_SaveRecordSucces ( object ) { return object && typeof object == "object" && 'fields' in object && 'query' in object ; } export interface PagedRowsRawResult { rows : ListRowRawData [ ] ; total : number ; lastRow : boolean ; } export interface GetPagedRowsParams { fromRow : number ; toRow : number ; query : Query ; } export abstract class BaseBackend { defaultRecordClass : typeof Record = Record ; defaultRecordDirector : typeof RecordDirector = RecordDirector ; modelNameField : string = '_model' ; messages = new Subject < Message > ( ) ; recordCreatedNotifications = new Subject < RecordCreatedNotification > ( ) ; recordLookupChangedNotifications = new Subject < RecordLookupChangedNotification > ( ) ; recordSavedNotifications = new Subject < RecordSavedNotification > ( ) ; recordDeletedNotifications = new Subject < RecordDeletedNotification > ( ) ; recordModels : Map < string , RecordModel > = new Map ( ) ; listModels : Map < string , ListModel > = new Map ( ) ; bootstrapped = new ReplaySubject < null > ( 1 ) ; constructor ( ) { } bootstrap ( ) { this . bootstrapped . next ( null ) ; this . bootstrapped . complete ( ) ; return Observable . of ( null ) ; } open ( ) { } close ( ) { } getRecordModel ( name ) { var model = this . recordModels . get ( name ) ; if ( ! model ) throw new Error ( 'Model "' + name + '" not defined' ) ; return model ; } getListModel ( name ) { return this . listModels . get ( name ) ; } getRecordClass ( model ) { return this . defaultRecordClass ; } getRecordDirector ( model ) { return this . defaultRecordDirector ; } getQueryset ( model ) { return new QuerySet ( model ) ; } constructRecord ( model , isNew , data = { } ) { var recordClass = this . getRecordClass ( model ) ; return new recordClass ( model , data , isNew ) ; } getRows ( query , fields ? ) { throw new Error ( 'getRows must be specified in the backend class.' ) ; } getRow ( query , fields ? ) { return this . getRows ( query , fields ) . map ( ( rawRows ) => { if ( rawRows . length == 0 ) { throw new RowDoesNotExistError ( ) ; } else if ( rawRows . length > 1 ) { throw new MultipleRowsReturnedError ( ) ; } return rawRows [ 0 ] ; } ) ; } saveRecord ( query , initiatorId , commit = true , returnNewData = true ) { throw new Error ( 'saveRecord must be specified in the backend class.' ) ; } ; deleteRecord ( query , initiatorId ) { throw new Error ( 'deleteRecord must be specified in the backend class.' ) ; } ; lockRecord ( record ) { return Observable . of ( true ) ; } ; unlockRecord ( record ) { return Observable . of ( true ) ; } ; isRecordLocked ( record ) { return Observable . of ( false ) ; } ; hasRecordEditPermission ( record ) { return Observable . of ( false ) ; } ; hasRecordDeletePermission ( record ) { return Observable . of ( false ) ; } ; hasCreateRecordPermission ( model ) { return Observable . of ( true ) ; } ; representRecord ( record ) { return undefined ; } } export class Backend extends BaseBackend { }