@@ -19,9 +19,11 @@ import {
19
19
type StatusHooks ,
20
20
Token ,
21
21
TransformAuth ,
22
+ Prettify ,
22
23
} from "./types.ts" ;
23
24
24
25
type Engines = Record < string , new ( emitter : Emitter < EmitterEvents > ) => Engine > ;
26
+ type R = Prettify < Record < string , unknown > > ;
25
27
26
28
export class Surreal {
27
29
public connection : Engine | undefined ;
@@ -397,44 +399,44 @@ export class Surreal {
397
399
* Selects all records in a table, or a specific record, from the database.
398
400
* @param thing - The table name or a record ID to select.
399
401
*/
400
- async select < T extends Record < string , unknown > > ( thing : string | RecordId ) {
402
+ async select < T extends R > ( thing : string ) : Promise < ActionResult < T > [ ] > ;
403
+ async select < T extends R > ( thing : RecordId ) : Promise < ActionResult < T > > ;
404
+ async select < T extends R > ( thing : RecordId | string ) {
401
405
await this . ready ;
402
406
const res = await this . rpc < ActionResult < T > > ( "select" , [ thing ] ) ;
403
- return this . outputHandler ( res ) ;
407
+ return this . outputHandler ( res , thing instanceof RecordId ) ;
404
408
}
405
409
406
410
/**
407
411
* Creates a record in the database.
408
412
* @param thing - The table name or the specific record ID to create.
409
413
* @param data - The document / record data to insert.
410
414
*/
411
- async create <
412
- T extends Record < string , unknown > ,
413
- U extends Record < string , unknown > = T
414
- > ( thing : string | RecordId , data ?: U ) {
415
+ async create < T extends R , U extends R = T > ( thing : string , data ?: U ) : Promise < ActionResult < T > [ ] > ;
416
+ async create < T extends R , U extends R = T > ( thing : RecordId , data ?: U ) : Promise < ActionResult < T > [ ] > ;
417
+ async create < T extends R , U extends R = T > ( thing : RecordId | string , data ?: U ) {
415
418
await this . ready ;
416
- const res = await this . rpc < ActionResult < T , U > > ( "create" , [
419
+ const res = await this . rpc < ActionResult < T > > ( "create" , [
417
420
thing ,
418
421
data ,
419
422
] ) ;
420
- return this . outputHandler ( res ) ;
423
+ return this . outputHandler ( res , thing instanceof RecordId ) ;
421
424
}
422
425
423
426
/**
424
427
* Inserts one or multiple records in the database.
425
428
* @param thing - The table name or the specific record ID to create.
426
429
* @param data - The document(s) / record(s) to insert.
427
430
*/
428
- async insert <
429
- T extends Record < string , unknown > ,
430
- U extends Record < string , unknown > = T
431
- > ( thing : string | RecordId , data ?: U | U [ ] ) {
431
+ async insert < T extends R , U extends R = T > ( thing : string , data ?: U | U [ ] ) : Promise < ActionResult < T > [ ] > ;
432
+ async insert < T extends R , U extends R = T > ( thing : RecordId , data ?: U ) : Promise < ActionResult < T > > ;
433
+ async insert < T extends R , U extends R = T > ( thing : RecordId | string , data ?: U | U [ ] ) {
432
434
await this . ready ;
433
- const res = await this . rpc < ActionResult < T , U > > ( "insert" , [
435
+ const res = await this . rpc < ActionResult < T > > ( "insert" , [
434
436
thing ,
435
437
data ,
436
438
] ) ;
437
- return this . outputHandler ( res ) ;
439
+ return this . outputHandler ( res , thing instanceof RecordId ) ;
438
440
}
439
441
440
442
/**
@@ -444,16 +446,15 @@ export class Surreal {
444
446
* @param thing - The table name or the specific record ID to update.
445
447
* @param data - The document / record data to insert.
446
448
*/
447
- async update <
448
- T extends Record < string , unknown > ,
449
- U extends Record < string , unknown > = T
450
- > ( thing : string | RecordId , data ?: U ) {
449
+ async update < T extends R , U extends R = T > ( thing : string , data ?: U ) : Promise < ActionResult < T > [ ] > ;
450
+ async update < T extends R , U extends R = T > ( thing : RecordId , data ?: U ) : Promise < ActionResult < T > > ;
451
+ async update < T extends R , U extends R = T > ( thing : RecordId | string , data ?: U ) {
451
452
await this . ready ;
452
- const res = await this . rpc < ActionResult < T , U > > ( "update" , [
453
+ const res = await this . rpc < ActionResult < T > > ( "update" , [
453
454
thing ,
454
455
data ,
455
456
] ) ;
456
- return this . outputHandler ( res ) ;
457
+ return this . outputHandler ( res , thing instanceof RecordId ) ;
457
458
}
458
459
459
460
/**
@@ -463,13 +464,15 @@ export class Surreal {
463
464
* @param thing - The table name or the specific record ID to change.
464
465
* @param data - The document / record data to insert.
465
466
*/
466
- async merge <
467
- T extends Record < string , unknown > ,
468
- U extends Record < string , unknown > = Partial < T >
469
- > ( thing : string | RecordId , data ?: U ) {
467
+ async merge < T extends R , U extends R = T > ( thing : string , data ?: U ) : Promise < ActionResult < T > [ ] > ;
468
+ async merge < T extends R , U extends R = T > ( thing : RecordId , data ?: U ) : Promise < ActionResult < T > > ;
469
+ async merge < T extends R , U extends R = T > ( thing : RecordId | string , data ?: U ) {
470
470
await this . ready ;
471
- const res = await this . rpc < ActionResult < U > > ( "merge" , [ thing , data ] ) ;
472
- return this . outputHandler ( res ) ;
471
+ const res = await this . rpc < ActionResult < T > > ( "merge" , [
472
+ thing ,
473
+ data ,
474
+ ] ) ;
475
+ return this . outputHandler ( res , thing instanceof RecordId ) ;
473
476
}
474
477
475
478
/**
@@ -479,22 +482,25 @@ export class Surreal {
479
482
* @param thing - The table name or the specific record ID to modify.
480
483
* @param data - The JSON Patch data with which to modify the records.
481
484
*/
482
- async patch ( thing : RecordId , data ?: Patch [ ] ) {
485
+ async patch < T extends R > ( thing : RecordId , data ?: Patch [ ] , diff ?: false ) : Promise < ActionResult < T > > ;
486
+ async patch < T extends R > ( thing : string , data ?: Patch [ ] , diff ?: false ) : Promise < ActionResult < T > [ ] > ;
487
+ async patch ( thing : RecordId | string , data : undefined | Patch [ ] , diff : true ) : Promise < Patch [ ] > ;
488
+ async patch ( thing : RecordId | string , data ?: Patch [ ] , diff ?: boolean ) {
483
489
await this . ready ;
484
- const res = await this . rpc < Patch > ( "patch" , [ thing , data ] ) ;
485
- return this . outputHandler ( res ) ;
490
+ const res = await this . rpc < any > ( "patch" , [ thing , data , diff ] ) ;
491
+ return this . outputHandler ( res , thing instanceof RecordId ) ;
486
492
}
487
493
488
494
/**
489
495
* Deletes all records in a table, or a specific record, from the database.
490
496
* @param thing - The table name or a record ID to select.
491
497
*/
492
- async delete < T extends Record < string , unknown > = Record < string , unknown > > (
493
- thing : string | RecordId < string >
494
- ) {
498
+ async delete < T extends R > ( thing : string ) : Promise < ActionResult < T > [ ] > ;
499
+ async delete < T extends R > ( thing : RecordId ) : Promise < ActionResult < T > > ;
500
+ async delete < T extends R > ( thing : RecordId | string ) {
495
501
await this . ready ;
496
502
const res = await this . rpc < ActionResult < T > > ( "delete" , [ thing ] ) ;
497
- return this . outputHandler ( res ) ;
503
+ return this . outputHandler ( res , thing instanceof RecordId ) ;
498
504
}
499
505
500
506
/**
@@ -516,15 +522,16 @@ export class Surreal {
516
522
* @param thing - What thing did you query (table vs record).
517
523
*/
518
524
private outputHandler < T extends Record < string , unknown > > (
519
- res : RpcResponse < T >
525
+ res : RpcResponse < T > ,
526
+ single ?: boolean ,
520
527
) {
521
528
if ( res . error ) throw new ResponseError ( res . error . message ) ;
522
529
if ( Array . isArray ( res . result ) ) {
523
- return res . result as T [ ] ;
530
+ return single ? res . result as T [ ] : res . result [ 0 ] as T | undefined ;
524
531
} else if ( "id" in ( res . result ?? { } ) ) {
525
- return [ res . result ] as T [ ] ;
532
+ return single ? res . result as T | undefined : [ res . result ] as T [ ] ;
526
533
} else if ( res . result === null ) {
527
- return [ ] as T [ ] ;
534
+ return single ? undefined : [ ] as T [ ] ;
528
535
}
529
536
530
537
throw new UnexpectedResponse ( ) ;
0 commit comments