---
title: Travailler avec le modèle de données
slug: oip-next/v4/usecase/creation-dun-modele-de-donnees
icon: {"lucideIcon":"📘"}
createdAt: 2025-10-09T12:20:37.000Z
---

# Vue d’ensemble

La spécification d'un modèle de données est l'une des grandes étapes du cycle de développement d'un OAV. Il intervient après le recueil des besoins du métier.

Le modèle de données est spécifié au format OpenAPI. L'OIP met à disposition des outils pour la génération du code des entités à partir de ce fichier.

Ce cas d'usage détaille l'utilisation de l'ensemble de ces outils.

# Prérequis

Ce cas d'usage nécessite un projet initialisé. Pour cela, référez-vous au [démarrage rapide](./002_quick_start/001_prerequisites.md).

# Cas d'usage

## Modèle de données

Considérez le modèle de données ci-dessous.

:::CodeblockTabs
entities.openapi.yml

```yaml
openapi: 3.1.2

info:
  title: Project
  version: 4.0.0
  contact:
    email: techlead@fasst.io

components:
  schemas:
    Project:
      type: object
      x-fasst-entity-category: OIP_ENTITY_ROOT
      description: L'entité Project est l'entité principale qui porte l'ensemble des autres entités
      required: ['name', 'externalId']
      properties:
        name:
          description: Nom du projet client
          type: string
        externalId:
          description: Identifiant externe du projet
          type: string
        companies:
          description: liste des entreprises associées au projet client
          type: array
          readOnly: false
          items:
            $ref: "#/components/schemas/Company"
        customers:
          description: liste des clients associées au projet client
          type: array
          readOnly: false
          items:
            $ref: "#/components/schemas/Customer"

    Company:
      x-fasst-entity-category: OIP_ENTITY
      description: L'entité Company contient les informations de l'entreprise
      type : object
      properties:
        name:
          description: Nom de l'entreprise
          type: string
        siret:
          description: Numéro SIRET de l'entreprise
          type: string

    Customer:
      x-fasst-entity-category: OIP_ENTITY
      description: L'entité Customer contient les informations du client
      type : object
      properties:
        firstName:
          description: Prénom du client
          type: string
        lastName:
          description: Nom du client
          type: string
```
:::

:::BlockQuote
**Info**
Le chemin de ce fichier doit être `config/entities/entities.openapi.yml`.
:::

Ce fichier définit trois relations et leurs relations :

- une entité `Project` (entité racine) ;
- une entité `Company` ;
- une entité `Customer` ;
- une relation de l'entité `Project` vers l'entité `Company` ;
- une relation de l'entité `Project` vers l'entité `Customer`.

## Génération du code

La commande pour générer le code des entités à partir du modèle de données est :

```shell
npm run entities:gen
```

La commande écrit sur la sortie standard du terminal :

- la liste des entités détectées ;
- la liste des fichiers générés ;
- les éventuelles erreurs.

:::BlockQuote
**Info**
Les fichiers générés ne doivent pas être modifiés manuellement.
:::

### Entités

La commande a généré 4 fichiers dans le dossier `entities` :

- `entities.mjs` ;
- `Company/index.mjs` ;
- `Customer/index.mjs` ;
- `Project/index.mjs`.

Le fichier `entities.mjs` contient la définition des entités.

Les fichiers `index.mjs` contiennent les fonctions de manipulation de l'entité et l'ensemble des accesseurs pour manipuler ses propriétés.

:::CodeblockTabs
entities/entities.mjs

```javascript
// Code generated by gen. DO NOT EDIT.

export default {
  Project: {
    properties: {
      name: { type: 'string' },
      externalId: { type: 'string' },
    },
    relations: ['Company', 'Customer'],
  },
  Company: {
    properties: {
      name: { type: 'string' },
      siret: { type: 'string' },
    },
  },
  Customer: {
    properties: {
      firstName: { type: 'string' },
      lastName: { type: 'string' },
    },
  },
};

```

entities/Company/index.mjs

```javascript
// Code generated by gen. DO NOT EDIT.

import { createEntity, deleteEntity, entities, getEntity, getter, importEntities, listEntities, setter, updateEntity } from '@fasstech/oip-core/entities';
import * as R from 'ramda';
import config from '../entities.mjs';

// ////////////////////////////////////////////////////////////////////////////
// `Company` entity helpers
// ////////////////////////////////////////////////////////////////////////////

/**
 * Creates or retrieves entities of type `Company`.
 * @param {{ __id: string; __type: 'Project' }} parentEntity - Parent entity.
 * @param {object | string} [filter] - Search criteria.
 * @param {{ create?: boolean | 'force'; limit?: number; list?: boolean; offset?: number; orderBy?: [string, 'asc' | 'desc']; auditInfo?: 'none' | 'last_updated' | 'all'; transactionId?: string }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string } | { __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string }[] | null>} The requested entities.
 */
export const $Company = async (parentEntity, filter, opts) => {
  return entities('Company', R.prop('Company', config), parentEntity, { opts, filter });
};

/**
 * Lists entities of type `Company`.
 * @param {{ __id: string; __type: 'Project' }} parentEntity - Parent entity.
 * @param {object} [filter] - Search criteria.
 * @param {{ create?: boolean; limit?: number; offset?: number; orderBy?: [string, 'asc' | 'desc']; auditInfo?: 'none' | 'last_updated' | 'all'; transactionId?: string }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string }[]>} The requested entities.
 */
export const listCompany = async (parentEntity, filter, opts) => {
  return listEntities('Company', R.prop('Company', config), parentEntity, { filter, opts });
};

/**
 * Gets an entity of type `Company`.
 * @param {{ __id: string; __type: 'Project' }} parentEntity - Parent entity.
 * @param {object | string} [filter] - Search criteria.
 * @param {{ create?: boolean; auditInfo?: 'none' | 'last_updated' | 'all'; transactionId?: string }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string } | null>} The requested entity.
 */
export const getCompany = async (parentEntity, filter, opts) => {
  return getEntity('Company', R.prop('Company', config), parentEntity, { filter, opts });
};

/**
 * Creates an entity of type `Company`.
 * @param {{ __id: string; __type: 'Project' }} parentEntity - Parent entity.
 * @param {{ name?: string; siret?: string }} [data] - Data of entity.
 * @param {{ transactionId?: string }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string } | null>} The created entity.
 */
export const createCompany = async (parentEntity, data, opts) => {
  return createEntity('Company', R.prop('Company', config), parentEntity, data, { opts });
};

/**
 * Updates an entity of type `Company`.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string }} entity - Entity.
 * @param {{ transactionId?: string; withChildren?: boolean }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string }>} The updated entity.
 */
export const updateCompany = async (entity, opts) => {
  return updateEntity('Company', R.prop('Company', config), entity, { opts });
};

/**
 * Deletes an entity of type `Company`.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string }} entity - Entity.
 * @param {{ transactionId?: string; withChildren?: boolean }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string }>} The deleted entity.
 */
export const deleteCompany = async (entity, opts) => {
  return deleteEntity('Company', R.prop('Company', config), entity, { opts });
};

/**
 * Imports a list of entities of type `Company`.
 * @param {{ __id: string; __type: 'Project' }} parentEntity - Parent entity.
 * @param {{ name?: string; siret?: string }[]} data - Data of entities.
 * @param {{ transactionId?: string; withChildren?: boolean }} [opts] - Options.
 * @returns {Promise<number>} The number of imported entities.
 */
export const importCompanyList = async (parentEntity, data, opts) => {
  return importEntities('Company', R.prop('Company', config), parentEntity, data, { opts });
};

// ////////////////////////////////////////////////////////////////////////////
// `Company` entity properties helpers
// ////////////////////////////////////////////////////////////////////////////

/**
 * Gets the value of the `__id` property of the `Company` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string }} entity - `Company` entity.
 * @returns {string} The value of `__id` property of `Company` entity.
 */
export const getCompanyId = (entity) => {
  return getter('Company', '__id', {})(entity);
};

/**
 * Gets the value of the `__createdAt` property of the `Company` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string }} entity - `Company` entity.
 * @returns {Date} The value of `__createdAt` property of `Company` entity.
 */
export const getCompanyCreatedAt = (entity) => {
  return getter('Company', '__createdAt', {})(entity);
};

/**
 * Gets the value of the `__updatedAt` property of the `Company` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string }} entity - `Company` entity.
 * @returns {Date} The value of `__updatedAt` property of `Company` entity.
 */
export const getCompanyUpdatedAt = (entity) => {
  return getter('Company', '__updatedAt', {})(entity);
};

/**
 * Gets the value of the `__auditInfo` property of the `Company` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string }} entity - `Company` entity.
 * @returns {{ publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }} The value of `__auditInfo` property of `Company` entity.
 */
export const getCompanyAuditInfo = (entity) => {
  return getter('Company', '__auditInfo', {})(entity);
};

/**
 * Sets the value of the `name` property of the `Company` entity.
 * @param {string} [value] - Value.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string }} entity - `Company` entity.
 * @returns {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string }} The updated `Company` entity.
 */
export const setCompanyName = R.curry((value, entity) => {
  return setter('Company', 'name', R.path(['Company', 'properties', 'name'], config))(value, entity);
});

/**
 * Gets the value of the `name` property of the `Company` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string }} entity - `Company` entity.
 * @returns {string | null | undefined} The value of `name` property of `Company` entity.
 */
export const getCompanyName = (entity) => {
  return getter('Company', 'name', R.path(['Company', 'properties', 'name'], config))(entity);
};

/**
 * Sets the value of the `siret` property of the `Company` entity.
 * @param {string} [value] - Value.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string }} entity - `Company` entity.
 * @returns {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string }} The updated `Company` entity.
 */
export const setCompanySiret = R.curry((value, entity) => {
  return setter('Company', 'siret', R.path(['Company', 'properties', 'siret'], config))(value, entity);
});

/**
 * Gets the value of the `siret` property of the `Company` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; name?: string; siret?: string }} entity - `Company` entity.
 * @returns {string | null | undefined} The value of `siret` property of `Company` entity.
 */
export const getCompanySiret = (entity) => {
  return getter('Company', 'siret', R.path(['Company', 'properties', 'siret'], config))(entity);
};

```

entities/Customer/index.mjs

```javascript
// Code generated by gen. DO NOT EDIT.

import { createEntity, deleteEntity, entities, getEntity, getter, importEntities, listEntities, setter, updateEntity } from '@fasstech/oip-core/entities';
import * as R from 'ramda';
import config from '../entities.mjs';

// ////////////////////////////////////////////////////////////////////////////
// `Customer` entity helpers
// ////////////////////////////////////////////////////////////////////////////

/**
 * Creates or retrieves entities of type `Customer`.
 * @param {{ __id: string; __type: 'Project' }} parentEntity - Parent entity.
 * @param {object | string} [filter] - Search criteria.
 * @param {{ create?: boolean | 'force'; limit?: number; list?: boolean; offset?: number; orderBy?: [string, 'asc' | 'desc']; auditInfo?: 'none' | 'last_updated' | 'all'; transactionId?: string }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string } | { __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string }[] | null>} The requested entities.
 */
export const $Customer = async (parentEntity, filter, opts) => {
  return entities('Customer', R.prop('Customer', config), parentEntity, { opts, filter });
};

/**
 * Lists entities of type `Customer`.
 * @param {{ __id: string; __type: 'Project' }} parentEntity - Parent entity.
 * @param {object} [filter] - Search criteria.
 * @param {{ create?: boolean; limit?: number; offset?: number; orderBy?: [string, 'asc' | 'desc']; auditInfo?: 'none' | 'last_updated' | 'all'; transactionId?: string }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string }[]>} The requested entities.
 */
export const listCustomer = async (parentEntity, filter, opts) => {
  return listEntities('Customer', R.prop('Customer', config), parentEntity, { filter, opts });
};

/**
 * Gets an entity of type `Customer`.
 * @param {{ __id: string; __type: 'Project' }} parentEntity - Parent entity.
 * @param {object | string} [filter] - Search criteria.
 * @param {{ create?: boolean; auditInfo?: 'none' | 'last_updated' | 'all'; transactionId?: string }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string } | null>} The requested entity.
 */
export const getCustomer = async (parentEntity, filter, opts) => {
  return getEntity('Customer', R.prop('Customer', config), parentEntity, { filter, opts });
};

/**
 * Creates an entity of type `Customer`.
 * @param {{ __id: string; __type: 'Project' }} parentEntity - Parent entity.
 * @param {{ firstName?: string; lastName?: string }} [data] - Data of entity.
 * @param {{ transactionId?: string }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string } | null>} The created entity.
 */
export const createCustomer = async (parentEntity, data, opts) => {
  return createEntity('Customer', R.prop('Customer', config), parentEntity, data, { opts });
};

/**
 * Updates an entity of type `Customer`.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string }} entity - Entity.
 * @param {{ transactionId?: string; withChildren?: boolean }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string }>} The updated entity.
 */
export const updateCustomer = async (entity, opts) => {
  return updateEntity('Customer', R.prop('Customer', config), entity, { opts });
};

/**
 * Deletes an entity of type `Customer`.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string }} entity - Entity.
 * @param {{ transactionId?: string; withChildren?: boolean }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string }>} The deleted entity.
 */
export const deleteCustomer = async (entity, opts) => {
  return deleteEntity('Customer', R.prop('Customer', config), entity, { opts });
};

/**
 * Imports a list of entities of type `Customer`.
 * @param {{ __id: string; __type: 'Project' }} parentEntity - Parent entity.
 * @param {{ firstName?: string; lastName?: string }[]} data - Data of entities.
 * @param {{ transactionId?: string; withChildren?: boolean }} [opts] - Options.
 * @returns {Promise<number>} The number of imported entities.
 */
export const importCustomerList = async (parentEntity, data, opts) => {
  return importEntities('Customer', R.prop('Customer', config), parentEntity, data, { opts });
};

// ////////////////////////////////////////////////////////////////////////////
// `Customer` entity properties helpers
// ////////////////////////////////////////////////////////////////////////////

/**
 * Gets the value of the `__id` property of the `Customer` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string }} entity - `Customer` entity.
 * @returns {string} The value of `__id` property of `Customer` entity.
 */
export const getCustomerId = (entity) => {
  return getter('Customer', '__id', {})(entity);
};

/**
 * Gets the value of the `__createdAt` property of the `Customer` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string }} entity - `Customer` entity.
 * @returns {Date} The value of `__createdAt` property of `Customer` entity.
 */
export const getCustomerCreatedAt = (entity) => {
  return getter('Customer', '__createdAt', {})(entity);
};

/**
 * Gets the value of the `__updatedAt` property of the `Customer` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string }} entity - `Customer` entity.
 * @returns {Date} The value of `__updatedAt` property of `Customer` entity.
 */
export const getCustomerUpdatedAt = (entity) => {
  return getter('Customer', '__updatedAt', {})(entity);
};

/**
 * Gets the value of the `__auditInfo` property of the `Customer` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string }} entity - `Customer` entity.
 * @returns {{ publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }} The value of `__auditInfo` property of `Customer` entity.
 */
export const getCustomerAuditInfo = (entity) => {
  return getter('Customer', '__auditInfo', {})(entity);
};

/**
 * Sets the value of the `firstName` property of the `Customer` entity.
 * @param {string} [value] - Value.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string }} entity - `Customer` entity.
 * @returns {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string }} The updated `Customer` entity.
 */
export const setCustomerFirstName = R.curry((value, entity) => {
  return setter('Customer', 'firstName', R.path(['Customer', 'properties', 'firstName'], config))(value, entity);
});

/**
 * Gets the value of the `firstName` property of the `Customer` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string }} entity - `Customer` entity.
 * @returns {string | null | undefined} The value of `firstName` property of `Customer` entity.
 */
export const getCustomerFirstName = (entity) => {
  return getter('Customer', 'firstName', R.path(['Customer', 'properties', 'firstName'], config))(entity);
};

/**
 * Sets the value of the `lastName` property of the `Customer` entity.
 * @param {string} [value] - Value.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string }} entity - `Customer` entity.
 * @returns {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string }} The updated `Customer` entity.
 */
export const setCustomerLastName = R.curry((value, entity) => {
  return setter('Customer', 'lastName', R.path(['Customer', 'properties', 'lastName'], config))(value, entity);
});

/**
 * Gets the value of the `lastName` property of the `Customer` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; firstName?: string; lastName?: string }} entity - `Customer` entity.
 * @returns {string | null | undefined} The value of `lastName` property of `Customer` entity.
 */
export const getCustomerLastName = (entity) => {
  return getter('Customer', 'lastName', R.path(['Customer', 'properties', 'lastName'], config))(entity);
};

```

entities/Project/index.mjs

```javascript
// Code generated by gen. DO NOT EDIT.

import { createEntity, deleteEntity, entities, getEntity, getter, listEntities, setter, updateEntity } from '@fasstech/oip-core/entities';
import * as R from 'ramda';
import config from '../entities.mjs';

// ////////////////////////////////////////////////////////////////////////////
// `Project` entity helpers
// ////////////////////////////////////////////////////////////////////////////

/**
 * Creates or retrieves entities of type `Project`.
 * @param {object | string} [filter] - Search criteria.
 * @param {{ create?: boolean | 'force'; limit?: number; list?: boolean; offset?: number; orderBy?: [string, 'asc' | 'desc']; total?: boolean; auditInfo?: 'none' | 'last_updated' | 'all'; transactionId?: string }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string } | { __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string }[] | { data: { __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string } } | { data: { __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string } }[] | null>} The requested entities.
 */
export const $Project = async (filter, opts) => {
  return entities('Project', R.prop('Project', config), null, { opts, filter });
};

/**
 * Lists entities of type `Project`.
 * @param {object} [filter] - Search criteria.
 * @param {{ create?: boolean; limit?: number; offset?: number; orderBy?: [string, 'asc' | 'desc']; total?: boolean; auditInfo?: 'none' | 'last_updated' | 'all'; transactionId?: string }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string }[] | { data: { __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string } }[]>} The requested entities.
 */
export const listProject = async (filter, opts) => {
  return listEntities('Project', R.prop('Project', config), null, { filter, opts });
};

/**
 * Gets an entity of type `Project`.
 * @param {object | string} [filter] - Search criteria.
 * @param {{ create?: boolean; auditInfo?: 'none' | 'last_updated' | 'all'; transactionId?: string }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string } | null>} The requested entity.
 */
export const getProject = async (filter, opts) => {
  return getEntity('Project', R.prop('Project', config), null, { filter, opts });
};

/**
 * Creates an entity of type `Project`.
 * @param {{ externalId?: string; name?: string }} [data] - Data of entity.
 * @param {{ transactionId?: string }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string } | null>} The created entity.
 */
export const createProject = async (data, opts) => {
  return createEntity('Project', R.prop('Project', config), null, data, { opts });
};

/**
 * Updates an entity of type `Project`.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string }} entity - Entity.
 * @param {{ transactionId?: string; withChildren?: boolean }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string }>} The updated entity.
 */
export const updateProject = async (entity, opts) => {
  return updateEntity('Project', R.prop('Project', config), entity, { opts });
};

/**
 * Deletes an entity of type `Project`.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string }} entity - Entity.
 * @param {{ transactionId?: string; withChildren?: boolean }} [opts] - Options.
 * @returns {Promise<{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string }>} The deleted entity.
 */
export const deleteProject = async (entity, opts) => {
  return deleteEntity('Project', R.prop('Project', config), entity, { opts });
};

// ////////////////////////////////////////////////////////////////////////////
// `Project` entity properties helpers
// ////////////////////////////////////////////////////////////////////////////

/**
 * Gets the value of the `__id` property of the `Project` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string }} entity - `Project` entity.
 * @returns {string} The value of `__id` property of `Project` entity.
 */
export const getProjectId = (entity) => {
  return getter('Project', '__id', {})(entity);
};

/**
 * Gets the value of the `__createdAt` property of the `Project` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string }} entity - `Project` entity.
 * @returns {Date} The value of `__createdAt` property of `Project` entity.
 */
export const getProjectCreatedAt = (entity) => {
  return getter('Project', '__createdAt', {})(entity);
};

/**
 * Gets the value of the `__updatedAt` property of the `Project` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string }} entity - `Project` entity.
 * @returns {Date} The value of `__updatedAt` property of `Project` entity.
 */
export const getProjectUpdatedAt = (entity) => {
  return getter('Project', '__updatedAt', {})(entity);
};

/**
 * Gets the value of the `__auditInfo` property of the `Project` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string }} entity - `Project` entity.
 * @returns {{ publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }} The value of `__auditInfo` property of `Project` entity.
 */
export const getProjectAuditInfo = (entity) => {
  return getter('Project', '__auditInfo', {})(entity);
};

/**
 * Sets the value of the `externalId` property of the `Project` entity.
 * @param {string} [value] - Value.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string }} entity - `Project` entity.
 * @returns {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string }} The updated `Project` entity.
 */
export const setProjectExternalId = R.curry((value, entity) => {
  return setter('Project', 'externalId', R.path(['Project', 'properties', 'externalId'], config))(value, entity);
});

/**
 * Gets the value of the `externalId` property of the `Project` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string }} entity - `Project` entity.
 * @returns {string | null | undefined} The value of `externalId` property of `Project` entity.
 */
export const getProjectExternalId = (entity) => {
  return getter('Project', 'externalId', R.path(['Project', 'properties', 'externalId'], config))(entity);
};

/**
 * Sets the value of the `name` property of the `Project` entity.
 * @param {string} [value] - Value.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string }} entity - `Project` entity.
 * @returns {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string }} The updated `Project` entity.
 */
export const setProjectName = R.curry((value, entity) => {
  return setter('Project', 'name', R.path(['Project', 'properties', 'name'], config))(value, entity);
});

/**
 * Gets the value of the `name` property of the `Project` entity.
 * @param {{ __id: string; __type: string; __createdAt: Date; __updatedAt: Date; __auditInfo: { publicId: string; createdAt: date; createdBy: string; updatedAt: date; updatedBy: string }; externalId?: string; name?: string }} entity - `Project` entity.
 * @returns {string | null | undefined} The value of `name` property of `Project` entity.
 */
export const getProjectName = (entity) => {
  return getter('Project', 'name', R.path(['Project', 'properties', 'name'], config))(entity);
};

```
:::

### Migrations

La commande a également généré 1 fichier dans le dossier `migrations` :

- `1776258982390_migration.js`.

:::BlockQuote
**Info**
Le nom du fichier est dépendant de sa date d'exécution. Il est en effet préfixé par un horodatage.
:::

Ce fichier contient le code SQL pour enregistrer les entités et leurs relations.

```javascript
// Code generated by gen. DO NOT EDIT.
// Detected entities: Company, Customer, Project
// Detected entity relations: Project->Company, Project->Customer
// Migration file generated at: 2026-04-15T13:16:22.389Z

export const up = (pgm) => {
  const { OAV_ID } = process.env;

  // insert entity Project
  pgm.sql(`
insert into entities_types (type, is_root, application_id)
values ('Project', true, (select id from applications where public_id = '${OAV_ID}'))
on conflict do nothing;
`);

  // insert entity Company
  pgm.sql(`
insert into entities_types (type, is_root, application_id)
values ('Company', false, (select id from applications where public_id = '${OAV_ID}'))
on conflict do nothing;
`);

  // insert entity Customer
  pgm.sql(`
insert into entities_types (type, is_root, application_id)
values ('Customer', false, (select id from applications where public_id = '${OAV_ID}'))
on conflict do nothing;
`);

  // insert relation Project->Company
  pgm.sql(`
insert into entities_relations_types (src_type_id, tgt_type_id)
values ((
select id from entities_types where type = 'Project' and application_id = (select id from applications where public_id = '${OAV_ID}')), (
select id from entities_types where type = 'Company' and application_id = (select id from applications where public_id = '${OAV_ID}')))
on conflict do nothing;
`);

  // insert relation Project->Customer
  pgm.sql(`
insert into entities_relations_types (src_type_id, tgt_type_id)
values ((
select id from entities_types where type = 'Project' and application_id = (select id from applications where public_id = '${OAV_ID}')), (
select id from entities_types where type = 'Customer' and application_id = (select id from applications where public_id = '${OAV_ID}')))
on conflict do nothing;
`);
};

export const down = false;
```

## Application des migrations

La commande pour appliquer les migrations SQL est :

```shell
npm run migrate:dev
```

La commande écrit sur la sortie standard du terminal :

- la liste des fichiers exécutés ;
- les éventuelles erreurs.

:::BlockQuote
**Info**
Il ne faut pas supprimer ou éditer un fichier de migration qui a été exécuté.
L’exécution des migrations futures échouera si tous les fichiers de migration ne sont pas présents.
:::

# Résumé

La génération du code des entités à partir d'un modèle de données se résume à l'exécution de deux commandes :

```shell
npm run entities:gen
npm run migrate:dev
```

Ces commandes sont à exécuter à chaque nouvelle version du fichier `config/entities/entities.openapi.yml`.
