npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@prisma-fts/algolia

v1.0.0

Published

This library performs Prisma full-text search in Algolia.

Downloads

2

Readme

codecov

prisma-fts

@prisma-fts/algolia

This is an enhancement library that allows PrismaClient to perform full-text search of Algolia.

Setup

It is assumed that you have already set up prisma and prepared your application and index in Algolia.

yarn add algoliasearch @prisma-fts/algolia

Suppose we have the following Person model and further assume that Algolia has a person index with name and descriptin as text data and id as objectID.

model Person {
  id         Int     @id @default(autoincrement())
  name       String
  descriptin String
  age        Int
  country    String
}
import { PrismaClient, Prisma } from "@prisma/client";
import { algoliaFTS } from "@prisma-fts/algolia";
import algolia from "algoliasearch";

const algoliaClient = algoliasearch("YourApplicationID", "YourAdminAPIKey");

const prisma = new PrismaClient();
const middleware = algoliaFTS(
  Prisma.dmmf,
  {
    Person: {
      objectID: "id",
      indexes: {
        nane: algoliaClient.initIndex("person"),
        description: algoliaClient.initIndex("person"),
      }
    },
  }
)
prisma.$use(middleware);

How to use

You can search for records via Algolia by setting search keywords to the columns specified in the index mapping by prefixing them with fts:.

await prisma.person.findMany({
  where: { 
    description: "fts:appple ceo",
                      // ^ it's typo
  },
});

/*
[
  {
    id: 1,
    title: "Steve Jobs",
    description: "Steven Paul Jobs was an American entrepreneur, ... He was the co-founder, the chairman, and CEO of Apple; ...and more",
    age: 56,
    country: "US"
  }
]
*/

Note: It is important to prefix it with fts:.
Without it, the query will be directed to a regular database.

If you do a search with a fts: prefix, it will bypass that keyword to Algolia for a full-text search. Based on the ID of the object obtained by that search, you can retrieve the actual data by searching the original database.

algoliaFTS

Configure the mapping of indexes to be used, synchronization of index objects, etc.
The return value of the algoliaFTS is used to set the prisma middleware (.$use).

prisma.$use(algoliaFTS(dmmf, indexes, options));

Params

dmmf (required)

Need dmmf of Prisma module exported from @prisma/client.

import { Prisma } from "@prisma/client"
// Prisma.dmmf
indexes (required)

Mapping of indexes to be linked to Algolia.
The first level is an object with the model name as the key and the index mapping object as the value.
Index mapping has the following members.

  • objectID: Primary key name of the table, such as id.
  • indexes: Object with column name as key and Algolia index as value.
prisma.$use(
  algoliaFTS(
    Prisma.dmmf,
    {
      [modelName1]: {
        objectID: "primaryKey (id column)",
        indexes: {
          [columnName1]: algoliaClient.initIndex("yourIndexName"),
          [columnName2]: algoliaClient.initIndex("yourIndexName"),
        },
      },
      [modelName2]: {
        objectID: "primaryKey (id column)",
        indexes: {
          [columnName1]: algoliaClient.initIndex("yourIndexName"),
          [columnName2]: algoliaClient.initIndex("yourIndexName"),
        },
      },
    }
  )
);
// example

/*
model Post {
  id            Int      @id @default(autoincrement())
  title         String
  content       String
  published     Boolean
  publishedAt   DateTime
}

model Mail {
  code          String   @id @default(uuid())
  from          String
  to            String
  subject       String
  body          String
  sentAt        DateTime @default(now())
}
*/

prisma.$use(
  algoliaFTS(
    Prisma.dmmf,
    {
      Post: {
        objectID: "id",
        indexes: {
          content: algoliaClient.initIndex("post"),
        },
      },
      Mail: {
        objectID: "code",
        indexes: {
          subject: algoliaClient.initIndex("mail"),
          body: algoliaClient.initIndex("mail"),
        },
      },
    }
  )
);
options (optional)
  • syncOn (optional): Array<"create" | "update" | "upsert" | "delete">
    • Set syncOn to synchronize indexes when creating, updating, or deleting records.
prisma.$use(
  algoliaFTS(
    Prisma.dmmf,
    {
      Post: {
        objectID: "id",
        content: algoliaClient.initIndex("post"),
      },
    },
    {
      syncOn: ["create", "update", "upsert", "delete"]
    }
  )
);

// The new object is added to the post index.
await prisma.post.create({ data: { content: "..." } });

// Updates the index object with objectID of "xxxxxx".
await prisma.post.update({ where: { id: "xxxxxx" }, data: { content: "..." } });

// Deletes the index object with objectID is "xxxxxx".
await prisma.post.delete({ where: { id: "xxxxxx" } });

Note: Operations on multiple records such as createMany, upadateMany, and deleteMany are not supported.
It is recommended to synchronize the database and Algolia directly using a database trigger function etc.

Advanced Usage

It is possible to pass search api parameters as JSON format after keywords when searching.

await prisma.person.findMany({
  where: { 
    description: "fts:スティーブジョブス" + JSON.stringify({ queryLanguages: ["ja"] })
  },
  // This is the same as `index.search("スティーブジョブス", { queryLanguages: ["ja"] })`.
});

It can be used in combination with other prisma search parameters such as where and select.

await prisma.person.findMany({
  where: {
    description: "fts:Steve",
    age: { gte: 60 },
  },
});
await prisma.post.findMany({
  select: {
    id: true,
    author: true,
    title: true,
    createdAt: true,
  },
  where: {
    OR: [
      { description: "fts:apple" },
      { description: "fts:orange" },   
    ],
    tags: {
      some: { name: "fruits" }
    }, 
  },
});

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

License

This project is licensed under the MIT License - see the LICENSE file for details