Commit f3b73674 authored by William Naslund's avatar William Naslund

Added blobl type

parent 61ee0135
import { DBType } from "./type";
export class DBBlob implements DBType {
create() {
return `BYTEA`;
}
}
......@@ -5,4 +5,5 @@ export * from "./text";
export * from "./integer";
export * from "./numeric";
export * from "./id-sequence";
export * from "./uuid";
\ No newline at end of file
export * from "./uuid";
export * from "./blob";
\ No newline at end of file
import * as assert from "assert";
import * as uuid from "uuid/v4"
import { Database, DBTable, DBModel, DBBlob, DBTableConstraint, DBUniqueConstraint, DBField } from "@swirl/db";
import { getTestAdapter } from "./db/adapter";
import { Account } from "./db/account";
import { Contact } from "./db/contact";
import { Organization } from "./db/org";
@DBTable('tests')
class Test extends DBModel {
@DBField('data', new DBBlob())
data: Buffer;
}
describe('DBType (Implementations)', function() {
let db: Database;
beforeEach('Setup the database', async () => {
db = new Database(getTestAdapter());
await db.register(Account).register(Contact).register(Organization).migrate();
});
describe('DBBlob', function() {
it('stores and loads binary data (Buffers)', async () => {
await db.register(Test).migrate();
let tst = new Test();
tst.data = Buffer.from(new Uint8Array([ 4, 5, 6, 128, 127, 126 ]));
await tst.insert();
let res = await db.adapter.command.query(`
SELECT *
FROM tests
`);
const loaded = new Uint8Array(res[0].data);
const stored = new Uint8Array(tst.data);
assert(loaded.length == stored.length);
for(let i=0; i < loaded.length; i++) {
assert(loaded[i] == stored[i]);
}
});
});
afterEach('Reset the database', async () => {
await db.delete();
await db.close();
db = null;
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment