export하면 다른 파일에서 사용할 수 있는데 여러개를 한다면 어떻게 해야하는지 궁금했다
GPT가 이랬다
모듈에서 여러 개의 모델을 export할 때, 일반적으로는 객체로 감싸서 export 하는 것이 일반적입니다. 이렇게 하면 다른 파일에서 해당 객체의 속성을 사용하여 필요한 모델에 접근할 수 있습니다.
// person-model.js
const mongoose = require('mongoose');
const PersonSchema = new mongoose.Schema({
name: String,
age: Number,
});
const AnimalSchema = new mongoose.Schema({
species: String,
color: String,
});
const Person = mongoose.model('Person', PersonSchema);
const Animal = mongoose.model('Animal', AnimalSchema);
module.exports = { Person, Animal };
다른 파일에서 가져다 샤용할 때
// 다른 파일
const { Person, Animal } = require('./person-model');
// Person 모델 사용
const newPerson = new Person({ name: 'John', age: 25 });
newPerson.save();
// Animal 모델 사용
const newAnimal = new Animal({ species: 'Dog', color: 'Brown' });
newAnimal.save();