1.2.5 • Published 3 years ago

wirentity v1.2.5

Weekly downloads
8
License
MIT
Repository
bitbucket
Last release
3 years ago

##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)
	}
}());
1.2.5

3 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.30

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.2.12

6 years ago

0.2.11

6 years ago

0.2.10

6 years ago

0.2.9

6 years ago

0.2.8

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.28

6 years ago

0.1.27

6 years ago

0.1.26

6 years ago

0.1.25

6 years ago

0.1.24

6 years ago

0.1.23

6 years ago

0.1.22

6 years ago

0.1.20

6 years ago

0.1.19

6 years ago

0.1.18

6 years ago

0.1.17

6 years ago

0.1.16

6 years ago

0.1.15

6 years ago

0.1.14

6 years ago

0.1.12

6 years ago

0.1.11

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.0

6 years ago