Comments (6)
You can already get pretty far with the JsonbConverter
I've suggested here if you don't need web support.
I've tried approaching this a few times, but I always backed out due to the complexity - this feature requires us to rewrite queries based on the result type, so if you do selectOnly(table)..addColumns([table.jsonbColumn])
, we'd have to recognize that and actually run SELECT json(jsonbColumn) FROM table
. And for default tables, the SELECT *
also wouldn't work anymore - we'd have to list the columns explicitly. It might not sound that hard, but there is nothing in the runtime currently ready for that, and so it requires a lot of refactoring to get right.
It's not closed, and I'll probably give this another go once I have a bit more time, but it's not on my near-term list.
from drift.
I agree that we should have JSONB
support. Using a type for this is kind of tricky because it requires things type converters are not yet able to do - altering the generated query.
For instance, if a user writes INSERT INTO tbl (dependencies) VALUES (?)
in a drift file, we need to turn that query into INSERT INTO tbl (dependencies) VALUES (jsonb(?))
. That's not super hard to do with the things we already have in place in the generator, but we also need to do it at runtime: into(tbl).insert(TblCompanion.insert(dependencies: [1, 2, 3]))
also needs to construct a jsonb
invocation somewhere.
Similarly, when selecting from a JSONB column, we'd have to wrap the expression in json()
to get a readable format.
(of course, we could cheat and open an in-memory database that just does json(?)
and jsonb(?)
synchronously in the converter. but that doesn't work with sqflite and requires even more setup on the web)
from drift.
Agree, this does not look like a trivial problem. It likely requires architectural changes.
As such, it might make sense to start a discussion and collect all the known and expected problems around types to see a broader picture.
From your comment I already see two (generalized):
- support different types at SQL query parameter level (
TEXT
) and database storage level (BLOB
) - inject necessary functions to convert between those during read (
json()
) and write (jsonb()
) - ...
from drift.
For those using NativeDatabase
(meaning that the sqlite3 library is available via dart:ffi
), a converter like this can support JSONB today:
import 'dart:convert';
import 'package:drift/drift.dart';
import 'package:sqlite3/sqlite3.dart' as sqlite3;
class JsonbConverter<T> implements TypeConverter<T, Uint8List> {
final T Function(Object?) _fromJson;
JsonbConverter(this._fromJson);
final sqlite3.Database _database = sqlite3.sqlite3.openInMemory();
late final sqlite3.PreparedStatement _toText =
_database.prepare('select json(?)');
late final sqlite3.PreparedStatement _toBinary =
_database.prepare('select jsonb(?)');
@override
T fromSql(Uint8List fromDb) {
final result = _toText.select([fromDb]).single;
final asText = result.values[0] as String;
return _fromJson(json.decode(asText));
}
@override
Uint8List toSql(T value) {
final asText = json.encode(value);
final result = _toBinary.select([asText]).single;
return result.values[0] as Uint8List;
}
}
It can be used in a column definition like this:
BlobColumn myJson => blob().map(JsonbConverter((json) => MyClass.fromJson(json)))();
from drift.
Hi @simolus3, is this feature in the roadmap or is hanged?
from drift.
Thank you so much for the fast update! 😀
from drift.
Related Issues (20)
- Code generation creates database table with wrong type HOT 9
- Drift generator doesn't strip custom row class from SQL HOT 1
- Primary Key not required in companion insert HOT 2
- Issue with generated code: database.g.dart file HOT 29
- Select int64 return type HOT 4
- Start a select with the Dart API without specifiying a table HOT 4
- Custom query generated type missing nullables HOT 4
- [WEB] DriftRemoteException: RangeError (start): Invalid value: Not in inclusive range 0..2322399232: -1972640192 HOT 1
- createdAt & updatedAt columns HOT 4
- SqliteException(5): while selecting from statement, database is locked, database is locked (code 5) HOT 2
- drift files: file formatting impossible
- How to initialize the database without the constructor syntax HOT 2
- Add toJson() for result class HOT 1
- Query stream cancellations can affect migrations
- No modification allowed Error HOT 4
- Schema migration helper fails when there are no migrations yet HOT 1
- Views code generation HOT 9
- Cannot test migrations using modular approach with store_date_time_values_as_text: true HOT 4
- Unable to override `runs_before` in `build.yaml` due to `Unrecognized keys` exception HOT 2
- Support remote SQLite databases HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from drift.