mirror of https://github.com/AMT-Cheif/drift.git
Documentation for the json1 and fts5 extensions
This commit is contained in:
parent
0e2ad81ac1
commit
6b776d47d7
|
@ -68,7 +68,7 @@ Here are some of the many ways moor helps you write awesome database code:
|
|||
Moor will generate efficient SQL for Dart queries.</li>
|
||||
<li><b>Easy to learn</b>: Instead of having to learn yet another ORM, moor lets you write queries in SQL and generates typesafe wrappers. Queries and tables
|
||||
can also be written in Dart that looks similar to SQL without loosing type-safety.</li>
|
||||
<li><b>Fast _and_ powerful</b>: With the new `moor_ffi` backend, moor can outperform key-value stores without putting any compromises and the integrity
|
||||
<li><b>Fast _and_ powerful</b>: With the new `moor_ffi` backend, moor can outperform key-value stores without putting any compromises on the integrity
|
||||
and flexibility that relational databases provide. Moor is the only major persistence library with builtin support for multiple isolates.</li>
|
||||
<li><b>Well tested and production ready</b>: Each component of moor is verified by a wide range of unit and integration tests. Moor powers many Flutter apps
|
||||
in production.</li>
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
---
|
||||
title: "Supported sqlite extensions"
|
||||
weight: 10
|
||||
description: Information on json1 and fts5 support in the generator
|
||||
---
|
||||
|
||||
_Note_: Since `moor_flutter` uses the sqlite version shipped on the device, the extensions might not
|
||||
be available on all devices. When using these extensions, using `moor_ffi` is strongly recommended.
|
||||
This enables the extensions on all Android devices and on iOS 11 and later.
|
||||
|
||||
## json1
|
||||
|
||||
To enable the json1 extension in moor files and compiled queries, modify the
|
||||
[build options]({{<relref "../Advanced Features/builder_options.md">}}) to include
|
||||
`json1` in the `sqlite_module` section.
|
||||
|
||||
The sqlite extension doesn't require any special tables and works on all text columns. In moor
|
||||
files and compiled queries, all `json` functions are available after enabling the extension.
|
||||
|
||||
Since the json extension is optional, enabling it in Dart requires a special import,
|
||||
`package:moor/extensions/json1.dart`. An example that uses json functions in Dart is shown below:
|
||||
```dart
|
||||
import 'package:moor/moor.dart';
|
||||
import 'package:moor/extensions/json1.dart';
|
||||
|
||||
class Contacts extends Table {
|
||||
IntColumn get id => integer().autoIncrement()();
|
||||
TextColumn get data => text()();
|
||||
}
|
||||
|
||||
@UseMoor(tables: [Contacts])
|
||||
class Database extends _$Database {
|
||||
// constructor and schemaVersion omitted for brevity
|
||||
|
||||
Future<List<Contacts>> findContactsWithNumber(String number) {
|
||||
return (select(contacts)
|
||||
..where((row) {
|
||||
// assume the phone number is stored in a json key in the `data` column
|
||||
final phoneNumber = row.data.jsonExtract<String, StringType>('phone_number');
|
||||
return phoneNumber.equals(number);
|
||||
})
|
||||
).get();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
You can learn more about the json1 extension on [sqlite.org](https://www.sqlite.org/json1.html).
|
||||
|
||||
## fts5
|
||||
|
||||
The fts5 extensions provides full-text search capabilities in sqlite tables.
|
||||
To enable the fts5 extension in moor files and compiled queries, modify the
|
||||
[build options]({{<relref "../Advanced Features/builder_options.md">}}) to include
|
||||
`fts5` in the `sqlite_module` section.
|
||||
|
||||
Just like you'd expect when using sqlite, you can create a fts5 table in a moor file
|
||||
by using a `CREATE VIRTUAL TABLE` statement.
|
||||
```sql
|
||||
CREATE VIRTUAL TABLE email USING fts5(sender, title, body);
|
||||
```
|
||||
|
||||
Queries on fts5 tables work like expected:
|
||||
```sql
|
||||
emailsWithFts5: SELECT * FROM email WHERE email MATCH 'fts5' ORDER BY rank;
|
||||
```
|
||||
|
||||
The `bm25`, `highlight` and `snippet` functions from fts5 can also be used in custom queries.
|
||||
|
||||
It's not possible to declare fts5 tables, or queries on fts5 tables, in Dart.
|
||||
You can learn more about the fts5 extension on [sqlite.org](https://www.sqlite.org/fts5.html).
|
|
@ -44,14 +44,17 @@ extension JsonExtensions on Expression<String, StringType> {
|
|||
/// Evaluating this expression will cause an error if [path] has an invalid
|
||||
/// format or `this` isn't well-formatted json.
|
||||
///
|
||||
/// Note that, since the return type of this function is dynamic, it can't be
|
||||
/// used in [JoinedSelectStatement.addColumns]. It's also recommended to use
|
||||
/// [Expression.equalsExp] with an explicit [Variable] instead of
|
||||
/// [Expression.equals].
|
||||
Expression jsonExtract(String path) {
|
||||
/// Note that the [T] and [S] type parameters have to be set if this function
|
||||
/// is used in [JoinedSelectStatement.addColumns] or compared via
|
||||
/// [Expression.equals]. The [T] parameter denotes the mapped Dart type for
|
||||
/// this expression, such as [String]. THe [S] parameter denotes the mapper
|
||||
/// from moor that's responsible for mapping Dart objects to sqlite and vice
|
||||
/// versa. If [T] was set to [String], the matching value for [S] would be
|
||||
/// [StringType].
|
||||
Expression<T, S> jsonExtract<T, S extends SqlType<T>>(String path) {
|
||||
return FunctionCallExpression('json_extract', [
|
||||
this,
|
||||
Variable.withString(path),
|
||||
]);
|
||||
]).dartCast<T, S>();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue