Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions cloud-sql/postgres/knex/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,13 @@ Then use this command to launch the proxy in the background:

### Testing the application

2. Next, install the Node.js packages necessary to run the app locally by running the following command:
1. Next, install the Node.js packages necessary to run the app locally by running the following command:

```
npm install
```

3. Run `createTable.js` to create the database table the app requires and to ensure that the database is properly configured.
With the Cloud SQL proxy running, run the following command to create the sample app's table in your Cloud SQL PostgreSQL database:

```
node createTable.js $DB_USER $DB_PW $DB_NAME $CLOUD_SQL_CONNECTION_NAME
```

4. Run the sample app locally with the following command:
2. Run the sample app locally with the following command:

```
npm start
Expand Down
60 changes: 0 additions & 60 deletions cloud-sql/postgres/knex/createTable.js

This file was deleted.

46 changes: 41 additions & 5 deletions cloud-sql/postgres/knex/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,21 @@ const logger = winston.createLogger({
// testing different configurations.
let pool;

app.use(async (req, res, next) => {
if (pool) {
return next();
}
try {
pool = await createPoolAndEnsureSchema();
next();
} catch (err) {
logger.error(err);
return next(err);
}
});

// [START cloud_sql_postgres_knex_create_tcp]
const createTcpPool = config => {
const createTcpPool = async config => {
// Extract host and port from socket address
const dbSocketAddr = process.env.DB_HOST.split(':'); // e.g. '127.0.0.1:5432'

Expand All @@ -71,7 +84,7 @@ const createTcpPool = config => {
// [END cloud_sql_postgres_knex_create_tcp]

// [START cloud_sql_postgres_knex_create_socket]
const createUnixSocketPool = config => {
const createUnixSocketPool = async config => {
const dbSocketPath = process.env.DB_SOCKET_PATH || '/cloudsql';

// Establish a connection to the database
Expand All @@ -90,7 +103,7 @@ const createUnixSocketPool = config => {
// [END cloud_sql_postgres_knex_create_socket]

// Initialize Knex, a Node.js SQL query builder library with built-in connection pooling.
const createPool = () => {
const createPool = async () => {
// Configure which instance and what database user to connect with.
// Remember - storing secrets in plaintext is potentially unsafe. Consider using
// something like https://cloud.google.com/kms/ to help keep secrets secret.
Expand Down Expand Up @@ -133,6 +146,29 @@ const createPool = () => {
}
};

const ensureSchema = async pool => {
const hasTable = await pool.schema.hasTable('votes');
if (!hasTable) {
return pool.schema.createTable('votes', table => {
table.increments('vote_id').primary();
table.timestamp('time_cast', 30).notNullable();
table.specificType('candidate', 'CHAR(6)').notNullable();
});
}
logger.info("Ensured that table 'votes' exists");
};

const createPoolAndEnsureSchema = async () =>
await createPool()
.then(async pool => {
await ensureSchema(pool);
return pool;
})
.catch(err => {
logger.error(err);
throw err;
});

// [START cloud_sql_postgres_knex_connection]
/**
* Insert a vote record into the database.
Expand Down Expand Up @@ -177,7 +213,7 @@ const getVoteCount = async (pool, candidate) => {
};

app.get('/', async (req, res) => {
pool = pool || createPool();
pool = pool || (await createPoolAndEnsureSchema());
try {
// Query the total count of "TABS" from the database.
const tabsResult = await getVoteCount(pool, 'TABS');
Expand Down Expand Up @@ -222,7 +258,7 @@ app.get('/', async (req, res) => {
});

app.post('/', async (req, res) => {
pool = pool || createPool();
pool = pool || (await createPoolAndEnsureSchema());
// Get the team from the request and record the time of the vote.
const {team} = req.body;
const timestamp = new Date();
Expand Down
101 changes: 0 additions & 101 deletions cloud-sql/postgres/knex/test/createTable.test.js

This file was deleted.