@@ -194,6 +194,29 @@ class SQLES extends BaseES {
194
194
let value = data [ col_name ] ;
195
195
value = await prop . sql_dereference ( value ) ;
196
196
197
+ // TODO: This is not an ideal implementation,
198
+ // but this is only 6 lines of code so doing this
199
+ // "properly" is not sensible at this time.
200
+ //
201
+ // This is here because:
202
+ // - SQLES has access to the "db" object
203
+ //
204
+ // Writing this in `json`'s `sql_reference` method
205
+ // is also not ideal because that places the concern
206
+ // of supporting different database backends to
207
+ // property types.
208
+ //
209
+ // Best solution: SQLES has a SQLRefinements by
210
+ // composition. This SQLRefinements is applied
211
+ // to property types for the duration of this
212
+ // function.
213
+ if ( prop . typ . name === 'json' ) {
214
+ value = this . db . case ( {
215
+ mysql : ( ) => value ,
216
+ otherwise : ( ) => JSON . parse ( value ?? '{}' ) ,
217
+ } ) ( ) ;
218
+ }
219
+
197
220
entity_data [ prop . name ] = value ;
198
221
}
199
222
const entity = await Entity . create ( { om : this . om } , entity_data ) ;
@@ -286,6 +309,13 @@ class SQLES extends BaseES {
286
309
}
287
310
288
311
value = await prop . sql_reference ( value ) ;
312
+
313
+ // TODO: This is done here for consistency;
314
+ // see the larger comment in sql_row_to_entity_
315
+ // which does the reverse operation.
316
+ if ( prop . typ . name === 'json' ) {
317
+ value = JSON . stringify ( value ) ;
318
+ }
289
319
290
320
if ( value && options . use_id ) {
291
321
if ( value . hasOwnProperty ( 'id' ) ) {
0 commit comments