forked from adrianObel/koa2-api-boilerplate
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c28456
commit 79b3596
Showing
5 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This directory contains utility functions for managing the database. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const mongoose = require('mongoose') | ||
|
||
const config = require('../../config') | ||
|
||
const USERNAME = 'test' | ||
const PASSWORD = 'pass' | ||
|
||
// Connect to the Mongo Database. | ||
mongoose.Promise = global.Promise | ||
|
||
async function addUser () { | ||
await mongoose.connect(config.database) | ||
|
||
const User = require('../../src/models/users') | ||
|
||
const userData = { | ||
username: USERNAME, | ||
password: PASSWORD | ||
} | ||
|
||
const user = new User(userData) | ||
|
||
// Enforce default value of 'user' | ||
user.type = 'user' | ||
|
||
await user.save() | ||
|
||
await mongoose.connection.close() | ||
|
||
console.log(`User ${USERNAME} created.`) | ||
} | ||
addUser() | ||
|
||
module.exports = { | ||
addUser | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const mongoose = require('mongoose') | ||
|
||
const config = require('../../config') | ||
|
||
const User = require('../../src/models/users') | ||
|
||
async function getUsers () { | ||
// Connect to the Mongo Database. | ||
mongoose.Promise = global.Promise | ||
mongoose.set('useCreateIndex', true) // Stop deprecation warning. | ||
await mongoose.connect(config.database, { useNewUrlParser: true }) | ||
|
||
const users = await User.find({}, '-password') | ||
console.log(`users: ${JSON.stringify(users, null, 2)}`) | ||
|
||
mongoose.connection.close() | ||
} | ||
getUsers() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const mongoose = require('mongoose') | ||
|
||
const config = require('../config') | ||
|
||
// Connect to the Mongo Database. | ||
mongoose.Promise = global.Promise | ||
mongoose.connect(config.database, () => { | ||
// mongoose.connection.db.dropDatabase() | ||
}) | ||
|
||
console.log(`config: ${JSON.stringify(config, null, 2)}`) | ||
|
||
/* | ||
// Wipe the DB. | ||
function cleanDb () { | ||
for (const collection in mongoose.connection.collections) { | ||
if (mongoose.connection.collections.hasOwnProperty(collection)) { | ||
mongoose.connection.collections[collection].remove() | ||
} | ||
} | ||
console.log(`Database wiped.`) | ||
} | ||
cleanDb() | ||
*/ | ||
|
||
mongoose.connection.close() | ||
|
||
console.log(` | ||
Here's how to wipe the db: | ||
1. mongo | ||
2. use p2pvps-server-dev | ||
3. db.dropDatabase() | ||
4. exit | ||
`) |