import * as ts from '@terascope/utils' ; import { FieldType , xLuceneTypeConfig , xLuceneVariables } from '@terascope/types' ; import { isArray } from './field-validator' ; import { DocumentMatcher } from '../document-matcher' ; import { Repository , RecordInput , InputType } from '../interfaces' ; import { isString } from '../validations/field-validator' ; export const repository = { required : { fn : required , config : { fields : { type : FieldType . String , array : true } } , primary_input_type : InputType . Object , } , select : { fn : select , config : { query : { type : FieldType . String , } , type_config : { type : FieldType . Object } , variables : { type : FieldType . Object } } , primary_input_type : InputType . Object , } , reject : { fn : reject , config : { query : { type : FieldType . String , } , type_config : { type : FieldType . Object } , variables : { type : FieldType . Object } } , primary_input_type : InputType . Object , } , } ; function _filterBy ( fn , input , args ? ) { const sanitized = input . filter ( ( data ) => ts . isNotNil ( data ) && ts . isObjectEntity ( data ) ) ; if ( sanitized . length === 0 ) return [ ] ; return sanitized . filter ( ( data ) => fn ( data , args ) ) ; } example param { AnyObject } obj param { { fields : string [ ] } } returns export function required ( input , _parentContext , args ) { if ( ts . isNil ( input ) ) return null ; if ( ! args ?. fields || ! isArray ( args . fields ) || ! isString ( args . fields ) ) { throw new Error ( 'Parameter fields must be provided and be an array of strings' ) ; } if ( isArray ( input ) ) { return _filterBy ( _checkKeys , input , args . fields ) ; } return _checkKeys ( input , args . fields ) ; } function _checkKeys ( data , fields ) { try { const keys = Object . keys ( data ) ; if ( fields . every ( ( rField ) => keys . includes ( rField ) ) ) return data ; return null ; } catch ( _err ) { return null ; } } interface DMOptions { query : string ; type_config ? : xLuceneTypeConfig ; variables ? : xLuceneVariables ; } example param { AnyObject } obj param { { query : string , type_config : xLuceneTypeConfig , variables : xLuceneVariables } } args returns export function select ( input , _parentContext , args ) { const matcher = _validateMatcher ( input , args ) ; if ( ! matcher ) return null ; if ( isArray ( input ) ) { const fn = ( data ) => ts . isObjectEntity ( data ) && matcher . match ( data ) ; return _filterBy ( fn , input ) ; } if ( ts . isObjectEntity ( input ) && matcher . match ( input ) ) return input ; return null ; } function _validateMatcher ( input , args ) { if ( ts . isNil ( input ) ) return null ; if ( ts . isNil ( args ) || ! ts . isObjectEntity ( args ) ) { throw new Error ( `Parameter args must be provided and be an object, got ${ ts . getTypeOf ( args ) }` ) ; } const { query = '*' , type_config , variables } = args ; if ( ! isString ( query ) ) throw new Error ( `Invalid query, must be a string, got ${ ts . getTypeOf ( args ) }` ) ; if ( ( type_config && ! ts . isObjectEntity ( type_config ) ) ) throw new Error ( `Invalid argument typeConfig must be an object got ${ ts . getTypeOf ( args ) }` ) ; if ( ( variables && ! ts . isObjectEntity ( variables ) ) ) throw new Error ( `Invalid argument variables must be an object got ${ ts . getTypeOf ( args ) }` ) ; return new DocumentMatcher ( query , { type_config , variables } ) ; } example param { AnyObject } obj param { DMOptions } args returns export function reject ( input , _parentContext , args ) { const matcher = _validateMatcher ( input , args ) ; if ( ! matcher ) return null ; if ( isArray ( input ) ) { const fn = ( data ) => ts . isObjectEntity ( data ) && ! matcher . match ( data ) ; return _filterBy ( fn , input ) ; } if ( ts . isObjectEntity ( input ) && ! matcher . match ( input ) ) return input ; return null ; }