Officials packages

Mongodb

The mongoDB module was designed exclusively for the Mineral framework, it allows you to communicate with a MongoDB database.


Use this package as a library

Run this command:

dart pub add mineral_mongodb

This will add a line like this to your package's pubspec.yaml (and run an implicit dart pub get):

dependencies:
  mineral_mongodb: ^2.0.0

Alternatively, your editor might support dart pub get. Check the docs for your editor to learn more.

The MongoDB module is compatible with the compilation of your Mineral application 🧱


Register the module

Each schema must be registered within the module constructor in order to be accessible through your application using the Transaction mixin.

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.register([mongoDB]);

  await kernel.init();
}

You cannot register our packages into our modules files, you need to use the main.dart file.


Usage

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');
  int get age => payload.get('age');
}

When you go to query your MongoDB database, it returns an object containing the keys of the "fields" in your schema.

Thanks to the Models provided by the package, Mineral will serialize the result of each of your queries to the model representing your schema.

In the same way as in SQL, it is necessary that each entry in your schema has an _id key.

This field is self-managed by Mineral and is transcribed by the id property of your model.

Furthermore, if you choose to use the MongoDB Query builder, then you must use _id and not just id

Using our models from classes

In order to use your models, it is essential to use the Transaction mixin.

Please note that you cannot simply import your class models directly.

import 'package:mineral_mongodb/mineral_mongodb.dart';

class MyClass with Transaction 👈 {
}

Now, you can use schema.use<Model>() function.

Get all foo

Retrieve all the data from your MongoDB schema by serializing it via your model.

import 'package:mineral_mongodb/mineral_mongodb.dart';

class MyClass with Transaction {
  Future<void> handle (event) async {
    await schema.use<FooModel>().all();
  }
}

Find one foo

Retrieve the first data matching (from _id column) from your MongoDB schema by serializing it via your model.

import 'package:mineral_mongodb/mineral_mongodb.dart';

class MyClass with Transaction {
  Future<void> handle (event) async {
    await schema.use<FooModel>().find('1234');
  }
}

Find one foo from defined column

Retrieve the first data matching from the given column and value.

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);
  }
}

Create one foo

Create one entry to the database schema from given data and return the new instance of this.

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);
  }
}

Create many foo

Create many entries to the database schema from given data and return it.

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);
  }
}

Update one foo

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);
  }
}

Delete one foo

import 'package:mineral_mongodb/mineral_mongodb.dart';

class MyClass with Transaction {
  Future<void> handle (event) async {
    await schema.use<FooModel>().delete();
  }
}

Access to mongodb query builder

When the "basic" methods are not enough for your needs, you can access the MongoDB client's query builder to perform more specific actions such as a search on several columns.

import 'package:mineral_mongodb/mineral_mongodb.dart';

class MyClass with Transaction {
  Future<void> handle (event) async {
    await schema.use<FooModel>().query();
  }
}
Previous
Components