first commit

This commit is contained in:
notnull 2019-07-09 11:24:05 -04:00
commit bd5a7ef113
14 changed files with 3693 additions and 0 deletions

20
.eslintrc Normal file
View File

@ -0,0 +1,20 @@
{
"extends": ["eslint:recommended"],
"parserOptions": {
"ecmaVersion": 8
},
"env": {
"es6": true,
"node": true
},
"rules": {
"quotes": ["warn", "single"],
"semi": ["warn", "never"],
"indent": ["warn", 2],
"no-unused-vars": ["warn"],
"no-console": 0
}
}

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
# dependencies
/node_modules
# production
/build
npm-debug.log*

10
README.md Normal file
View File

@ -0,0 +1,10 @@
# MWE of Sequelize Association for comments
## To run:
- git clone
- ```npm i```
- ```createdb mwe-comments```
- ```npm run seed```
-``` npm start```
- open http://localhost:1337/api/comments in browser

13
api/comments.js Executable file
View File

@ -0,0 +1,13 @@
const router = require('express').Router()
const { Comment } = require('../db/models')
module.exports = router
router.get('/', async (req, res, next) => {
try {
const comments = await Comment.findAll({include: 'replies'})
res.send(comments)
} catch (err) {
next(err)
}
})

20
api/index.js Executable file
View File

@ -0,0 +1,20 @@
const router = require('express').Router()
module.exports = router
const ascii = require('../ascii')
router.use('/comments', require('./comments'))
router.get('/', async (req, res, next) => {
try {
res.json({ ascii })
} catch (err) {
console.log(err)
next()
}
})
router.use((req, res, next) => {
const error = new Error('Not Found!!!!!!!')
error.status = 404
next(error)
})

26
ascii.js Normal file
View File

@ -0,0 +1,26 @@
const ascii = String.raw`
. .
* . . . . *
. . . . . .
o . .
. . . .
0 .
. . , , ,
. \ . .
. . \ ,
. o . . .
. . . \ , .
#\##\# . .
# #O##\### .
. . #*# #\##\### .
. ##*# #\##\## .
. . ##*# #o##\# .
. *# #\# . .
\ . .
____^/\___^--____/\____O______________/\/\---/\___________--
/\^ ^ ^ ^ ^^ ^ '\ ^ ^
-- - -- - - --- __ ^
-- __ ___-- ^ ^`
module.exports = ascii

25
db/db.js Executable file
View File

@ -0,0 +1,25 @@
const Sequelize = require('sequelize')
const pkg = require('../package.json')
const databaseName = pkg.name + (process.env.NODE_ENV === 'test' ? '-test' : '')
const createDB = () => {
const db = new Sequelize(
process.env.DATABASE_URL || `postgres://localhost:5432/${databaseName}`,
{
logging: false,
operatorsAliases: false
}
)
return db
}
const db = createDB()
module.exports = db
// This is a global Mocha hook used for resource cleanup.
// Otherwise, Mocha v4+ does not exit after tests.
if (process.env.NODE_ENV === 'test') {
after('close database connection', () => db.close())
}

6
db/index.js Executable file
View File

@ -0,0 +1,6 @@
const db = require('./db')
// register models
require('./models')
module.exports = db

6
db/models/comment.js Executable file
View File

@ -0,0 +1,6 @@
const Sequelize = require('sequelize')
const db = require('../db')
const Comment = db.define('comments', { text: Sequelize.STRING })
module.exports = Comment

6
db/models/index.js Executable file
View File

@ -0,0 +1,6 @@
const Comment = require('./comment')
Comment.belongsTo(Comment, {as: 'parent'})
Comment.belongsToMany(Comment, {through: 'CommentReplies', as: { singular: 'reply', plural: 'replies' } })
module.exports = {Comment}

41
db/seed.js Executable file
View File

@ -0,0 +1,41 @@
const db = require('../db')
const { Comment } = require('./models')
const tc1 = {text: 'c1'}
const tc2 = {text: 'c2'}
const tc3 = {text: 'c3'}
// const tc4 = {text: 'c4'}
// const tc5 = {text: 'c5'}
// const tc6 = {text: 'c6'}
async function runSeed() {
await db.sync({ force: true })
console.log('db synced!')
console.log('seeding...')
try {
const c1 = await Comment.create(tc1)
const c2 = await Comment.create(tc2)
const c3 = await Comment.create(tc3)
// const c4 = await Comment.create(tc4)
// const c5 = await Comment.create(tc5)
// const c6 = await Comment.create(tc6)
await c1.addReply(2)
await c2.setParent(1)
await c2.addReply(3)
await c3.setParent(2)
console.log('seeded successfully')
} catch (err) {
console.error(err)
process.exitCode = 1
} finally {
console.log('closing db connection')
await db.close()
console.log('db connection closed')
}
}
runSeed()

36
index.js Executable file
View File

@ -0,0 +1,36 @@
const express = require('express')
const path = require('path')
const app = express()
const morgan = require('morgan')
const ascii = require('./ascii')
//const proxy = require('http-proxy-middleware')
const port = process.env.PORT || 1337
app.use(morgan('tiny'))
// body parsing middleware
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(require('body-parser').text())
app.use('/api', require('./api'))
if (process.env.NODE_ENV === 'production') {
// Express will serve up production assets
app.use(express.static(path.join(__dirname, 'dist')))
}
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '..', 'public', 'index.html'))
})
// error handling endware
app.use((err, req, res, next) => {
console.error(err)
console.error(err.stack)
res.status(err.status || 500).send(err.message || 'Internal server error.')
next()
})
app.listen(port, () => {
console.log('\n' + ascii + '\n')
console.log(`Doin' haxor stuff on port ${port}`)
})

3457
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

20
package.json Normal file
View File

@ -0,0 +1,20 @@
{
"name": "mwe-comments",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"axios": "^0.19.0",
"concurrently": "^4.0.1",
"express": "^4.17.1",
"http-proxy-middleware": "^0.19.0",
"morgan": "^1.9.1",
"nodemon": "^1.19.1",
"pg": "^7.11.0",
"sequelize": "^5.8.11"
},
"scripts": {
"seed": "node db/seed.js",
"start": "nodemon index.js"
}
}