pgStructure

module.exports(pgOptions, [schemas], options) ⇒ Promise.<Db>

Creates and returns Db instance by reverse engineering PostgreSQL database.

Kind: Exported function
Returns: Promise.<Db> - - Db.
Throws:

  • Error - Throws if one of the requested shchemas does not exists on database.
Param Type Default Description
pgOptions pgOptions | pg#client node-postgres client or connection parameters. Parameters passed directly to node-postgres. See it for details.
[schemas] string | Array.<string> "['public']" PostgreSQL schemas to be parsed.
options Object pg-structure options.
[options.cache] boolean true Use cache to memoize calculated results.

Example

var pgStructure = require('pg-structure');

pgStructure({database: 'db', user: 'user', password: 'password'}, ['public', 'other_schema'])
    .then((db) => { console.log( db.get('public.account').columns[0].name ); })
    .catch(err => console.log(err.stack));

module.exports.save(file, db) ⇒ Promise.<string>

Saves given database structure to a disk file. If given file name ends with .zip extension, file will be saved as compressed zip file.

Kind: static method of module.exports
Returns: Promise.<string> - - Serialized string.

Param Type Description
file string | undefined File path to save database structure.
db Db Db object to save.

Example

var pgStructure = require('pg-structure');

pgStructure({database: 'db', user: 'user', password: 'password', host: 'localhost', port: 5432}, ['public', 'other_schema'])
    .then(db => pgStructure.save('./db.json', db))
    .catch(err => console.log(err.stack));

module.exports.load(file) ⇒ Promise.<(Db|undefined)>

Loads database structure from previously saved file. Much faster than getting structure from database. If file is a zip file which contains a json file with same name as zip file, this function decompresses the file automatically.
pgStructure cannot load files saved by incompatible pg-structure module versions and returns undefined. In this case you should fetch structure from database and create a new save file.

Kind: static method of module.exports
Returns: Promise.<(Db|undefined)> - - Db instance or undefined if saved file is generated with incompatible module version.

Param Type Description
file string File path to load db structure from.

Example

var pgStructure = require('pg-structure');

pgStructure.load('./db.json')
    .then(db => console.log(db.schemas[0].name))
    .catch(err => console.log(err.stack));

module.exports.serialize(db) ⇒ string

Serializes database structure to make it possible to store or transfer.

Kind: static method of module.exports
Returns: string - - Serialized database structure.

Param Type Description
db Db Db instance to serialize.

Example

pgStructure({database: 'db', user: 'user', password: 'password', host: 'localhost', port: 5432}, ['public', 'other_schema'])
    .then(db => pgStructure.serialize(db))
    .then(data => console.log(data))
    .catch(err => console.log(err.stack));

module.exports.toString(db) ⇒ string

Alias of {@link module:pgStructure.serialize). Serializes database structure to make it possible to store or transfer.

Kind: static method of module.exports
Returns: string - - Serialized database structure.
See: {@link module:pgStructure.serialize)

Param Type Description
db Db Db instance to serialize.

module.exports.deserialize(serializedDbJSON) ⇒ Db | undefined

Creates and returns Db instance using previously serialized string.
pgStructure cannot deserialize incompatible pg-structure module versions and returns undefined. In this case you should fetch structure from database.

Kind: static method of module.exports
Returns: Db | undefined - - Db instance. If serialized string is from incompatible module version, this is undefined var pgStructure = require('pg-structure');

pgStructure.deserialize('./db.json') .then(db => console.log(db.schemas[0].name) .catch(err => console.log(err.stack));

Param Type Description
serializedDbJSON string Serialized database structure to create Db instance from.

module.exports.parse(serializedDb) ⇒ Db | undefined

Alias of deserialize. Creates and returns Db instance using previously serialized string.
pgStructure cannot deserialize incompatible pg-structure module versions and returns undefined. In this case you should fetch structure from database.

Kind: static method of module.exports
Returns: Db | undefined - - Db instance. If serialized string is from incompatible module version, this is undefined
See: deserialize

Param Type Description
serializedDb string Serialized database structure to create Db instance from.

module.exports~pgOptions : Object

PostgreSQL connection options which are passed directly to node-postgres.

Kind: inner typedef of module.exports
Properties

Name Type Default Description
database string Database name
[host] string "localhost" Hostname of the database.
[port] number 5432 Port of the database.
[user] string Username for connecting to db.
[password] string Password to connecting to db.
[ssl] boolean | Object false Pass the same options as tls.connect().