Skip to content

Instantly share code, notes, and snippets.

@pantchox
Last active October 2, 2017 18:45
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 pantchox/f1df06ca8db98e9cf30af9032da54bc4 to your computer and use it in GitHub Desktop.
Save pantchox/f1df06ca8db98e9cf30af9032da54bc4 to your computer and use it in GitHub Desktop.
import mongoose, { Schema } from 'mongoose'
const bookListsSchema = new Schema({
// sub resources ref fields
books: [{
type: Schema.ObjectId,
ref: 'Books'
}],
// normal entity fields
user: {
type: Schema.ObjectId,
ref: 'User',
required: true
},
title: {
type: String,
required: true
}
}, {
timestamps: true
})
bookListsSchema.methods = {
view () {
return = {
// simple view
id: this.id,
user: this.user.view(full),
books: this.books.map((books) => books.view()),
title: this.title,
createdAt: this.createdAt,
updatedAt: this.updatedAt
}
}
}
const model = mongoose.model('BookLists', cardListsSchema)
export const schema = model.schema
export default model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment