@@ -8,7 +8,7 @@ import Delete from '../src/builder/Delete';
88import Insert from '../src/builder/Insert' ;
99import Select from '../src/builder/Select' ;
1010import Update from '../src/builder/Update' ;
11- import Mysql from '../src/dialect/Mysql' ;
11+ import Mysql , { MysqlDialect } from '../src/dialect/Mysql' ;
1212
1313describe ( 'Mysql Dialect Tests' , ( ) => {
1414 it ( 'Should translate alter' , async ( ) => {
@@ -399,4 +399,64 @@ describe('Mysql Dialect Tests', () => {
399399
400400 expect ( query [ 0 ] . values ) . to . be . empty ;
401401 } ) ;
402- } ) ;
402+
403+ it ( 'Should translate JSON selectors in select queries' , ( ) => {
404+ //Use a fresh dialect instance so selector mutations stay local.
405+ const dialect = new MysqlDialect ( ) ;
406+ const select = new Select ( [
407+ { column : 'profile.data:info.name' , alias : 'displayName' } ,
408+ { column : 'profile.data:tags.0' }
409+ ] ) ;
410+
411+ //Build a query that touches selector, where, JSON where, contains, and sort.
412+ select . from ( { name : 'users' , alias : 'u' } ) ;
413+ select . where ( 'profile.data:info.name = ?' , [ 'Ada' ] ) ;
414+ select . whereJson (
415+ '__json__ = ?' ,
416+ [ 'profile.data:info.name' , '__json__' ] ,
417+ 'Ada'
418+ ) ;
419+ select . whereJsonContains ( 'profile.data:tags' , [ 'admin' , 'owner' ] ) ;
420+ select . order ( { name : 'profile.data:info.name' } , 'desc' ) ;
421+
422+ //Confirm the dialect rewrote JSON selectors consistently.
423+ const query = dialect . select ( select ) ;
424+ expect ( query . query ) . to . contain ( 'JSON_UNQUOTE(JSON_EXTRACT(`profile`.`data`,' ) ;
425+ expect ( query . query ) . to . contain ( '$."info"."name"' ) ;
426+ expect ( query . query ) . to . contain ( '$."tags"[0]' ) ;
427+ expect ( query . query ) . to . contain (
428+ "JSON_CONTAINS(`profile`.`data`, '$.\"tags\"')"
429+ ) ;
430+ expect ( query . query ) . to . contain ( 'ORDER BY' ) ;
431+ expect ( query . values ) . to . deep . equal ( [
432+ 'Ada' ,
433+ 'Ada' ,
434+ '"admin"' ,
435+ '"owner"'
436+ ] ) ;
437+ } ) ;
438+
439+ it ( 'Should expose configurable JSON dialect helpers' , ( ) => {
440+ //Read and write the shared JsonTrait configuration through Mysql.
441+ const dialect = new MysqlDialect ( ) ;
442+ dialect . separator = '/' ;
443+ dialect . splitter = '->' ;
444+
445+ //Create helpers through both overloads so parse branches are covered.
446+ const parsed = dialect . json ( 'profile.data->info/name' , '->' , '/' ) ;
447+ const direct = dialect . json ( 'profile.data' , [ 'tags' , '0' ] ) ;
448+
449+ //Confirm the helper metadata matches the expected MySQL JSON syntax.
450+ expect ( dialect . separator ) . to . equal ( '/' ) ;
451+ expect ( dialect . splitter ) . to . equal ( '->' ) ;
452+ expect ( parsed . extract ) . to . equal (
453+ "JSON_UNQUOTE(JSON_EXTRACT(`profile`.`data`, '$.\"info\".\"name\"'))"
454+ ) ;
455+ expect ( parsed . where ( '__json__ = ?' , '__json__' ) ) . to . equal (
456+ "JSON_UNQUOTE(JSON_EXTRACT(`profile`.`data`, '$.\"info\".\"name\"')) = ?"
457+ ) ;
458+ expect ( direct . contains ) . to . equal (
459+ "JSON_CONTAINS(`profile`.`data`, '$.\"tags\"[0]')"
460+ ) ;
461+ } ) ;
462+ } ) ;
0 commit comments