Skip to content
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

Open
elfassy opened this issue Apr 9, 2015 · 2 comments
Open

minimongoid schema fields #29

elfassy opened this issue Apr 9, 2015 · 2 comments

Comments

@elfassy
Copy link
Contributor

elfassy commented Apr 9, 2015

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:

class @Recipe extends Minimongoid
  @_collection: new Meteor.Collection('recipes')

  @fields: {
    name: {type: String, label: "Title", max: 200},
    description: {type: String}
  }

  # model relations
  @belongs_to: [
    {name: 'user'}
  ]
  @embeds_many: [
    {name: 'ingredients'}
  ]
  @has_many: [
    {name: 'spices'}
  ]
end

The fields hash could include automatically userId and spiceIds 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?

@kaptron
Copy link
Contributor

kaptron commented Apr 10, 2015

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 fields hash though, it seems that just attaching the Schema is enough to handle validation, required fields, default values, etc. -- what do you think?

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
)

@elfassy
Copy link
Contributor Author

elfassy commented Apr 11, 2015

I'm doing something similar right now. My thought was that adding a fields hash would avoid having to explicitly add the course_id in your example. But then again, it would not allow you to set the autoform options like you did in your example. The options would need to be in a helper instead. Do you find that too limiting?

For the Course object, why not use @embeds_many: [ {name: 'holes'}]

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}
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants