github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/acceptance/testdata/node/client.js (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 const pg = require('pg'); 12 const fs = require('fs'); 13 14 // If we are running within the acceptance tests, we should use the env vars 15 // they've set. Otherwise, assume that we just want to run against a local 16 // Cockroach instance. 17 const config = { 18 user: 'root', 19 host: process.env.PGHOST || 'localhost', 20 port: process.env.PGPORT || 26257, 21 }; 22 if (process.env.PGSSLCERT && process.env.PGSSLKEY) { 23 config.ssl = { 24 cert: fs.readFileSync(process.env.PGSSLCERT), 25 key: fs.readFileSync(process.env.PGSSLKEY), 26 }; 27 } 28 29 const client = new pg.Client(config); 30 31 before(done => { 32 client 33 .connect() 34 .then(() => { 35 return client.query('DROP DATABASE IF EXISTS node_test'); 36 }) 37 .then(() => { 38 return client.query('CREATE DATABASE node_test'); 39 }) 40 .then(() => { 41 return client.query('USE node_test'); 42 }) 43 .then(() => { 44 done(); 45 }); 46 }); 47 48 after(() => { 49 client.query('DROP DATABASE IF EXISTS node_test').catch(() => {}); 50 client.query('USE ""').catch(() => {}); 51 client.end(); 52 }); 53 54 module.exports = client;