Skip to content

Instantly share code, notes, and snippets.

/1imageModel.js Secret

Created May 19, 2017 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/950a943c13042bc049bf054cda7fc7dd to your computer and use it in GitHub Desktop.
Save anonymous/950a943c13042bc049bf054cda7fc7dd to your computer and use it in GitHub Desktop.
import mongoose from 'mongoose';
const { Schema } = mongoose;
mongoose.Promise = global.Promise;
const imageSchema = new Schema({
title: String,
categories: [{type: Schema.Types.ObjectId, ref: 'Category'}],
description: String,
path: String,
createdAt: {type: Date, default:Date.now},
isDeleted: {type: Boolean, default: false}
});
const Image = mongoose.model('Image', imageSchema);
export default Image;
import mongoose from 'mongoose';
const { Schema } = mongoose;
mongoose.Promise = global.Promise;
const categorySchema = new Schema({
name: {type:String, ref:'Image'},
createdAt: {type:Date, default: Date.now},
isDeleted: {type: Boolean, default: false}
});
const Category = mongoose.model('Category', categorySchema);
export default Category;
imageController.getAll = (req,res)=>{
db.Image.find({}).populate('categories')
.then( images =>{
res.json(images)
})
};
[
{
"_id": "590787970a477629a4dd45d3",
"title": "Bra Girl",
"description": "Girl portrait",
"path": "http://res.cloudinary.com/amazecpk/image/upload/v1493665693/acu1vuajlg1rklzfqduk.png",
"__v": 0,
"isDeleted": false,
"createdAt": "2017-05-01T19:08:07.441Z",
"categories": [
{
"_id": "58fc3d9fa5ed6233486cc977",
"name": "Personal",
"__v": 0,
"isDeleted": false,
"createdAt": "2017-04-23T05:37:35.352Z"
},
{
"_id": "58fc3dc0a5ed6233486cc979",
"name": "Portraits",
"__v": 0,
"isDeleted": false,
"createdAt": "2017-04-23T05:38:08.734Z"
}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment