@@ -56,10 +56,41 @@ export async function createKernel(options: KernelOptions) {
5656 return match || null ;
5757 }
5858 if ( method === 'update' ) {
59- return ql . update ( params . object , { ...params . data , id : params . id } ) ;
59+ if ( params . id ) {
60+ // Robust check: Manually find the record in memory since ql.find(obj, id) might not be supported by this specific mock driver setup
61+ let all = await ql . find ( params . object ) ;
62+
63+ if ( all && ( all as any ) . value ) all = ( all as any ) . value ;
64+ if ( ! all ) all = [ ] ;
65+
66+ const existing = all . find ( ( i : any ) => i . id === params . id || i . _id === params . id ) ;
67+
68+ if ( ! existing ) {
69+ console . warn ( `[BrokerShim] Update failed: Record ${ params . id } not found.` ) ;
70+ throw new Error ( '[ObjectStack] Not Found' ) ;
71+ }
72+
73+ // Perform update using the ObjectQL Engine signature: update(object, data, options)
74+ // where options.filter can be the ID string
75+ try {
76+ await ql . update ( params . object , params . data , { filter : params . id } ) ;
77+ } catch ( err : any ) {
78+ console . warn ( `[BrokerShim] update failed: ${ err . message } ` ) ;
79+ throw err ;
80+ }
81+
82+ return { ...existing , ...params . data } ;
83+ }
84+ return null ;
6085 }
6186 if ( method === 'delete' ) {
62- return ql . delete ( params . object , { filter : params . id } ) ;
87+ try {
88+ // ql.delete(object, options) where options.filter is ID
89+ return await ql . delete ( params . object , { filter : params . id } ) ;
90+ } catch ( err : any ) {
91+ console . warn ( `[BrokerShim] delete failed: ${ err . message } ` ) ;
92+ throw err ;
93+ }
6394 }
6495 if ( method === 'find' || method === 'query' ) {
6596 let all = await ql . find ( params . object ) ;
@@ -77,6 +108,17 @@ export async function createKernel(options: KernelOptions) {
77108 if ( ! all ) all = [ ] ;
78109
79110 const filters = params . filters ;
111+ // Extract standard query options possibly passed via filters (due to MSW plugin mapping)
112+ let queryOptions : any = { } ;
113+ if ( filters && typeof filters === 'object' ) {
114+ const reserved = [ 'top' , 'skip' , 'sort' , 'select' , 'expand' , 'count' , 'search' ] ;
115+ reserved . forEach ( opt => {
116+ if ( filters [ opt ] !== undefined ) {
117+ queryOptions [ opt ] = filters [ opt ] ;
118+ }
119+ } ) ;
120+ }
121+
80122 if ( filters && typeof filters === 'object' && ! Array . isArray ( filters ) ) {
81123 // Filter out reserved query parameters that are NOT field names
82124 const reserved = [ 'top' , 'skip' , 'sort' , 'select' , 'expand' , 'count' , 'search' ] ;
@@ -92,6 +134,47 @@ export async function createKernel(options: KernelOptions) {
92134 } ) ;
93135 }
94136 }
137+
138+ // --- Sort ---
139+ if ( queryOptions . sort ) {
140+ const sortFields = String ( queryOptions . sort ) . split ( ',' ) . map ( s => s . trim ( ) ) ;
141+ all . sort ( ( a : any , b : any ) => {
142+ for ( const field of sortFields ) {
143+ const desc = field . startsWith ( '-' ) ;
144+ const key = desc ? field . substring ( 1 ) : field ;
145+ if ( a [ key ] < b [ key ] ) return desc ? 1 : - 1 ;
146+ if ( a [ key ] > b [ key ] ) return desc ? - 1 : 1 ;
147+ }
148+ return 0 ;
149+ } ) ;
150+ }
151+
152+ // --- Select ---
153+ if ( queryOptions . select ) {
154+ const selectFields = Array . isArray ( queryOptions . select )
155+ ? queryOptions . select
156+ : String ( queryOptions . select ) . split ( ',' ) . map ( ( s : string ) => s . trim ( ) ) ;
157+
158+ all = all . map ( ( item : any ) => {
159+ const projected : any = { id : item . id , _id : item . _id } ; // Always include ID
160+ selectFields . forEach ( ( f : string ) => {
161+ if ( item [ f ] !== undefined ) projected [ f ] = item [ f ] ;
162+ } ) ;
163+ return projected ;
164+ } ) ;
165+ }
166+
167+ // --- Skip/Top ---
168+ const skip = parseInt ( queryOptions . skip ) || 0 ;
169+ const top = parseInt ( queryOptions . top ) ; // undefined is fine
170+
171+ if ( skip > 0 ) {
172+ all = all . slice ( skip ) ;
173+ }
174+ if ( ! isNaN ( top ) ) {
175+ all = all . slice ( 0 , top ) ;
176+ }
177+
95178 console . log ( `[BrokerShim] find/query(${ params . object } ) -> count: ${ all . length } ` ) ;
96179 return { data : all , count : all . length } ;
97180 }
0 commit comments