tasks-backend/db/seed.js
2019-07-09 20:33:42 +02:00

82 lines
1.8 KiB
JavaScript
Executable File

const db = require('../db')
const { Task, Project, User, Article, Tag, Comment, Vote } = require('./models')
const testTasks = [
{
desc: 'make app',
completed: false,
projectId: 1,
},
{
desc: 'update backend',
completed: false,
projectId: 1,
},
{
desc: 'eat dinner',
completed: false,
projectId: 1,
},
]
const testProjects = [{ name: 'Anarchy Planet' }, { name: 'Tilde' }]
const testUsers = [
{ name: 'nn', email: 'nn@ap.org' },
{ name: 'dn', email: 'dn@ap.org' },
]
const testArticles = [{ title: 'latest news', text: 'waddup?!' }]
const testTags = [{ name: 'dox' }]
const testComments = [{ text: 'pretty good' }]
const testVotes = [{ upvote: 0, downvote: 1 }]
async function runSeed() {
await db.sync({ force: true })
console.log('db synced!')
console.log('seeding...')
try {
const p1 = await Project.create(testProjects[0])
const p2 = await Project.create(testProjects[1])
const u1 = await User.create(testUsers[0])
const u2 = await User.create(testUsers[1])
const t1 = await Task.create(testTasks[0])
const t2 = await Task.create(testTasks[1])
const t3 = await Task.create(testTasks[2])
await p1.addTask(t1)
await p1.addTask(t2)
await p2.addTask(t3)
await u1.addTasks([t1, t2])
await u2.addTask(t3)
const a1 = await Article.create(testArticles[0])
const t4 = await Tag.create(testTags[0])
const c1 = await Comment.create(testComments[0])
const v1 = await Vote.create(testVotes[0])
await a1.setUser(u1)
await a1.addTag(t4)
await a1.addComment(c1)
await c1.setUser(u2)
await c1.addVote(v1)
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()