Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Make ingestion idempotent with deterministic ids so reprocessing is safe
Use a deterministic document id and upsert so an at-least-once pipeline can safely reprocess the same input.
Bad example
| 1 | async function saveArticle(data: Article) { |
| 2 | // server generates a new id each time |
| 3 | await client.post(`/${index}/_doc`, data); |
| 4 | } |
Explanation (EN)
If the same input is seen twice (retry, failed move that leaves the file in place, redelivery), a POST that auto-generates ids creates a duplicate record each time.
Objašnjenje (HR)
Ako se isti ulaz vidi dvaput (retry, neuspjeli premjestaj koji ostavi datoteku, ponovna isporuka), POST koji sam generira id stvara duplikat svaki put.
Good example
| 1 | async function upsertArticle(data: Article, fileName: string): Promise<boolean> { |
| 2 | const docId = fileName.split('.')[0]; // deterministic, derived from the source |
| 3 | await client.put(`/${index}/_doc/${docId}`, data); // upsert: same id overwrites |
| 4 | return true; |
| 5 | } |
Explanation (EN)
Deriving a stable id from the source and upserting makes reprocessing a no-op rather than a duplicate. This pairs with leaving inputs in place on failure so they are safely retried next run.
Objašnjenje (HR)
Izvodenje stabilnog id-a iz izvora i upsert cine ponovnu obradu no-op-om umjesto duplikata. To se uparuje s ostavljanjem ulaza na mjestu pri gresci kako bi se sigurno ponovili sljedeci put.
Notes (EN)
Design at-least-once pipelines so steps are idempotent: deterministic ids + upsert for writes, and leave the source artifact unconsumed until downstream work succeeds so a failure triggers a clean retry.
Bilješke (HR)
Dizajniraj at-least-once pipeline tako da su koraci idempotentni: deterministicki id-evi + upsert za zapise, i ostavi izvor nepotrosenim dok nizvodni rad ne uspije kako bi greska pokrenula cisti retry.