Home - Hookyns/unimapperjs GitHub Wiki
Welcome to the UniMapperJS wiki!
To see some examples, go to test folder.
Basic terminology
Domain
In this case Domain is some storage area which hold Adapter and its connection and provide access to data storage for your Entities defined in given Domain. Instance of Domain has specific Adapter and connection to one database. You can have more instances of Domain created, each can have different Adapter or different database connection.
const $um = require("unimapperjs");
const MySqlAdapter = require("unimapperjs/adapters/MySqlAdapter");
const type = $um.type;
// Domain creation - connect to MySQL
const domain = $um.createDomain(MySqlAdapter, "connection string or object with options - specific to adapter");
Entity
Entity is some kind of object model with additional logic for data storing.
/**
* Define User entity in domain
* @class User
* @extends UniMapperEntity
*/
const User = domain.createEntity("User", {
name: type.string.length(100),
email: type.string.length(100).unique(),
password: type.string.length(40),
created: type.date.now(), // .now() is shotcut for .default(() => new Date())
deleted: type.boolean.default(false),
income: type.number.decimals(2).default(0)
});
Unit of Work
Unit of Work is something like transaction but it cares about more Domains and Entities at once (it creates transaction for eg. MongoDB and MySQL at once). You work with your entities inside created unit of work, your work is tracked and saved after you commit that or rolled back automatically if you not commit that.
await $uow.create(async uow => {
var newUser = new User();
newUser.name = "John";
newUser.email = "[email protected]";
newUser.password = "f5sd4f5s4df54df48we79ra4sfd";
uow.insert(newUser);
var secondUser = new User({
name: "Jane",
email: "[email protected]",
password: "68d79gds4fv5sd4g645fgad6fg4"
income: 48465.48
};
uow.insert(secondUser);
await uow.saveChanges(); // If not saveChanges() called, it's gonna be rolled back automatically.
});
Adapter
Adapter is some class which creates interface for your specific data storage. You can write adapter for whatever you want, you must just implement basic methods.
Navigation Property
Navigation property is entity's lazy async property which targets to related entity/entities. It does just in time query when you ask for it and then store it inside entity instance.