Update known identities with expected revisions
Keep create and update behavior explicit. A host validates every identity and expected revision before replacing the snapshot once.
Keyboard
- Paste or choose a CSV and map ID, revision and editable columns.
- Use preview pages and linked errors to review the complete supplied batch.
- Apply only after reviewing. Re-importing an old revision is rejected without changing another row.
States covered
- update mode
- canonical identities
- expected revisions
- atomic validation
- host-owned records
- retry receipt
Install this complete example and its component dependencies.
npx shadcn@latest add https://ui.bld8.dev/r/example-import-update-existing-records.jsonComplete source
/*!
MIT License
Copyright (c) 2026 Jaison
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import {useRef,useState} from 'react';
import {ImportWizard,UIProvider,type ImportField,type ImportRequest,type ImportApplyResult} from '@bld8/ui';
const fields:ImportField[]=[{key:'id',label:'ID',type:'text',required:true},{key:'revision',label:'Revision',type:'number',required:true,min:1,validate:value=>Number.isSafeInteger(value)?undefined:'Use a whole revision.'},{key:'quantity',label:'Quantity',type:'number',required:true,min:0,max:1000,validate:value=>Number.isInteger(value)?undefined:'Use a whole quantity.'}];
export default function UpdateImportExample(){
const records=useRef([{id:'SKU-101',revision:1,quantity:12},{id:'SKU-102',revision:1,quantity:8}]),receipts=useRef(new Map<string,{signature:string;count:number}>());const [summary,setSummary]=useState(records.current);
async function apply(request:ImportRequest,{signal}:{signal:AbortSignal}):Promise<ImportApplyResult>{if(signal.aborted)throw new DOMException('Aborted','AbortError');const signature=JSON.stringify(request.rows),receipt=receipts.current.get(request.requestId);if(receipt)return receipt.signature===signature?{ok:true,requestId:request.requestId,count:receipt.count}:{ok:false,message:'The request ID was already used for other values.'};for(const row of request.rows){const current=records.current.find(record=>record.id===row.values.id);if(!current||current.revision!==row.values.revision)return {ok:false,message:'The source has changed. Reload its revisions before importing again.',issues:[{sourceRow:row.sourceRow,field:'revision',message:current?'Expected revision '+current.revision+'.':'This ID does not exist.'}]};}const next=records.current.map(record=>{const row=request.rows.find(row=>row.values.id===record.id);return row?{...record,revision:record.revision+1,quantity:Number(row.values.quantity)}:record;});records.current=next;receipts.current.set(request.requestId,{signature,count:request.rows.length});setSummary(next);return {ok:true,requestId:request.requestId,count:request.rows.length};}
return <UIProvider><div style={{display:'grid',gap:24}}><ImportWizard label="Update stock counts" mode="update" identityField="id" fields={fields} onApply={apply}/><output aria-live="polite">{summary.map(row=>row.id+': '+row.quantity+' (revision '+row.revision+')').join(' · ')}</output><p style={{fontSize:12}}>The illustrative memory adapter validates the entire batch before replacing its snapshot. Production authorization, storage, idempotency and revision checks belong to your service.</p></div></UIProvider>;
}