@cerebruminc/prisma-stable-sort-middleware
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

Prisma Stable Sort Middleware

A Prisma middleware for ensuring stable sorting of results on any model


When using Prisma to query a database and ordering the results by a non-unique field the order of the results is not guaranteed to be stable. This is because the database does not guarantee the order of results if multiple rows have the same value for the field that the results are being ordered by. This can be problematic if you are using the results of a query to generate a list of items that the user can interact with, as the order of the items may change between requests. This middleware solves this problem by adding a secondary sort order to the query, base on a unique field in the model. This ensures that the order of the results is stable, even if multiple rows have the same value for the field that the results are being ordered by.

Installation

npm i @cerebruminc/prisma-stable-sort-middleware

Usage

The middleware introspects the Prisma model to determine which fields can be used as a secondary sort order. This means that you need to pass the Prisma client to the middleware when you initialize it.

import { stableSortMiddleware } from "@cerebruminc/prisma-stable-sort-middleware";
import { PrismaClient } from "@prisma/client";

const client = new PrismaClient();
client.$use(stableSortMiddleware(client));

License

The project is licensed under the MIT license.



Cerebrum Cerebrum

Readme

Keywords

none

Package Sidebar

Install

npm i @cerebruminc/prisma-stable-sort-middleware

Weekly Downloads

184

Version

2.0.1

License

MIT

Unpacked Size

46.8 kB

Total Files

9

Last publish

Collaborators

  • rennen
  • tuliomitre
  • lucianbuzzo
  • sebmellen
  • shubham0850
  • davesauce
  • byennen
  • sarjuhansaliya
  • lanceennen