<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Palwisha Baloch</title>
    <description>The latest articles on DEV Community by Palwisha Baloch (@balochpalwisha).</description>
    <link>https://dev.to/balochpalwisha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1110074%2Ffbe46916-aa5c-4dcc-95f5-ef18facc1554.jpeg</url>
      <title>DEV Community: Palwisha Baloch</title>
      <link>https://dev.to/balochpalwisha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/balochpalwisha"/>
    <language>en</language>
    <item>
      <title>Drift Database: A Step-by-Step Guide for Beginners</title>
      <dc:creator>Palwisha Baloch</dc:creator>
      <pubDate>Wed, 28 Jun 2023 18:28:23 +0000</pubDate>
      <link>https://dev.to/balochpalwisha/drift-database-a-step-by-step-guide-for-beginners-57gj</link>
      <guid>https://dev.to/balochpalwisha/drift-database-a-step-by-step-guide-for-beginners-57gj</guid>
      <description>&lt;p&gt;Before we dive into the step-by-step guide, let us get an understanding of What is Drift database and its concept?&lt;/p&gt;

&lt;p&gt;Drift database is a modern, reactive database for Flutter apps. It is built on top of SQLite, but it provides several features that make it easier to use and more powerful. For example, the Drift database supports auto-updating streams, which means that your app will always be up-to-date with the latest data. Drift database also supports type safety, which means that you can be sure that your queries will be executed correctly.&lt;/p&gt;

&lt;p&gt;The basic concept of the Drift database is that it is a reactive database. This means that it automatically updates your app when the data changes. This is in contrast to traditional databases, which require you to manually poll the database for changes.&lt;/p&gt;

&lt;p&gt;Now we know about the basic concept of the Drift database let's move forward with the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;embed Getting started&lt;/li&gt;
&lt;li&gt;Configuration of Drift database&lt;/li&gt;
&lt;li&gt;Perform CURD operation&lt;/li&gt;
&lt;li&gt;Advance queries(Joins)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;Inside your Flutter project, add the following dependencies in &lt;code&gt;pubspec.yaml&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Note: add the latest version of following packages&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dependencies:
  drift: ^2.9.0
  sqlite3_flutter_libs: ^0.5.0
  path_provider: ^2.0.0
  path: ^1.8.3

dev_dependencies:
  drift_dev: ^2.9.0
  build_runner: ^2.4.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;drift:&lt;/code&gt; This is the core package defining most APIs&lt;br&gt;
&lt;code&gt;sqlite3_flutter_libs:&lt;/code&gt; Ships the latest sqlite3 version with your Android or iOS app. This is not required when you're not using Flutter, but then you need to take care of including sqlite3 yourself.&lt;br&gt;
&lt;code&gt;path_provider and path:&lt;/code&gt; Used to find a suitable location to store the database. Maintained by the Flutter and Dart team&lt;br&gt;
&lt;code&gt;drift_dev:&lt;/code&gt; This development-only dependency generates query code based on your tables. It will not be included in your final app.&lt;br&gt;
&lt;code&gt;build_runner:&lt;/code&gt; Common tool for code generation, maintained by the Dart team&lt;br&gt;
Configuration of Drift database&lt;br&gt;
Inside your root project create a database file called &lt;code&gt;database.dart&lt;/code&gt; and create a class MyDatabase which extends _$database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyDatabase extends _$MyDatabase {
  MyDatabase() : super();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now create a function &lt;code&gt;_openConnection&lt;/code&gt; which will create a database file in the defined location.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LazyDatabase _openConnection() {
  return LazyDatabase(() async {
    final dbFolder = await getApplicationDocumentsDirectory();
    final file = File(p.join(dbFolder.path, 'db.sqlite'));

    return NativeDatabase.createInBackground(file);
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;LazyDatabase&lt;/code&gt; util lets us find the right location for the file async.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;db.sqlite&lt;/code&gt; is the name of the database file located in the application’s database folder. Add the schemaVersion in &lt;code&gt;MyDatabase&lt;/code&gt; class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@override
  int get schemaVersion =&amp;gt; 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To generate the database.g.dart which contains the _$MyDatabase superclass, run &lt;code&gt;dart run build_runner build&lt;/code&gt; on the command line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The final code will be:

import 'dart:io';

import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;

part 'database.g.dart';

@DriftDatabase()
class AppDb extends _$AppDb {
  AppDb() : super(_openConnection());

  @override
  int get schemaVersion =&amp;gt; 1;
}

LazyDatabase _openConnection() {
  return LazyDatabase(() async {
    final dbFolder = await getApplicationDocumentsDirectory();
    final file = File(p.join(dbFolder.path, 'db.sqlite'));

    return NativeDatabase.createInBackground(file);
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Perform CURD operation
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Create Table
&lt;/h2&gt;

&lt;p&gt;Let’s create an employee table by creating a file &lt;code&gt;employee_table.dart&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:drift/drift.dart';

class Employees extends Table {
  IntColumn get id =&amp;gt; integer().autoIncrement()();
  TextColumn get name =&amp;gt; text().nullable()();
  TeColumn get post =&amp;gt; text().nullable()();
  IntColumn get salary =&amp;gt; integer().nullable()();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After creating a table, add the Employee class to MyDatabase inside &lt;code&gt;DriftDabse()&lt;/code&gt; function and run dart run build_runner build on the terminal. If you want to continuously rebuild the generated code where you change your code, run dart run build_runner watch instead. When you run the build runner command it will create a&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@DriftDatabase(tables: [Employee])&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Insert
&lt;/h2&gt;

&lt;p&gt;Before inserting data into the table, create a helper file &lt;code&gt;employeeTableHelper&lt;/code&gt;.dart where we define our all queries. You can very easily insert any valid object into tables. As some values can be absent (like default values that we don’t have to set explicitly), we again use the companion version.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// returns the generated id
Future&amp;lt;int&amp;gt; addEmployee(EmployeeCompanion entry) {
  return into(employee).insert(entry);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All row classes generated will have a constructor that can be used to create objects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;addEmployee(
  EmployeeCompanion(
    name: Value('John Doe'),
    post: Value('Manager'),
    salary: Value('200000'),
  ),
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also insert multiple entries by using a &lt;code&gt;batch&lt;/code&gt;. To do that, you can use the &lt;code&gt;insertAll&lt;/code&gt; method inside a batch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Future&amp;lt;void&amp;gt; insertMultipleEntries() async{
  await batch((batch) {
    // functions in a batch don't have to be awaited - just
    // await the whole batch afterwards.
    batch.insertAll(employee, [listOfEmployees);
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Select
&lt;/h2&gt;

&lt;p&gt;You can create &lt;code&gt;select&lt;/code&gt; statements by starting them with &lt;code&gt;select(tableName)&lt;/code&gt;, where the table name is a field generated for you by drift. Each table used in a database will have a matching field to run queries against. Any query can be run once with &lt;code&gt;get()&lt;/code&gt; or be turned into an auto-updating stream using &lt;code&gt;watch()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Future&amp;lt;List&amp;lt;Employee&amp;gt;&amp;gt; get allEmployeeData =&amp;gt; select(employee).get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;watches of all employee entries in a given category. The stream will automatically emit new items whenever the underlying data changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Stream&amp;lt;List&amp;lt;Employee&amp;gt;&amp;gt; watchEntriesInCategory(Category c) {
    return (select(employee)..where((t) =&amp;gt; t.category.equals(c.id))).watch();
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also use &lt;code&gt;Where&lt;/code&gt;, &lt;code&gt;Limit&lt;/code&gt; and &lt;code&gt;Ordering&lt;/code&gt; in dart query as same as we use in SQL queries. Let's see how its works&lt;/p&gt;

&lt;h2&gt;
  
  
  Where
&lt;/h2&gt;

&lt;p&gt;You can apply filters to a query by calling &lt;code&gt;where()&lt;/code&gt;. The where method takes a function that should map the given table to an Expression of boolean. A common way to create such expression is by using equals on expressions. Integer columns can also be compared with &lt;code&gt;isBiggerThan&lt;/code&gt; and &lt;code&gt;isSmallerThan&lt;/code&gt;. You can compose expressions using &lt;code&gt;a &amp;amp; b, a | b and a.not()&lt;/code&gt;. For more details on expressions, see this guide.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
Future&amp;lt;Employee&amp;gt; get employeeData =&amp;gt; select(employee).where((t) =&amp;gt; t.name.equals("john Doe")).getSingle();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Limit
&lt;/h2&gt;

&lt;p&gt;You can limit the number of results returned by calling &lt;code&gt;limit&lt;/code&gt; on queries. The method accepts the number of rows to return and an optional offset.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Future&amp;lt;List&amp;lt;Employee&amp;gt;&amp;gt; limitEmployee(int limit, {int offset}) {
  return (select(employee)..limit(limit, offset: offset)).get();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Ordering
&lt;/h2&gt;

&lt;p&gt;You can use the orderBy method on the &lt;code&gt;select&lt;/code&gt; statement. It expects a list of functions that extract the individual ordering terms from the table. You can use any expression as an ordering term - for more details, see &lt;a href="https://drift.simonbinder.eu/docs/getting-started/expressions/"&gt;this guide&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Future&amp;lt;List&amp;lt;Employee&amp;gt;&amp;gt; sortEntriesAlphabetically() {
  return (select(employee)..orderBy([(t) =&amp;gt; OrderingTerm(expression: t.name)])).get();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also reverse the order by setting the mode property of the &lt;code&gt;OrderingTerm&lt;/code&gt; to &lt;code&gt;OrderingMode.desc&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Update
&lt;/h2&gt;

&lt;p&gt;You can use the generated classes to update individual fields of any row. For updates, we use the “companion” version of a generated class. This wraps the fields in a “Value” type which can be set to be absent using &lt;code&gt;“Value.absent()”&lt;/code&gt;. This allows us to separate between &lt;code&gt;“SET category = NULL” (&lt;/code&gt;category: Value(null)&lt;code&gt;)&lt;/code&gt; and not update the category at all: &lt;code&gt;category: Value.absent()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Future updateEmployeePost() {
return (update(employee)
      ..where((t) =&amp;gt; t.name.equals("John Doe")
    ).write(EmployeeCompanion(
      post: Value("Assistant Manager"),
    ),
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;replace&lt;/code&gt; will update all fields from the entry that are not marked as a primary key. It will also make sure that only the entry with the same primary key will be updated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Future updateEmployee(Employee employeeData) {
  return update(employee).replace(employeeData);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Delete
&lt;/h2&gt;

&lt;p&gt;you can delete a single row by using &lt;code&gt;Where()&lt;/code&gt; in the query as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Future deleteEmployeeRow() {
  return (delete(employee)..where((t) =&amp;gt; t.id.equals(10))).go();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you don’t explicitly add a &lt;code&gt;where&lt;/code&gt; the clause on deletes, the statement will affect all rows in the table&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Future deleteEmployee() {
  return delete(employee).go();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above query will delete all the data in the employee table.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advance queries(Joins)
&lt;/h2&gt;

&lt;p&gt;Drift supports SQL joins to write queries that operate on more than one table. To use that feature, start a select regular select statement with &lt;code&gt;select(table)&lt;/code&gt; and then add a list of joins using .join(). For inner and left outer joins, an &lt;code&gt;ON&lt;/code&gt; expression needs to be specified. Here's an example, we use an employee table and create another table &lt;code&gt;employee-info&lt;/code&gt; for join.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:drift/drift.dart';

class EmployeesInfo extends Table {
  IntColumn get id =&amp;gt; integer().autoIncrement()();
  TextColumn get employeeId =&amp;gt; text().nullable()();
  IntColumn get contact =&amp;gt; text().nullable()();
  TextColumn get adress =&amp;gt; integer().nullable()();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let us suppose we have added employee information in &lt;code&gt;employee-info&lt;/code&gt; table by following the above steps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Future&amp;lt;List&amp;lt;Employee&amp;gt;&amp;gt; employeeWithInfo() async {

  final query = select(employee).join([
    // In joins, `useColumns: false` tells drift to not add columns of the
    // joined table to the result set. This is useful here, since we only join
    // the tables so that we can refer to them in the where clause.
    leftJoin(employee-info, employee-info.employeeId.equals(employee.id),
        useColumns: false)
  ])
    ..where(employee.salary.isSmallerOrEqual(80000));

  return query.map((row) =&amp;gt; row.readTable(otherTodos)).get();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Calling &lt;code&gt;get()&lt;/code&gt;on a &lt;code&gt;select&lt;/code&gt; statement with &lt;code&gt;join&lt;/code&gt; returns a Future of &lt;code&gt;List&amp;lt;TypedResult&amp;gt;&lt;/code&gt;, respectively. Each &lt;code&gt;TypedResult&lt;/code&gt; represents a row from which data can be read. It contains a &lt;code&gt;rawData&lt;/code&gt; getter to obtain the raw columns. But more importantly, the readTable method can be used to read a data class from a table.&lt;/p&gt;

&lt;p&gt;In the example query above, we can read the employee entry and the &lt;code&gt;employee-info&lt;/code&gt; from each row like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return query.map((rows) {
  return rows.map((row) {
    return EmployeeWithInfo(
      row.readTable(employee),
      row.readTableOrNull(employee-info),
    );
  }).toList();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: &lt;code&gt;readTable&lt;/code&gt; will throw an ArgumentError when a table is not present in the row. For instance, &lt;code&gt;employeeId&lt;/code&gt; might not be in any &lt;code&gt;empoyee-info&lt;/code&gt;. To account for that, we use &lt;code&gt;row.readTableOrNull&lt;/code&gt; to load employee info.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By using the drift database we can write type-safe queries in Dart or SQL, and auto-updating streams that can easily manage transactions and so much more. In this article, we have covered the basic and advanced queries in dart. For further detail and so many advanced features see the drift complete guide &lt;a href="https://drift.simonbinder.eu/docs/getting-started/"&gt;https://drift.simonbinder.eu/docs/getting-started/&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
