Skip to main content

CRUD Operations

All data mutations in PomegranateDB happen inside a db.write() block. Reads can happen anywhere.

Create

await db.write(async () => {
const post = await db.get(Post).create({
title: 'New Post',
body: 'Content here...',
status: 'draft',
});
console.log(post.id); // auto-generated UUID
});

Fields with defaults can be omitted:

// If status has default('draft'), you can skip it:
await db.get(Post).create({ title: 'Hello', body: 'World' });

Read

Find by ID

const post = await db.get(Post).findById('some-uuid');
// Returns the model instance, or null if not found

Query

const posts = await db.get(Post)
.query()
.where('status', 'published')
.orderBy('created_at', 'desc')
.limit(20)
.fetch();

Count

const count = await db.get(Post)
.query()
.where('status', 'draft')
.count();
const results = await db.get(Post).search({
query: 'react native',
columns: ['title', 'body'],
limit: 50,
});

Update

await db.write(async () => {
await post.update({
title: 'Updated Title',
status: 'published',
});
});

Only the fields you include in the patch will change. If the model schema marks a field as readonly(), attempting to update it will throw an error.

Delete

await db.write(async () => {
await post.markAsDeleted();
});

The record's _status is set to 'deleted'. It will be physically removed after the next successful sync push.

Hard Delete

await db.write(async () => {
await post.destroyPermanently();
});

Permanently removes the record from the database. Use this for local-only data that doesn't need sync.

Batch Operations

For bulk writes, use batch operations for better performance:

await db.write(async () => {
await db._adapter.batch([
{ type: 'create', table: 'posts', rawRecord: { id: '1', title: 'A', ... } },
{ type: 'create', table: 'posts', rawRecord: { id: '2', title: 'B', ... } },
{ type: 'update', table: 'posts', rawRecord: { id: '3', title: 'Updated', ... } },
{ type: 'markAsDeleted', table: 'posts', id: '4' },
]);
});

Batch operations run in a single database transaction for atomicity and performance.