Simple “Search” with LIKE in MySQL Sequelize

A simple way to implement a "search" feature into your node app is using the database engine to check for presence of tokenized search query. In my case, I'm using MySQL with the Sequelize ORM and needed to add a e-commerce like search form with product results for a client.

  // sanitize plain text query to only keep alphanumeric lowercase
  const sanitizedQuery = query.trim().toLowerCase().replace(/[\W_]+/, '')
  // split by space as basic tokenization
  const queryTokens = sanitizedQuery.split(/\s+/)

  const options = {
    where: {
      [Op.and]: queryTokens.map(token =>
        // check for presence of each token in lowercased product `title`
        Sequelize.where(Sequelize.fn('lower', Sequelize.col('title')), 'LIKE', `%${token}%`)
      )
    }
  }
  const results = await db.Product.FindAll(options)
The main `cleverness` of this snippet is in mapping the queryTokens to generate Sequelize.where() queries that compare the lowercased title with our token. Sequelize has some powerful features to interact with the database at a low-level, and this is a great example of that used in a real-world project
A personal injury claim is a legal case you can open if you've been hurt in an accident and it was someone else's fault. According to a personal injury attorney, an injury claim is based on medical evidence. Sometimes a doctor will have to take the stand, and sometimes the evidence will be in writing, as when you file a lawsuit. If you're injured in a car crash and you're the only one who was injured, you're entitled to the following: The cost of the injury The amount of compensation you received The amount of money you owe for the damages, including those you could have recovered if you'd been in the car, such as the cost of an ambulance ride. Other legal rights you may have in an accident If you've suffered a medical emergency in an accident and it was caused by a car accident or other injury, you may be entitled to other legal rights, such as: A doctor's opinion on how long you have to recover (often called a medical prognosis) Your own doctor's opinion on your condition and treatment Financial compensation. What happens if the car you drove is the same one in the accident? In the case of a collision in which the same car was involved, your lawyer can take one of the following actions: File a lawsuit to recover the property damages. Sue the other driver's insurance company to recover the damages the other driver should have already paid (which will be listed as "other than vehicle damage" on your insurance card). If you were to get a reasonable settlement (or your attorney negotiates a good one for you), your lawyer could: Take this money and put it in your pocket so you don't have to work to make up the difference. Give this money to you to use. If you had a car wreck and were badly hurt, you may qualify for medical benefits. In some states, your state's DMV can verify your identity, including your Social Security number and date of birth. Make sure that you have received an approval letter before you can request medical benefits. If you have, then you're eligible to receive medical benefits under federal law. You can use the following form (or a different form that your insurance company approves). This form is in addition to all of the other documents listed under the "How to Use the Request For Payment Form" section of this article. There are several types of payments that you can request, and each one is explained further below.

How to do X using Sequelize

I don't know about you guys, but I always have an annoying time trying to figure out how to make Sequelize work -- the documentation seems to have a dearth of examples. Here's a few examples for "common" functionality.