-
Notifications
You must be signed in to change notification settings - Fork 389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
clientGeneratedAs column option #3413
Comments
I think I understand what you're getting at, but I'm still trying to understand how this use case is distinct from a stored I guess what mainly worries me is how this would play out with e.g. a It's worth mentioning that this is possible already (at least in SQLite) by defining a custom function: final db = Database(NativeDatabase.memory(setup: (database) {
database.createFunction(
functionName: 'remove_diacritics',
function: (args) {
return (args[0] as String).removeDiacritics();
},
);
})); And then calling something like this in extension RemoveDiacritics on Expression<String> {
Expression<String> removeDiacritics() => FunctionCallExpression('remove_diacritics', [this]);
} (obviously, possible is not the same thing as simple and we should perhaps find a better solution for this) |
I completely forgot about custom sqlite functions. Maybe this solution won't work using |
@simolus3 custom functions work in web database? |
It's possible to get them to work, but it requires using a custom drift worker. You can create a Dart file that looks like this: import 'package:drift/wasm.dart';
void main() {
WasmDatabase.workerMainForOpen(setupAllDatabases: (database) {
database.createFunction(
functionName: 'remove_diacritics',
function: (args) {
return (args[0] as String).removeDiacritics();
},
);
});
} You can compile it with await WasmDatabase.open(
databaseName: 'instantlist',
sqlite3Uri: Uri.parse('sqlite3.wasm'),
driftWorkerUri: Uri.parse('worker.dart.js'),
localSetup: (database) {
database.createFunction(
functionName: 'remove_diacritics',
function: (args) {
return (args[0] as String).removeDiacritics();
},
);
},
); |
It would be nice if Drift implements the column option
clientGeneratedAs
. It would look likeclientDefault
, taking a function that accepts the companion as input, and returns aValue<ColumnType>?
, but it would be executed every time we mutate the data in the database.For example:
It would allow us to specify how data mutates in the database without the need of using
companion.copyWith()
to mutate thesearch
, which in this case changes whenname
changes.The text was updated successfully, but these errors were encountered: