Skip to content

Commit

Permalink
feat: modify seeder
Browse files Browse the repository at this point in the history
改寫語法,並且從一位使用者變成兩位,這時也將類別資料獨立出來,到時再新增用戶時可以同時創建該用戶的預設類別
  • Loading branch information
JHIH-LEI committed Aug 14, 2021
1 parent b0cb403 commit 200b25c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 68 deletions.
32 changes: 32 additions & 0 deletions categories.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"name": "家居物業",
"icon": "fas fa-home",
"userId": null
},
{
"name": "交通出行",
"icon": "fas fa-shuttle-van",
"userId": null
},
{
"name": "休閒娛樂",
"icon": "fas fa-grin-beam",
"userId": null
},
{
"name": "餐飲食品",
"icon": "fas fa-utensils",
"userId": null
},
{
"name": "其他",
"icon": "fas fa-pen",
"userId": null
},
{
"name": "薪水",
"type": "收入",
"userId": null
}
]
47 changes: 11 additions & 36 deletions models/seeds/categorySeeder.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,21 @@
const db = require('../../config/mongoose')
const Category = require('../category')
const User = require('../user')
const categoryList = require('../../categories.json')

db.once('open', () => {
console.log('categorySeeder is connect!')
return User.find()
.then(async user => {
const userId = user[0]._id
await Category.create(
{
name: '家居物業',
icon: 'fas fa-home',
userId
},
{
name: '交通出行',
icon: 'fas fa-shuttle-van',
userId
},
{
name: '休閒娛樂',
icon: 'fas fa-grin-beam',
userId
},
{
name: '餐飲食品',
icon: 'fas fa-utensils',
userId
},
{
name: '其他',
icon: 'fas fa-pen',
userId
},
{
name: '薪水',
type: '收入',
userId
.then(users => {
Promise.all(Array.from(users, (user, i) => {
//為每個使用者新增類別
const userId = user._id
categoryList.forEach(category => category.userId = userId) //將每個類別寫入自己的User id
return Category.create(categoryList)
}))
.then(() => {
console.log('done')
process.exit()
})
})
.then(() => {
console.log('done')
process.exit()
})
})
74 changes: 42 additions & 32 deletions models/seeds/recordSeeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,50 @@ const User = require('../user')
const bcrypt = require('bcryptjs')


db.once('open', async () => {
db.once('open', () => {
console.log('recordSeeder is connect!')
const SEED_USER = {
name: 'Alpha Camp',
email: '[email protected]',
password: '123'
}
bcrypt
.genSalt(10)
.then(salt => bcrypt.hash(SEED_USER.password, salt))
.then(hash => {
User.create({
name: SEED_USER.name,
email: SEED_USER.email,
password: hash
})
.then(async user => {
await Record.create({
name: '神戶牛排',
category: '餐飲食品',
date: Date.now(),
amount: -2000,
userId: user._id
}, {
name: 'H1Z1',
category: '休閒娛樂',
date: Date.now(),
amount: -799,
userId: user._id
})
const SEED_USERS = [
{
name: 'Alpha Camp',
email: '[email protected]',
password: '123'
},
{
name: 'Alicia',
email: '[email protected]',
password: '123'
}
]
//迭代seed_user並且創建user及其record資料
Promise.all(Array.from(SEED_USERS, (SEED_USER, index) => {
return bcrypt
.genSalt(10)
.then(salt => bcrypt.hash(SEED_USER.password, salt))
.then(hash => {
return User.create({
name: SEED_USER.name,
email: SEED_USER.email,
password: hash
})
.then(() => {
console.log('recordSeeder done!')
process.exit()
})
.then(user => {
return Record.create({
name: '神戶牛排',
category: '餐飲食品',
date: Date.now(),
amount: -2000,
userId: user._id
}, {
name: 'H1Z1',
category: '休閒娛樂',
date: Date.now(),
amount: -799,
userId: user._id
})
})
}))
.then(() => {
console.log('recordSeeder done!')
process.exit()
})
})

0 comments on commit 200b25c

Please sign in to comment.