e-scooter-rental-system/seed-payments.js

48 lines
1.8 KiB
JavaScript

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/e-scooter-rental')
.then(async () => {
console.log('Connected to MongoDB');
// 定义新的 Payment 模型
const paymentSchema = new mongoose.Schema({
paymentId: String,
type: String,
party: String,
amount: Number,
method: String,
category: String,
remark: String,
createdAt: Date
});
const Payment = mongoose.model('Payment', paymentSchema, 'payments');
// 删除旧数据
await Payment.deleteMany({});
console.log('Deleted old data');
// 添加新数据
const payments = [
{ paymentId: 'PAY001', type: 'income', party: '张三', amount: 1500, method: '微信', category: '租金收入', remark: '租车费' },
{ paymentId: 'PAY002', type: 'income', party: '李四', amount: 2000, method: '支付宝', category: '租金收入', remark: '租车费' },
{ paymentId: 'PAY003', type: 'income', party: '王五', amount: 500, method: '现金', category: '押金退还', remark: '还车退押金' },
{ paymentId: 'PAY004', type: 'expense', party: '赵六', amount: 300, method: '微信', category: '退款', remark: '提前还车退款' },
{ paymentId: 'PAY005', type: 'expense', party: '房东', amount: 1500, method: '银行卡', category: '房租', remark: '门店房租' },
{ paymentId: 'PAY006', type: 'expense', party: '电工', amount: 200, method: '现金', category: '水电', remark: '门店电费' }
];
await Payment.insertMany(payments);
console.log('Added new payments');
// 验证
const count = await Payment.countDocuments();
console.log('Total payments:', count);
process.exit(0);
})
.catch(e => {
console.error(e);
process.exit(1);
});