Skip to content

Commit da2280a

Browse files
committed
adjust .env variables and readme
1 parent ff21666 commit da2280a

File tree

4 files changed

+44
-55
lines changed

4 files changed

+44
-55
lines changed

README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
A full-fledged Apollo Server with Apollo Client starter project with React and Express. [Read more about it in this tutorial to build it yourself](https://www.robinwieruch.de/graphql-apollo-server-tutorial/).
66

7+
Further Readings:
8+
* [TODO: write setup tutorial]
9+
* [TODO: write migration tutorial]
10+
711
**This repository is the fullstack Apollo Server with Express and MongoDB project. You can find a working client application that can be used with this server in the list below:**
812

913
* [React Client](https://github.com/the-road-to-graphql/fullstack-apollo-react-boilerplate)
@@ -39,27 +43,19 @@ A full-fledged Apollo Server with Apollo Client starter project with React and E
3943

4044
#### .env file
4145

42-
Since this boilerplate project is using MongoDB, you have to install it for your machine and get a database up and running. You find everything for the set up over here: [Setup MongoDB with Mongoose in Express Tutorial](https://www.robinwieruch.de/mongodb-express-setup-tutorial) [TODO: write tutorial]. After you have created a database and a database user, you can fill out the environment variables in the *server/.env* file.
46+
Since this boilerplate project is using MongoDB, you have to install it for your machine and get a database up and running. You find everything for the set up over here: [Setup MongoDB with Mongoose in Express Tutorial](https://www.robinwieruch.de/mongodb-express-setup-tutorial) [TODO: write setup tutorial]. After you have created a MongoDB database, you can fill out the environment variables in the *server/.env* file.
4347

4448
```
45-
DATABASE=mydatabase
46-
47-
DATABASE_USER=postgres
48-
DATABASE_PASSWORD=postgres
49-
5049
SECRET=asdlplplfwfwefwekwself.2342.dawasdq
5150
52-
MONGO_URI=mongodb://localhost:27017/mydatabase
51+
DATABASE_URL=mongodb://localhost:27017/mydatabase
5352
```
5453

5554
The `SECRET` is just a random string for your authentication. Keep all these information secure by adding the *.env* file to your *.gitignore* file. No third-party should have access to this information.
5655

5756
#### Testing
5857

59-
[TODO: change psql to MongoDB equivalent]
60-
61-
* adjust `test:run-server` npm script with `TEST_DATABASE` environment variable in package.json to match your testing database name
62-
* to match it from package.json: `createdb mytestdatabase` with psql
58+
* adjust `test:run-server` npm script with `TEST_DATABASE_URL` environment variable in package.json to match your testing database name
6359
* one terminal: npm run test:run-server
6460
* second terminal: test:execute-test
6561

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"scripts": {
1010
"start": "nodemon --exec babel-node src/index.js",
11-
"test:run-server": "TEST_DATABASE=mytestdatabase npm start",
11+
"test:run-server": "TEST_DATABASE_URL=mongodb://localhost:27017/mytestdatabase npm start",
1212
"test:execute-test": "mocha --require @babel/register 'src/**/*.spec.js'",
1313
"test": "echo \"No test specified\" && exit 0"
1414
},

src/index.js

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,18 @@ server.applyMiddleware({ app, path: '/graphql' });
8585
const httpServer = http.createServer(app);
8686
server.installSubscriptionHandlers(httpServer);
8787

88-
const isTest = !!process.env.TEST_DATABASE;
89-
const isProduction = !!process.env.DATABASE_URL;
88+
const isTest = !!process.env.TEST_DATABASE_URL;
89+
const isProduction = process.env.NODE_ENV === 'production';
9090
const port = process.env.PORT || 8000;
9191

9292
connectDb().then(async () => {
9393
if (isTest || isProduction) {
94-
// resetDb
94+
// reset database
9595
await Promise.all([
9696
models.User.remove({}),
9797
models.Message.remove({}),
9898
]);
99+
99100
createUsersWithMessages(new Date());
100101
}
101102

@@ -105,40 +106,35 @@ connectDb().then(async () => {
105106
});
106107

107108
const createUsersWithMessages = async date => {
108-
try {
109-
await models.User.create({
110-
username: 'rwieruch',
111-
email: 'hello@robin.com',
112-
password: 'rwieruch',
113-
role: 'ADMIN',
114-
}).then(createdUser => {
115-
return models.Message.create({
116-
text: 'Published the Road to learn React',
117-
createdAt: date.setSeconds(date.getSeconds() + 1),
118-
userId: createdUser._id,
119-
});
109+
await models.User.create({
110+
username: 'rwieruch',
111+
email: 'hello@robin.com',
112+
password: 'rwieruch',
113+
role: 'ADMIN',
114+
}).then(createdUser => {
115+
return models.Message.create({
116+
text: 'Published the Road to learn React',
117+
createdAt: date.setSeconds(date.getSeconds() + 1),
118+
userId: createdUser._id,
120119
});
120+
});
121121

122-
await models.User.create({
123-
username: 'ddavids',
124-
email: 'hello@david.com',
125-
password: 'ddavids',
126-
}).then(createdUser => {
127-
return models.Message.create(
128-
{
129-
text: 'Happy to release ...',
130-
createdAt: date.setSeconds(date.getSeconds() + 1),
131-
userId: createdUser._id,
132-
},
133-
{
134-
text: 'Published a complete ...',
135-
createdAt: date.setSeconds(date.getSeconds() + 1),
136-
userId: createdUser._id,
137-
},
138-
);
139-
});
140-
} catch (e) {
141-
console.log(e);
142-
throw e;
143-
}
122+
await models.User.create({
123+
username: 'ddavids',
124+
email: 'hello@david.com',
125+
password: 'ddavids',
126+
}).then(createdUser => {
127+
return models.Message.create(
128+
{
129+
text: 'Happy to release ...',
130+
createdAt: date.setSeconds(date.getSeconds() + 1),
131+
userId: createdUser._id,
132+
},
133+
{
134+
text: 'Published a complete ...',
135+
createdAt: date.setSeconds(date.getSeconds() + 1),
136+
userId: createdUser._id,
137+
},
138+
);
139+
});
144140
};

src/models/index.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ export function connectDb() {
77
if (process.env.DATABASE_URL) {
88
return mongoose.connect(process.env.DATABASE_URL);
99
}
10-
if (process.env.TEST_DATABASE) {
11-
return mongoose.connect(
12-
`mongodb://localhost:27017/${process.env.TEST_DATABASE}`,
13-
);
14-
}
1510

16-
return mongoose.connect(process.env.MONGO_URI);
11+
if (process.env.TEST_DATABASE_URL) {
12+
return mongoose.connect(process.env.TEST_DATABASE_URL);
13+
}
1714
}
1815

1916
export default {

0 commit comments

Comments
 (0)