Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support raw sql #8

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/factories/createSqlTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ sql.unnest = (
};
};

sql.raw = (
rawSql: string,
): SqlSqlToken => {
return {
sql: rawSql,
type: SqlToken,
values: [],
};
};

export const createSqlTag = <T extends QueryResultRow = QueryResultRow>(): SqlTaggedTemplate<T> => {
return sql;
};
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,12 @@ export type SqlTaggedTemplate<T extends UserQueryResultRow = QueryResultRow> = {
json: (value: SerializableValue) => JsonSqlToken,
jsonb: (value: SerializableValue) => JsonBinarySqlToken,
literalValue: (value: string) => SqlSqlToken,
/**
* **CAUTION:** Use this function with care.
*
* Directly injects a raw SQL string into the query.
*/
raw: (rawSql: string) => SqlSqlToken,
timestamp: (date: Date) => TimestampSqlToken,
unnest: (
// Value might be ReadonlyArray<ReadonlyArray<PrimitiveValueExpression>>,
Expand Down
27 changes: 27 additions & 0 deletions test/helpers/createIntegrationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,33 @@ export const createIntegrationTests = (
await t.throwsAsync(firstConnection.oneFirst(sql`SELECT 1`));
});

test('allows sql.raw to be used', async (t) => {
const pool = await createPool(t.context.dsn, {
PgPool,
});

await pool.query(sql`
CREATE TABLE people (
id SERIAL PRIMARY KEY,
name text
)
`);

await pool.query(sql`
INSERT INTO people (name)
VALUES (${sql.raw('\'foo\'')})
`);

const result = await pool.oneFirst(sql`
SELECT name
FROM people
`);

t.is(result, 'foo');

await pool.end();
});

// We have to test serialization due to the use of different drivers (pg and postgres).
test('serializes json', async (t) => {
const pool = await createPool(t.context.dsn, {
Expand Down
19 changes: 19 additions & 0 deletions test/slonik/templateTags/sql/raw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
createSqlTag,
} from '../../../../src/index.js';
import {
SqlToken,
} from '../../../../src/tokens.js';
import test from 'ava';

const sql = createSqlTag();

test('creates an object describing a query with an inlined raw value', (t) => {
const query = sql`CREATE USER foo WITH PASSWORD ${sql.raw('\'bar\'')}`;

t.deepEqual(query, {
sql: 'CREATE USER foo WITH PASSWORD \'bar\'',
type: SqlToken,
values: [],
});
});
Loading