Skip to content

Commit

Permalink
Remove bs-json usage
Browse files Browse the repository at this point in the history
  • Loading branch information
scull7 committed Jul 27, 2019
1 parent 7b7573f commit acce994
Show file tree
Hide file tree
Showing 6 changed files with 462 additions and 338 deletions.
43 changes: 32 additions & 11 deletions __tests__/query.re
Expand Up @@ -32,6 +32,32 @@ let onMutation = (next, fn, res) =>
| `Select(_) => fail("unexpected_select_result") |> next
};

module DatabaseListRecord = {
[@bs.deriving abstract]
type t = {
[@bs.as "Database"]
databaseName: string,
};

external decode: 'a => t = "%identity";
};

module DatabaseNullRecord = {
type t;

external decode: 'a => t = "%identity";
};

module DatabaseCodeRecord = {
[@bs.deriving abstract]
type t = {
id: int,
code: string,
};

external decode: 'a => t = "%identity";
};

describe("Raw SQL Query Test", () => {
let conn = connect();
afterAll(() => MySql2.Connection.close(conn));
Expand All @@ -41,8 +67,8 @@ describe("Raw SQL Query Test", () => {
"SHOW DATABASES",
None,
onSelect(finish, (select, next) =>
select->(MySql2.Select.flatMap(Json.Decode.dict(Json.Decode.string)))
|> Js.Array.map(x => Js.Dict.unsafeGet(x, "Database"))
select->MySql2.Select.flatMap(DatabaseListRecord.decode)
|> Js.Array.map(DatabaseListRecord.databaseNameGet)
|> Expect.expect
|> Expect.toContain("test")
|> next
Expand Down Expand Up @@ -116,13 +142,12 @@ describe("Raw SQL Query Test Sequence", () => {
});
testAsync("Expect a SELECT NULL to return an empty array", finish => {
let sql = "SELECT NULL FROM `test`.`simple` WHERE false";
let decoder = Json.Decode.dict(Json.Decode.nullable(Json.Decode.string));
MySql2.execute(
conn,
sql,
None,
onSelect(finish, (select, next) =>
select->(MySql2.Select.flatMap(decoder))
select->(MySql2.Select.flatMap(DatabaseNullRecord.decode))
|> Expect.expect
|> Expect.toHaveLength(0)
|> next
Expand All @@ -131,14 +156,10 @@ describe("Raw SQL Query Test Sequence", () => {
});
testAsync("Expect a SELECT * to respond with all the columns", finish => {
let sql = "SELECT * FROM `test`.`simple`";
let decoder = json =>
Json.Decode.{
id: json |> field("id", int),
code: json |> field("code", string),
};
let decoder = DatabaseCodeRecord.decode;
let first_row = x => {
let idIsOne = x[0].id == 1;
let codeIsFoo = x[0].code == "foo";
let idIsOne = DatabaseCodeRecord.idGet(x[0]) == 1;
let codeIsFoo = DatabaseCodeRecord.codeGet(x[0]) == "foo";
[|idIsOne, codeIsFoo|];
};
MySql2.execute(
Expand Down
40 changes: 22 additions & 18 deletions __tests__/with_params.re
Expand Up @@ -12,28 +12,35 @@ let connect = () =>

type result = {result: int};

module Decoder = {
[@bs.deriving abstract]
type t = {result: int};

external decode: 'a => t = "%identity";
};

describe("Test parameter interpolation", () => {
let conn = connect();
let decoder = json => Json.Decode.{result: json |> field("result", int)};
let decoder = Decoder.decode;

afterAll(() => MySql2.Connection.close(conn));

describe("Standard (positional) parameters", () =>
testAsync("Expect parameters to be substituted properly", finish => {
let sql = "SELECT 1 + ? + ? AS result";
let params =
Some(
MySql2.Params.positional(
Belt_Array.map([|5, 6|], Json.Encode.int)
|> Json.Encode.jsonArray,
Belt_Array.map([|5, 6|], float_of_int) |> Js.Json.numberArray,
),
);
MySql2.execute(conn, sql, params, res =>
switch (res) {
| `Error(e) => raise(e |> MySql2.Exn.toExn)
| `Mutation(_) => fail("unexpected_mutation_result") |> finish
| `Select(select) =>
select
|. MySql2.Select.flatMapWithMeta((row, _) => row |. decoder)
|> Belt_Array.map(_, x => x.result)
select->(MySql2.Select.flatMapWithMeta((row, _) => row->decoder))
|> Belt_Array.map(_, x => x->Decoder.resultGet)
|> Expect.expect
|> Expect.toBeSupersetOf([|12|])
|> finish
Expand All @@ -44,23 +51,20 @@ describe("Test parameter interpolation", () => {
describe("Named parameters", () =>
testAsync("Expect parameters to be substituted properly", finish => {
let sql = "SELECT :x + :y AS result";
let params =
Some(
MySql2.Params.named(
Json.Encode.object_([
("x", Json.Encode.int(1)),
("y", Json.Encode.int(2)),
]),
),
);

let obj = Js.Dict.empty();
obj->Js.Dict.set("x", float_of_int(1)->Js.Json.number);
obj->Js.Dict.set("y", float_of_int(2)->Js.Json.number);

let params = Some(obj->Js.Json.object_->MySql2.Params.named);

MySql2.execute(conn, sql, params, res =>
switch (res) {
| `Error(e) => raise(e |> MySql2.Exn.toExn)
| `Mutation(_) => fail("unexpected_mutation_result") |> finish
| `Select(select) =>
select
|. MySql2.Select.flatMap(decoder)
|> Belt_Array.map(_, x => x.result)
select->(MySql2.Select.flatMap(decoder))
|> Belt_Array.map(_, x => x->Decoder.resultGet)
|> Expect.expect
|> Expect.toBeSupersetOf([|3|])
|> finish
Expand Down
2 changes: 1 addition & 1 deletion bsconfig.json
Expand Up @@ -17,7 +17,7 @@
"type": "dev"
}
],
"bs-dev-dependencies": [ "@glennsl/bs-jest", "@glennsl/bs-json" ],
"bs-dev-dependencies": [ "@glennsl/bs-jest" ],
"bs-dependencies": [],
"bsc-flags": [ "-bs-super-errors" ],
"refmt": 3
Expand Down
20 changes: 8 additions & 12 deletions examples/prepared_statements.re
Expand Up @@ -3,9 +3,9 @@ let conn =

let positional =
Some(
MySql2.Params.positional(
Belt_Array.map([|5, 6|], Json.Encode.int) |> Json.Encode.jsonArray,
),
Belt_Array.map([|5, 6|], float_of_int)
|> Js.Json.numberArray
|> MySql2.Params.positional,
);

MySql2.execute(conn, "SELECT 1 + ? + ? AS result", positional, res =>
Expand All @@ -16,15 +16,11 @@ MySql2.execute(conn, "SELECT 1 + ? + ? AS result", positional, res =>
}
);

let named =
Some(
MySql2.Params.named(
Json.Encode.object_([
("x", Json.Encode.int(1)),
("y", Json.Encode.int(2)),
]),
),
);
let obj = Js.Dict.empty();
obj->Js.Dict.set("x", float_of_int(1)->Js.Json.number);
obj->Js.Dict.set("y", float_of_int(2)->Js.Json.number);

let named = Some(obj->Js.Json.object_->MySql2.Params.named);

MySql2.execute(conn, "SELECT :x + :y AS result", named, res =>
switch (res) {
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -40,7 +40,6 @@
},
"devDependencies": {
"@glennsl/bs-jest": "^0.4.8",
"@glennsl/bs-json": "^4.0.0",
"bs-platform": "^5.0.4",
"coveralls": "^3.0.3",
"husky": "^2.3.0",
Expand Down

0 comments on commit acce994

Please sign in to comment.