Skip to content

Commit 15ef0bf

Browse files
moved around db access information. Set up proper testing blocks - will be adding more soon.
1 parent 43d1fe3 commit 15ef0bf

File tree

14 files changed

+116
-98
lines changed

14 files changed

+116
-98
lines changed

knexfile.js

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,52 @@
1-
//This knexfile is just used for migrations. The actual
2-
//db object is built in ./src/db/db and is used throughout the app.
3-
41
require('dotenv').config();
52

63
module.exports = {
7-
8-
client: 'mysql',
9-
connection: {
10-
host: process.env.DB_HOST,
11-
port: process.env.DB_PORT,
12-
user: process.env.DB_USER,
13-
password: process.env.DB_PASSWORD,
14-
database: process.env.DB_DATABASE,
4+
testing: {
5+
client: 'mysql',
6+
debug: false,
7+
connection: {
8+
host: process.env.DB_HOST,
9+
port: process.env.DB_PORT,
10+
user: process.env.DB_USER,
11+
password: process.env.DB_PASSWORD,
12+
database: process.env.DB_DATABASE + '_testing',
13+
},
14+
migrations: {
15+
directory: './src/db/migrations',
16+
},
17+
seeds: {
18+
directory: './src/db/seeds/dev',
19+
},
1520
},
16-
migrations: {
17-
directory: './src/db/migrations',
21+
development: {
22+
client: 'mysql',
23+
debug: false,
24+
connection: {
25+
host: process.env.DB_HOST,
26+
port: process.env.DB_PORT,
27+
user: process.env.DB_USER,
28+
password: process.env.DB_PASSWORD,
29+
database: process.env.DB_DATABASE,
30+
},
31+
migrations: {
32+
directory: './src/db/migrations',
33+
},
34+
seeds: {
35+
directory: './src/db/seeds/dev',
36+
},
1837
},
19-
seeds: {
20-
directory: './src/db/seeds/dev',
21-
},
22-
23-
};
38+
production: {
39+
client: 'mysql',
40+
debug: false,
41+
connection: {
42+
host: process.env.DB_HOST,
43+
port: process.env.DB_PORT,
44+
user: process.env.DB_USER,
45+
password: process.env.DB_PASSWORD,
46+
database: process.env.DB_DATABASE,
47+
},
48+
migrations: {
49+
directory: './src/db/migrations',
50+
},
51+
}
52+
}

package-lock.json

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "koa-vue-notes-api",
3-
"version": "1.1.1",
3+
"version": "1.2.1",
44
"description": "A SPA using Koa as the backend and Vue as the frontend.",
55
"author": "John Datserakis",
66
"private": false,
77
"scripts": {
88
"watch": "NODE_ENV=development nodemon app.js --exec",
9-
"test": "NODE_ENV=testing jest -forceExit",
9+
"test": "NODE_ENV=testing jest --forceExit",
1010
"build": "NODE_ENV=production babel src -d build && npm run pretty",
1111
"pretty": "prettier --write --print-width 80 --single-quote --trailing-comma es5 --tab-width 4 --no-semi 'src/**/*.js'",
1212
"start-production": "NODE_ENV=production pm2 start ecosystem.json",
@@ -35,9 +35,9 @@
3535
"koa-router": "^7.0.1",
3636
"koa-useragent": "^1.0.0",
3737
"log4js": "^2.2.0",
38+
"mysql": "^2.14.1",
3839
"npm": "^5.3.0",
3940
"pm2": "^2.6.1",
40-
"promise-mysql": "^3.0.2",
4141
"randexp": "^0.4.5"
4242
},
4343
"devDependencies": {

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ We use controllers to keep our router thin. The controller's responsibility is t
109109

110110
Here is our database setup. This project uses Knex to manage migrations and execute queries. I initially wrote wrote all the MySQL calls using raw SQL, but the need for a migrations maanager pushed me towards an ORM for the MySQL database. Knex is awesome - very powerful, easy to use and make queries, and the migrations are nice to have for sure - especially for testing.
111111

112+
For this project you'll need to make two databases in your development environment, `koa_vue_notes` and `koa_vue_notes_testing`. In your production environment you would just have `koa_vue_notes`. Tests use a different database because the data there is extremely volatile - as table information is created and destroyed on every test.
113+
114+
The `knexfile.js` in the root of the project is all setup with the ability to read your `.env` file. Let's say you download this project - first you'll `npm install`, then create a `koa_vue_notes` database and a `koa_vue_notes_testing` database, then `knex migrate:latest` and `knex seed:run` to create and seed your tables. Currently it's set up to make five users and 100 notes distributed to those users.
115+
112116
### middleware
113117

114118
Here I place any custom middleware the app is using. The custom middleware we're using is based on the `koa-jwt` library - but I had to tweak it because it mysteriously didn't report an expired token correctly. Strange, as I thought that would be an important requirement. No biggie.
@@ -129,6 +133,10 @@ Static files - just used for the favicon.
129133

130134
index.js isn't a folder - it's the brain of the app. Here you'll see we are attaching a bunch of middleware to our Koa instance. Very slick and straight-forward.
131135

136+
## Todo
137+
138+
- Finish writing all the tests
139+
132140
## Hit Me Up
133141

134142
Go ahead and fork the project! Message me here if you have questions or submit an issue if needed. I'll be making touch-ups as time goes on. Have fun with this!

src/controllers/NoteController.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {} from 'dotenv/config'
21
import joi from 'joi'
32
import dateFormat from 'date-fns/format'
43

src/controllers/UserActionController.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {} from 'dotenv/config'
21
import db from '../db/db'
32
import joi from 'joi'
43
import rand from 'randexp'
@@ -80,19 +79,21 @@ class UserController {
8079
var [result] = await db('users').insert(request).returning('id')
8180

8281
//Let's send a welcome email.
83-
let email = await fse.readFile('./src/email/welcome.html', 'utf8')
84-
const emailData = {
85-
to: request.email,
86-
from: process.env.APP_EMAIL,
87-
subject: 'Welcome To Koa-Vue-Notes-Api',
88-
html: email,
89-
categories: ['koa-vue-notes-api-new-user'],
90-
substitutions: {
91-
appName: process.env.APP_NAME,
92-
appEmail: process.env.APP_EMAIL,
93-
},
82+
if (process.env.NODE_ENV !== 'testing') {
83+
let email = await fse.readFile('./src/email/welcome.html', 'utf8')
84+
const emailData = {
85+
to: request.email,
86+
from: process.env.APP_EMAIL,
87+
subject: 'Welcome To Koa-Vue-Notes-Api',
88+
html: email,
89+
categories: ['koa-vue-notes-api-new-user'],
90+
substitutions: {
91+
appName: process.env.APP_NAME,
92+
appEmail: process.env.APP_EMAIL,
93+
},
94+
}
95+
await sgMail.send(emailData)
9496
}
95-
await sgMail.send(emailData)
9697

9798
//And return our response.
9899
ctx.body = { message: 'SUCCESS', id: result }

src/db/db.js

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,4 @@
1-
const knex = require('knex')
1+
var environment = process.env.NODE_ENV || 'development';
2+
var config = require('../../knexfile.js')[environment];
23

3-
const config = {
4-
client: 'mysql',
5-
connection: {
6-
host: process.env.DB_HOST,
7-
port: process.env.DB_PORT,
8-
user: process.env.DB_USER,
9-
password: process.env.DB_PASSWORD,
10-
database: process.env.DB_DATABASE,
11-
},
12-
migrations: {
13-
directory: './src/db/migrations',
14-
},
15-
}
16-
17-
//Here we check to see if we are in testing mode. If we are,
18-
//we set the database name to one with a '_tests' at the end,
19-
//which we'll build and tear-down for each test.
20-
if (process.env.NODE_ENV === 'testing') {
21-
config.connection.database = process.env.DB_DATABASE + '_tests'
22-
}
23-
24-
const db = knex(config)
25-
export default db
4+
module.exports = require('knex')(config);

src/db/seeds/dev/seed_notes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ exports.seed = async function(knex, Promise) {
66
let seedData = [];
77
for (let i = 0; i < 100; i++) {
88
let testNote = {
9-
userId: faker.random.number({min: 1, max: 10}),
9+
userId: faker.random.number({min: 1, max: 5}),
1010
title: faker.lorem.sentence(),
1111
content: faker.lorem.sentences(Math.floor(Math.random() * 10) + 1)
1212
}

src/db/seeds/dev/seed_users.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ exports.seed = async function(knex, Promise) {
77
//the passwords to make it exactly like the real app. All their
88
//passwords will be 'secret'
99
let seedData = [];
10-
for (let i = 0; i < 10; i++) {
10+
for (let i = 0; i < 5; i++) {
1111
let password = 'secret'
1212
try {
1313
password = await bcrypt.hash(password, 12)

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import Koa from 'koa'
44
import bodyParser from 'koa-bodyparser'
55
import cors from 'kcors'
6-
import {} from 'dotenv/config'
76
import logger from './logs/log'
87
import userAgent from 'koa-useragent'
98
import error from 'koa-json-error'

0 commit comments

Comments
 (0)