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

1.0.5: README tweaks + more tests #12

Merged
merged 1 commit into from
Mar 28, 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ npm i --save sql-template-builder

- **sql\`Statements go here = ${\`value goes here\`}\`** - create a new `SQLQuery`
- **sql(sql\`query\`, 'value', 'other value', sql\`other query\`, ...)** - create a new `SQLQuery` from queries/values joined by `','`
- **sql(...).joinBy(string)** - create a new `SQLQuery` as a statement joined from the underlying queries/values with `.joinBy` argument as the delimiter
- **sql(sql\`query\`, 'value', sql\`other query\`).joinBy(string)** - create a new `SQLQuery` as a statement joined from the underlying queries/values with `.joinBy` argument as the delimiter
- **sql.raw(string)** - create a new `SQLQuery` statement from raw value. **Be careful!**
- query.joinBy(string) - create a new `SQLQuery` with the given string to be used to join the top-level statements
- query.setName(string) - create a new `SQLQuery` with the given name set as the prepared statement name (for Postgres)
Expand All @@ -46,7 +46,7 @@ const rawTableName = sql.raw("my_table_1");

const conditions = [sql`a = ${1}`, sql`c = ${2}`, sql`e = ${3}`];

const conditionQuery = sql(conditions).joinBy(" AND "); // It will join all statements by ' AND '
const conditionQuery = sql(...conditions).joinBy(" AND "); // It will join all statements by ' AND '

const prepared =
sql`SELECT * FROM ${tableName} LEFT OUTER JOIN ${rawTableName} ON(${conditionQuery})`.setName(
Expand Down
25 changes: 25 additions & 0 deletions __tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,31 @@ SQLQuery {
}
`;

exports[`sql-query joins queries and values properly 1`] = `"Hello there $1 how are you?"`;

exports[`sql-query joins queries and values properly 2`] = `"Hello there ? how are you?"`;

exports[`sql-query joins queries and values properly 3`] = `
[
"my friend",
]
`;

exports[`sql-query joins queries and values properly 4`] = `undefined`;

exports[`sql-query joins queries and values properly 5`] = `"$1+2+$2"`;

exports[`sql-query joins queries and values properly 6`] = `"?+2+?"`;

exports[`sql-query joins queries and values properly 7`] = `
[
1,
3,
]
`;

exports[`sql-query joins queries and values properly 8`] = `undefined`;

exports[`sql-query mixes queries and values 1`] = `"$1,query nested query nested nested query = $2, f = $3,$4,other query"`;

exports[`sql-query mixes queries and values 2`] = `"?,query nested query nested nested query = ?, f = ?,?,other query"`;
Expand Down
11 changes: 9 additions & 2 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ describe("sql-query", () => {
testQuery(rootStatement);
});

it("joins queries and values properly", () => {
testQuery(
sql(sql`Hello there`, "my friend", sql`how are you?`).joinBy(" ")
);
testQuery(sql(1, sql`2`, 3).joinBy("+"));
});

it("creates nested query with customly joined statements", () => {
const statements = [sql`a`, 1, sql`with ${sql(99, "Hello")}`, sql`c`, 2];
const statement = sql`WITH ${sql(...statements).joinBy("+")} as A, ${sql(
Expand Down Expand Up @@ -163,7 +170,7 @@ describe("sql-query", () => {
});

it("mixes queries and values", () => {
const data = jest.fn(() => 'fn data');
const data = jest.fn(() => "fn data");

const query = sql(
"value",
Expand All @@ -174,7 +181,7 @@ describe("sql-query", () => {

for (let i = 0; i < 3; i++) {
testQuery(query);
testQuery(query.joinBy('|'));
testQuery(query.joinBy("|"));
}
expect(data).toBeCalledTimes(2);
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Oleg Nosov <olegnosov1@gmail.com>",
"name": "sql-template-builder",
"version": "1.0.4",
"version": "1.0.5",
"main": "./index.js",
"description": "Complex SQL query builder",
"keywords": [
Expand Down
Loading