@@ -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
@@ -206,7 +208,7 @@ export class Session {
206
208
* @type {Promise }
207
209
*/
208
210
this . initializing = this . call <
209
- [ QueryServerInformationResponse , QueryResponse ]
211
+ [ QueryServerInformationResponse , QuerySchemasResponse ]
210
212
> ( operations ) . then ( ( responses ) => {
211
213
this . serverInformation = responses [ 0 ] ;
212
214
this . schemas = responses [ 1 ] ;
@@ -222,13 +224,13 @@ export class Session {
222
224
*
223
225
* @return {Array|null } List of primary key attributes.
224
226
*/
225
- getPrimaryKeyAttributes ( entityType : string ) {
227
+ getPrimaryKeyAttributes ( entityType : string ) : string [ ] | null {
226
228
if ( ! this . schemas ) {
227
229
logger . warn ( "Schemas not available." ) ;
228
230
return null ;
229
231
}
230
- const schema = this . schemas . find ( ( item : any ) => item . id === entityType ) ;
231
- if ( ! schema || ! schema . primary_key ) {
232
+ const schema = this . schemas . find ( ( item ) => item . id === entityType ) ;
233
+ if ( ! schema || ! schema . primary_key || ! schema . primary_key . length ) {
232
234
logger . warn ( "Primary key could not be found for: " , entityType ) ;
233
235
return null ;
234
236
}
@@ -613,7 +615,7 @@ export class Session {
613
615
"Ensuring entity with data using identifying keys: " ,
614
616
entityType ,
615
617
data ,
616
- identifyingKeys
618
+ keys
617
619
) ;
618
620
619
621
if ( ! keys . length ) {
@@ -629,6 +631,9 @@ export class Session {
629
631
}
630
632
631
633
const primaryKeys = this . getPrimaryKeyAttributes ( entityType ) ;
634
+ if ( primaryKeys === null || primaryKeys . length === 0 ) {
635
+ throw new Error ( `Primary key could not be found for: ${ entityType } ` ) ;
636
+ }
632
637
let expression = `select ${ primaryKeys . join (
633
638
", "
634
639
) } from ${ entityType } where`;
@@ -677,7 +682,7 @@ export class Session {
677
682
entityType ,
678
683
primaryKeys . map ( ( key : string ) => updateEntity [ key ] ) ,
679
684
Object . keys ( data ) . reduce < T > ( ( accumulator , key : keyof T ) => {
680
- if ( primaryKeys . indexOf ( key ) === - 1 ) {
685
+ if ( primaryKeys . indexOf ( key . toString ( ) ) === - 1 ) {
681
686
accumulator [ key ] = data [ key ] ;
682
687
}
683
688
return accumulator ;
@@ -694,14 +699,9 @@ export class Session {
694
699
* @param {string } schemaId Id of schema model, e.g. `AssetVersion`.
695
700
* @return {Object|null } Schema definition
696
701
*/
697
- getSchema ( schemaId : string ) : Data | null {
698
- for ( const index in this . schemas ) {
699
- if ( this . schemas [ index ] . id === schemaId ) {
700
- return this . schemas [ index ] ;
701
- }
702
- }
703
-
704
- return null ;
702
+ getSchema ( schemaId : string ) : Schema | null {
703
+ const schema = this . schemas ?. find ( ( s ) => s . id === schemaId ) ;
704
+ return schema ?? null ;
705
705
}
706
706
707
707
/**
0 commit comments