Working with a Prisma Model

Marickian
By -
0
Creating a Model with Prisma

Creating a Token Model with Prisma

In this article, we will walk through the process of creating a basic token model using Prisma. This model will handle token management, allowing us to fetch, create, and manage user tokens. The process involves setting up the model, generating tokens for a user, and creating basic CRUD (Create, Read, Update, Delete) operations.

Setting Up the Token Model

The first step is to define a new token model in the Prisma schema file. This model will store tokens associated with a user. For this example, the clerkID will be the primary identifier, and each user will have an initial number of tokens, which can later be adjusted.

model Token {
  clerkID  String @id
  tokens   Int    @default(1000)
}
    

Here, we define two fields:

  • clerkID: This is the primary key that identifies each user.
  • tokens: This is an integer field that holds the number of tokens a user has, with a default value of 1000.

Pushing Changes to the Database

After creating the model in the schema, we push the changes to the database. Prisma’s CLI allows us to easily update the database schema:

npx prisma db push
    

This command ensures that the new Token model is added to the database.

Fetching User Tokens

We can now create a function to fetch the tokens for a given user using the clerkID. Here’s how you can do it:

async function fetchUserTokens(clerkID) {
  const result = await prisma.token.findUnique({
    where: { clerkID: clerkID }
  });
  return result ? result.tokens : null;
}
    

This function uses Prisma’s findUnique method to retrieve the token associated with the provided clerkID.

Generating Tokens for New Users

If a user does not yet have tokens, we need to create a new entry. We can use the following function:

async function generateUserTokens(clerkID) {
  const result = await prisma.token.create({
    data: {
      clerkID: clerkID,
      tokens: 1000
    }
  });
  return result.tokens;
}
    

This function creates a new token entry with the default value of 1000 tokens for a user.

Combining Fetch and Generate

We can create a helper function that fetches tokens if they exist, or generates new tokens if none are found:

async function fetchOrGenerateTokens(clerkID) {
  let tokens = await fetchUserTokens(clerkID);
  if (tokens === null) {
    tokens = await generateUserTokens(clerkID);
  }
  return tokens;
}
    

This approach simplifies the logic in places where we need to ensure that a user has tokens, regardless of whether they already exist or not.

Subtracting Tokens

In many scenarios, you may want to decrement a user's tokens after certain actions. Here is how you can subtract tokens:

async function subtractTokens(clerkID, amount) {
  const result = await prisma.token.update({
    where: { clerkID: clerkID },
    data: {
      tokens: { decrement: amount }
    }
  });
  return result.tokens;
}
    

The decrement operator in Prisma allows us to decrease the number of tokens without manually calculating the new value.

Conclusion

In this guide, we have walked through the creation of a basic token model using Prisma. By setting up the model, pushing it to the database, and creating several helper functions for token management, you now have a solid foundation for managing user tokens in your application.

These examples demonstrate key functionalities such as fetching, generating, and subtracting tokens, allowing you to build more complex logic on top of them.

Post a Comment

0Comments

Post a Comment (0)