UniqueModel.Js is a abstract layer of data model.
Important! It is not a stable package now.
Important! All the session operation must be in the Fibers environment. Like this:
Fiber(function() {
let user = UserDao.findOne({
name: 'ttt'
});
process.exit();
}).run();
Node The README written in difference languages are in doc/README directory of source codehere.
When I use Node.js, I always encounter two problems.
So I plan to write a library to solve these problems.
First install node.js. Because I use some features of ES6, so it is preferred to use the latest stable version of Node.js. Then:
$ npm install unique-model
The UniqueModel is divided to two layers:
The unique model layer is used to define model and operate the model instance. The persistent session layer is used to manage the model instance which is persisted in somewhere.
You can use SQL databases or NoSQL databases like MongoDB to persist the model instance. But we only support MongoDB now(Because I am using MongoDB).
If you want to persist the model instance into database, you must define a persistent session:
let um = require('..');
um.enablePersist();
let session = um.createSession({
backend: 'mongodb',
uri: 'mongodb://localhost/userman',
options: {
user: 'user',
pass: '123456'
}
});
You must call enablePersist function to enable the persist module of UniqueModel because It is off-by-default. Or you can not create the persistent session.
The argument of createSession is an object which has three properties:
After create the persistent session, UniqueModel will create the session backend and connect to database.
The next step is to define the model. Unique model will create the model class which record the information of fields.
let Types = um.Types;
let Tag = um.model.createModel('Tag', {
name: Types.Text()
});
let Image = um.model.createModel('Image', {
path: Types.Text(),
type: Types.Text(),
width: Types.Integer(),
height: Types.Integer(),
tags: Types.UObjectArray({
type: 'Tag'
})
});
let User = um.model.createModel('User', {
name: Types.Text(),
age: Types.Integer(),
height: Types.Double(),
weight: Types.Double(),
photo: Types.UObject({
type: 'Image'
})
});
The first argument is the name of model.
The second argument is the fields information. UniqueModel use the function under um.Types to define types. Every type can have some options which is specified in the argument of function.
Now we support following types:
UObject and UObjectArray have a option called type. It is used to specify the name of associated model.
Then we use new to create the model instance. You can create an empty object and specify the property like following code:
let user = new User();
user.name = 'kkk';
user.age = 28;
You can also use the JavaScript object the specify the initial value of fields. It also support create model recursivly(auto create sub models).
let user = new User({
name: 'ttt',
age: 20,
height: 155,
weight: 50.5,
photo: new Image({
path: 'map.png',
type: 'png',
width: 500,
height: 400,
tags: [
new Tag({
name: 'Color'
}),
new Tag({
name: 'High'
})
]
})
});
Now we can persist the created model instance into databases. But I seperate the persist operations into DAO objects. Every model will have one DAO object. We can use the getDao method of session object to get DAO.
let TagDao = session.getDao(Tag);
let ImageDao = session.getDao(Image);
let UserDao = session.getDao(User);
Every DAO object will be associated with one session. So you can use different session backend in one project.
We can persist the model instance now. The method to persist model instances is create method of DAO:
user.photo.tags.forEach(tag => {
TagDao.create(tag);
});
ImageDao.create(user.photo);
UserDao.create(user);
console.log(user.toString());
console.log(user.photo.toString());
user.photo.tags.forEach(tag => {
console.log(tag.toString());
});
The DAO object do not persist sub model instances recursivly! So you must persist all the models manully. Now the user instance and the associated photo and tags have been persisted into database.
Then let's find the persisted user in the database. The find method is used to find all model instances by query conditions. The query conditions is passed to the backend now. So if you use mongodb backend, the format query conditions is identical to Mongoose. mongoosejs.com
The return value of find is an array of objects. We use forEach methods to ergodic the array.
let users = UserDao.find({
name: 'ttt'
});
console.log(users.length);
users.forEach(user => {
console.log(user.toString());
});
You can also use findOne method to get one of the objects which match query conditions.
let user = UserDao.findOne({
name: 'ttt'
});
if ( user != null ) {
console.log(user.toString());
}
else {
console.log(null);
}
If there are any matched objects in database, it will return null.
If you want to update model instance, you can use the update method.
OK. Let's find and update the user information.
let user = UserDao.findOne({
name: 'ttt'
});
console.log(user.toString());
user.age = 25;
UserDao.update(user);
console.log(user.toString());
After calling update, UniqueModel will replaces the model data in databases with new data.
Removing model from database is very easy, too.
let user = new User({
id: userId
});
UserDao.remove(user);
After calling remove, the model has been removed from database.
The DAO object has more methods to update and remove model instances. You can read the test/test.js in the source directory.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。