mura1
mura1

Reputation: 472

Creating Schema in Node/MongoDb

So, I am just starting with Node/Mongo db and typescript, and I am kind of stuck in creating a Schema for this nested JSON Database. I have started something and, I would like to know if I am on the right track. So please if anyone would help me it would be great. Thanks in Advance

Data

[{
   "a_class":[
   {    
    "brand":"A-class",
    "id":"1",
    "year":"2015"
    "price":"12665"
    ...
    "engine_spec":{
     ...
     }
     ...
      }]

}
]

Interfaces

import { Document } from 'mongoose';
export default interface ICars extends Document {
   a_class: BrandList[],
}


interface BrandList {
 brand: string;
 id:number;
 year:number;
 main_image: string;
 price:number;
 transmission:string;
 fuel_type:string;
 seating_capacity:number;
 engine:number;
 engine_specs:Specs;
 volume_weights:Volume;
 performance: Performance
 images_interior: Interior
 images_exterior: Exterior
}

interface Specs {
    power:number;
    power_per_litre:number;
    torque:number;
    fuel_system:number
}

interface Volume {
    max_weights:number;
    fuel_tank:number
}


interface Performance {
    acceleration:number;
    maximum_speed:number;
    fuel_urban:number;
    fuel_extra_urban:number
    fuel_combined:number
}

interface Interior {
    image_one:string;
    image_two:string;
    image_three:string;
    image_four:string;
}

interface Exterior {
    image_one:string;
    image_two:string;
    image_three:string;
    image_four:string;
}

Schema


import mongoose, { Schema } from 'mongoose';

import ICars from '../interfaces/cars'

const CarsSchema: Schema = new Schema({

  a_class:Array,
  brand: String,
  year:Number,
})

export default mongoose.model<ICars>('Cars', CarsSchema);

Get Route, So when I try to get all the data through postman, it is not allowing me, it is saying that the route is not being found 404. I have imported it to the server.js import carsRouter from './routes/cars'; router.use('./cars',carsRouter ), I don't know where the error could be

import express from 'express'
import Cars from '../models/cars'

const router = express();


router.get('/cars', (req:any, res:any )=>{
    Cars.find()
    .then(car=> res.json(car))
    .catch(err => res.status(400).json(`Error: ${err}`))
})

 export=router

Upvotes: 2

Views: 211

Answers (1)

lngovn
lngovn

Reputation: 91

According to mongoose document, we should avoid extending mongoose Document.

This approach works, but we recommend your document interface not extend Document. Using extends Document makes it difficult for Mongoose to infer which properties are present on query filters, lean documents, and other cases.

We recommend your document interface contain the properties defined in your schema and line up with what your documents look like in MongoDB. Although you can add instance methods to your document interface, we do not recommend doing so.

For more information, please reference: https://mongoosejs.com/docs/typescript.html#using-extends-document

And It seems that we can use mongoose sub documents for this requirement. For more information regarding the sub documents, please reference: https://mongoosejs.com/docs/subdocs.html#subdocuments

So in this case, we can rephrase your code a little bit:

Interfaces:

export default interface ICars {
   a_class: BrandList[],
}

// The other interfaces can be kept

Schema:

const Exterior = new Schema({
   // own fields
});


const Interior = new Schema({
   // own fields
});

const Performance = new Schema({
   // own fields
});

const Volume = new Schema({
   // own fields
});

const SpecsSchema = new Schema({
   // own fields
});

const BrandListSchema = new Schema({
  // other fields
  engine_specs: [SpecsSchema],
  volume_weights: [VolumeSchema],
  performance: [PerformanceSchema],
  images_interior: [InteriorSchema],
  images_exterior: [ExteriorSchema],
});

const CarsSchema: Schema = new Schema({
  a_class: [BrandListSchema],
});

Upvotes: 2

Related Questions