11import { describe , expect , test } from 'vitest' ;
2- import {
3- CreateSourceParams ,
4- ScopedParameterLookup ,
5- UnscopedParameterLookup ,
6- SqlParameterQuery ,
7- SqlSyncRules
8- } from '../../src/index.js' ;
2+ import { CreateSourceParams , ScopedParameterLookup , SqlSyncRules } from '../../src/index.js' ;
93
4+ import { DEFAULT_HYDRATION_STATE , HydrationState } from '../../src/HydrationState.js' ;
5+ import { SqlBucketDescriptor } from '../../src/SqlBucketDescriptor.js' ;
6+ import { StaticSqlParameterQuery } from '../../src/StaticSqlParameterQuery.js' ;
107import {
118 ASSETS ,
129 BASIC_SCHEMA ,
@@ -16,9 +13,6 @@ import {
1613 normalizeQuerierOptions ,
1714 normalizeTokenParameters
1815} from './util.js' ;
19- import { SqlBucketDescriptor } from '../../src/SqlBucketDescriptor.js' ;
20- import { StaticSqlParameterQuery } from '../../src/StaticSqlParameterQuery.js' ;
21- import { DEFAULT_HYDRATION_STATE } from '../../src/HydrationState.js' ;
2216
2317describe ( 'sync rules' , ( ) => {
2418 const hydrationParams : CreateSourceParams = { hydrationState : DEFAULT_HYDRATION_STATE } ;
@@ -166,6 +160,76 @@ bucket_definitions:
166160 ) . toEqual ( [ ] ) ;
167161 } ) ;
168162
163+ test ( 'bucket with parameters with custom hydrationState' , ( ) => {
164+ // "end-to-end" test with custom hydrationState.
165+ // We don't test complex details here, but do cover bucket names and parameter lookup scope.
166+ const rules = SqlSyncRules . fromYaml (
167+ `
168+ config:
169+ edition: 2
170+ bucket_definitions:
171+ mybucket:
172+ parameters:
173+ - SELECT token_parameters.user_id as user_id
174+ - SELECT users.id as user_id FROM users WHERE users.id = token_parameters.user_id AND users.is_admin
175+ data:
176+ - SELECT id, description FROM assets WHERE assets.user_id = bucket.user_id AND NOT assets.archived
177+ ` ,
178+ PARSE_OPTIONS
179+ ) ;
180+ const hydrationState : HydrationState = {
181+ getBucketSourceScope ( source ) {
182+ return { bucketPrefix : `${ source . uniqueName } -test` } ;
183+ } ,
184+ getParameterIndexLookupScope ( source ) {
185+ return {
186+ lookupName : `${ source . defaultLookupScope . lookupName } .test` ,
187+ queryId : `${ source . defaultLookupScope . queryId } .test`
188+ } ;
189+ }
190+ } ;
191+ const hydrated = rules . hydrate ( { hydrationState } ) ;
192+ const querier = hydrated . getBucketParameterQuerier (
193+ normalizeQuerierOptions ( { user_id : 'user1' } , { device_id : 'device1' } )
194+ ) ;
195+ expect ( querier . errors ) . toEqual ( [ ] ) ;
196+ expect ( querier . querier . staticBuckets ) . toEqual ( [
197+ {
198+ bucket : 'mybucket-test["user1"]' ,
199+ definition : 'mybucket' ,
200+ inclusion_reasons : [ 'default' ] ,
201+ priority : 3
202+ }
203+ ] ) ;
204+ expect ( querier . querier . parameterQueryLookups ) . toEqual ( [
205+ ScopedParameterLookup . direct ( { lookupName : 'mybucket.test' , queryId : '2.test' } , [ 'user1' ] )
206+ ] ) ;
207+
208+ expect ( hydrated . evaluateParameterRow ( USERS , { id : 'user1' , is_admin : 1 } ) ) . toEqual ( [
209+ {
210+ bucketParameters : [ { user_id : 'user1' } ] ,
211+ lookup : ScopedParameterLookup . direct ( { lookupName : 'mybucket.test' , queryId : '2.test' } , [ 'user1' ] )
212+ }
213+ ] ) ;
214+
215+ expect (
216+ hydrated . evaluateRow ( {
217+ sourceTable : ASSETS ,
218+ record : { id : 'asset1' , description : 'test' , user_id : 'user1' , device_id : 'device1' }
219+ } )
220+ ) . toEqual ( [
221+ {
222+ bucket : 'mybucket-test["user1"]' ,
223+ id : 'asset1' ,
224+ data : {
225+ id : 'asset1' ,
226+ description : 'test'
227+ } ,
228+ table : 'assets'
229+ }
230+ ] ) ;
231+ } ) ;
232+
169233 test ( 'parse bucket with parameters and OR condition' , ( ) => {
170234 const rules = SqlSyncRules . fromYaml (
171235 `
0 commit comments