node4good / formage

Bootstraped Admin GUI addon for mongoose, jugglingdb, or just as a form generator
formage.io
MIT License
185 stars 55 forks source link

Confusing Code in ModelRegistry #127

Open skilesare opened 9 years ago

skilesare commented 9 years ago

The following code in the ModelRegistry constructor doesn't seem to make sense. In the callback modelName is actually the index for the lodash _.foreach function

Seems like it should be this.registerMode(model,model.modelName). Maybe you are meaning to name things as the index, but later you try to humanize this term. I keep getting an error when you call capitalize on the 0(the index of the first item). I guess I'd pass this if I had a label on my model, but I don't.

_.forEach(models, function (model, modelName) {
    this.registerModel(model, modelName);
    _.forEach(model.discriminators, function (discModel, discName) {
        this.registerModel(discModel, discName, {hidden: true});
    }.bind(this));
}.bind(this));
refack commented 9 years ago

Can you provide minimal failing models code?

skilesare commented 9 years ago

This is what is failing:

@conn.chat = new BChattySchema
      mongoConnection: @conn.db
      accountConfig: @ac

adminModels = [
    @conn.chat.PostModel.model
    @conn.chat.ThreadModel.model
    @conn.chat.PageModel.model
    @conn.chat.AuthorModel.model
    @conn.chat.AccountModel.model
    @conn.chat.StarModel.model
    @conn.chat.MessageModel.model
    @conn.chat.MailBoxModel.model
    @conn.chat.TagModel.model
    @conn.chat.TagListModel.model
    @conn.chat.StreamModel.model
    @conn.chat.FollowAuthorModel.model
    @conn.chat.FollowStarModel.model
    @conn.chat.SettingModel.model
    @conn.chat.AccountSubModel.model
    @conn.chat.InstitutionModel.model
    @conn.chat.ContentModel.model
    @conn.chat.PFModel.model
    @conn.chat.FollowPFModel.model
    @conn.chat.EventModel.model
    @conn.chat.AttendingEventModel.model
    @conn.chat.InterestedEventModel.model

  ]

  #console.log @conn.chat
  @formage = require('formage')
  @formage.init @app, @express, adminModels,
    title: 'Admin'
    root: '/admin'
    default_section:'main'
    password: '***********'
    username: 'admin'
    admin_users_gui: true

I freely admit that I may be doing this completely wrong. The pattern of my project doesn't match up with the example at all. I can't just require('./models')...all my models (and schema0 are in the BChattySchema:

class BChattySchema
  constructor: (options)->
    @async = require('async')
    @options = options
    @conn = null
    @accontConfig = null
    if options?
      @accountConfig = options.accountConfig if @options.accountConfig?
      if options.mongoConnection?
        @conn = options.mongoConnection
        @defineSchema()

  defineSchema: =>
    $context = @
    @mongoose = require('mongoose')

    @PostSchema = new  PostSchema
      $context : $context

    @PostModel = new PostModel
      $context : $context
    @Posts = @PostModel.model

    @ThreadSchema = new ThreadSchema
      $context : $context

    @ThreadModel = new ThreadModel
      $context: $context

    @Threads = @ThreadModel.model

    @PageSchema = new PageSchema
      $context: $context

    @PageModel = new PageModel
      $context: $context

    @Pages = @PageModel.model

    @AuthorSchema = new AuthorSchema
      $context: $context

    @AuthorModel = new AuthorModel
      $context: $context

    @Authors = @AuthorModel.model

    @StarSchema = new StarSchema
      $context: $context

    @StarModel = new StarModel
      $context: $context

    @Stars = @StarModel.model

    @AccountSchema = new AccountSchema
      $context: $context

    @AccountModel = new AccountModel
      $context: $context

    @Accounts = @AccountModel.model

    @MessageSchema = new MessageSchema
      $context: $context

    @MessageModel = new MessageModel
      $context: $context

    @Messages = @MessageModel.model

    @MailBoxSchema = new MailBoxSchema
      $context: $context

    @MailBoxModel = new MailBoxModel
      $context: $context

    @MailBoxes = @MailBoxModel.model

    @TagSchema = new TagSchema
      $context: $context

    @TagModel = new TagModel
      $context: $context

    @Tags = @TagModel.model

    @TagListSchema = new TagListSchema
      $context: $context

    @TagListModel = new TagListModel
      $context: $context

    @TagLists = @TagListModel.model

    @StreamSchema = new StreamSchema
      $context: $context

    @StreamModel = new StreamModel
      $context: $context

    @Streams = @StreamModel.model

    @FollowAuthorSchema = new FollowAuthorSchema
      $context: $context

    @FollowAuthorModel = new FollowAuthorModel
      $context: $context

    @FollowAuthors = @FollowAuthorModel.model

    @FollowStarSchema = new FollowStarSchema
      $context: $context

    @FollowStarModel = new FollowStarModel
      $context: $context

    @FollowStars = @FollowStarModel.model

    @SettingSchema = new SettingSchema
      $context: $context

    @SettingModel = new SettingModel
      $context: $context

    @Settings = @SettingModel.model

    @AccountSubSchema = new AccountSubSchema
      $context: $context

    @AccountSubModel = new AccountSubModel
      $context: $context

    @AccountSubs = @AccountSubModel.model

    @ContentSchema = new ContentSchema
      $context: $context

    @ContentModel = new ContentModel
      $context: $context

    @Contents = @ContentModel.model

    @InstitutionSchema = new InstitutionSchema
      $context: $context

    @InstitutionModel = new InstitutionModel
      $context: $context

    @Institutions = @InstitutionModel.model

    @PFSchema = new PFSchema
      $context: $context

    @PFModel = new PFModel
      $context: $context

    @PFs = @PFModel.model

    @FollowPFSchema = new FollowPFSchema
      $context: $context

    @FollowPFModel = new FollowPFModel
      $context: $context

    @FollowPFs = @FollowPFModel.model

    @EventSchema = new EventSchema
      $context: $context

    @EventModel = new EventModel
      $context: $context

    @Events = @EventModel.model

    @AttendingEventSchema = new AttendingEventSchema
      $context: $context

    @AttendingEventModel = new AttendingEventModel
      $context: $context

    @AttendingEvents = @AttendingEventModel.model

    @InterestedEventSchema = new InterestedEventSchema
      $context: $context

    @InterestedEventModel = new InterestedEventModel
      $context: $context

    @InterestedEvents = @InterestedEventModel.model

    return
exports.BChattySchema = BChattySchema
refack commented 9 years ago

Basicly formage needs human names for you models. You could try and init adminModels like so:

adminModels =
    Post: @conn.chat.PostModel.model
    Thread: @conn.chat.ThreadModel.model
    Page: @conn.chat.PageModel.model
    Author: @conn.chat.AuthorModel.model
    Account: @conn.chat.AccountModel.model
    Star: @conn.chat.StarModel.model
    Message: @conn.chat.MessageModel.model
    MailBox: @conn.chat.MailBoxModel.model
    Tag: @conn.chat.TagModel.model
    TagList: @conn.chat.TagListModel.model
    Stream: @conn.chat.StreamModel.model
    FollowAuthor: @conn.chat.FollowAuthorModel.model
    FollowStar: @conn.chat.FollowStarModel.model
    Setting: @conn.chat.SettingModel.model
    AccountSub: @conn.chat.AccountSubModel.model
    Institution: @conn.chat.InstitutionModel.model
    Content: @conn.chat.ContentModel.model
    PF: @conn.chat.PFModel.model
    FollowPF: @conn.chat.FollowPFModel.model
    Event: @conn.chat.EventModel.model
    AttendingEvent: @conn.chat.AttendingEventModel.model
    InterestedEvent: @conn.chat.InterestedEventModel.model

P.S. it seems to me that you over normalized your data model. If for you that is more comfortable, you could use a relational DBMS like postgresSQL or MySQL, which have better back-office tools.

skilesare commented 9 years ago

Good news! I got past the first error and am on the next. Looks like I had a field in one of my Schemas that was just set to 'Array' and formage wasn't liking that. I changed it to [] and same issue. It was a placeholder anyway so commenting it out worked.

Now I'm getting:Fatal error: Most middleware (like urlencoded) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.

Which I guess relates to Express 4. I'm guessing formage isn't compatible? If so is there an upgrade guide? Please close this issue. Thanks for the help.

PS... most of these collections are just annotation of quick stream look up collections. Most of this started in a relational DB and I moved it to mongo because it was much better/faster at the clustered indexable tables I was using.

jontonsoup commented 8 years ago

@skilesare did you solve the connect middleware issue? I'm getting the same thing.