@@ -44,22 +44,41 @@ class SQLES extends BaseES {
44
44
}
45
45
} ,
46
46
async read ( uid ) {
47
- const id_prop = this . om . properties [ this . om . primary_identifier ] ;
48
- let id_col =
49
- id_prop . descriptor . sql ?. column_name ?? id_prop . name ;
50
47
51
- console . log ( 'CALLING READ WITH UID ' , uid ) ;
52
- // Temporary hack until multiple identifiers are supported
53
- // (allows us to query using an internal ID; users can't do this)
54
- if ( typeof uid === 'number' ) {
55
- id_col = 'id' ;
56
- }
48
+ const [ stmt_where , where_vals ] = await ( async ( ) => {
49
+ if ( typeof uid !== 'object' ) {
50
+ const id_prop =
51
+ this . om . properties [ this . om . primary_identifier ] ;
52
+ let id_col =
53
+ id_prop . descriptor . sql ?. column_name ?? id_prop . name ;
54
+ // Temporary hack until multiple identifiers are supported
55
+ // (allows us to query using an internal ID; users can't do this)
56
+ if ( typeof uid === 'number' ) {
57
+ id_col = 'id' ;
58
+ }
59
+ return [ ` WHERE ${ id_col } = ?` , [ uid ] ]
60
+ }
61
+
62
+ if ( ! uid . hasOwnProperty ( 'predicate' ) ) {
63
+ throw new Error (
64
+ 'SQLES.read does not understand this input: ' +
65
+ 'object with no predicate property' ,
66
+ ) ;
67
+ }
68
+ let predicate = uid . predicate ; // uid is actually a predicate
69
+ if ( predicate instanceof Predicate ) {
70
+ predicate = await this . om_to_sql_condition_ ( predicate ) ;
71
+ }
72
+ const stmt_where = ` WHERE ${ predicate . sql } LIMIT 1` ;
73
+ const where_vals = predicate . values ;
74
+ return [ stmt_where , where_vals ] ;
75
+ } ) ( ) ;
57
76
58
77
const stmt =
59
- `SELECT * FROM ${ this . om . sql . table_name } WHERE ${ id_col } = ? ` ;
78
+ `SELECT * FROM ${ this . om . sql . table_name } ${ stmt_where } ` ;
60
79
61
80
const rows = await this . db . read (
62
- stmt , [ uid ]
81
+ stmt , where_vals
63
82
) ;
64
83
65
84
if ( rows . length === 0 ) {
0 commit comments