Ai
2 Star 6 Fork 1

island-coder/ExpressApi

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
seedGoods.js 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
岛上码农 提交于 2021-06-29 22:08 +08:00 . initial commit
const { Category } = require('./models/category');
const { Goods } = require('./models/goods');
const mongoose = require('mongoose');
const config = require('config');
const data = [
{
name: '日用品',
goods: [
{ name: '香皂', price: 2.5, liked: false },
{ name: '洗发水', price: 10.2, liked: false },
{ name: '毛巾', price: 15, liked: false },
],
},
{
name: '食品',
goods: [
{ name: '面包', price: 5, liked: false },
{ name: '红酒', price: 110, liked: false },
{ name: '水果', price: 15, liked: true },
],
},
{
name: '数码产品',
goods: [
{ name: 'iPhone', price: 4565, liked: false },
{ name: '电脑', price: 3090, liked: false },
{ name: '手机', price: 195, liked: false },
],
},
{
name: '服装',
goods: [
{ name: '男装', price: 55, liked: false },
{ name: '女装', price: 105, liked: false },
{ name: '童装', price: 155, liked: false },
],
},
];
async function seed() {
await mongoose.connect(config.get('db'));
await Goods.deleteMany({});
await Category.deleteMany({});
for (let category of data) {
const { _id: categoryId } = await new Category({ name: category.name }).save();
const goods = category.goods.map(item => ({
...item,
category: { _id: categoryId, name: category.name },
}));
await Goods.insertMany(goods);
}
mongoose.disconnect();
console.info('Done!');
}
seed();
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/island-coder/express-api.git
git@gitee.com:island-coder/express-api.git
island-coder
express-api
ExpressApi
master

搜索帮助