API
Adapters

Adapters

An adapter is a class implementing various methods allowing you to query the data to the database your app uses.

Out of the box, next-crud provides an adapter for Prisma (opens in a new tab), but it is easy to create your own one. Your adapter must follow the following interface:

export interface IAdapter<
  T,
  Q = IParsedQueryParams,
  M extends string = string
> {
  models: M[]
  init?: () => Promise<void> // Called before creating the handler in case you need to initialize the adapter with asynchronous code
  parseQuery(resourceName: M, query?: IParsedQueryParams): Q
  getAll(resourceName: M, query?: Q): Promise<T> // GET /api/resourceName
  getPaginationData(resourceName: M, query: Q): Promise<TPaginationData> // Used for pagination
  getOne(resourceName: M, resourceId: string | number, query?: Q): Promise<T> // GET /api/resourceName/:id
  create(resourceName: M, data: any, query?: Q): Promise<T> // POST /api/resourceName
  update(
    resourceName: M,
    resourceId: string | number,
    data: any,
    query?: Q
  ): Promise<T> // PUT/PATCH /api/resourceName/:id
  delete(resourceName: M, resourceId: string | number, query?: Q): Promise<T> // DELETE /api/resourceName/:id
  connect?: () => Promise<void> // connect function to your DB
  disconnect?: () => Promise<void> // disconnect function to your DB
  handleError?: (err: Error) => void // Used to throw a custom error of your own
  getModels(): M[]
  getModelsJsonSchema?: () => any // JSON Schema of your models, used for Swagger
  mapModelsToRouteNames?: () => { [key in M]?: string } // Object associating the model name with a route name
}

The parseQuery function is used to transform out basic query parsing into a shape that your database would expect. If you don't need that kind of transformation, your function can just return the query argument, which has the following properties:

select

An object with either boolean values or nested objects for nested fields.

Example:

{
  user: true,
  articles: {
    user: true
  }
}

includes

Same as select

where

An object obtained by applying JSON.parse on the associated query param

orderBy

An object obtained by applying JSON.parse on the associated query param

limit

A number

skip

A number

distinct

A string

Additionally, you will receive the potential query params that has not been parsed.

Prisma adapter

Options

The PrismaAdapter class can be instantiated with the following options

models

An optional array of the Prisma models you want to support. By default it takes the model names stored in the Prisma client. You can customise that behavior using the models configuration option of the NextCrud function.

primaryKey

The name of the key used to retrieve the your unique resources. Defaults to id.

prismaClient

An instance of your Prisma client.

manyRelations

An object of arrays containing all the relations and nested relations of your model that could be queried from the where filter. For nested relations, split them with a ., e.g: posts.author The keys of the object must correspond to the models names.

Example:

new PrismaAdapter<User | Post, ModelName>({
  prismaClient: myPrismaClient,
  manyRelations: {
    User: ['posts'],
  },
})

Additional query params

The Prisma adapter allows you to use an additional query param, which is related to a property used by prisma:

cursor

A JSON object containing only 1 key and a matching value corresponding to an entry in the database which is the starting point of the result of the query. You can see that as an offset.

Example :

/api/users?cursor={"id":1}