---
title: "Type-Specific Identifier Generation"
url: "https://docs.ravendb.net/6.2/client-api/configuration/identifier-generation/type-specific"
version: "6.2"
language: "nodejs"
---

# Type-Specific Identifier Generation

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/client-api/configuration/identifier-generation/type-specific.md), [Java](https://docs.ravendb.net/6.2/client-api/configuration/identifier-generation/type-specific.java.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

\#Type-Specific Identifier Generation

[In the previous article](https://docs.ravendb.net/6.2/client-api/configuration/identifier-generation/global.nodejs.md), Global Identifier generation conventions were introduced. Any customization made by using those conventions changes the behavior for all stored entities.
Now we will show how to override the default ID generation in a more granular way, for particular types of entities.

To override default document identifier generation algorithms, you can register custom conventions per an entity type. You can include your own identifier generation logic.

```javascript
conventions.registerIdConvention(clazz, idConvention);
```

| Parameters | | |
| - | - | - |
| clazz | class or object | Entity type |
| idConvention | function `(databaseName, entity) => Promise<string>` | Identifier generation function that supplies a result for given database name and entity object. Must return a `Promise` resolving to a string. |

| Return Value | |
| - | - |
| DocumentConventions | Current `DocumentConventions` instance. |

> **Info:** Database name parameter {#database-name-parameter}
>
> The database name parameter is passed to the register convention methods to allow users to make Id generation decision per database.

## Example

Let's say that you want to use semantic identifiers for `Employee` objects. Instead of `employee/[identity]` you want to have identifiers like `employees/[lastName]/[firstName]`
(for the sake of simplicity, let us not consider the uniqueness of such identifiers). What you need to do is to create the convention that will combine the `employee` prefix, `LastName` and `FirstName` properties of an employee.

```javascript
store.conventions.registerIdConvention(Employee,
  (dbName, entity) => Promise.resolve(`employees/${entity.lastName}/${entity.firstName}`)); 

// or using async keyword
store.conventions.registerIdConvention(Employee,
  async (dbName, entity) => `employees/${entity.lastName}/${entity.firstName}`); 
```

Now, when you store a new entity:

```javascript
const session = store.openSession();
const employee = new Employee("James", "Bond");

await session.store(employee);
await session.saveChanges();
```

the client will associate the `employees/Bond/James` identifier with it.

> **Info**
>
> ID convention function must return a Promise since it *can* be asynchronous.

## Example: Object literal based entities

```javascript
// for object literal based entities you can pass type descriptor object
const typeDescriptor = {
  name: "Employee",
  isType(entity) {
      // if it quacks like a duck... ekhm employee
      return entity 
          && "firstName" in entity 
          && "lastName" in entity 
          && "boss" in entity;
  }
};

store.conventions.registerIdConvention(typeDescriptor, 
  async (dbName, entity) => `employees/${entity.lastName}/${entity.firstName}`);
```

## See also

- [Document identifier generation](https://docs.ravendb.net/6.2/server/kb/document-identifier-generation.md)
- [Working with document identifiers](https://docs.ravendb.net/6.2/client-api/document-identifiers/working-with-document-identifiers.md)
- [Global identifier generation conventions](https://docs.ravendb.net/6.2/client-api/configuration/identifier-generation/global.nodejs.md)
