CodeBlanks Logo Icon CodeBlanks

ORM

Learn Dart database with the drift ORM.

drift ORM

drift is a reactive ORM for Dart. It is built on top of sqflite. You define tables as Dart classes; drift offers type-safe queries and code generation. Instead of raw SQL you use a Dart API.

A table is defined as class Notes extends Table { TextColumn get title => text()(); }. Table is the base class; TextColumn is a text column, text()() is the column-producing helper. In this lesson we learn drift table definitions.

Table definition

In drift a table is a Dart class extending Table. Each column is a getter: TextColumn get title => text()(); is a text column, IntColumn get id => integer().autoIncrement()(); is an auto-incrementing integer column.

From this definition drift generates type-safe query methods via code generation. You query with calls like db.select(notes).get(); no SQL needed. In this lesson we define the Notes table.

Practice Define the Notes table

Add import 'package:drift/drift.dart';. Define class Notes extends Table { ... } with TextColumn get title => text()(); inside. In main add print('drift: Notes table') and print('Column: title (text)').

Starter code

main.dart
// import the drift package
// define the Notes table (Table class, title text column)

void main() {
  // print the drift table info
}

Sample solution

main.dart
import 'package:drift/drift.dart';

class Notes extends Table {
  TextColumn get title => text()();
}

void main() {
  print('drift: Notes table');
  print('Column: title (text)');
}

Expected output

drift: Notes table
Column: title (text)

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 The drift import

Question
import 'package:____/drift/drift.dart';
Answer
import 'package:drift/drift/drift.dart';

Explanation: the drift package is imported via package:drift/drift.dart.

2 Match the drift parts

Term Meaning
drift Reactive ORM
Table Table class
TextColumn Text column
text()() Column builder

3 What does drift provide?

  • Web server
  • HTTP client
  • Type-safe ORM Correct answer
  • File compression

Explanation: drift provides a type-safe ORM.

All lessons in Advanced