Skip to content

Commit

Permalink
Add express and postgresql with knex
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelalmeidatk committed Mar 28, 2019
1 parent 3bfdba0 commit cee961f
Show file tree
Hide file tree
Showing 10 changed files with 700 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "es5"
}
20 changes: 20 additions & 0 deletions server/knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const path = require('path');
const config = require('./src/config');

const migrations = {
tableName: 'knex_migrations',
directory: path.normalize(path.join(__dirname, 'src/db/migrations')),
};

module.exports = {
[config.env]: {
client: 'pg',
connection: config.databaseConnection,
pool: {
min: config.databasePool.min,
max: config.databasePool.max,
},
debug: config.databaseDebug,
migrations,
},
};
10 changes: 8 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
"license": "MIT",
"scripts": {
"dev": "backpack",
"build": "backpack build"
"build": "backpack build",
"migrate": "knex-migrate",
"reset-db": "knex-migrate down --to 0 && knex-migrate up"
},
"dependencies": {
"backpack-core": "^0.8.3"
"backpack-core": "^0.8.3",
"express": "^4.16.4",
"knex": "^0.16.3",
"knex-migrate": "^1.7.2",
"pg": "^7.9.0"
}
}
Empty file added server/src/app.js
Empty file.
34 changes: 34 additions & 0 deletions server/src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const env = process.env.NODE_ENV || 'development';

// Used on development
const base = {
databaseConnection: {
host: '127.0.0.1',
user: 'twitterclone',
password: 'twitterclone',
database: 'dev_twitter_clone',
},
databasePool: {
min: 2,
max: 10,
},
databaseDebug: true,
env,
};

const test = {
...base,
databaseConnection: {
...base.databaseConnection,
database: 'test_twitter_clone',
},
databaseDebug: false,
};

const production = {
...base,
databaseConnection: process.env.PROD_DB_URL,
databaseDebug: false,
};

module.exports = { development: base, test, production }[env];
5 changes: 5 additions & 0 deletions server/src/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import knex from 'knex';
import knexfile from '../../knexfile';
import config from '../config';

export default knex(knexfile[config.env]);
8 changes: 8 additions & 0 deletions server/src/db/migrations/20190328003112_setup_db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports.up = async function(knex) {
// UUID extension
await knex.raw('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"');
};

exports.down = async function(knex) {
await knex.raw('DROP EXTENSION IF EXISTS "uuid-ossp"');
};
16 changes: 16 additions & 0 deletions server/src/db/migrations/20190328003119_initial_tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
exports.up = async function(knex) {
// User table
await knex.schema.createTable('users', table => {
table
.uuid('id')
.notNullable()
.primary()
.defaultTo(knex.raw('uuid_generate_v4()'));
table.string('username').unique();
table.timestamps(true, true);
});
};

exports.down = async function(knex) {
await knex.schema.dropTableIfExists('users');
};
19 changes: 18 additions & 1 deletion server/src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
console.log("Hello Server!")
import express from 'express';
import db from './db';

const app = express();

app.get('/', (req, res) => {
db('users')
.first()
.then(res => {
console.log(' res', res);
});

res.send('hello!');
});

app.listen(4000, () => {
console.log('Listening at port 4000');
});
Loading

0 comments on commit cee961f

Please sign in to comment.