Insert & Query

Add records to collections and query them with filters, ordering, and pagination.

Insert a record

Pass any JSON-serialisable map. The returned record includes the auto-generated id, created_at, and created_by.

lib/database.dartDart
final record = await Koolbase.db.collection('posts').insert({
  'title': 'My first post',
  'body': 'Hello from Koolbase',
  'published': true,
  'tags': ['flutter', 'mobile'],
});

print(record.id);         // UUID
print(record.createdAt);  // DateTime
print(record.data);       // the JSON you inserted

Get all records

Fetch all records in a collection. Returns up to 20 records by default.

lib/database.dartDart
final result = await Koolbase.db.collection('posts').get();

for (final record in result.records) {
  print(record.data['title']);
}
print(result.total); // total count in collection

Query with filters

Use .where() to filter records by field values. Multiple filters are combined with AND.

lib/database.dartDart
// Filter by a single field
final published = await Koolbase.db
  .collection('posts')
  .where({'published': true})
  .get();

// Filter by multiple fields
final filtered = await Koolbase.db
  .collection('posts')
  .where({
    'published': true,
    'category': 'flutter',
  })
  .get();

Limit and offset

lib/database.dartDart
// Get first 10 records
final page1 = await Koolbase.db
  .collection('posts')
  .limit(10)
  .get();

// Get next 10 records
final page2 = await Koolbase.db
  .collection('posts')
  .limit(10)
  .offset(10)
  .get();

Order results

lib/database.dartDart
// Order by created_at descending (newest first)
final recent = await Koolbase.db
  .collection('posts')
  .orderBy('created_at', descending: true)
  .limit(20)
  .get();

Get a single record

lib/database.dartDart
final record = await Koolbase.db
  .collection('posts')
  .doc('record-uuid-here')
  .get();

print(record.data['title']);

Access rules apply

All query and fetch operations are subject to the collection's read rule. If the rule is authenticated, the user must be logged in. If owner, only records created by the current user are returned.