Nodejs Object Document Model for Redis - node.js

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.

Related

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.

Using Mongoose Rather than the MongoDB Driver to Find Document

We are in the process of writing tests for our Node/MongoDB backend and I have a question about finding documents.
My understanding is it's preferable to use Mongoose to get your documents as opposed to the MongoDB driver. In other words, doing Customer.findOne().exec() instead of setting up a db connection and then doing db.collection("customers").findOne().
Other than the first option (using Mongoose to find the doc) being slightly less verbose, I'm curious what the other reasons are. Is a straight MongoDB lookup a bigger drag on the database?
One of the great feature of mongoose is the built in validation mechanism. Also the Populate method to get data from multiple collections is an awesome characteristic of Mongoose.
In terms of query performance, here is a good read:
https://medium.com/#bugwheels94/performance-difference-in-mongoose-vs-mongodb-60be831c69ad
Hope this helps :)

define a schema with JSON-Schema and use Mongoose?

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.

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.

Are the Node.JS MongoDB sorting/filtering functions available outside the database?

The MongoDB sorting functions are pretty neato. Can you use them on objects and/or arrays that have nothing to do with the database itself?
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db,
sortingFun = mongo.internalSortFilterFunction(); // By the miracle of imagination, this is a made-up line.
There is, for example, this awesome little node project called sift: MongoDB inspired array filtering. But there are more similar tools, different opinions, and projects merging and disappearing.
Considering it's popularity, MongoDB is quite probably gonna hang around. For that reason, plus the added bonus of being exactly similar instead of pretty similar, I was wondering if a specific object/model/function within node-mongodb could be linked from the require('mongodb') specifically for using the sorting and filtering functions on custom objects/arrays.
The sorting is done in the mongo server, not the client. It's also not particularily fast -- big collections should be pre-sorted, but that's another issue.
The mongo server is afaik written in C++ and uses custom types, separate from the JS engine, called BSON.
So if there is no sort implementation on the client for javascript, which would be an absurd feature, you can't use server sort.
Edit: If you really really want to use the sort, performance be damned, you could insert js objects into the DB, effectively converting them to BSON in mongo collections. Then sort it and pull it from the DB. Indexes etc will need to be recreated for every call to that function. Mongodb also refuses to sort for big collections sans index (limit being somewhere around 1000 I believe)
PS. I haven't read the source. I can't imagine a JS realtime, indexless sort that matches the speed of MongoDB's sort esp. when distributed (sharded). But you can write node.js modules in C++, and if BSON is similar enough to V8 JS objects (wouldn't think so), you might be able to port it. I wouldn't go down that road because it's probably not going to be a big speed increase compared to reimplementing it in JS, a reimplementation which would be a lot easier to create and maintain.

Resources