-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
改寫語法,並且從一位使用者變成兩位,這時也將類別資料獨立出來,到時再新增用戶時可以同時創建該用戶的預設類別
- Loading branch information
Showing
3 changed files
with
85 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
}) | ||
}) |