wirentity
v1.2.5
Published
Wirentity
Downloads
16
Readme
##Wirentity
###How the Entity client works ...
import { Entity, EntityTransport } from 'wirentity';
let httpClientTransport = new EntityTransport(EntityTransport.HTTP);
await httpClientTransport.connect({ host: 'http://localhost', port: 3001 });
// ** create entity with this transport
let UserEntity = Entity.generateClass(httpClientTransport, 'users', '_id');
// ** All actions are returning a promise
// ** INSTANCE METHODS
// ** imagine we created a user object
// ** let user = await UserEntity.create({age: 18, name: "Trump"});
// ** set the key, val pair or an object {}
user.set("age", 20);
user.set({firstName: "Jhon", lastName: "Snow"});
user.get(key);
// ** reset the key value to the last saved snapshot's value
user.reset(key);
// ** check if there are any changes not saved
user.isDirty();
await user.save();
// ** gets the object of user
user.value();
// ** returns user id
user.getId();
// ** removes the user
await user.remove();
await user.runCommand(command, data);
// ** STATIC METHODS
// ** entityAction = { action: 'create', payload }
let user = await UserEntity.create({age: 18, name: "Trump"});
// ** entityAction = { action : 'loadById', payload: { query: { id }, select } }
let user = await UserEntity.loadById(id);
// ** entityAction = { action: 'loadAll', payload : { select } }
let allUsers = await UserEntity.loadAll({select});
// ** entityAction = { action: 'loadByQuery', payload : { query, select, limit, skip } }
let users = await UserEntity.loadByQuery({ query, select, limit, skip });
// ** entityAction = { action : 'updateById', payload: { query: { id }, update } }
await UserEntity.updateById(id, update);
// ** entityAction ={ action: 'update', payload : { query, update } }
await UserEntity.update(query, update);
// ** entityAction = { action : 'removeById', payload: { query: { id } } }
await UserEntity.removeById(id);
// ** entityAction = { action : 'removeByQuery', payload: { query } }
await UserEntity.removeByQuery(id)
// ** entityAction = { action: 'runCommand', payload: { command, data } }
UserEntity.runCommand()
// ** entityAction = { action: 'incrementById', payload: { query: { id }, bins } }
UserEntity.incrementById(id, bins)
###How the Entity backend works ...
import { Entity, EntityTransport } from 'wirentity';
let httpBackendTransport = new EntityTransport(EntityTransport.HTTP);
let mongoTransport = new EntityTransport(EntityTransport.MONGO);
// ** Mongo transport
const schemas = [{
pk: '_id',
name: 'users',
schema: new Schema({
name: String,
age: String
})
}];
await mongoTransport.bind({ schemas, url: 'mongodb://localhost/wire_entity', options: { debug: false } });
let mongoClient = await mongoTransport.connect();
let httpBackend = await httpBackendTransport.bind();
httpBackend.proxy(mongoClient);
// ** how to add middlewares
httpBackend.use((req, res, next) => {
// ** something goes here
// ** you can get the actual entity action with httpBackend.parseRequest(req)
let { action, collection, payload } = httpBackend.parseRequest(req);
});
// ** adds general middleware for every action for every collection
httpBackend.use(midllewareHandler);
// ** adds middleware for every action for specific collection
httpBackend.use(collection, midllewareHandler);
// ** adds middleware on specific action for all collections
httpBackend.addMiddleware("create", midllewareHandler);
// ** adds middleware on specific action on specific collection
httpBackend.addMiddleware("user.create", midllewareHandler);
###kitoo-core example ... ##client side code
import {Entity, EntityTransport} from 'wirentity';
let kitooClientTransport = new EntityTransport(EntityTransport.KITOO);
async function client() {
try {
const client = await kitooClientTransport.connect({host: '127.0.0.1', port: 7896})
let UserEntity = client.getEntity('users', '_id');
let user = await UserEntity.create({age: 20, name: "Shelby"});
} catch (e) {
console.error(e)
}
}
client()
##backend side code
import { Entity, EntityTransport } from 'wirentity';
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
let kitooBackendTransport = new EntityTransport(EntityTransport.KITOO);
let mongoTransport = new EntityTransport(EntityTransport.MONGO);
const schemas = [{
pk: '_id',
name: 'users',
schema: new Schema({
name: String,
age: String
})
}];
async function backend() {
await mongoTransport.bind({ schemas, url: 'mongodb://localhost/wire_entity', options: { debug: false } });
let mongoClient = await mongoTransport.connect();
let kitooBackend = await kitooBackendTransport.bind({router: 'tcp://127.0.0.1:7896'});
//add middleware to check if user exists
kitooBackend.addMiddleware("users.create", async (req, res, next) => {
if (user.value()) {
return res.json({ error: true });
}
next();
})
kitooBackend.proxy(mongoClient);
}
backend();
you must run router aside
import {Router} from 'kitoo-core'
(async function () {
try {
let router = new Router({ bind: 'tcp://127.0.0.1:7896' });
await router.start();
console.log('router started')
} catch (err) {
console.error(err)
}
}());