Closed shahidcodes closed 3 years ago
Currently multiple schemas in a single file are not supported (see #12). For now, you can define each schema in a separate file like this:
home.ts
import mongoose, { Schema } from 'mongoose';
import { HomeDocument, HomeModel } from './types/mongoose.gen';
const homeSchema = new Schema(
{
homeId: String,
homeName: String,
},
{ _id: false },
);
export default mongoose.model<HomeDocument, HomeModel>('Home', homeSchema);
device.ts
import mongoose, { Schema } from 'mongoose';
import { DeviceDocument, DeviceModel } from './types/mongoose.gen';
import Home from './home'
const schema = new Schema({
name: String,
home: Home.schema,
});
export default mongoose.model<DeviceDocument, DeviceModel>('Device', schema);
Feel free to re-open this issue if this doesn't solve your problem!
Is there any plan to fix it? I am very early in my project. Till now mongoose-tsgen
is been very much helpful. Extracting nested schemas into a different file will clutter the directory uselessly and will create confusion. Because in the above example home
is embedded into the device
schema to save lookup
time and there is another home
collection. Above is one example, there are many more collections which follows the same. I'd love if you could try supporting this as this is very common usecase. I tried looking at the source code but couldn't understand much of it to contribute.
Yes I'd like to provide support for this sometime in the next week, I agree that it is not ideal.
As another workaround, you can define the sub-schema directly within the first one (without using a whole separate schema) like so:
import mongoose, { Schema } from 'mongoose';
import { DeviceDocument, DeviceModel } from './types/mongoose.gen';
const schema = new Schema({
name: String,
home: {
homeId: String,
homeName: String,
},
});
export default mongoose.model<DeviceDocument, DeviceModel>('Device', schema);
I understand that this is not always ideal, so I will re-open this for now and keep you updated on progress!
This has been fixed in 7.1.0, PR #43 🚀
Schema
device.ts
:But it is not generating DeviceHomeMethods interface for nested
home
which is expected inmongoose.gen.ts
file