arangodb_repository 1.0.0-rc.1 arangodb_repository: ^1.0.0-rc.1 copied to clipboard
Concrete implementation of the nosql_repository package for the ArangoDB database.
import 'package:arango_driver/arango_driver.dart';
import 'package:arangodb_repository/arangodb_repository.dart';
import 'package:nosql_repository/nosql_repository.dart';
import 'ingredient.dart';
import 'principal.dart';
import 'recipe.dart';
void main(List<String> args) async {
// first we create a connection to ArangoDB
final db = _connectArangoDb();
// now, we create a repository on that connection to the recipes collection.
final repository = ArangoDbRepository<Recipe>(db, 'recipes');
// first we need to create a principal to represent the
// user performing the operations to the database
final principal = Principal('Alice');
var recipe = Recipe(
title: 'Scrambled eggs',
ingredients: [
Ingredient(description: 'eggs', quantity: 6.0),
Ingredient(description: 'salt', quantity: 0.01),
Ingredient(description: 'milk', quantity: 0.5),
Ingredient(description: 'olive oil', quantity: 0.02),
],
);
var map = _recipeToMap(recipe);
// this following line will create a new recipe in
// the database, so long has the 'create_recipe' permission.
map = await repository.create(map, principal, 'create_recipe');
// we will assume that create returns a key in the _key field:
var key = map['_key'];
// and with that key we can retrieve the object again from
// the database by using get
map = await repository.get(key, principal, 'read_recipe');
// which we finally convert again to a Recipe
recipe = _recipeFromMap(map);
print(recipe.title);
final search = ArangoDbSearch<Recipe>(repository);
final criteria = SearchCriteria(searchConditions: [
Expression.like('entity.title', '%eggs%'),
]);
var searchResult = await search
.search(criteria, principal, permission: 'search_recipes')
.toList();
for (var item in searchResult) {
print(item.toString());
}
// print('The total number of results is ${searchResult.count}.');
// map = await searchResult.page.first;
// recipe = _recipeFromMap(map);
// print('The first element is ${recipe.title}');
}
ArangoDBClient _connectArangoDb() {
var client = ArangoDBClient(
scheme: 'http',
host: 'localhost',
port: 8529,
db: 'mydatabase',
user: 'user',
pass: 'password',
);
return client;
}
Map<String, dynamic> _recipeToMap(Recipe recipe) => {
'_key': recipe.key,
'title': recipe.title,
'ingredients': List<Map<String, dynamic>>.from(
recipe.ingredients.map((e) => _ingredientToMap(e))),
};
Recipe _recipeFromMap(Map<String, dynamic> map) {
return Recipe(
key: map['_key'],
title: map['title'],
ingredients: List<Ingredient>.from(
map['ingredients'].map((e) => _ingredientFromMap(e))),
);
}
Ingredient _ingredientFromMap(Map<String, dynamic> map) => Ingredient(
description: map['description'], quantity: map['quantity'].toDouble());
Map<String, dynamic> _ingredientToMap(Ingredient value) => <String, dynamic>{
'description': value.description,
'quantity': value.quantity,
};