# Add custom methods to your models

Extend the functionality of Prisma Client, model component

You can use the `model` [Prisma Client extensions](/content/docs/orm/prisma-client/client-extensions/index.html) component type to add custom methods to your models.

Possible uses for the `model` component include the following:

- New operations to operate alongside existing Prisma Client operations, such as `findMany`
- Encapsulated business logic
- Repetitive operations
- Model-specific utilities

## [Add a custom method](/content/docs/orm/prisma-client/client-extensions/model#add-a-custom-method/index.html)

Use the `$extends` [client-level method](/content/docs/orm/reference/prisma-client-reference#client-methods/index.html) to create an _extended client_. An extended client is a variant of the standard Prisma Client that is wrapped by one or more extensions. Use the `model` extension component to add methods to models in your schema.

### [Add a custom method to a specific model](/content/docs/orm/prisma-client/client-extensions/model#add-a-custom-method-to-a-specific-model/index.html)

To extend a specific model in your schema, use the following structure. This example adds a method to the `user` model.

```javascript
const prisma = new PrismaClient().$extends({
  name?: '<name>',  // (optional) names the extension for error logs
  model?: {
    user: { ... }   // in this case, we extend the `user` model
  },
});
```

#### [Example](/content/docs/orm/prisma-client/client-extensions/model#example/index.html)

The following example adds a method called `signUp` to the `user` model. This method creates a new user with the specified email address:

```javascript
const prisma = new PrismaClient().$extends({
  model: {
    user: {
      async signUp(email: string) {
        await prisma.user.create({ data: { email } });
      },
    },
  },
});
```

You would call `signUp` in your application as follows:

```javascript
const user = await prisma.user.signUp("john@prisma.io");
```

### [Add a custom method to all models in your schema](/content/docs/orm/prisma-client/client-extensions/model#add-a-custom-method-to-all-models-in-your-schema/index.html)

To extend _all_ models in your schema, use the following structure:

```javascript
const prisma = new PrismaClient().$extends({
  name?: '<name>', // `name` is an optional field that you can use to name the extension for error logs
  model?: {
    $allModels: { ... }
  },
})
```

#### [Example](/content/docs/orm/prisma-client/client-extensions/model#example-1/index.html)

The following example adds an `exists` method to all models.

```javascript
const prisma = new PrismaClient().$extends({
  model: {
    $allModels: {
      async exists<T>(this: T, where: Prisma.Args<T, "findFirst">["where"]): Promise<boolean> {
        // Get the current model at runtime
        const context = Prisma.getExtensionContext(this);

const result = await (context as any).findFirst({ where });
        return result !== null;
      },
    },
  },
});
```

You would call `exists` in your application as follows:

```javascript
// `exists` method available on all models
await prisma.user.exists({ name: "Alice" });
await prisma.post.exists({
  OR: [{ title: { contains: "Prisma" } }, { content: { contains: "Prisma" } }],
});
```

## [Call a custom method from another custom method](/content/docs/orm/prisma-client/client-extensions/model#call-a-custom-method-from-another-custom-method/index.html)

You can call a custom method from another custom method, if the two methods are declared on the same model. For example, you can call a custom method on the `user` model from another custom method on the `user` model. It does not matter if the two methods are declared in the same extension or in different extensions.

To do so, use `Prisma.getExtensionContext(this).methodName`. Note that you cannot use `prisma.user.methodName`. This is because `prisma` is not extended yet, and therefore does not contain the new method.

For example:

```javascript
const prisma = new PrismaClient().$extends({
  model: {
    user: {
      firstMethod() {
        ...
      },
      secondMethod() {
          Prisma.getExtensionContext(this).firstMethod()
      }
    }
  }
})
```

## [Get the current model name at runtime](/content/docs/orm/prisma-client/client-extensions/model#get-the-current-model-name-at-runtime/index.html)

You can get the name of the current model at runtime with `Prisma.getExtensionContext(this).$name`. You might use this to write out the model name to a log, to send the name to another service, or to branch your code based on the model.

For example:

```javascript
// `context` refers to the current model
const context = Prisma.getExtensionContext(this);

// `context.$name` returns the name of the current model
console.log(context.$name);

// Usage
await (context as any).findFirst({ args });
```

Refer to [Add a custom method to all models in your schema](/content/docs/orm/prisma-client/client-extensions/model#example-1/index.html) for a concrete example for retrieving the current model name at runtime.

## [Advanced type safety: type utilities for defining generic extensions](/content/docs/orm/prisma-client/client-extensions/model#advanced-type-safety-type-utilities-for-defining-generic-extensions/index.html)

You can improve the type-safety of `model` components in your shared extensions with [type utilities](/content/docs/orm/prisma-client/client-extensions/type-utilities/index.html).
