Skip to content

Commit

Permalink
Allow passing raw SQL string to sql method (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
DallasHoff committed Jul 20, 2024
1 parent 5b5dc82 commit 15e5fc4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,17 @@ export class SQLocal {
};

sql = async <T extends Record<string, any>>(
queryTemplate: TemplateStringsArray,
queryTemplate: TemplateStringsArray | string,
...params: unknown[]
): Promise<T[]> => {
const statement = sqlTag(queryTemplate, ...params);
let statement: Statement;

if (typeof queryTemplate === 'string') {
statement = { sql: queryTemplate, params };
} else {
statement = sqlTag(queryTemplate, ...params);
}

const resultRecords = await this.execAndConvert<T>(statement);
return resultRecords;
};
Expand Down
4 changes: 4 additions & 0 deletions test/sql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ describe('sql', () => {

const select2 = await sql`SELECT name FROM groceries ORDER BY id DESC`;
expect(select2).toEqual([{ name: 'white rice' }, { name: 'bread' }]);

const sqlStr = 'SELECT name FROM groceries WHERE id = ?';
const select3 = await sql(sqlStr, 1);
expect(select3).toEqual([{ name: 'bread' }]);
});

it('should be cross-origin isolated', () => {
Expand Down

0 comments on commit 15e5fc4

Please sign in to comment.