11import { DataSource , Source } from "../types" ;
22const parser = new ( require ( 'node-sql-parser' ) . Parser ) ( ) ;
33
4- async function createCacheTable ( dataSource ?: DataSource ) {
5- const statement = `
6- CREATE TABLE IF NOT EXISTS "main"."tmp_cache"(
7- "id" INTEGER PRIMARY KEY AUTOINCREMENT,
8- "timestamp" REAL NOT NULL,
9- "ttl" INTEGER NOT NULL,
10- "query" TEXT UNIQUE NOT NULL,
11- "results" TEXT
12- );
13- `
14-
15- await dataSource ?. internalConnection ?. durableObject . executeQuery ( statement , undefined , false )
16- }
17-
184function hasModifyingStatement ( ast : any ) : boolean {
195 // Check if current node is a modifying statement
206 if ( ast . type && [ 'insert' , 'update' , 'delete' ] . includes ( ast . type . toLowerCase ( ) ) ) {
@@ -33,28 +19,30 @@ function hasModifyingStatement(ast: any): boolean {
3319 }
3420 }
3521 }
36-
22+
3723 return false ;
3824}
3925
40- export async function beforeQueryCache ( sql : string , params ?: any [ ] , dataSource ?: DataSource ) : Promise < any | null > {
26+ export async function beforeQueryCache ( sql : string , params ?: any [ ] , dataSource ?: DataSource , dialect ?: string ) : Promise < any | null > {
4127 // Currently we do not support caching queries that have dynamic parameters
4228 if ( params ?. length ) return null
29+ if ( dataSource ?. source === Source . internal || ! dataSource ?. request . headers . has ( 'X-Starbase-Cache' ) ) return null
4330
44- let ast = parser . astify ( sql ) ;
31+ if ( ! dialect ) dialect = 'sqlite'
32+ if ( dialect . toLowerCase ( ) === 'postgres' ) dialect = 'postgresql'
4533
46- if ( ! hasModifyingStatement ( ast ) && dataSource ?. source === Source . external && dataSource ?. request . headers . has ( 'X-Starbase-Cache' ) ) {
47- await createCacheTable ( dataSource )
48- const fetchCacheStatement = `SELECT timestamp, ttl, query, results FROM tmp_cache WHERE query = ?`
49- const result = await dataSource . internalConnection ?. durableObject . executeQuery ( fetchCacheStatement , [ sql ] , false ) as any [ ] ;
34+ let ast = parser . astify ( sql , { database : dialect } ) ;
35+ if ( hasModifyingStatement ( ast ) ) return null
36+
37+ const fetchCacheStatement = `SELECT timestamp, ttl, query, results FROM tmp_cache WHERE query = ?`
38+ const result = await dataSource . internalConnection ?. durableObject . executeQuery ( fetchCacheStatement , [ sql ] , false ) as any [ ] ;
5039
51- if ( result ?. length ) {
52- const { timestamp, ttl, results } = result [ 0 ] ;
53- const expirationTime = new Date ( timestamp ) . getTime ( ) + ( ttl * 1000 ) ;
54-
55- if ( Date . now ( ) < expirationTime ) {
56- return JSON . parse ( results )
57- }
40+ if ( result ?. length ) {
41+ const { timestamp, ttl, results } = result [ 0 ] ;
42+ const expirationTime = new Date ( timestamp ) . getTime ( ) + ( ttl * 1000 ) ;
43+
44+ if ( Date . now ( ) < expirationTime ) {
45+ return JSON . parse ( results )
5846 }
5947 }
6048
@@ -67,16 +55,19 @@ export async function beforeQueryCache(sql: string, params?: any[], dataSource?:
6755// to look into include using Cloudflare Cache but need to find a good way to cache the
6856// response in a safe way for our use case. Another option is another service for queues
6957// or another way to ingest it directly to the Durable Object.
70- export async function afterQueryCache ( sql : string , params : any [ ] | undefined , result : any , dataSource ?: DataSource ) {
58+ export async function afterQueryCache ( sql : string , params : any [ ] | undefined , result : any , dataSource ?: DataSource , dialect ?: string ) {
7159 // Currently we do not support caching queries that have dynamic parameters
7260 if ( params ?. length ) return ;
61+ if ( dataSource ?. source === Source . internal || ! dataSource ?. request . headers . has ( 'X-Starbase-Cache' ) ) return null
7362
7463 try {
75- let ast = parser . astify ( sql ) ;
64+ if ( ! dialect ) dialect = 'sqlite'
65+ if ( dialect . toLowerCase ( ) === 'postgres' ) dialect = 'postgresql'
66+
67+ let ast = parser . astify ( sql , { database : dialect } ) ;
7668
7769 // If any modifying query exists within our SQL statement then we shouldn't proceed
78- if ( hasModifyingStatement ( ast ) ||
79- ! ( dataSource ?. source === Source . external && dataSource ?. request . headers . has ( 'X-Starbase-Cache' ) ) ) return ;
70+ if ( hasModifyingStatement ( ast ) ) return ;
8071
8172 const timestamp = Date . now ( ) ;
8273 const results = JSON . stringify ( result ) ;
0 commit comments