-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reat): allow compilable query as hook argument (#150)
Co-authored-by: DominicGBauer <[email protected]>
- Loading branch information
1 parent
c5a9eb5
commit c94be6a
Showing
13 changed files
with
334 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@powersync/common": minor | ||
"@powersync/react": minor | ||
--- | ||
|
||
Allow compilable queries to be used as hook arguments |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface CompilableQuery<T> { | ||
execute(): Promise<T[]>; | ||
compile(): CompiledQuery; | ||
} | ||
|
||
export interface CompiledQuery { | ||
readonly sql: string; | ||
readonly parameters: ReadonlyArray<unknown>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { CompilableQuery } from '../types/types'; | ||
|
||
export interface ParsedQuery { | ||
sqlStatement: string; | ||
parameters: any[]; | ||
} | ||
|
||
export const parseQuery = <T>(query: string | CompilableQuery<T>, parameters: any[]): ParsedQuery => { | ||
let sqlStatement: string; | ||
|
||
if (typeof query == 'string') { | ||
sqlStatement = query; | ||
} else { | ||
const hasAdditionalParameters = parameters.length > 0; | ||
if (hasAdditionalParameters) { | ||
throw new Error('You cannot pass parameters to a compiled query.'); | ||
} | ||
|
||
const compiled = query.compile(); | ||
sqlStatement = compiled.sql; | ||
parameters = compiled.parameters as any[]; | ||
} | ||
|
||
return { sqlStatement, parameters: parameters }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": "./", | ||
"esModuleInterop": true, | ||
"jsx": "react", | ||
"rootDir": "../", | ||
"composite": true, | ||
"outDir": "./lib", | ||
"lib": ["esnext", "DOM"], | ||
"module": "esnext", | ||
"sourceMap": true, | ||
"moduleResolution": "node", | ||
"noFallthroughCasesInSwitch": true, | ||
"noImplicitReturns": true, | ||
"noImplicitUseStrict": false, | ||
"noStrictGenericChecks": false, | ||
"resolveJsonModule": true, | ||
"skipLibCheck": true, | ||
"target": "esnext" | ||
}, | ||
"include": ["../src/**/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import * as SUT from '../../src/utils/parseQuery'; | ||
|
||
describe('parseQuery', () => { | ||
it('should do nothing if the query is a string', () => { | ||
const query = 'SELECT * FROM table'; | ||
const parameters = ['one']; | ||
const result = SUT.parseQuery(query, parameters); | ||
|
||
expect(result).toEqual({ sqlStatement: query, parameters: ['one'] }); | ||
}); | ||
|
||
it('should compile the query and return the sql statement and parameters if the query is compilable', () => { | ||
const sqlStatement = 'SELECT * FROM table'; | ||
const parameters = []; | ||
const query = { | ||
compile: () => ({ sql: sqlStatement, parameters: ['test'] }), | ||
execute: () => Promise.resolve([]) | ||
}; | ||
const result = SUT.parseQuery(query, parameters); | ||
|
||
expect(result).toEqual({ sqlStatement, parameters: ['test'] }); | ||
}); | ||
|
||
it('should throw an error if there is an additional parameter included in a compiled query', () => { | ||
const sqlStatement = 'SELECT * FROM table'; | ||
const parameters = ['additional parameter']; | ||
const query = { | ||
compile: () => ({ sql: sqlStatement, parameters: ['test'] }), | ||
execute: () => Promise.resolve([]) | ||
}; | ||
const result = () => SUT.parseQuery(query, parameters); | ||
|
||
expect(result).toThrowError('You cannot pass parameters to a compiled query.'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.