Leitura, escrita e operações em lote
Use os métodos de registro único para ações de UI e os métodos em lote para setup, imports, lotes de sincronização e migrações.
Escritas de registro único
type Task = {
id: string;
title: string;
categoryId: string;
completed: boolean;
priority: 'low' | 'medium' | 'high';
createdAt: number;
updatedAt: number;
};
const tasks = client.table<Task>('tasks');
await tasks.add({
id: 'task-1',
title: 'Draft the tutorial',
categoryId: 'work',
completed: false,
priority: 'high',
createdAt: 1,
updatedAt: 1
});
await tasks.update('task-1', {
completed: true,
updatedAt: 2
});
const afterUpdate = await tasks.get('task-1');
await tasks.delete('task-1');
const afterDelete = await tasks.get('task-1');
console.log({ afterUpdate, afterDelete });Escolha do método
| Método | Chave existente | Chave ausente | Melhor uso |
|---|---|---|---|
add(record) | Falha com ConstraintViolation. | Insere. | Ações de criação pura. |
put(record) | Substitui o registro inteiro. | Insere. | Upsert vindo de sync/import. |
update(key, changes) | Mescla campos. | Falha com NotFound. | Edições de UI que não devem ressuscitar linhas apagadas. |
delete(key) | Apaga. | Nenhum registro fica armazenado. | Ações de remoção. |
clear() | Remove todos os registros da store. | A store fica vazia. | Fluxos de reset/import. |
Escritas em lote
type Category = {
id: string;
name: string;
color: string;
createdAt: number;
updatedAt: number;
};
const now = Date.now();
const categories = client.table<Category>('categories');
const tasks = client.table<Task>('tasks');
await categories.bulkAdd([
{ id: 'work', name: 'Work', color: '#2563eb', createdAt: now, updatedAt: now },
{ id: 'home', name: 'Home', color: '#16a34a', createdAt: now, updatedAt: now }
]);
const insertedKeys = await tasks.bulkAdd([
{
id: 'task-1',
title: 'Write the guide',
categoryId: 'work',
completed: false,
priority: 'high',
createdAt: now,
updatedAt: now
},
{
id: 'task-2',
title: 'Review examples',
categoryId: 'home',
completed: false,
priority: 'medium',
createdAt: now,
updatedAt: now + 1
}
]);
console.log({ insertedKeys });bulkAdd() continua sendo uma única transação IndexedDB. Se um registro viola
uma constraint, o erro nomeia a store e a operação para o caller reportar a
falha do lote sem adivinhar.
Execute aqui
CRUD
Crie, leia, atualize e remova uma Task.
### CRUD
```ts
const client = cana.createClient({
name: dbName,
schema: {
version: 1,
stores: [
{ name: 'categories', keyPath: 'id', indexes: [{ name: 'byName', keyPath: 'name', unique: true }] },
{
name: 'tasks',
keyPath: 'id',
indexes: [
{ name: 'byCategory', keyPath: 'categoryId' },
{ name: 'byCompleted', keyPath: 'completed' },
{ name: 'byUpdatedAt', keyPath: 'updatedAt' }
]
}
]
}
});
await client.open();
await client.table('categories').add({ id: 'work', name: 'Work', color: '#2563eb' });
const tasks = client.table('tasks');
await tasks.add({
id: 'task-1',
title: 'Draft the tutorial',
categoryId: 'work',
completed: false,
priority: 'high',
createdAt: 1,
updatedAt: 1
});
await tasks.update('task-1', { completed: true, updatedAt: 2 });
const afterUpdate = await tasks.get('task-1');
await tasks.delete('task-1');
return { afterUpdate, afterDelete: await tasks.get('task-1') };
```const client = cana.createClient({
name: dbName,
schema: {
version: 1,
stores: [
{ name: 'categories', keyPath: 'id', indexes: [{ name: 'byName', keyPath: 'name', unique: true }] },
{
name: 'tasks',
keyPath: 'id',
indexes: [
{ name: 'byCategory', keyPath: 'categoryId' },
{ name: 'byCompleted', keyPath: 'completed' },
{ name: 'byUpdatedAt', keyPath: 'updatedAt' }
]
}
]
}
});
await client.open();
await client.table('categories').add({ id: 'work', name: 'Work', color: '#2563eb' });
const tasks = client.table('tasks');
await tasks.add({
id: 'task-1',
title: 'Draft the tutorial',
categoryId: 'work',
completed: false,
priority: 'high',
createdAt: 1,
updatedAt: 1
});
await tasks.update('task-1', { completed: true, updatedAt: 2 });
const afterUpdate = await tasks.get('task-1');
await tasks.delete('task-1');
return { afterUpdate, afterDelete: await tasks.get('task-1') };Operacoes em lote
Popule Category e Task com operacoes em lote.
### Operacoes em lote
```ts
const client = cana.createClient({
name: dbName,
schema: {
version: 1,
stores: [
{ name: 'categories', keyPath: 'id', indexes: [{ name: 'byName', keyPath: 'name', unique: true }] },
{
name: 'tasks',
keyPath: 'id',
indexes: [
{ name: 'byCategory', keyPath: 'categoryId' },
{ name: 'byCompleted', keyPath: 'completed' },
{ name: 'byUpdatedAt', keyPath: 'updatedAt' }
]
}
]
}
});
await client.open();
const now = Date.now();
await client.table('categories').bulkAdd([
{ id: 'work', name: 'Work', color: '#2563eb', createdAt: now, updatedAt: now },
{ id: 'home', name: 'Home', color: '#16a34a', createdAt: now, updatedAt: now }
]);
await client.table('tasks').bulkAdd([
{
id: 'task-1',
title: 'Write the Cana tutorial',
categoryId: 'work',
completed: false,
priority: 'high',
createdAt: now,
updatedAt: now
},
{
id: 'task-2',
title: 'Review category filters',
categoryId: 'home',
completed: true,
priority: 'medium',
createdAt: now,
updatedAt: now + 1
}
]);
const put = await client.table('tasks').bulkPut([
{
id: 'task-2',
title: 'Review category filters',
categoryId: 'home',
completed: false,
priority: 'high',
createdAt: Date.now(),
updatedAt: Date.now()
},
{
id: 'task-3',
title: 'Publish the example app',
categoryId: 'work',
completed: false,
priority: 'medium',
createdAt: Date.now(),
updatedAt: Date.now()
}
]);
return {
put,
categories: await client.table('categories').query({ index: 'byName' }),
tasks: await client.table('tasks').query({ index: 'byUpdatedAt' })
};
```const client = cana.createClient({
name: dbName,
schema: {
version: 1,
stores: [
{ name: 'categories', keyPath: 'id', indexes: [{ name: 'byName', keyPath: 'name', unique: true }] },
{
name: 'tasks',
keyPath: 'id',
indexes: [
{ name: 'byCategory', keyPath: 'categoryId' },
{ name: 'byCompleted', keyPath: 'completed' },
{ name: 'byUpdatedAt', keyPath: 'updatedAt' }
]
}
]
}
});
await client.open();
const now = Date.now();
await client.table('categories').bulkAdd([
{ id: 'work', name: 'Work', color: '#2563eb', createdAt: now, updatedAt: now },
{ id: 'home', name: 'Home', color: '#16a34a', createdAt: now, updatedAt: now }
]);
await client.table('tasks').bulkAdd([
{
id: 'task-1',
title: 'Write the Cana tutorial',
categoryId: 'work',
completed: false,
priority: 'high',
createdAt: now,
updatedAt: now
},
{
id: 'task-2',
title: 'Review category filters',
categoryId: 'home',
completed: true,
priority: 'medium',
createdAt: now,
updatedAt: now + 1
}
]);
const put = await client.table('tasks').bulkPut([
{
id: 'task-2',
title: 'Review category filters',
categoryId: 'home',
completed: false,
priority: 'high',
createdAt: Date.now(),
updatedAt: Date.now()
},
{
id: 'task-3',
title: 'Publish the example app',
categoryId: 'work',
completed: false,
priority: 'medium',
createdAt: Date.now(),
updatedAt: Date.now()
}
]);
return {
put,
categories: await client.table('categories').query({ index: 'byName' }),
tasks: await client.table('tasks').query({ index: 'byUpdatedAt' })
};Próximo
Continue em queries e planos.