0.0.12 • Published 5 years ago
oscrud v0.0.12
oscrud
CRUD Microservice using Cote & ORMs controlling all models access
Installation
Using npm:
$ npm i --save oscrud
In Server-Side:
const Oscrud = require('oscrud').CrudInitializer;
const responder = Oscrud({
models,
orm: 'sequelize',
connection: 'postgresql://localhost:5432/oskang09',
server: 'pg', // can be whatever name just dont repeat..
timeout: 3000 // default timeout 3000
});
// Will return responder that already subsribe to crud action.
In Client-Side:
const Oscrud = require('oscrud').CrudRequester;
const requester = Oscrud({
orm: 'sequelize',
server: 'pg', // server's name
client: 'req1', // can be whatever name.
});
Example Usage
const Sequelize = require('sequelize');
const connection = new Sequelize('postgres://localhost:5432/oskang09');
const item = connection.define('item', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: Sequelize.TEXT,
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
});
const Oscrud = require('oscrud');
const responder = Oscrud.CrudInitializer({
models,
orm: 'sequelize',
connection: 'postgresql://localhost:5432/oskang09',
server: 'pg',
timeout: 3000
});
const query = Oscrud.CrudRequester({
orm: 'sequelize',
server: 'pg',
client: 'Item Service',
});
const test = async (log) => {
// Supported transaction.
const trx = await query.startTransaction(); // Receive transaction id
log(await query.create('item', { name: 'Testing Item' }, trx));
log(await query.create('item', { name: 'Item2' }, trx));
log(await query.readone('item', 1, trx));
/* You can call without transaction by removing transaction id at last of the paramteres */
log(await query.create('item', { name: 'Without transaction' }));
/* Transaction must commit or rollback at the end if not will stop at there waiting for completed singal... */
await query.commitTransaction(trx); // Commit the transaction
await query.rollbackTransaction(trx); // Rollback the transaction
};
test();
Output:
> Item { id: 1, name: 'Testing Item' }
> Item { id: 2, name: 'Item2' }
> Item { id: 1, name: 'Testing Item' }
> Item { id: 3, name: 'Without transaction' }
Todo
- Support more ORMs
- Fully documentation
- Make FuncQ more dynamic
- Expose CRUD Rest