--- tags: Node.js 直播班 - 2023 春季班 --- # 🏅 Day 10 - 拆分 Model 因專案會使用到的資料不只有一個 Collection,像是最後會完成的貼文動態牆,會使用到貼文及使用者等等資料,所有資料的 Model、Schema 都集中在 server.js,可能會造成維護、管理不易 因此可以嘗試將不同資料的 Model 拆分出來,再引入至 server.js 做使用 ### 拆分步驟 - 在專案中建立 models 資料夾 - 分別依 Collection 建立檔案,如:`posts.js`、`users.js`...等等 - 因 Schema、Model 建立都需要 Mongoose 套件,因此需記得分別在 `posts.js`、`users.js` 引入 Mongoose 套件 - 將 Schema、Model 拆出來 ```javascript const postSchema = new mongoose.Schema( ... ); const Post = mongoose.model('Post', postSchema); module.exports = Post; ``` - 接著引入 models 資料夾中的 Model 到 server.js,即可直接針對 Model 操作新增、查詢、修改、刪除資料 註:引入 Model 時通常變數會使用大寫開頭,例如 Post、User,即可使用 `Post.create(...)` 等方式直接針對 Post 操作 ### 參考資源 - 課程影音「model export 模組化」 ## 題目(將答案貼在 HackMD 並提交至回報區) 嘗試將此[檔案](https://drive.google.com/drive/folders/1oRjCzs3OajeUXVroNO6QS7fNomO1hwZ0?usp=sharing)的 Model 拆分到 models 資料夾,並引入 server.js 做使用 並附上拆分後的 models 程式碼以及將 models 引入 server.js 的程式碼 提交範例 - models 資料夾 - posts.js ```javascript const mongoose = require('mongoose'); ... ``` - server.js ```javascript const __ = require('...'); ``` ## 回報流程 將答案連結貼至底下回報就算完成了喔! 解答位置請參考下圖(需打開程式碼的部分觀看) ![](https://i.imgur.com/vftL5i0.png) <!-- 解答: - models 資料夾 - posts.js ```javascript const mongoose = require('mongoose'); const postsSchema = new mongoose.Schema({ /* Schema 可自行設計 */ }); const Post = mongoose.model( 'Post', postsSchema ); module.exports = Post; ``` - server.js ```javascript const Post = require('./model/posts'); ``` --> 回報區 --- | 報數 | 組別/Discord 名字 | Codepen/HackMD/其他回饋 | |:----:|:-------------------:|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:| | 1 | 北 10 組 / Benson | [Github Benson - Day 10](https://github.com/ioveasdkre/HexschoolOperation/tree/main/NodejsEnterpriseClass/day40-tasks/day10) | | 2 | 北 8 組 / Zhao Chen | [GitHub - Day 10 - server.ts](https://github.com/zhao1995chen/NodejsEnterpriseClass/blob/master/daily-task/day10/server.ts) <br> [GitHub - Day 10 - models/drink.ts](https://github.com/zhao1995chen/NodejsEnterpriseClass/blob/master/daily-task/day10/models/drink.ts) | | 3 | 北 1 組 / PittWu | [Blog Post - Day 10](https://pitt-wu-blog.vercel.app/docs/day10-model) | | 4 | 中 3 組 / Wendy | [Notion - Day 10](https://jewel-cellar-80e.notion.site/Day-10-Model-c954fcb8ac314130b82da52f155e9739) | | 5 | 中 4 組 / jimkk159 | [HackMD - Day 10](https://hackmd.io/HA-XPVvJSs2MbUDEyvtSbQ) | | 6 | 北 5 組 / 圈圈 | [HackMD - Day 10](https://hackmd.io/@j6ccRYgTTEiVcfMfmAELcw/Sk_T3Nig2) | | 7 | 北 12 組 / Sam Lin | [GitHub - Day 10](https://github.com/samlin1122/hex-school/blob/main/daily-challenges/day10.js) | 8 | 北 1 組 / MayYi | [HackMD - Day 10](https://hackmd.io/GGmRQKvjQfucyA9waF8k3w) | 9 | 北 13 組 / Louisa | [GitHub - Day 10](https://github.com/louisa0416/NodejsEnterpriseClass/tree/master/daily-task/day10) | | 10 | 北 13 組 / sasha | [GitHub - Day 10](https://hackmd.io/@sashaye/SkUXMALWh) | | 11 | 北 5 組 / albee | [GitHub-Day 10](https://github.com/albee-chang/hexschool-node-dailytask/blob/main/day10/index.md)| | 12 | 南 1 組 / hiYifang | [HackMD - Day 10](https://hackmd.io/@gPeowpvtQX2Om6AmD-s3xw/HyTyStcV5)| | 13 | 北 16 組 / 啊培培 | [GitHub - Day 10](https://github.com/LABIBI-LG/Courses/tree/main/hexschool/nodeJS/Live_Course/Daily_Tasks/day10)|