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

Added ability to pass custom SQL queries into find methods. #259

Merged
merged 1 commit into from
Oct 24, 2018
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
22 changes: 22 additions & 0 deletions packages/webiny-entity-mysql/__tests__/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ describe("count test", () => {
queryStub.restore();
});

test("must generate correct query - custom query and values must be accepted", async () => {
const queryStub = sandbox
.stub(SimpleEntity.getDriver().getConnection(), "query")
.callsFake(() => {
return [[], [{ count: null }]];
});

await SimpleEntity.count({
sql: {
query: "SELECT COUNT(*) FROM `SimpleEntity` WHERE age > ? OR type = ?",
values: [20, "developer"]
},
groupBy: ["something"]
});

expect(queryStub.getCall(0).args[0]).toEqual(
"SELECT COUNT(*) FROM `SimpleEntity` WHERE age > 20 OR type = 'developer'"
);

queryStub.restore();
});

test("should count entities", async () => {
sandbox.stub(SimpleEntity.getDriver().getConnection(), "query").callsFake(() => {
return [{ count: 1 }];
Expand Down
24 changes: 24 additions & 0 deletions packages/webiny-entity-mysql/__tests__/find.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ describe("find test", () => {
queryStub.restore();
});

test("find - must accept given passed custom query and variables", async () => {
const queryStub = sandbox
.stub(SimpleEntity.getDriver().getConnection(), "query")
.callsFake(() => {
return [[], [{ count: null }]];
});

// Left "groupBy", just to make sure it's totally ignored.
await SimpleEntity.find({
sql: {
query: "SELECT SQL_CALC_FOUND_ROWS * FROM `SimpleEntity` WHERE age > ? OR type = ?",
values: [20, "developer"]
},
groupBy: ["something"]
});

expect(queryStub.getCall(0).args[0]).toEqual([
"SELECT SQL_CALC_FOUND_ROWS * FROM `SimpleEntity` WHERE age > 20 OR type = 'developer'",
"SELECT FOUND_ROWS() as count"
]);

queryStub.restore();
});

test("should find entities and total count", async () => {
sandbox.stub(SimpleEntity.getDriver().getConnection(), "query").callsFake(() => [
[
Expand Down
22 changes: 22 additions & 0 deletions packages/webiny-entity-mysql/__tests__/findOne.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ describe("findOne test", () => {
queryStub.restore();
});

test("findOne - must accept custom query and values", async () => {
const queryStub = sandbox
.stub(SimpleEntity.getDriver().getConnection(), "query")
.callsFake(() => {
return [[], [{ count: null }]];
});

await SimpleEntity.findOne({
groupBy: ["something"],
sql: {
query: "SELECT * FROM `SimpleEntity` WHERE age > ? OR type = ? LIMIT 1",
values: [20, "developer"]
}
});

expect(queryStub.getCall(0).args[0]).toEqual(
"SELECT * FROM `SimpleEntity` WHERE age > 20 OR type = 'developer' LIMIT 1"
);

queryStub.restore();
});

test("findOne - should find previously inserted entity", async () => {
sandbox.stub(SimpleEntity.getDriver().getConnection(), "query").callsFake(() => {
return [
Expand Down
1 change: 1 addition & 0 deletions packages/webiny-entity-mysql/src/mysqlDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class MySQLDriver extends Driver {
search: options.search,
groupBy: options.groupBy,
sort: options.sort,
sql: options.sql,
page: options.page,
limit: 1
};
Expand Down
8 changes: 8 additions & 0 deletions packages/webiny-entity-mysql/src/statements/select.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
// @flow
import Statement from "./statement";
import SqlString from "sqlstring";

class Select extends Statement {
generate() {
const options = this.options;
if (options.sql) {
const { query, values } = options.sql;
if (typeof query === "string") {
return SqlString.format(query, values);
}
}

let output = `SELECT`;
if (options.calculateFoundRows) {
output += ` SQL_CALC_FOUND_ROWS`;
Expand Down
3 changes: 2 additions & 1 deletion packages/webiny-entity-mysql/src/statements/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ declare type StatementOptions = {
sort?: string,
where?: Object,
columns?: Array<string>,
onDuplicateKeyUpdate?: boolean
onDuplicateKeyUpdate?: boolean,
sql: { query: string, values: any }
};

class Statement {
Expand Down