I have a use case where I need to retrieve the latest public bookmarks public added on www.codever.land. They are store in a mongo database, and I use Mongoose as ORM.

I want to have two possibilites to achieve this:

  1. specify the number of days to look back
  2. specify the timestamp since when to look forward

I think the code is self explanatory

/**
 * Returns the bookmarks added recently.
 *
 * The since query parameter is a timestamp which specifies the date since we want to look forward to present time.
 * If this parameter is present it has priority. If it is not present, we might specify the number of days to look back via
 * the query parameter numberOfDays. If not present it defaults to 7 days, last week.
 *
 */
router.get('/latest-entries', async (req, res) => {
  try
  {

    if(req.query.since) {
      const bookmarks = await Bookmark.find(
        {
          createdAt: { $gte: new Date(parseFloat(req.query.since,0)) }
        }).sort({createdAt: 'desc'}).lean().exec();

      res.send(bookmarks);
    } else {
      const numberOfDaysToLookBack = req.query.days ? req.query.days : 7;

      const bookmarks = await Bookmark.find(
        {
          createdAt: { $gte: new Date((new Date().getTime() - (numberOfDaysToLookBack * 24 * 60 * 60 * 1000))) }
        }).sort({createdAt: 'desc'}).lean().exec();

      res.send(bookmarks);
    }

  }
  catch (err)
  {
    return res.status(500).send(err);
  }
});

Did you find yourself one or several times, copying your favourite java project, renaming it, removing files, keeping some best practice classes and so on, all that to create new projects similar to it? This is time consuming and can be very annoying… Well, you might wanna consider instead creating a maven archetype out of the template project and use to create new projects. Not only this will save you time, but it will make you look with a more critical view on your “best of breed” project. In this blog post I will show how to create a maven archetype based on an existing project and how to generate a new project from this template.

Continue Reading ...

Some days ago, I presented how the code got much cleaner when I replaced Mongoose callbacks with the async-await feature from ES7, which NodeJs already support. I mentioned then, I was gonna do a post to demonstrate how async-await can also be used to make parallel calls. Here am I doing just that. I was actually blown away by its simplicity and performance gain when doing so. Let me show you what I mean.

Continue Reading ...

Jax-RS REST Client example demonstrating how to GET a resource with a query parameter. The resource is secured with Basic Authentication.

A lot has been written already about the transition from callbacks to promises and now to the new async/await1 feature in ES7.

"The purpose of `async/await` functions is to simplify the behavior of using promises synchronously and to perform some behavior on a group of `Promises`. Just as `Promises` are similar to structured callbacks, `async/await` is similar to combining generators and promises."[^1]

In this blog post I present what this code “upgrade” meant for CRUD operations performed on dev bookmarks. I use Moongoose in an ExpressJS/NodeJS backend to perform the operations against a MongoDB database.

In a later refactoring the database access part has been decoupled from ExpressJS and moved to separate service functions. Thanks to unified error handling the database errors are now handled centrally.

Octocat Source code for codever.land is available on Github - Star

Continue Reading ...