The mongoDB module was designed exclusively for the Mineral framework, it allows you to communicate with a MongoDB database.
After installing the module, please register it within ./src/main.dart
following the scheme below
import 'package:mineral_mongodb/mineral_mongodb.dart';
Future<void> main() async {
final mongoDB = MongoDB({
FooModel: () => FooModel()
});
Kernel kernel = Kernel()
..intents.defined(all: true)
..plugins.use([mongoDB]);
await kernel.init();
}
Like a classic use of MongoDB technology, the Mineral framework requires you to use Models representing your noSQL schema.
We will create our first model :
class FooModel extends Schema<FooModel> {
String get bar => payload.get('bar');
}
import 'package:mineral_mongodb/mineral_mongodb.dart';
class MyClass with Transaction {
Future<void> handle (event) async {
final result = await schema.use<FooModel>().all();
print(result);
}
}
import 'package:mineral_mongodb/mineral_mongodb.dart';
class MyClass with Transaction {
Future<void> handle (event) async {
final result = await schema.use<FooModel>().find('1234');
print(result);
}
}
import 'package:mineral_mongodb/mineral_mongodb.dart';
class MyClass with Transaction {
Future<void> handle (event) async {
final result = await schema.use<FooModel>().findBy('column', 'value');
print(result);
}
}
import 'package:mineral_mongodb/mineral_mongodb.dart';
class MyClass with Transaction {
Future<void> handle (event) async {
final result = await schema.use<FooModel>().create({ bar: 'bar' });
print(result);
}
}
import 'package:mineral_mongodb/mineral_mongodb.dart';
class MyClass with Transaction {
Future<void> handle (event) async {
final result = await schema.use<FooModel>().createMany([
{ bar: 'Bar1' },
{ bar: 'Bar2' }
]);
print(result);
}
}
import 'package:mineral_mongodb/mineral_mongodb.dart';
class MyClass with Transaction {
Future<void> handle (event) async {
final foo = await schema.use<FooModel>().find('1234');
final result = await foo?.update({ bar: 'foo' });
print(result);
}
}
import 'package:mineral_mongodb/mineral_mongodb.dart';
class MyClass with Transaction {
Future<void> handle (event) async {
await schema.use<FooModel>().delete();
}
}
import 'package:mineral_mongodb/mineral_mongodb.dart';
class MyClass with Transaction {
Future<void> handle (event) async {
await schema.use<FooModel>().query();
}
}