Coder Social home page Coder Social logo

kinjalrk2k / prisma-extension-pg-trgm Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 2.0 158 KB

Prisma extension for supporting alphanumeric similarity using PostgreSQL pg_trgm module

Home Page: https://www.npmjs.com/package/prisma-extension-pg-trgm

License: MIT License

TypeScript 100.00%
pg postgres prisma prisma-client prisma-extension prisma-orm sql pg-trgm

prisma-extension-pg-trgm's Introduction

prisma-extension-pg-trgm

Extending Prisma Client to support pg_trgm functions, exclusively for PostgreSQL Databases. pg_trm is used for determining similarity between texts based on trigram matching. For extensive documentation on pr_trgm, refer here

Features

  • Queries similar to native Prisma's sysntax
  • Fully Typed
  • Support for filtering and sorting based on similarity scores
  • List of functions implemented:
    • similarity (text, text)
    • word_similarity (text, text)
    • strict_word_similarity (text, text)
  • JSDoc

Installation

npm install prisma-extension-pg-trgm

Extending Prisma client

import { PrismaClient } from "@prisma/client";
import { withPgTrgm } from "prisma-extension-pg-trgm";

const prisma = new PrismaClient().$extends(withPgTrgm({ logQueries: true }));

Note

logQueries can be set to true to log the queries on the console. It's useful for debugging, however should be turned off while deploying to production environments

Usage

Basic usage

async function main() {
  const result = await prisma.post.similarity({
    query: {
      title: {
        similarity: { text: "interpreter", order: "desc" },
        word_similarity: { text: "interpreter", threshold: { gt: 0.01 } },
        strict_word_similarity: { text: "interpreter", threshold: { gt: 0.002, lte: 0.3 } },
      },
    },
  });

  console.log(result);
}

main();

The query in the above example is converted to the following SQL query:

SELECT *,
  similarity(title, 'interpreter') AS title_similarity_score,
  word_similarity(title, 'interpreter') AS title_word_similarity_score,
  strict_word_similarity(title, 'interpreter') AS title_strict_word_similarity_score
FROM "Post"
WHERE
  word_similarity(title, 'interpreter') > 0.01 AND
  strict_word_similarity(title, 'interpreter') > 0.002 AND
  strict_word_similarity(title, 'interpreter') <= 0.3
ORDER BY
  similarity(title, 'interpreter') desc

Renamed model and field names

Prisma allows you to rename the model and field names using @@map and @map through the Prisma Schema. This has been explained in their official documentation here

However, the extension has no way to get those modified names. To counter that, look into the following example

Here the tagName field has been renamed to tag_name and the tags model has been renamed to label. So, in the database level, you'll find a table name as label with a column name as tag_name. As this extension relies on Raw Prisma queries, the actual table and column names are essential

  • Prisma Schema:
model tags {
  id      Int     @id @default(autoincrement())
  tagName String? @map("tag_name")

  @@map("label")
}
  • Query:
async function main() {
  const result = await prisma.tags.similarity({
    query: {
      // the field in Prisma model is tagName, still in database it's tag_name
      tag_name: {
        similarity: { text: "or", threshold: { gte: 0.01 }, order: "desc" },
      },
    },
    __meta: {
      tableName: "label", // here the actual table name is passed
    },
  });

  console.log(result);
}

main();

The query in the above example is converted to the following SQL query:

SELECT *,
  similarity(tag_name, 'or') AS tag_name_similarity_score
FROM "label"
WHERE similarity(tag_name, 'or') >= 0.01
ORDER BY similarity(tag_name, 'or') desc

Known Issues

  • This extension relies on Raw Prisma queries. So, running un-safe queries might come into play. This extension doesn't handle any sanitization of the inputs internally. So, developers implementing this extenstions should put in the right checks before using this in a production system
  • There's currently a quirky way to handle renamed model and field values described above. If there's a better way to handle this, please consider opening a Issue or a Pull Request detailing the approach
  • Selecting specified fields is currently not supported. Currently all the fields in the model as well as the similarity scores are outputted.
  • Joining tables are not supported. I'm not a fan of Prisma's joining techniques (prisma/prisma#12715) and supporting native join might be shelved for a future release

prisma-extension-pg-trgm's People

Contributors

kinjalrk2k avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

merdangulmanow

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.