Search code examples
mysqlnode.jsormloopbackjs

Loopback filter NOT BETWEEN date


I'm using loopback models to filter a list of events with start and end dates and I want to return a list of events that are NOT between two specific dates. I thought something like this should work:

const eventList = await Events.find({ 
  where: {
    startDate: {
      not: {
        between: [unavailableStarting, unavailableEnding],
      },
    },
    endDate: {
      not: {
        between: [unavailableStarting, unavailableEnding],
      },
    },
  },
});

If you take out the not part, this works fine for getting all events that ARE between the dates. How do I make a NOT BETWEEN filter work?


Solution

  • As far as I can tell there is no way to use NOT BETWEEN with loopback models, but I did make it work with lt gt:

    const eventList = await Events.find({
      where: {
        and:[{
          or: [
            { startDate: { lt: unavailableStarting } },
            { startDate: { gt: unavailableEnding } },
          ]
        },
        {
          or: [
            { endDate: { gt: unavailableStarting } },
            { endDate: { lt: unavailableEnding } },
          ]
        }
      ]},
    });