Added util dir

This commit is contained in:
Chris Troutner
2019-02-03 17:33:47 -08:00
parent 3c28456252
commit 79b3596685
5 changed files with 90 additions and 1 deletions
+1 -1
View File
@@ -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
+1
View File
@@ -0,0 +1 @@
This directory contains utility functions for managing the database.
+36
View File
@@ -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
View File
@@ -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
View File
@@ -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
`)