Skip to content

Latest commit

 

History

History
70 lines (53 loc) · 1.72 KB

README.md

File metadata and controls

70 lines (53 loc) · 1.72 KB

Build Status

jaguar_serializer_mongo

Mongo serializer helpers for jaguar_serializer

Convert Mongo's ObjectId

Mongo expects ids to be of type ObjectIds. But in Code, we expect them to be Strings most of the time. [MongoId] custom field processor can be used to automatically encode and decode to ObjectId from String.

@GenSerializer(fields: const {
  'id': const EnDecode(alias: '_id', processor: const MongoId()),
})
class PlayerSerializer extends Serializer<Player> with _$PlayerSerializer {
  Player createModel() => new Player();

  PlayerSerializer();
}

/// Player model for the game
class Player {
  /// Id of the player
  String id;

  /// Name of the player
  String name;

  Player();
}

Simple example

import 'package:mongo_dart/mongo_dart.dart';
import '../models/player/player.dart';

main() async {
  Db db = new Db('mongodb://localhost:27017/jaguar_mongo_test');
  await db.open();

  try {
    DbCollection coll = db.collection('one');
    ObjectId id = new ObjectId();
    ObjectId allianceId = new ObjectId();

    Player player = new Player()
      ..id = id.toHexString()
      ..allianceId = allianceId.toHexString()
      ..name = 'KnightsOT'
      ..email = '[email protected]'
      ..age = 27
      ..score = 500000
      ..emailConfirmed = true;

    await coll.insert(player.toMongo());

    Map result = await coll.findOne(where.id(id));
    print(result);

    Player decoded = Player.fromMap(result);
    print(decoded);
  } finally {
    await db.drop();
    await db.close();
  }
}