Skip to content

Typed records

Generate Dart classes for your collections from a schema snapshot with dart run vaultbase:types.

dart run vaultbase:types writes a Dart file with a class per collection, from the same schema snapshot the TypeScript vb-types reads. No Node needed.

Terminal window
# Capture the schema once and commit it
curl -H "Authorization: Bearer $ADMIN_TOKEN" \
https://api.example.com/api/v1/admin/migrations/snapshot \
> vaultbase-schema.json
# Anyone can regenerate from the committed file
dart run vaultbase:types --schema=vaultbase-schema.json
# writes lib/vaultbase_schema.g.dart; --out=<path> to change it
# Or straight from a dev server
dart run vaultbase:types --url=https://api.example.com --admin-token=$ADMIN_TOKEN

The file is formatted for your package, so a dart format check passes. Regenerate it when the schema changes.

import 'vaultbase_schema.g.dart';
final page = await vb.collections.posts.list(sort: '-created');
for (final post in page.items) {
post.title; // String
post.views; // num?
post.tags; // List<String>
post.publishAt; // DateTime?
post.place; // ({double lat, double lng})?
}
await vb.collections.posts.create(
PostsCreate(title: 'Hi', rating: 4.5, pinned: false).toJson());
final site = await vb.collections.siteSettings.get(); // a singleton

Per collection:

  • XRecord extends RecordModel, a getter per field. Required fields are non-null ('', 0 or false when absent, for example when fields= left them out); optional fields are nullable; lists are never null. The RecordModel helpers (getString, expandOne, …) still work.

  • XCreate for base and auth collections: the fields to create a record with, required ones required. toJson() leaves out what you didn’t set and encodes dates and geo points the way the server takes them. Auth records are made by signing up:

    const u = UsersCreate(email: '[email protected]', password: '...', name: 'Ann');
    await vb.auth.collection('users').register(u.email, u.password, data: u.toJson());
  • vb.collections.<name>: Collection<XRecord>, or Singleton<XRecord> for singletons.

Field names become camelCase (publish_at → publishAt). One that clashes with a Dart keyword or a RecordModel member gets Field added: class → classField, data → dataField. Password fields are never generated.

Field typeGetter type
text, email, url, editor, color, relation, select, fileString
numbernum
boolbool
date, autodateDateTime?
select / file with multipleList<String>
vectorList<double>
geoPoint({double lat, double lng})?
jsonObject?
groupList<XFieldItem>, or XField? with multiple: false

A group field gets a class of its own, named after the collection and the field: OrdersItemsItem for the items of orders.items, OrdersAddress for a single-object address. It has a final member per subfield (typed as above; required ones non-null), the item’s id (null on a new item), fromJson and toJson. Send items back with their ids to keep them:

final order = await vb.collections.orders.create(OrdersCreate(items: [
const OrdersItemsItem(sku: 'A-1', qty: 2),
]).toJson());
final items = order.items; // List<OrdersItemsItem>
items.first.id; // set by the server
await vb.collections.orders.update(order.id, {
'items': [for (final i in items) i.toJson(), const OrdersItemsItem(sku: 'B-2', qty: 1).toJson()],
});