node-pg-large-object
Large object support for PostgreSQL clients using the node-postgres library.
The API of this library resembles the JDBC library for PostgreSQL.
Installation
npm install --save pg-large-object
You will also need to install either the pg library, or the pg-promise library:
npm install --save pg
# or
npm install --save pg-promise
Some of the methods in this library require PostgreSQL 9.3 (server) and up:
- LargeObject.seek()
- LargeObject.tell()
- LargeObject.size()
- LargeObject.truncate()
All other methods should work on PostgreSQL 8.4 and up.
Large Objects
Large Objects in PostgreSQL lets you store files/objects up to 4 TiB in size. The main benefit of using Large Objects instead of a simple column is that the data can be read and written in chunks (e.g. as a stream), instead of having to load the entire column into memory.
Examples
The easiest way to use this library is in combination with promises and pg-promise. This library exposes a callback style interface (for backwards compatibility) and a promise style interface (see API Documentation). All functions that end with "Async" will return a promise
Reading a large object using a stream and pg-promise:
const pgp = ;const LargeObjectManager = ;const createWriteStream = ; const db = ; // When working with Large Objects, always use a transactiondb;
Creating a new large object using a stream and pg-promise:
const pgp = ;const LargeObjectManager = ;const createReadStream = ; const db = ; // When working with Large Objects, always use a transactiondb;
Reading a large object using a stream and callbacks:
var pg = ;var LargeObjectManager = LargeObjectManager;var conString = "postgres:https://postgres:1234@localhost/postgres"; pg;
Creating a new large object using a stream:
var pg = ;var LargeObjectManager = LargeObjectManager;var conString = "postgres:https://postgres:1234@localhost/postgres"; pg;
Using low level LargeObject functions:
var pg = ;var LargeObjectManager = LargeObjectManager;var LargeObject = LargeObject;var conString = "postgres:https://postgres:1234@localhost/postgres"; pg;
Testing
You can test this library by running:
npm install pg-large-object
npm test
The test assumes that postgres:https://nodetest:nodetest@localhost/nodetest is a valid database. You also need to place a large file named "test.jpg" in the test folder.
API Documentation
Modules
- pg-large-object
- pg-large-object/lib/LargeObject
Represents an opened large object.
- pg-large-object/lib/LargeObjectManager
This class lets you use the Large Object functionality of PostgreSQL. All usage of Large Object should take place within a transaction block! (BEGIN ... COMMIT)
- pg-large-object/lib/promiseFromCallback ⇒
Promise
- pg-large-object/lib/ReadStream ⇐
stream.Readable
- pg-large-object/lib/WriteStream ⇐
stream.Writable
pg-large-object
- pg-large-object
- .LargeObjectManager :
function
- .LargeObject :
function
- .ReadStream :
function
- .WriteStream :
function
- .LargeObjectManager :
function
pg-large-object.LargeObjectManager : pg-large-object/lib/LargeObjectManager
Kind: static constant of pg-large-object
function
pg-large-object.LargeObject : pg-large-object/lib/LargeObject
Kind: static constant of pg-large-object
function
pg-large-object.ReadStream : pg-large-object/lib/ReadStream
Kind: static constant of pg-large-object
function
pg-large-object.WriteStream : pg-large-object/lib/WriteStream
Kind: static constant of pg-large-object
pg-large-object/lib/LargeObject
Represents an opened large object.
- pg-large-object/lib/LargeObject
- instance
- .close([callback])
- .closeAsync() ⇒
Promise
- .read(length, callback)
- .readAsync(length) ⇒
Promise.<Buffer>
- .write(buffer, [callback])
- .writeAsync(buffer) ⇒
Promise
- .seek(position, ref, [callback])
- .seekAsync(position, ref) ⇒
Promise.<number>
- .tell(callback)
- .tellAsync() ⇒
Promise.<number>
- .size(callback)
- .sizeAsync() ⇒
Promise.<number>
- .truncate(length, [callback])
- .truncateAsync(length) ⇒
Promise
- .getReadableStream([bufferSize]) ⇒
pg-large-object/lib/ReadStream
- .getWritableStream([bufferSize]) ⇒
pg-large-object/lib/WriteStream
- static
- inner
- ~closeCallback :
function
- ~readCallback :
function
- ~writeCallback :
function
- ~seekCallback :
function
- ~tellCallback :
function
- ~sizeCallback :
function
- ~truncateCallback :
function
- ~closeCallback :
- instance
pg-large-object/lib/LargeObject.close([callback])
Closes this large object. You should no longer call any methods on this object.
Kind: instance method of pg-large-object/lib/LargeObject
Param | Type |
---|---|
[callback] | closeCallback |
Promise
pg-large-object/lib/LargeObject.closeAsync() ⇒ Closes this large object. You should no longer call any methods on this object.
Kind: instance method of pg-large-object/lib/LargeObject
pg-large-object/lib/LargeObject.read(length, callback)
Reads some data from the large object.
Kind: instance method of pg-large-object/lib/LargeObject
Param | Type | Description |
---|---|---|
length | Number |
How many bytes to read |
callback | readCallback |
Promise.<Buffer>
pg-large-object/lib/LargeObject.readAsync(length) ⇒ Reads some data from the large object.
Kind: instance method of pg-large-object/lib/LargeObject
Returns: Promise.<Buffer>
- The binary data that was read.
If the length of this buffer is less than the supplied
length param, there is no more data to be read.
Param | Type | Description |
---|---|---|
length | Number |
How many bytes to read |
pg-large-object/lib/LargeObject.write(buffer, [callback])
Writes some data to the large object.
Kind: instance method of pg-large-object/lib/LargeObject
Param | Type | Description |
---|---|---|
buffer | Buffer |
data to write |
[callback] | writeCallback |
Promise
pg-large-object/lib/LargeObject.writeAsync(buffer) ⇒ Writes some data to the large object.
Kind: instance method of pg-large-object/lib/LargeObject
Param | Type | Description |
---|---|---|
buffer | Buffer |
data to write |
pg-large-object/lib/LargeObject.seek(position, ref, [callback])
Sets the position within the large object. Beware floating point rounding with values greater than 2^53 (8192 TiB)
Kind: instance method of pg-large-object/lib/LargeObject
Param | Type | Description |
---|---|---|
position | Number |
|
ref | Number |
One of SEEK_SET, SEEK_CUR, SEEK_END |
[callback] | seekCallback |
Promise.<number>
pg-large-object/lib/LargeObject.seekAsync(position, ref) ⇒ Sets the position within the large object. Beware floating point rounding with values greater than 2^53 (8192 TiB)
Kind: instance method of pg-large-object/lib/LargeObject
Returns: Promise.<number>
- The new position
Param | Type | Description |
---|---|---|
position | Number |
|
ref | Number |
One of SEEK_SET, SEEK_CUR, SEEK_END |
pg-large-object/lib/LargeObject.tell(callback)
Retrieves the current position within the large object. Beware floating point rounding with values greater than 2^53 (8192 TiB)
Kind: instance method of pg-large-object/lib/LargeObject
Param | Type |
---|---|
callback | tellCallback |
Promise.<number>
pg-large-object/lib/LargeObject.tellAsync() ⇒ Retrieves the current position within the large object. Beware floating point rounding with values greater than 2^53 (8192 TiB)
Kind: instance method of pg-large-object/lib/LargeObject
pg-large-object/lib/LargeObject.size(callback)
Find the total size of the large object.
Kind: instance method of pg-large-object/lib/LargeObject
Param | Type |
---|---|
callback | sizeCallback |
Promise.<number>
pg-large-object/lib/LargeObject.sizeAsync() ⇒ Find the total size of the large object.
Kind: instance method of pg-large-object/lib/LargeObject
pg-large-object/lib/LargeObject.truncate(length, [callback])
Truncates the large object to the given length in bytes. If the number of bytes is larger than the current large object length, the large object will be filled with zero bytes. This method does not modify the current file offset.
Kind: instance method of pg-large-object/lib/LargeObject
Param | Type |
---|---|
length | Number |
[callback] | truncateCallback |
Promise
pg-large-object/lib/LargeObject.truncateAsync(length) ⇒ Truncates the large object to the given length in bytes. If the number of bytes is larger than the current large object length, the large object will be filled with zero bytes. This method does not modify the current file offset.
Kind: instance method of pg-large-object/lib/LargeObject
Param | Type |
---|---|
length | Number |
pg-large-object/lib/ReadStream
pg-large-object/lib/LargeObject.getReadableStream([bufferSize]) ⇒ Return a stream to read this large object. Call this within a transaction block.
Kind: instance method of pg-large-object/lib/LargeObject
Param | Type | Default | Description |
---|---|---|---|
[bufferSize] | Number |
16384 |
A larger buffer size will require more memory on both the server and client, however it will make transfers faster because there is less overhead (less read calls to the server). his overhead is most noticeable on high latency connections because each ransfered chunk will incur at least RTT of additional transfer time. |
pg-large-object/lib/WriteStream
pg-large-object/lib/LargeObject.getWritableStream([bufferSize]) ⇒ Return a stream to write to this large object. Call this within a transaction block.
Kind: instance method of pg-large-object/lib/LargeObject
Param | Type | Default | Description |
---|---|---|---|
[bufferSize] | Number |
16384 |
A larger buffer size will require more memory on both the server and client, however it will make transfers faster because there is less overhead (less read calls to the server). his overhead is most noticeable on high latency connections because each ransfered chunk will incur at least RTT of additional transfer time. |
Number
pg-large-object/lib/LargeObject.SEEK_SET : A seek from the beginning of a object
Kind: static constant of pg-large-object/lib/LargeObject
Number
pg-large-object/lib/LargeObject.SEEK_CUR : A seek from the current position
Kind: static constant of pg-large-object/lib/LargeObject
Number
pg-large-object/lib/LargeObject.SEEK_END : A seek from the end of a object
Kind: static constant of pg-large-object/lib/LargeObject
function
pg-large-object/lib/LargeObject~closeCallback : Kind: inner typedef of pg-large-object/lib/LargeObject
Param | Type | Description |
---|---|---|
error | Error |
If set, an error occurred. |
function
pg-large-object/lib/LargeObject~readCallback : Kind: inner typedef of pg-large-object/lib/LargeObject
Param | Type | Description |
---|---|---|
error | Error |
If set, an error occurred. |
data | Buffer |
The binary data that was read. If the length of this buffer is less than the supplied length param, there is no more data to be read. |
function
pg-large-object/lib/LargeObject~writeCallback : Kind: inner typedef of pg-large-object/lib/LargeObject
Param | Type | Description |
---|---|---|
error | Error |
If set, an error occurred. |
function
pg-large-object/lib/LargeObject~seekCallback : Kind: inner typedef of pg-large-object/lib/LargeObject
Param | Type | Description |
---|---|---|
error | Error |
If set, an error occurred. |
position | Number |
The new position |
function
pg-large-object/lib/LargeObject~tellCallback : Kind: inner typedef of pg-large-object/lib/LargeObject
Param | Type | Description |
---|---|---|
error | Error |
If set, an error occurred. |
position | Number |
The position |
function
pg-large-object/lib/LargeObject~sizeCallback : Kind: inner typedef of pg-large-object/lib/LargeObject
Param | Type | Description |
---|---|---|
error | Error |
If set, an error occurred. |
size | Number |
Object size in bytes |
function
pg-large-object/lib/LargeObject~truncateCallback : Kind: inner typedef of pg-large-object/lib/LargeObject
Param | Type | Description |
---|---|---|
error | Error |
If set, an error occurred. |
pg-large-object/lib/LargeObjectManager
This class lets you use the Large Object functionality of PostgreSQL. All usage of Large Object should take place within a transaction block! (BEGIN ... COMMIT)
Param | Type | Description |
---|---|---|
options | object |
Either pg or pgPromise must be given |
options.pg | module:pg/Client |
A pg (https://www.npmjs.com/package/pg) Client object |
options.pgPromise | module:pg-promise/Task |
A pg-promise (https://www.npmjs.com/package/pg-promise) transaction object as given by db.tx() |
Example
client
- pg-large-object/lib/LargeObjectManager
- instance
- .open(oid, mode, callback)
- .openAsync(oid, mode) ⇒
Promise.<pg-large-object/lib/LargeObject>
- .create(callback)
- .createAsync() ⇒
Promise.<number>
- .unlink(oid, [callback])
- .unlinkAsync(oid) ⇒
Promise
- .openAndReadableStream(oid, [bufferSize], callback)
- .openAndReadableStreamAsync(oid, [bufferSize]) ⇒
Promise.<Array>
- .createAndWritableStream([bufferSize], [callback])
- .createAndWritableStreamAsync([bufferSize]) ⇒
promise.<Array>
- static
- .WRITE :
Number
- .READ :
Number
- .READWRITE :
Number
- .WRITE :
- inner
- ~openCallback :
function
- ~createCallback :
function
- ~unlinkCallback :
function
- ~openAndReadableStreamCallback :
function
- ~createAndWritableStreamCallback :
function
- ~openCallback :
- instance
pg-large-object/lib/LargeObjectManager.open(oid, mode, callback)
Open an existing large object, based on its OID. In mode READ, the data read from it will reflect the contents of the large object at the time of the transaction snapshot that was active when open was executed, regardless of later writes by this or other transactions. If opened using WRITE (or READWRITE), data read will reflect all writes of other committed transactions as well as writes of the current transaction.
Kind: instance method of pg-large-object/lib/LargeObjectManager
Param | Type | Description |
---|---|---|
oid | Number |
|
mode | Number |
One of WRITE, READ, or READWRITE |
callback | openCallback |
Promise.<pg-large-object/lib/LargeObject>
pg-large-object/lib/LargeObjectManager.openAsync(oid, mode) ⇒ Open an existing large object, based on its OID. In mode READ, the data read from it will reflect the contents of the large object at the time of the transaction snapshot that was active when open was executed, regardless of later writes by this or other transactions. If opened using WRITE (or READWRITE), data read will reflect all writes of other committed transactions as well as writes of the current transaction.
Kind: instance method of pg-large-object/lib/LargeObjectManager
Param | Type | Description |
---|---|---|
oid | Number |
|
mode | Number |
One of WRITE, READ, or READWRITE |
pg-large-object/lib/LargeObjectManager.create(callback)
Creates a large object, returning its OID. After which you can open() it.
Kind: instance method of pg-large-object/lib/LargeObjectManager
Param | Type |
---|---|
callback | createCallback |
Promise.<number>
pg-large-object/lib/LargeObjectManager.createAsync() ⇒ Creates a large object, returning its OID. After which you can open() it.
Kind: instance method of pg-large-object/lib/LargeObjectManager
Returns: Promise.<number>
- oid
pg-large-object/lib/LargeObjectManager.unlink(oid, [callback])
Unlinks (deletes) a large object
Kind: instance method of pg-large-object/lib/LargeObjectManager
Param | Type |
---|---|
oid | number |
[callback] | unlinkCallback |
Promise
pg-large-object/lib/LargeObjectManager.unlinkAsync(oid) ⇒ Unlinks (deletes) a large object
Kind: instance method of pg-large-object/lib/LargeObjectManager
Param | Type |
---|---|
oid | number |
pg-large-object/lib/LargeObjectManager.openAndReadableStream(oid, [bufferSize], callback)
Open a large object, return a stream and close the object when done streaming. Only call this within a transaction block.
Kind: instance method of pg-large-object/lib/LargeObjectManager
Param | Type | Default |
---|---|---|
oid | Number |
|
[bufferSize] | Number |
16384 |
callback | openAndReadableStreamCallback |
Promise.<Array>
pg-large-object/lib/LargeObjectManager.openAndReadableStreamAsync(oid, [bufferSize]) ⇒ Open a large object, return a stream and close the object when done streaming. Only call this within a transaction block.
Kind: instance method of pg-large-object/lib/LargeObjectManager
Returns: Promise.<Array>
- The total size and a ReadStream
Param | Type | Default |
---|---|---|
oid | Number |
|
[bufferSize] | Number |
16384 |
pg-large-object/lib/LargeObjectManager.createAndWritableStream([bufferSize], [callback])
Create and open a large object, return a stream and close the object when done streaming. Only call this within a transaction block.
Kind: instance method of pg-large-object/lib/LargeObjectManager
Param | Type | Default |
---|---|---|
[bufferSize] | Number |
16384 |
[callback] | createAndWritableStreamCallback |
promise.<Array>
pg-large-object/lib/LargeObjectManager.createAndWritableStreamAsync([bufferSize]) ⇒ Create and open a large object, return a stream and close the object when done streaming. Only call this within a transaction block.
Kind: instance method of pg-large-object/lib/LargeObjectManager
Returns: promise.<Array>
- The oid and a WriteStream
Param | Type | Default |
---|---|---|
[bufferSize] | Number |
16384 |
Number
pg-large-object/lib/LargeObjectManager.WRITE : Kind: static constant of pg-large-object/lib/LargeObjectManager
Number
pg-large-object/lib/LargeObjectManager.READ : Kind: static constant of pg-large-object/lib/LargeObjectManager
Number
pg-large-object/lib/LargeObjectManager.READWRITE : Kind: static constant of pg-large-object/lib/LargeObjectManager
function
pg-large-object/lib/LargeObjectManager~openCallback : Kind: inner typedef of pg-large-object/lib/LargeObjectManager
Param | Type | Description |
---|---|---|
error | Error |
If set, an error occurred. |
result | pg-large-object/lib/LargeObject |
function
pg-large-object/lib/LargeObjectManager~createCallback : Kind: inner typedef of pg-large-object/lib/LargeObjectManager
Param | Type | Description |
---|---|---|
error | Error |
If set, an error occurred. |
oid | Number |
function
pg-large-object/lib/LargeObjectManager~unlinkCallback : Kind: inner typedef of pg-large-object/lib/LargeObjectManager
Param | Type | Description |
---|---|---|
error | Error |
If set, an error occurred. |
function
pg-large-object/lib/LargeObjectManager~openAndReadableStreamCallback : Kind: inner typedef of pg-large-object/lib/LargeObjectManager
Param | Type | Description |
---|---|---|
error | Error |
If set, an error occurred. |
size | Number |
The total size of the large object |
stream | pg-large-object/lib/ReadStream |
function
pg-large-object/lib/LargeObjectManager~createAndWritableStreamCallback : Kind: inner typedef of pg-large-object/lib/LargeObjectManager
Param | Type | Description |
---|---|---|
error | Error |
If set, an error occurred. |
oid | Number |
|
stream | pg-large-object/lib/WriteStream |
Promise
pg-large-object/lib/promiseFromCallback ⇒ Param | Type |
---|---|
fn | function |
self | object |
[options] | object |
stream.Readable
pg-large-object/lib/ReadStream ⇐
stream.Writable
pg-large-object/lib/WriteStream ⇐ Extends: stream.Writable