Dynamic Table Specification Per Model Object

162 views Asked by At

I am trying to figure out a way to create dynamic model types, stored in their own respective collections (for a CMS). I want to be able to define a new "content type" and have it stored in it's own collection, and use a standard Sails model to control each model.

I am trying setting the tableName before saving and before fetching, however with no success.

So something like this:

(Model) Content:
 - id
 - createdDate
 - contentType
 - {n...fields}

(Model) ContentType:
 - id
 - collection (to which each content object would store)
 - name
 - fields
   - field{n}
     - type
     - name
     - required
     - …

I am just not sure how to make that happen in sails.js/waterline

2

There are 2 answers

0
Addo Solutions On BEST ANSWER

This is not possible, at least not the way I am hoping to do it, according to the Sails GitHub page:

https://github.com/balderdashy/sails/issues/1160

Bummer! If someone has a good solution though, let me know and I will mark it as the solution :)

2
nodnarB On

This is a possible solution for sails >=0.11.0: https://github.com/sgress454/sails-hook-autoreload

It sounds like it does exactly what you describe: Reload models on the fly without lowering/re-lifting sails.

So as a basic approach: in your admin interface that you're programming, add a content type and it writes to the /api/models/ folder. But please please please, beware the security issues of this! Then when a model gets created, this hook will detect that and load the new model into memory. Same should happen for altering and removing models, but I haven't tested this so I couldn't tell you.