How to use pg

Comprehensive pg code examples:

How to use pg.types:

67
68
69
70
71
72
73
74
75
76
77
loadTypeParser()


function loadTypeParser () {
  // https://github.com/tgriesser/knex/issues/387
  // converts PostgreSQL `bigint` into Javascript integers
  pg.types.setTypeParser(20, 'text', (value) => {
    if (Number.MAX_SAFE_INTEGER < value) {
      throw new Error(`The value ${value} exceeds the JS max integer value ${Number.MAX_SAFE_INTEGER}`)
    }

How to use pg.default:

45
46
47
48
49
50
51
52
53
54
const team_route_1 = __importDefault(require("./endpoints/team.route"));
const rank_route_1 = __importDefault(require("./endpoints/rank.route"));
const score_route_1 = __importDefault(require("./endpoints/score.route"));
const leaderboard_route_1 = __importDefault(require("./endpoints/leaderboard.route"));
const pgSession = (0, connect_pg_simple_1.default)(express_session_1.default);
const pgPool = new pg_1.default.Pool({
    database: sequelize_1.DB_NAME,
    host: sequelize_1.HOST,
    user: sequelize_1.USERNAME,
    password: sequelize_1.PASSWORD,

How to use pg.defaults:

119
120
121
122
123
124
125
126
127
128
129
const startWorker = async (workerId) => {
  log.warn(`Starting worker ${workerId}`);


  const pgConfigs = {
    development: {
      user:     process.env.DB_USER || pg.defaults.user,
      password: process.env.DB_PASS || pg.defaults.password,
      database: process.env.DB_NAME || 'mastodon_development',
      host:     process.env.DB_HOST || pg.defaults.host,
      port:     process.env.DB_PORT || pg.defaults.port,

How to use pg.Pool:

11
12
13
14
15
16
17
18
19
20
21
  return connect(json, cb);
}


function connectPool(json, cb) {
  var numConnections = json.connectionLimit || 0;
  var pool = new Pool({
    host: json.host,
    port: json.port,
    user: json.user,
    database: json.database,

How to use pg.Client:

16
17
18
19
20
21
22
23
24
25
26
const code = require('../message-formats')


describe('copy-to', () => {
  describe('integration tests (postgres)', () => {
    function getClient() {
      const client = new pg.Client()
      client.connect()
      return client
    }