Some checks failed
Deploy / deploy (push) Has been cancelled
Full-stack web application for Telegram management - Frontend: Vue 3 + Vben Admin - Backend: NestJS - Features: User management, group broadcast, statistics 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
35 lines
837 B
JavaScript
35 lines
837 B
JavaScript
import mongoose from 'mongoose';
|
|
import { MongoMemoryServer } from 'mongodb-memory-server';
|
|
|
|
let mongoServer;
|
|
|
|
export const connectDatabase = async () => {
|
|
mongoServer = await MongoMemoryServer.create();
|
|
const uri = mongoServer.getUri();
|
|
|
|
await mongoose.connect(uri, {
|
|
useNewUrlParser: true,
|
|
useUnifiedTopology: true,
|
|
});
|
|
};
|
|
|
|
export const closeDatabase = async () => {
|
|
await mongoose.connection.dropDatabase();
|
|
await mongoose.connection.close();
|
|
if (mongoServer) {
|
|
await mongoServer.stop();
|
|
}
|
|
};
|
|
|
|
export const clearDatabase = async () => {
|
|
const collections = mongoose.connection.collections;
|
|
|
|
for (const key in collections) {
|
|
const collection = collections[key];
|
|
await collection.deleteMany();
|
|
}
|
|
};
|
|
|
|
export const seedDatabase = async (model, data) => {
|
|
return await model.create(data);
|
|
}; |