17
17
import * as arrify from 'arrify' ;
18
18
import * as extend from 'extend' ;
19
19
import * as is from 'is' ;
20
+ import { Query , QueryProto } from './query' ;
20
21
21
22
// tslint:disable-next-line no-namespace
22
23
export namespace entity {
24
+ export interface InvalidKeyErrorOptions {
25
+ code : string ;
26
+ }
27
+
23
28
export class InvalidKeyError extends Error {
24
- constructor ( opts ) {
29
+ constructor ( opts : InvalidKeyErrorOptions ) {
25
30
const errorMessages = {
26
31
MISSING_KIND : 'A key should contain at least a kind.' ,
27
32
MISSING_ANCESTOR_ID : 'Ancestor keys require an id or name.' ,
28
- } ;
33
+ } as { [ index : string ] : string } ;
29
34
super ( errorMessages [ opts . code ] ) ;
30
35
this . name = 'InvalidKey' ;
31
36
}
@@ -69,7 +74,7 @@ export namespace entity {
69
74
* @param {* } value
70
75
* @returns {boolean }
71
76
*/
72
- export function isDsDouble ( value ) {
77
+ export function isDsDouble ( value ?: { } ) {
73
78
return value instanceof entity . Double ;
74
79
}
75
80
@@ -102,7 +107,7 @@ export namespace entity {
102
107
* @param {* } value
103
108
* @returns {boolean }
104
109
*/
105
- export function isDsInt ( value ) {
110
+ export function isDsInt ( value ?: { } ) {
106
111
return value instanceof entity . Int ;
107
112
}
108
113
@@ -151,10 +156,15 @@ export namespace entity {
151
156
* @param {* } value
152
157
* @returns {boolean }
153
158
*/
154
- export function isDsGeoPoint ( value ) {
159
+ export function isDsGeoPoint ( value ?: { } ) {
155
160
return value instanceof entity . GeoPoint ;
156
161
}
157
162
163
+ export interface KeyOptions {
164
+ namespace ?: string ;
165
+ path : Array < string | number > ;
166
+ }
167
+
158
168
/**
159
169
* Build a Datastore Key object.
160
170
*
@@ -172,14 +182,14 @@ export namespace entity {
172
182
* });
173
183
*/
174
184
export class Key {
175
- namespace : string ;
185
+ namespace ? : string ;
176
186
id ?: string ;
177
187
name ?: string ;
178
188
kind : string ;
179
189
parent ?: Key ;
180
- path ;
190
+ path ! : Array < string | number > ;
181
191
182
- constructor ( options ) {
192
+ constructor ( options : KeyOptions ) {
183
193
/**
184
194
* @name Key#namespace
185
195
* @type {string }
@@ -192,13 +202,13 @@ export namespace entity {
192
202
const identifier = options . path . pop ( ) ;
193
203
194
204
if ( is . number ( identifier ) || isDsInt ( identifier ) ) {
195
- this . id = identifier . value || identifier ;
205
+ this . id = ( ( identifier as { } as Int ) . value || identifier ) as string ;
196
206
} else if ( is . string ( identifier ) ) {
197
- this . name = identifier ;
207
+ this . name = identifier as string ;
198
208
}
199
209
}
200
210
201
- this . kind = options . path . pop ( ) ;
211
+ this . kind = options . path . pop ( ) as string ;
202
212
203
213
if ( options . path . length > 0 ) {
204
214
this . parent = new Key ( options ) ;
@@ -229,7 +239,7 @@ export namespace entity {
229
239
* @param {* } value
230
240
* @returns {boolean }
231
241
*/
232
- export function isDsKey ( value ) {
242
+ export function isDsKey ( value ?: { } ) {
233
243
return value instanceof entity . Key ;
234
244
}
235
245
@@ -256,8 +266,8 @@ export namespace entity {
256
266
* });
257
267
* // <Buffer 68 65 6c 6c 6f>
258
268
*/
259
- export function decodeValueProto ( valueProto ) {
260
- const valueType = valueProto . valueType ;
269
+ export function decodeValueProto ( valueProto : ValueProto ) {
270
+ const valueType = valueProto . valueType ! ;
261
271
const value = valueProto [ valueType ] ;
262
272
263
273
switch ( valueType ) {
@@ -311,9 +321,9 @@ export namespace entity {
311
321
* // stringValue: 'Hi'
312
322
* // }
313
323
*/
314
- export function encodeValue ( value ) {
315
- // tslint:disable-next-line no- any
316
- const valueProto : any = { } ;
324
+ // tslint:disable-next-line no-any
325
+ export function encodeValue ( value ?: any ) : ValueProto {
326
+ const valueProto : ValueProto = { } ;
317
327
318
328
if ( is . boolean ( value ) ) {
319
329
valueProto . booleanValue = value ;
@@ -325,7 +335,7 @@ export namespace entity {
325
335
return valueProto ;
326
336
}
327
337
328
- if ( is . number ( value ) ) {
338
+ if ( typeof value === 'number' ) {
329
339
if ( value % 1 === 0 ) {
330
340
value = new entity . Int ( value ) ;
331
341
} else {
@@ -334,17 +344,17 @@ export namespace entity {
334
344
}
335
345
336
346
if ( isDsInt ( value ) ) {
337
- valueProto . integerValue = value . value ;
347
+ valueProto . integerValue = ( value as Int ) . value ;
338
348
return valueProto ;
339
349
}
340
350
341
351
if ( isDsDouble ( value ) ) {
342
- valueProto . doubleValue = value . value ;
352
+ valueProto . doubleValue = ( value as Double ) . value ;
343
353
return valueProto ;
344
354
}
345
355
346
356
if ( isDsGeoPoint ( value ) ) {
347
- valueProto . geoPointValue = value . value ;
357
+ valueProto . geoPointValue = ( value as GeoPoint ) . value ;
348
358
return valueProto ;
349
359
}
350
360
@@ -369,7 +379,7 @@ export namespace entity {
369
379
return valueProto ;
370
380
}
371
381
372
- if ( is . array ( value ) ) {
382
+ if ( Array . isArray ( value ) ) {
373
383
valueProto . arrayValue = {
374
384
values : value . map ( entity . encodeValue ) ,
375
385
} ;
@@ -428,9 +438,10 @@ export namespace entity {
428
438
* // name: 'Stephen'
429
439
* // }
430
440
*/
431
- export function entityFromEntityProto ( entityProto ) {
432
- const entityObject = { } ;
433
-
441
+ // tslint:disable-next-line no-any
442
+ export function entityFromEntityProto ( entityProto : EntityProto ) : any {
443
+ // tslint:disable-next-line no-any
444
+ const entityObject : any = { } ;
434
445
const properties = entityProto . properties || { } ;
435
446
436
447
// tslint:disable-next-line forin
@@ -472,11 +483,11 @@ export namespace entity {
472
483
* // }
473
484
* // }
474
485
*/
475
- export function entityToEntityProto ( entityObject ) {
486
+ export function entityToEntityProto ( entityObject : Entity ) : EntityProto {
476
487
const properties = entityObject . data ;
477
488
const excludeFromIndexes = entityObject . excludeFromIndexes ;
478
489
479
- const entityProto = {
490
+ const entityProto : EntityProto = {
480
491
key : null ,
481
492
482
493
properties : Object . keys ( properties )
@@ -485,7 +496,8 @@ export namespace entity {
485
496
encoded [ key ] = entity . encodeValue ( properties [ key ] ) ;
486
497
return encoded ;
487
498
} ,
488
- { } ) ,
499
+ // tslint:disable-next-line no-any
500
+ { } as any ) ,
489
501
} ;
490
502
491
503
if ( excludeFromIndexes && excludeFromIndexes . length > 0 ) {
@@ -496,7 +508,7 @@ export namespace entity {
496
508
497
509
return entityProto ;
498
510
499
- function excludePathFromEntity ( entity , path ) {
511
+ function excludePathFromEntity ( entity : EntityProto , path : string ) {
500
512
const arrayIndex = path . indexOf ( '[]' ) ;
501
513
const entityIndex = path . indexOf ( '.' ) ;
502
514
@@ -531,7 +543,7 @@ export namespace entity {
531
543
532
544
const delimiter = firstPathPartIsArray ? '[]' : '.' ;
533
545
const splitPath = path . split ( delimiter ) ;
534
- const firstPathPart = splitPath . shift ( ) ;
546
+ const firstPathPart = splitPath . shift ( ) ! ;
535
547
const remainderPath = splitPath . join ( delimiter ) . replace ( / ^ ( \. | \[ \] ) / , '' ) ;
536
548
537
549
if ( ! ( entity . properties && entity . properties [ firstPathPart ] ) ) {
@@ -543,7 +555,8 @@ export namespace entity {
543
555
// check also if the property in question is actually an array value.
544
556
entity . properties [ firstPathPart ] . arrayValue ) {
545
557
const array = entity . properties [ firstPathPart ] . arrayValue ;
546
- array . values . forEach ( value => {
558
+ // tslint:disable-next-line no-any
559
+ array . values . forEach ( ( value : any ) => {
547
560
if ( remainderPath === '' ) {
548
561
// We want to exclude *this* array property, which is
549
562
// equivalent with excluding all its values
@@ -589,10 +602,10 @@ export namespace entity {
589
602
* //
590
603
* });
591
604
*/
592
- export function formatArray ( results ) {
605
+ export function formatArray ( results : ResponseResult [ ] ) {
593
606
return results . map ( result => {
594
607
const ent = entity . entityFromEntityProto ( result . entity ) ;
595
- ent [ entity . KEY_SYMBOL ] = entity . keyFromKeyProto ( result . entity . key ) ;
608
+ ent [ entity . KEY_SYMBOL ] = entity . keyFromKeyProto ( result . entity . key ! ) ;
596
609
return ent ;
597
610
} ) ;
598
611
}
@@ -608,8 +621,8 @@ export namespace entity {
608
621
* isKeyComplete(new Key(['Company', 'Google'])); // true
609
622
* isKeyComplete(new Key('Company')); // false
610
623
*/
611
- export function isKeyComplete ( key ) {
612
- const lastPathElement = entity . keyToKeyProto ( key ) . path . pop ( ) ;
624
+ export function isKeyComplete ( key : Key ) {
625
+ const lastPathElement = entity . keyToKeyProto ( key ) . path . pop ( ) ! ;
613
626
return ! ! ( lastPathElement . id || lastPathElement . name ) ;
614
627
}
615
628
@@ -634,7 +647,7 @@ export namespace entity {
634
647
* ]
635
648
* });
636
649
*/
637
- export function keyFromKeyProto ( keyProto ) {
650
+ export function keyFromKeyProto ( keyProto : KeyProto ) : Key {
638
651
// tslint:disable-next-line no-any
639
652
const keyOptions : any = {
640
653
path : [ ] ,
@@ -647,7 +660,7 @@ export namespace entity {
647
660
keyProto . path . forEach ( ( path , index ) => {
648
661
keyOptions . path . push ( path . kind ) ;
649
662
650
- let id = path [ path . idType ] ;
663
+ let id = path [ path . idType ! ] ;
651
664
652
665
if ( path . idType === 'id' ) {
653
666
id = new entity . Int ( id ) ;
@@ -683,15 +696,15 @@ export namespace entity {
683
696
* // ]
684
697
* // }
685
698
*/
686
- export function keyToKeyProto ( key ) {
699
+ export function keyToKeyProto ( key : Key ) : KeyProto {
687
700
if ( is . undefined ( key . kind ) ) {
688
701
throw new InvalidKeyError ( {
689
702
code : 'MISSING_KIND' ,
690
703
} ) ;
691
704
}
692
705
693
706
// tslint:disable-next-line no-any
694
- const keyProto : any = {
707
+ const keyProto : KeyProto = {
695
708
path : [ ] ,
696
709
} ;
697
710
@@ -727,7 +740,7 @@ export namespace entity {
727
740
728
741
keyProto . path . unshift ( pathElement ) ;
729
742
// tslint:disable-next-line no-conditional-assignment
730
- } while ( ( key = key . parent ) && ++ numKeysWalked ) ;
743
+ } while ( ( key = key . parent ! ) && ++ numKeysWalked ) ;
731
744
732
745
return keyProto ;
733
746
}
@@ -765,7 +778,7 @@ export namespace entity {
765
778
* // groupBy: []
766
779
* // }
767
780
*/
768
- export function queryToQueryProto ( query ) {
781
+ export function queryToQueryProto ( query : Query ) : QueryProto {
769
782
const OP_TO_OPERATOR = {
770
783
'=' : 'EQUAL' ,
771
784
'>' : 'GREATER_THAN' ,
@@ -780,8 +793,7 @@ export namespace entity {
780
793
'+' : 'ASCENDING' ,
781
794
} ;
782
795
783
- // tslint:disable-next-line no-any
784
- const queryProto : any = {
796
+ const queryProto : QueryProto = {
785
797
distinctOn : query . groupByVal . map ( groupBy => {
786
798
return {
787
799
name : groupBy ,
@@ -863,3 +875,36 @@ export namespace entity {
863
875
return queryProto ;
864
876
}
865
877
}
878
+
879
+ export interface ValueProto {
880
+ // tslint:disable-next-line no-any
881
+ [ index : string ] : any ;
882
+ valueType ?: string ;
883
+ values ?: ValueProto [ ] ;
884
+ // tslint:disable-next-line no-any
885
+ value ?: any ;
886
+ }
887
+
888
+ export interface EntityProto {
889
+ key : KeyProto | null ;
890
+ // tslint:disable-next-line no-any
891
+ properties : any ;
892
+ excludeFromIndexes ?: boolean ;
893
+ }
894
+
895
+ // tslint:disable-next-line no-any
896
+ export type Entity = any ;
897
+
898
+ export interface KeyProto {
899
+ path : Array < {
900
+ // tslint:disable-next-line no-any
901
+ [ index : string ] : any ; id : string ; name : string ;
902
+ kind ?: string ;
903
+ idType ?: string ;
904
+ } > ;
905
+ partitionId ?: { namespaceId : { } } ;
906
+ }
907
+
908
+ export interface ResponseResult {
909
+ entity : EntityProto ;
910
+ }
0 commit comments