@@ -27,8 +27,10 @@ import type {
27
27
MutationOptions ,
28
28
QueryOptions ,
29
29
QueryResponse ,
30
+ QuerySchemasResponse ,
30
31
QueryServerInformationResponse ,
31
32
ResponseError ,
33
+ Schema ,
32
34
SearchOptions ,
33
35
SearchResponse ,
34
36
SessionOptions ,
@@ -75,7 +77,7 @@ export class Session {
75
77
initialized : boolean ;
76
78
initializing : Promise < Session > ;
77
79
serverInformation ?: Data ;
78
- schemas ?: Data ;
80
+ schemas ?: Schema [ ] ;
79
81
serverVersion ?: string ;
80
82
additionalHeaders : Data ;
81
83
@@ -214,7 +216,7 @@ export class Session {
214
216
* @type {Promise }
215
217
*/
216
218
this . initializing = this . call <
217
- [ QueryServerInformationResponse , QueryResponse ]
219
+ [ QueryServerInformationResponse , QuerySchemasResponse ]
218
220
> ( operations ) . then ( ( responses ) => {
219
221
this . serverInformation = responses [ 0 ] ;
220
222
this . schemas = responses [ 1 ] ;
@@ -230,13 +232,13 @@ export class Session {
230
232
*
231
233
* @return {Array|null } List of primary key attributes.
232
234
*/
233
- getPrimaryKeyAttributes ( entityType : string ) {
235
+ getPrimaryKeyAttributes ( entityType : string ) : string [ ] | null {
234
236
if ( ! this . schemas ) {
235
237
logger . warn ( "Schemas not available." ) ;
236
238
return null ;
237
239
}
238
- const schema = this . schemas . find ( ( item : any ) => item . id === entityType ) ;
239
- if ( ! schema || ! schema . primary_key ) {
240
+ const schema = this . schemas . find ( ( item ) => item . id === entityType ) ;
241
+ if ( ! schema || ! schema . primary_key || ! schema . primary_key . length ) {
240
242
logger . warn ( "Primary key could not be found for: " , entityType ) ;
241
243
return null ;
242
244
}
@@ -641,7 +643,7 @@ export class Session {
641
643
"Ensuring entity with data using identifying keys: " ,
642
644
entityType ,
643
645
data ,
644
- identifyingKeys
646
+ keys
645
647
) ;
646
648
647
649
if ( ! keys . length ) {
@@ -657,6 +659,9 @@ export class Session {
657
659
}
658
660
659
661
const primaryKeys = this . getPrimaryKeyAttributes ( entityType ) ;
662
+ if ( primaryKeys === null || primaryKeys . length === 0 ) {
663
+ throw new Error ( `Primary key could not be found for: ${ entityType } ` ) ;
664
+ }
660
665
let expression = `select ${ primaryKeys . join (
661
666
", "
662
667
) } from ${ entityType } where`;
@@ -705,7 +710,7 @@ export class Session {
705
710
entityType ,
706
711
primaryKeys . map ( ( key : string ) => updateEntity [ key ] ) ,
707
712
Object . keys ( data ) . reduce < T > ( ( accumulator , key : keyof T ) => {
708
- if ( primaryKeys . indexOf ( key ) === - 1 ) {
713
+ if ( primaryKeys . indexOf ( key . toString ( ) ) === - 1 ) {
709
714
accumulator [ key ] = data [ key ] ;
710
715
}
711
716
return accumulator ;
@@ -722,14 +727,9 @@ export class Session {
722
727
* @param {string } schemaId Id of schema model, e.g. `AssetVersion`.
723
728
* @return {Object|null } Schema definition
724
729
*/
725
- getSchema ( schemaId : string ) : Data | null {
726
- for ( const index in this . schemas ) {
727
- if ( this . schemas [ index ] . id === schemaId ) {
728
- return this . schemas [ index ] ;
729
- }
730
- }
731
-
732
- return null ;
730
+ getSchema ( schemaId : string ) : Schema | null {
731
+ const schema = this . schemas ?. find ( ( s ) => s . id === schemaId ) ;
732
+ return schema ?? null ;
733
733
}
734
734
735
735
/**
0 commit comments