Skip to content

Commit

Permalink
Add raw method
Browse files Browse the repository at this point in the history
  • Loading branch information
brombal committed Jan 31, 2024
1 parent f554c5d commit 107c3f9
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gha.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This file is renamed to .npmrc during the build process. See the github actions files.
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
2 changes: 2 additions & 0 deletions .github/workflows/build-test-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:

- name: Build & publish core
run: |
mv ./.gha.npmrc ./.npmrc
mv ../README.md ./
npm run build --workspace=core
npm publish --workspace=core
env:
Expand Down
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ const [rows] = await sql`

**Remember that embedding plain strings without the `sql` tag will escape/parameterize them.**

- `sql.raw(value: string)`

To embed variables directly without parameterizing them, use the tag's `.raw()` method:

```js
Expand Down
11 changes: 11 additions & 0 deletions core/SqlTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ export class SqlTag<TQueryInfo, TCursorOptions> {
return new SqlQuery(this.driver, [this.driver.escapeIdentifier(identifier)], []);
}

/**
* Embeds variables directly without parameterizing them.
*
* @param string The string to embed.
* @returns A SqlExpression representing the raw string.
* @example sql.raw('NOW()') // NOW()
*/
raw(string: string): SqlExpression {
return new SqlQuery(this.driver, [string], []);
}

/**
* Joins a series of values or expressions with 'AND', wrapped in parentheses.
*
Expand Down
5 changes: 2 additions & 3 deletions core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sqltags/core",
"version": "0.0.22",
"version": "0.0.24",
"description": "Safely create & execute parameterized SQL queries using template strings 🔧✨ minimal API and works with any db driver (pg, mysql, sqlite, etc).",
"license": "MIT",
"author": {
Expand All @@ -26,8 +26,7 @@
"url": "git://github.com/brombal/sqltags.git"
},
"scripts": {
"build": "rm -rf ./dist && parcel build --no-cache",
"prepublishOnly": "cp ../README.md ./"
"build": "rm -rf ./dist && parcel build --no-cache"
},
"engines": {
"node": ">=16.5"
Expand Down
2 changes: 1 addition & 1 deletion drivers/mysql/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sqltags/mysql",
"version": "0.0.22",
"version": "0.0.24",
"description": "MySQL driver for sqltags (@sqltags/core) 🔧✨ Safely create & execute parameterized SQL queries using template strings",
"license": "MIT",
"author": {
Expand Down
2 changes: 1 addition & 1 deletion drivers/postgres/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sqltags/pg",
"version": "0.0.22",
"version": "0.0.24",
"description": "PostgreSQL driver for SqlTags (@sqltags/core) 🔧✨ Safely create & execute parameterized SQL queries using template strings",
"license": "MIT",
"author": {
Expand Down
2 changes: 1 addition & 1 deletion drivers/sqlite/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sqltags/sqlite",
"version": "0.0.22",
"version": "0.0.24",
"description": "SQLite driver for sqltags (@sqltags/core) 🔧✨ Safely create & execute parameterized SQL queries using template strings",
"license": "MIT",
"author": {
Expand Down
13 changes: 13 additions & 0 deletions test/sqltags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,19 @@ describe('sqltags', () => {
]);
});

test('.raw embeds raw strings', async () => {
const [sql] = createMockSqlTag();
const [, info] = await sql`
SELECT * FROM users
WHERE date = ${sql.raw('NOW()')}
`;

expect(info).toEqual([
`SELECT * FROM users
WHERE date = NOW()`,
]);
});

test('calling .compile() returns sql and params', async () => {
const [sql] = createMockSqlTag();
const debug = sql`
Expand Down

0 comments on commit 107c3f9

Please sign in to comment.