Skip to content

Commit

Permalink
Node.js Blob
Browse files Browse the repository at this point in the history
  • Loading branch information
mewim committed Jul 7, 2023
1 parent d587f85 commit 15ced33
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions tools/nodejs_api/src_cpp/node_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ Napi::Value Util::ConvertToNapiObject(const Value& value, Napi::Env env) {
case LogicalTypeID::DOUBLE: {
return Napi::Number::New(env, value.getValue<double>());
}
case LogicalTypeID::STRING:
case LogicalTypeID::BLOB: {
case LogicalTypeID::STRING: {
return Napi::String::New(env, value.getValue<std::string>());
}
case LogicalTypeID::BLOB: {
auto blobVal = value.getValue<std::string>();
return Napi::Buffer<char>::Copy(env, blobVal.c_str(), blobVal.size());
}
case LogicalTypeID::DATE: {
auto dateVal = value.getValue<date_t>();
// Javascript Date type contains both date and time information. This returns the Date at
Expand Down
18 changes: 18 additions & 0 deletions tools/nodejs_api/test/test_data_type.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ describe("STRING", function () {
});
});

describe("BLOB", function () {
it("should convert BLOB type", async function () {
const queryResult = await conn.query(
"RETURN BLOB('\\\\xAA\\\\xBB\\\\xCD\\\\x1A');"
);
const result = await queryResult.getAll();
assert.equal(result.length, 1);
assert.equal(Object.keys(result[0]).length, 1);
const value = Object.values(result[0])[0];
assert.isTrue(value instanceof Uint8Array);
assert.equal(value.length, 4);
assert.equal(value[0], 0xaa);
assert.equal(value[1], 0xbb);
assert.equal(value[2], 0xcd);
assert.equal(value[3], 0x1a);
});
});

describe("DATE", function () {
it("should convert DATE type", async function () {
const queryResult = await conn.query(
Expand Down

0 comments on commit 15ced33

Please sign in to comment.