@@ -56,6 +56,43 @@ describe('pongoCollection cache integration', () => {
5656 expect ( spies . get ) . toHaveBeenCalled ( ) ;
5757 } ) ;
5858
59+ it ( 'db-level cache flows to collection' , async ( ) => {
60+ const { cache, spies } = spyCache ( 'db' ) ;
61+ client = pongoClient ( {
62+ driver : sqlite3Driver ,
63+ connectionString : memoryConnectionString ( ) ,
64+ } ) ;
65+ await client . connect ( ) ;
66+
67+ const col = client . db ( 'db' , { cache } ) . collection < User > ( 'users' ) ;
68+ const { insertedId } = await col . insertOne ( { name : 'Alice' } ) ;
69+ await col . findOne ( { _id : insertedId ! } ) ;
70+
71+ expect ( spies . set ) . toHaveBeenCalled ( ) ;
72+ expect ( spies . get ) . toHaveBeenCalled ( ) ;
73+ } ) ;
74+
75+ it ( 'db-level cache overrides client-level' , async ( ) => {
76+ const { cache : clientCache , spies : clientSpies } = spyCache ( 'client' ) ;
77+ const { cache : dbCache , spies : dbSpies } = spyCache ( 'db' ) ;
78+
79+ client = pongoClient ( {
80+ driver : sqlite3Driver ,
81+ connectionString : memoryConnectionString ( ) ,
82+ cache : clientCache ,
83+ } ) ;
84+ await client . connect ( ) ;
85+
86+ const col = client . db ( 'db' , { cache : dbCache } ) . collection < User > ( 'users' ) ;
87+ const { insertedId } = await col . insertOne ( { name : 'Carol' } ) ;
88+ await col . findOne ( { _id : insertedId ! } ) ;
89+
90+ expect ( dbSpies . set ) . toHaveBeenCalled ( ) ;
91+ expect ( dbSpies . get ) . toHaveBeenCalled ( ) ;
92+ expect ( clientSpies . set ) . not . toHaveBeenCalled ( ) ;
93+ expect ( clientSpies . get ) . not . toHaveBeenCalled ( ) ;
94+ } ) ;
95+
5996 it ( 'collection-level cache overrides client-level' , async ( ) => {
6097 const { cache : clientCache , spies : clientSpies } = spyCache ( 'client' ) ;
6198 const { cache : colCache , spies : colSpies } = spyCache ( 'collection' ) ;
@@ -79,6 +116,28 @@ describe('pongoCollection cache integration', () => {
79116 expect ( clientSpies . get ) . not . toHaveBeenCalled ( ) ;
80117 } ) ;
81118
119+ it ( 'collection-level cache overrides db-level' , async ( ) => {
120+ const { cache : dbCache , spies : dbSpies } = spyCache ( 'db' ) ;
121+ const { cache : colCache , spies : colSpies } = spyCache ( 'collection' ) ;
122+
123+ client = pongoClient ( {
124+ driver : sqlite3Driver ,
125+ connectionString : memoryConnectionString ( ) ,
126+ } ) ;
127+ await client . connect ( ) ;
128+
129+ const col = client
130+ . db ( 'db' , { cache : dbCache } )
131+ . collection < User > ( 'users' , { cache : colCache } ) ;
132+ const { insertedId } = await col . insertOne ( { name : 'Bob' } ) ;
133+ await col . findOne ( { _id : insertedId ! } ) ;
134+
135+ expect ( colSpies . set ) . toHaveBeenCalled ( ) ;
136+ expect ( colSpies . get ) . toHaveBeenCalled ( ) ;
137+ expect ( dbSpies . set ) . not . toHaveBeenCalled ( ) ;
138+ expect ( dbSpies . get ) . not . toHaveBeenCalled ( ) ;
139+ } ) ;
140+
82141 it ( "'disabled' at client, enabled at collection" , async ( ) => {
83142 const { cache : colCache , spies : colSpies } = spyCache ( 'collection' ) ;
84143
@@ -494,5 +553,74 @@ describe('pongoCollection cache integration', () => {
494553
495554 expect ( spies . set ) . toHaveBeenCalled ( ) ;
496555 } ) ;
556+
557+ it ( 'commit flushes to inherited client-level cache' , async ( ) => {
558+ const { cache, spies } = spyCache ( 'client' ) ;
559+ await client . close ( ) ;
560+ client = pongoClient ( {
561+ driver : sqlite3Driver ,
562+ connectionString : memoryConnectionString ( ) ,
563+ cache,
564+ } ) ;
565+ await client . connect ( ) ;
566+
567+ const col = client . db ( 'db' ) . collection < User > ( 'users' ) ;
568+
569+ const session = client . startSession ( ) ;
570+ session . startTransaction ( ) ;
571+ await col . insertOne ( { name : 'Ivy' } , { session } ) ;
572+
573+ expect ( spies . set ) . not . toHaveBeenCalled ( ) ;
574+
575+ await session . commitTransaction ( ) ;
576+ await session . endSession ( ) ;
577+
578+ expect ( spies . set ) . toHaveBeenCalled ( ) ;
579+ } ) ;
580+
581+ it ( 'rollback does not populate inherited client-level cache' , async ( ) => {
582+ const { cache, spies } = spyCache ( 'client' ) ;
583+ await client . close ( ) ;
584+ client = pongoClient ( {
585+ driver : sqlite3Driver ,
586+ connectionString : memoryConnectionString ( ) ,
587+ cache,
588+ } ) ;
589+ await client . connect ( ) ;
590+
591+ const col = client . db ( 'db' ) . collection < User > ( 'users' ) ;
592+
593+ const session = client . startSession ( ) ;
594+ session . startTransaction ( ) ;
595+ await col . insertOne ( { name : 'Jack' } , { session } ) ;
596+ await session . abortTransaction ( ) ;
597+ await session . endSession ( ) ;
598+
599+ expect ( spies . set ) . not . toHaveBeenCalled ( ) ;
600+ } ) ;
601+
602+ it ( 'commit flushes to db-level cache, not client-level' , async ( ) => {
603+ const { cache : clientCache , spies : clientSpies } = spyCache ( 'client' ) ;
604+ const { cache : dbCache , spies : dbSpies } = spyCache ( 'db' ) ;
605+
606+ await client . close ( ) ;
607+ client = pongoClient ( {
608+ driver : sqlite3Driver ,
609+ connectionString : memoryConnectionString ( ) ,
610+ cache : clientCache ,
611+ } ) ;
612+ await client . connect ( ) ;
613+
614+ const col = client . db ( 'db' , { cache : dbCache } ) . collection < User > ( 'users' ) ;
615+
616+ const session = client . startSession ( ) ;
617+ session . startTransaction ( ) ;
618+ await col . insertOne ( { name : 'Kim' } , { session } ) ;
619+ await session . commitTransaction ( ) ;
620+ await session . endSession ( ) ;
621+
622+ expect ( dbSpies . set ) . toHaveBeenCalled ( ) ;
623+ expect ( clientSpies . set ) . not . toHaveBeenCalled ( ) ;
624+ } ) ;
497625 } ) ;
498626} ) ;
0 commit comments