microgen-v3-sdk
v0.17.1
Published
Microgen SDK
Downloads
202
Maintainers
Readme
Microgen SDK
This is the Official Node JS and Browser client/library for Microgen API. Visit https://microgen.id. More information about the product and see documentation at http://docs.microgen.id for more technical details.
API Documentation
Please check Microgen API Reference.
Installation
Node.js
To install microgen in a node project:
npm install --save microgen-v3-sdk
Usage
Configure package with your account's API key obtained from your Microgen Dashboard.
const { MicrogenClient } = require('microgen-v3-sdk');
const microgen = new MicrogenClient({
apiKey: '*******************',
});
Authentication
Use microgen auth services for manage your user.
Register
const { user, token, error } = await microgen.auth.register({
firstName: 'Ega',
lastName: 'Radiegtya',
email: '[email protected]',
password: 'password',
});
Login
const { user, token, error } = await microgen.auth.login({
email: '[email protected]',
password: 'password',
});
Login with Google
const { user, token, error } = await microgen.auth.loginWithGoogle({
token: '*******************',
});
Login with Facebook
const { user, token, error } = await microgen.auth.loginWithFacebook({
token: '*******************',
});
Login with Regol QR
const { content, error } = await microgen.auth.loginWithRegolQR({
deviceId: 'B2C3-D4E5-F6G7-8901',
deviceModel: 'SurfacePro8',
deviceName: 'User Surface Pro 8',
deviceOs: 'Windows',
deviceVersion: '11.0.22000.100',
platformName: 'desktop',
});
User
const { user, error } = await microgen.auth.user();
// Get user with filter:
// lookup
const { user, error } = await microgen
.auth
.user({ filterKey: filterValue, ... });
Update
const { user, error } = await microgen.auth.update({ firstName: 'Ega' });
Logout
const { user, token, error } = await microgen.auth.logout();
Database
Create
const { data, error } = await microgen.service('posts').create({
name: 'Post 1',
notes: 'Hello microgen',
});
Create Many
const { data, error } = await microgen.service('posts').createMany([
{
name: 'Post 1',
notes: 'Hello microgen',
},
{
name: 'Post 2',
notes: 'Hello world',
},
]);
Get
const { data, error } = await microgen
.service('posts')
.getById('605a251d7b8678bf6811k3b1');
// Get record with filters:
// select
// lookup
const { data, error } = await microgen
.service('posts')
.getById('605a251d7b8678bf6811k3b1', { filterKey: filterValue, ... });
Update
const { data, error } = await microgen
.service('posts')
.updateById('605a251d7b8678bf6811k3b1', {
notes: 'Hello world',
});
Update Many
const { data, error } = await microgen.service('posts').updateMany([
{
_id: '605a251d7b8678bf6811k3b1',
notes: 'Hello world',
},
{
_id: '605a251d7b8678bf6811k3b2',
notes: 'Hello microgen',
},
]);
Delete
const { data, error } = await microgen
.service('posts')
.deleteById('605a251d7b8678bf6811k3b1');
Delete Many
const { data, error } = await microgen
.service('posts')
.deleteMany(['605a251d7b8678bf6811k3b1', '605a251d7b8678bf6811k3b2']);
Link
const { data, error } = await microgen
.service('posts')
.link('605a251d7b8678bf6811k3b1', {
categories: '61d26e8e2adb12b85c33029c',
});
Unlink
const { data, error } = await microgen
.service('posts')
.unlink('605a251d7b8678bf6811k3b1', {
categories: '61d26e8e2adb12b85c33029c',
});
Find
const { data, error } = await microgen.service('posts').find();
// sort
// 1 = ascending
// -1 = descending
const { data, error } = await microgen
.service('posts')
.find({ sort: [{ name: 1 }] });
// skip
const { data, error } = await microgen.service('posts').find({ skip: 10 });
// limit
const { data, error } = await microgen.service('posts').find({ limit: 10 });
// select
const { data, error } = await microgen
.service('posts')
.find({ select: ['name'] });
// lookup into multiple link to record fields
const { data, error } = await microgen
.service('posts')
.find({ lookup: ['categories'] });
// lookup into all link to record fields
const { data, error } = await microgen.service('posts').find({ lookup: '*' });
// lookup into all link to record fields but only show the ids
const { data, error } = await microgen
.service('posts')
.find({ lookup: { _id: '*' } });
// lookup into all link to record fields and show all data
const { data, error } = await microgen
.service('posts')
.find({ lookup: { '*': '*' } });
// where
const { data, error } = await microgen
.service('posts')
.find({ where: { name: 'Ega' } });
// not equal
const { data, error } = await microgen
.service('posts')
.find({ where: { name: { $ne: 'Ega' } } });
// contains
const { data, error } = await microgen
.service('posts')
.find({ where: { name: { $contains: 'Ega' } } });
// not contains
const { data, error } = await microgen
.service('posts')
.find({ where: { name: { $notContains: 'Ega' } } });
// include
const { data, error } = await microgen
.service('posts')
.find({ where: { name: { $in: ['Ega'] } } });
// not include
const { data, error } = await microgen
.service('posts')
.find({ where: { name: { $nin: ['Ega'] } } });
// less then
const { data, error } = await microgen
.service('posts')
.find({ where: { total: { $lt: 10 } } });
// less then equal
const { data, error } = await microgen
.service('posts')
.find({ where: { total: { $lte: 10 } } });
// greater then
const { data, error } = await microgen
.service('posts')
.find({ where: { total: { $gt: 10 } } });
// greater then equal
const { data, error } = await microgen
.service('posts')
.find({ where: { total: { $gte: 10 } } });
Count
// count all records
const { data, error } = await microgen.service('posts').count();
// count with filters
const { data, error } = await microgen
.service('posts')
.count({ filterKey: filterValue, ... });
Storage
Upload
// from client
const file = event.target.files[0];
const { data, error } = await microgen.storage.upload(file);
// from server
const file = req.files[0];
const { data, error } = await microgen.storage.upload(
file.buffer,
file.originalname,
);
Realtime
Event
*
CREATE_RECORD
UPDATE_RECORD
DELETE_RECORD
LINK_RECORD
UNLINK_RECORD
Get tableId
by Table Name
const { tableId, error } = await microgen.realtime.getTableId('posts');
Subscribe
microgen.realtime.subscribe(tableId, { event: '*' }, (message) => {
if (message.error) {
console.log(message.error);
return;
}
console.log(message.event, message.payload);
});
Unsubscribe
microgen.realtime.unsubscribe(tableId);
Field
Find
const { data, error } = await microgen.service('posts').field.find();
Get
const { data, error } = await microgen
.service('posts')
.field.getById('605a251d7b8678bf6811k3b1');