Skip to content

kakasoo/sortable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sortable

getSortable

$ npm i @kakasoo/sortable

This is very simple function to create object for make prisma orderBy statement. For example, you can just write user.articles.created_at as parameter, like below code.

getSortable("user.articles.created_at", "desc"); // { user: { articles: { created_at: "desc" } } }

Because it is a type inference, it is much easier to use if you create and use a function in advance.

namespace IUser {
  namespace IRequest {
    type SortbaleColumns =
      | "user.articles.created_at"
      | "user.articles.updated_at";
  }
}

export const orderBy = <T extends IUser.IRequest.SortableColumns>(
  key: T,
  direction: "asc" | "desc"
) => {
  return getSortable(
    key,
    direction
  ) satisfies Prisma.usersOrderByWithRelationInput;
};

then, You can use like it, Right?

prisma.user.findMany({
  orderBy: orderBy("user.articles.created_at", "desc"),
});

createSortable

interface Example {
  user: {
    name: string;
    createdAt: string;
  };
}

// #1
const sortable = createSortable<Example>();
sortable("user.createdAt", "asc"); // It guarantees type inference.

// #2
// also you can use like this:
createSortable<Example>()("user.createdAt", "asc");

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published