Docs
Database

Client

Learn how to use the database client in Achromatic.

The database client is an export of the Prisma Client from the @prisma/client package. Prisma generates this client automatically based on your defined schema and it is exposed as the prisma object within the database package in the monorepo.

This guide outlines core operations with the database client, such as querying, creating, updating and deleting records. For in-depth details and advanced usage, visit the Prisma Client documentation.

Query records

To retrieve records from the database, you can use the findMany method provided by the Prisma client. This method accepts an object with optional fields such as where, orderBy, skip and take, which allow you to filter, sort, and paginate the results.

Here’s an example of how to query all contacts from the database:

import { prisma } from '@workspace/database/client';
 
const contacts = await prisma.contacts.findMany({
   orderBy: { createdAt: 'desc' }
});

Create record

To insert a new record into the database you can use the create method on the Prisma client by specifying the data to be saved:

import { prisma } from '@workspace/database/client';
 
const contact = await prisma.contacts.create({
    data: {
        name: 'John Doe',
        email: 'john.doe@gmail.com'
    }
});

By default Prisma is returning the full row. You can use select to make the create method more efficient.

Update record

To update an existing record in the database, you can use the update method on the Prisma client. You need to specify the where condition to identify the record and the data object with the fields you want to update:

import { prisma } from '@workspace/database/client';
 
const updatedContact = await prisma.contacts.update({
    where: {
        id: 1, // This is just an example, we mostly use UUIDs
    },
    data: {
        name: 'John Doe Updated',
        email: 'john.doe.updated@gmail.com'
    }
});

You can also use select to limit the returned fields and make the operation more efficient.

Delete record

To delete a record from the database, you can use the delete method on the Prisma client. Specify the where condition to identify the record to delete:

import { prisma } from '@workspace/database/client';
 
const deletedContact = await prisma.contacts.delete({
    where: {
        id: 1, // This is just an example, we mostly use UUIDs
    }
});

This will remove the record from the database permanently. You can use select to limit the fields returned after the deletion, if needed.