pg-type
v1.0.2
Published
Adds new types to the database and updates enums
Downloads
104
Maintainers
Readme
PG Type
Dynamically adds new types to the database and adds new values to existing enums
install
npm install -S pg-type
usage
// Tell pg-type how to query your db
const pg = require('pg')
const query = (query, values, callback) => {
const client = new pg.Client('postgres://localhost/mydb')
client.connect(error => {
if (error) return callback(error)
client.query(query, values, (error, results) => {
client.end()
if (error) return callback(error)
callback(null, results)
})
})
}
require('pg-type')()
.query(query)
.types({
some_enum_type: ['val_1', 'val_2', 'val_3']
, some_other_type: { type: 'domain', as: 'int check ( value < 100 )' }
})
.create( function( error, results ){
/* Types added/modified! */
})
Then later on you end up modifying your apps types:
require('pg-type')()
.query('postgres://localhost/mydb')
.types({
some_enum_type: ['val_1', 'val_2', 'val_3', 'val_4', 'val_5']
, some_other_type: { type: 'domain', as: 'int check ( value < 100 )' }
, some_other_type2: { type: 'domain', as: 'int check ( value > 200 )' }
})
.create( function( error, results ){
/* Types added/modified! */
})
pgtype will modify your enums (never drops) and add new types.
API
Root Namespace
require('pg-type') -> Function
Pg Type creator factory
TypeCreator
.query(query: string, values: any[], callback: NodeCallback) -> Function|This
Gets or sets the query interface
.types([types]) -> String|This
Gets or mixes in types
.create(callback) -> This
Creates new types/enums and checks to see if enums need to be modified.
Will not remove enum values so you'll still need to use something like pg-delta for that.