keystonejs / keystone-classic

Node.js CMS and web app framework
http://v4.keystonejs.com
MIT License
14.64k stars 2.2k forks source link

Exporting mongoose schemas #4160

Open creynders opened 7 years ago

creynders commented 7 years ago

description

I really need some way to export the mongoose schemas as generated by the fields in Keystone. This is useful for a few reasons:

  1. my specific use case: I've got some complex data migrations going on (i.e. schema changes for existing documents) and there's a lot of options to do this, if you have the mongoose schemas.
  2. export implies import: as a future path it might be interesting to be able to base a Keystone model on an existing mongoose schema, making migrations from non-keystone projects easier
  3. reuse of schemas, allowing to set up parallel mongoose connections using the same schemas w/o having to plug into the whole keystone system.

proposed solutions

For the moment I'll be monkey patching in my own projects, since time is of issue. But I'm wondering what solution you guys would prefer?

@JedWatson @mxstbr

creynders commented 7 years ago

Another solution, probably even easier:

  1. use the schema.paths property to export a schema in memory
vadimshvetsov commented 7 years ago

I'm trying to find another solution instead keystone now because it's force me to rewrite 2 models (keystone and mongoose) in migrations. It's difficult to test keystone models at now, but if @creynders will add possibility to import existing model in app it would more flexible and would be the best admin UI for me.

creynders commented 7 years ago

@VadimShvetsov importing existing models won't be for the near future. The problem is that Keystone expands the functionality and data types. E.g. an e-mail field will be a simple String in mongoose, but will be of type Email in Keystone, so you need some way to tell keystone that the simple string declaration in the mongoose model should be treated as Email in keystone. I.e it will require some kind of inheritance scheme where you can say that a keystone model should use a mongoose schema as a base, but allows you redefining/detailing the fields.

vadimshvetsov commented 7 years ago

@creynders Yeah, I understand, but for me it's more convenient to not expand my model with Keystone types, I need to tweak paths like 'email' with validators and my own methods easily, it's quite difficult with defined types. Adding existing models can add more flexibility to the framework but it's too much refactoring needed for it and I understand it's not necessary now.