@amsom-habitat/nest-rest
v2.0.0
Published
Lecture REST façon API Platform : pagination, filtres, tri, 404, CRUD sur table Oracle.
Readme
@amsom-habitat/nest-rest
Lecture REST façon API Platform au-dessus d'Oracle : pagination, tri par identifiant, filtres
ApiFilter, 404, CRUD générique sur table et lecture du corps des actions. Sans module ni configuration.
const VIEW = view('AHVWS_CALLY_X', { schema: { booleans: ['ACTIF'] } });
const FILTERS: FilterDefinition = {
idReclamant: 'ID_RECLAMANT', // exact
nom: { column: 'NOM', operator: 'ipartial' },
actif: { column: 'ACTIF', operator: 'boolean' },
idModule: { column: 'ID_MODULE', operator: 'in' }, // a,b ou ["a","b"]
dateFin: { column: 'DATE_FIN', operator: 'exists' }, // ?exists[dateFin]=false
annee: { column: 'ANNEE', operator: 'range' }, // ?annee[gte]=2020
};
@Get() findAll(@Query() params: Record<string, unknown>, @Pagination() pagination: Pagination) { … }
const query = this.db(VIEW.name);
applyFilters(query, params, FILTERS);
return listPaginated(VIEW, query, pagination); // ou { sortColumn: 'ID_MODULE' }
return orNotFound(await this.service.findOne(id), `Ressource ${id}`);@Post('cloturer_sollicitation')
cloturer(@Pec() pec: PecAgent | null, @Body() corps: RequestBody) {
return this.service.cloturer(exigerPec(pec), {
idSollicitation: requiredField(corps, 'idSollicitation'),
motifCloture: optionalField(corps, 'motifCloture', 'TRA'),
});
}Comportements à connaître
- Pagination :
limit/offsetseulement siitemsPerPageest fourni (leROWNUMralentit les vues lourdes). Une valeur invalide (0,-1,abc) est ignorée comme en v1 (pagination_enabled: false). - Paramètre répété (
?x=a&x=b) : la dernière valeur gagne, comme PHP. - Filtres : sémantique d'API Platform 4 (
SearchFilter,BooleanFilter,ExistsFilter,RangeFilter) et des filtres maison v1 (InFilter,ArrayIncludesFilter).?x=(vide) filtre sur'', donc 0 ligne sous Oracle ; avecinteger: true, une valeur non entière est ignorée. Les colonnes interpolées dans du SQL brut doivent être des identifiants Oracle (sinon erreur). orNotFound: 404 surnull,undefinedet0(aucune ligne touchée par un DELETE).CrudService: relit chaque écriture viafindOne; PATCH partiel, sansUPDATEsi aucun champ connu ; POST sanssequence→ 500 explicite ; booléen non interprétable ou timestamp invalide → 400.integerId: truecaste l'identifiant comme API Platform (abc→ 0 → 404).- Corps d'une action : reçu brut,
nullcompris (RequestBody, que leValidationPipene touche pas), pour pouvoir servir tel quel à un mail ou un document. La conversion vers Oracle se fait au bind (nest-oracle).requiredField: clé absente →MissingFieldError(400),nullaccepté.optionalField: absent ounull→ défaut.isEmptysuitempty()de PHP, le contrat des fronts :0,'0','',[],{}sont vides. Les clés héritées du prototype sont ignorées.
Migration 1.x → 2.0
| 1.x | 2.0 |
| --- | --- |
| trierEtPaginer(q, p, 'COL') | sortAndPaginate(q, p, { sortColumn: 'COL' }) |
| VUE.lignes(await trierEtPaginer(q, p)) | listPaginated(VIEW, q, p) |
| ouIntrouvable(v, l) | orNotFound(v, l) |
| parseFiltreBooleen | parseBooleanFilter (+ applyBoolean) |
| appliquerRecherche(q, c, v, 'ipartial') | applySearch(q, c, v, { strategy: 'ipartial' }) |
| appliquerIn / appliquerNotIn / appliquerExists | applyIn / applyNotIn / applyExists |
| appliquerArrayIncludes / appliquerRange / extraireBornes / Bornes | applyArrayIncludes / applyRange / extractBounds / RangeBounds |
| StrategieRecherche | SearchStrategy |
| appliquerSimpleSearch | supprimé |
| ServiceCrud | CrudService |
| CrudTable { vue, champs, dates: ['COL'] } | CrudTableDefinition { table, fields, dates: { COL: ORACLE_DATE_FORMAT } } |
