mongoschemad 3.1.0
MongoDB Schema support
To use this package, put the following dependency into your project's dependencies section:
dub.json
dub.sdl
MongoSchemaD
A simple library for vibe.d adding support for structured Bson data using structs/classes and functions to simplify saving, updating and finding Mongo documents.
Can also be used without MongoDB for Bson (de)serialization.
Example
import vibe.db.mongo.mongo;
import mongoschema;
import mongoschema.aliases : name, ignore, unique, binary;
auto client = connectMongoDB("localhost");
struct Permission
{
string name;
int priority;
}
struct User
{
mixin MongoSchema; // Adds save, update, etc.
@unique
string username;
@binary()
ubyte[] hash;
@binary()
ubyte[] salt;
@name("profile-picture")
string profilePicture = "default.png";
Permission[] permissions;
@ignore:
int sessionID;
}
// Links the `test.users` collection to the `User` struct.
client.getCollection("test.users").register!User;
User register(string name, string password)
{
User user;
user.username = name;
user.salt = generateSalt().dup; // needs dup because array gets messed up otherwise when leaving function
user.hash = complicatedHashFunction(password, user.salt).dup;
user.permissions ~= Permission("forum.access", 1);
// Automatically serializes and puts the object in the registered database
// If save was already called or the object got retrieved from the
// collection `save()` will just update the existing object.
user.save();
// ->
// {
// username: name,
// hash: <binary>,
// salt: <binary>,
// profile-picture: "default.png",
// permissions: [{
// name: "forum.access",
// priority: 1
// }]
// }
return user;
}
User find(string name)
{
return User.findOneOrThrow(["username": name]);
}
- Registered by WebFreak
- 3.1.0 released 30 days ago
- WebFreak001/MongoSchemaD
- MIT
- Copyright © 2017, webfreak
- Authors:
- Dependencies:
- vibe-d:data, vibe-d:mongodb
- Versions:
-
3.1.0 2019-Jan-23 3.0.0 2019-Jan-15 2.6.0 2018-Jul-09 2.5.3 2017-Oct-04 2.5.2 2017-Aug-28 - Download Stats:
-
-
0 downloads today
-
0 downloads this week
-
0 downloads this month
-
444 downloads total
-
- Score:
- 1.1
- Short URL:
- mongoschemad.dub.pm