Skip to content

Commit

Permalink
Added util dir
Browse files Browse the repository at this point in the history
  • Loading branch information
christroutner committed Feb 4, 2019
1 parent 3c28456 commit 79b3596
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bin/server.js
Expand Up @@ -19,8 +19,8 @@ async function startServer () {

// Connect to the Mongo Database.
mongoose.Promise = global.Promise
await mongoose.connect(config.database, { useNewUrlParser: true })
mongoose.set('useCreateIndex', true) // Stop deprecation warning.
await mongoose.connect(config.database, { useNewUrlParser: true })

// MIDDLEWARE START

Expand Down
1 change: 1 addition & 0 deletions util/README.md
@@ -0,0 +1 @@
This directory contains utility functions for managing the database.
36 changes: 36 additions & 0 deletions util/users/createUsers.js
@@ -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
}
18 changes: 18 additions & 0 deletions util/users/getUsers.js
@@ -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()
34 changes: 34 additions & 0 deletions util/wipe-db.js
@@ -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
`)

0 comments on commit 79b3596

Please sign in to comment.