-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
minimongoid schema fields #29
Comments
It's a good question, and when I started down that path I realized that there are other packages which are managing schema really nicely. So the short answer is yes, I also use SimpleSchema and it works perfectly. I'm not sure if you even need the separate Here's an example of some Golf Course models I used in an app: class @Course extends BaseModel
@_collection: new Mongo.Collection('courses')
@has_many: [
{name: 'tournaments'}
]
ordered_holes: ->
_.sortBy @holes, 'number'
# return hole matching number
hole: (number) ->
_.findWhere @holes, {number: number}
@Course._collection.attachSchema(
new SimpleSchema
title:
type: String
holes:
type: [Object]
"holes.$.number":
type: Number
"holes.$.handicap":
type: Number
"holes.$.par":
type: Number
)
class @Tournament extends BaseModel
@_collection: new Mongo.Collection('tournaments')
@belongs_to: [
{name: 'course'}
]
@current_round: ->
# Maybe could store this in the session?
if @first() then @first().current_round else 1
@Tournament._collection.attachSchema(
new SimpleSchema
title:
type: String
course_id:
type: String
autoform:
options: ->
Course.find().map (c) ->
{label: c.title, value: c._id}
current_round:
type: Number
defaultValue: 1
)
|
I'm doing something similar right now. My thought was that adding a For the your example would become class @Course extends BaseModel
@_collection: new Mongo.Collection('courses')
@has_many: [
{name: 'tournaments'}
]
@embeds_many: [
{name: 'holes'}
]
@fields:
title:
type: String
"holes.$.number":
type: Number
"holes.$.handicap":
type: Number
"holes.$.par":
type: Number
ordered_holes: ->
_.sortBy @holes, 'number'
# return hole matching number
hole: (number) ->
_.findWhere @holes, {number: number}
class @Tournament extends BaseModel
@_collection: new Mongo.Collection('tournaments')
@belongs_to: [
{name: 'course'}
]
@fields:
title:
type: String
current_round:
type: Number
defaultValue: 1
@current_round: ->
# Maybe could store this in the session?
if @first() then @first().current_round else 1
# On the client
Template.select_course.helpers(
options: ->
Course.find().map (c) ->
{label: c.title, value: c._id}
) |
What do you think of having minimongoid be able to define the schema? Something like http://mongoid.org/en/mongoid/v3/documents.html#fields
It could look like:
The fields hash could include automatically
userId
andspiceIds
for the belongs_to and has_many relations. I'm thinking of using https://github.com/aldeed/meteor-simple-schema as a dependency to make this happen. Any attributes supported by simple-schema would work in the fields hash.Thoughts?
The text was updated successfully, but these errors were encountered: