Today, almost every application has to be connected and to share data with other applications. The best way to do that is throughAPIs. For a long time there hasn’t been any industry standard for designing and documenting APIs. And API without a good documentation on how to use it, is useless. Because of that, developers have …
Laravel Impersonate
Sometimes we face situations when all the tests are passing, we find no bugs, but still, on our users’ part, something broke. By impersonating our users, we can see what they see and track the bugs down easily. We can use laravel-impersonate package for this use case. https://github.com/404labfr/laravel-impersonate The installation and configuration is described in …
Make a mapping object from an array of objects using Reduce function in Javascript
Sometimes, we face some usecases to make a mapping object from an array of object. For example, we have a Users array, we need to find a user by tagId. Here tagIdis supposed to be unique. Generally, we may try to use array.find() function to find the object with tagId. But using array.reduce()function in Javascript, …
Export streaming for large MongoDB collections
When we export large MongoDB collections, the problem is it consumes a lot of memory and execution time because it will load all query results in memory at once. For example, below code is exporting all users. In this case, we can use MongoDB cursor. Cursor returns one document as readable stream so you can …
Cancel Preceded Requests from Frontend side Using Axios
There are some cases that user sends simultaneous requests while preceded requests are still pending. In this case, you will need to cancel the preceding requests. Axios provides cancelTokenfeature for this purpose. This stores cancelToken of previous request and use it to cancel the preceded requests with the token value.
Forced update for Mixed type in Mongoose
There is a Mixed data type in Mongoose schema definition. The problem with Mixed type is, it doesn’t recognize update event for Mixed type. To fix this, you should call markModified function.
Mongoose Virtuals
Mongoose supports virtual attributes. Virtual attributes are attributesthat are convenient to have around but that do not get persisted to mongodb. When we have m:1, m:n relationship, we may need to fetch all related data. For example; we have User and Portfolio schemas and Portfolio has user ID as foreign key. Let’s suppose when we …
Arbitrary reference in Mongoose schema
Supposing you need parent reference of Task schema, and parent can be either manager or developer. You don’t have to define both reference IDs in Task schema. Instead MongoDB provides feature to define arbitrary reference. You can assign value as Manager schema ID or Developer schema ID.
Set or update field automatically without manual additional in Mongoose/MongoDB
I had a chance that I need to add new field for each schema in the MongoDB. First time, I thought I should add them manually for each model definition. I investigated a better way and found that MongoDB provides plugin feature and provides hooks for each Mongoose events like find, save, update, delete. For …
Get and set request-scoped context anywhere in Express app
We may need to store user information to somewhere and want to grab the information from a utility function. Assuming I need to get current logged user and use it in mongoose schema functions. Using express-http-context npm package, you can easily implement this. In where you want to grab the information; If you want to …