define a schema with JSON-Schema and use Mongoose? - node.js

Hullo,
I have a crux to bear with Mongoose.
Is there a way of using JSON-Shema with Mongoose Schemas?
Say I want to define my API data schema using a standard like JSON-Schema, because it's nice.
It seems like I need to define it again when I want to use Mongoose / MongoDB!
That's quite some ugly duplication I like to avoid. Ideally, changing the JSON-Schema definition would also change the MongoDB schema.
A similar problem would appear if I would use JOI.JS validation library.
Has anyone found a solution to that?
Or is there an alternative approach?
thanks

Try this library: https://www.npmjs.com/package/json-schema-to-mongoose There are others out there too. I created json-schema-to-mongoose since the other libraries didn't quite fit my needs.
Also, I like to generate the json-schema from TypeScript using Typson. It makes it so the json-schema is more statically typed.
Update
It appears the Typson project is dead. Here's another (typescript-json-schema) project which does the same thing, although I've never used it.

Chiming in here since I've also run into this problem and found a solution that is alternative to the suggestions provided.
One can use https://github.com/nijikokun/generate-schema to take a plain JS object and convert it to both JSON Schema and a Mongoose Schema. I find this tool to be easier in the case of retrofitting existing code with validation and persistence since you will likely already have an example object to start with.

Related

Django Wagtail dynamically create form without new model

How would I allow my primary user to dynamically create forms they can issue to their end clients. Each of my primary users has their own unique information they would like to collect that I do not know before hand. I would like to avoid creating new models in code for their dynamic needs and then having to migrate the models.
I came across this which had an interesting response but it starts with disclaimer
The flexibility of Python and Django allow developers to dynamically create models to store and access data using Django’s ORM. But you need to be careful if you go down this road, especially if your models are set to change at runtime. This documentation will cover a number of things to consider when making use of runtime dynamic models.
Which leads me to believe a lot can go wrong.
However because I'm using wagtail I believe there is probably a way to use StructBlocks & StreamFields to accomplish it.
Any guidance would be helpful.
Wagtail provides a form builder module for this purpose.
I have two possible solutions for you, although it should be said that there is probably some library with Django that I don't know about that does this, but that being said.
Prompt your user for which fields they want and the field type.
Pass this as a dictionary to some function that would generate the HTML code for the form.
When this form is used, instead of worrying about storing the fields seperately, store a dictionary in the Models. There are two ways to do that here
Another way that you could do this, albeit more convoluted but more suited to your needs, is to use MongoDB for the database for Django instead. Because it is unstructured, it might be better suited for your use case. Instructions on using MongoDB for Django are here

Best way to convert query results to domain entities

I am using Knex.js to build SQL queries. It works well but I need to convert my query results into domain entities (a type representing an object from the domain) for my graphql resolvers. I used Knex to avoid using an ORM because a number of people online made it seem like an ORM will make queries more difficult. My current best idea is to follow the Repository pattern and have the ugly code for converting results to classes in the repo class. Better ideas are welcome :)
As I understood you want to just make a db-call based on GraphQL query (which is mean you already have db and want to use simple ORM instead of EF for example).
I don't know which platform do you have, but if you have .net, you can take a look NReco.GraphQL. It allows to set db-connection and define graphql schema in the json file (graphql schmea to db-table including relation between schemas), definately, it's worth take a look.

dynamic ORM in node.js+mongodb

Is it possible to create a model where the relationships are dynamically generated by the application?
I saw the KeystoneJS project that does a nice job of defining the model (see: http://keystonejs.com/docs/database/#relationship-definitions)
But these need to be defined by node, I'm interested in creating these within the application. Are there any ORMs or framework projects that already do that? I I've seen frameworks like the MODxCMS that allow users to create additional fields, by putting everything from the custom (templatevar) values into one table. think mongodb would be great for setting this up without this single table approach.
Any idea how to go about setting this kind of system up? I'm not sure where to start.
I guess mongoose might help you here. And you may want to have a look at mongo-relation too.

Update my mongodb schema

Currently, my schema for my mongodb app is very straightforward. However, I'd like to simplify and clean it up further. What's the best way of updating my schema design? Should I just write a remapper in my language of choice using a library (fairly trivial), or is there a simpler way?
I don't mind doing the above, I just would like to know if there's a really obvious way of doing it reliably.
You dont have to migrate your schema, mongodb like any NoSQl system is an answer to schema problems of RDBMS.
That said, you will have to migrate your data by making a migration script.
You might find this answer useful.

Nodejs Object Document Model for Redis

I'm want use an ODM for redis in node.js. Does anybody have any experience using any? I ideally want something that is somewhat the equivalent of mongoose (except instead for Redis instead of Mongodb).
The two I have heard of so far are nohm and ron. Can someone compare the experience of using either of these? Also I have read about redback and I am curious if it would work well with an ODM.
Redis is pretty simple to work with directly or to wrap your own objects around, but you could use an OHM (Object Hash Mapper) like Nohm:
source: https://github.com/maritz/nohm
documentation: http://maritz.github.com/nohm/
You can try redblade
Just one file. Help you create and remove index fields automatically.

Resources