4.2.3 • Published 4 years ago
qlink-server v4.2.3
qlink is package built on qreal to make process of get data and restructure it more easy
to download package from npm
npm install qlink-server
or install from yarn
yarn add qlink-server
Setup qlink
to setup qlink in server side
Qlink.listen(
{
payload : /* data what will pass to computed data */,
query : /* query */
},
/* call back function */
)
setup with express js
const express = require('express');
const http = require('http');
const app = express();
const server = http.createServer(app)
const qlink = require('../src/index');
const bodyParser = require('body-parser'); // you should use to it parse json query
// subscript new endpoint
qlink.subscribe('articles', { ... })
app.use(bodyParser.json()); // to parse json query
app.use(bodyParser.urlencoded({ extended: true })); // to parse json query
// Make qlink listen to client side
app.post('/qlink', (req, res) => {
qlink.listen({
payload : req,
query : req.body
}, d => res.json(d))
})
server.listen(8080, () => console.log('server is running...') )
Subscibe endpoints
to subscibe data use qlink.subscribe
method
get method :
// server side
qlink.subscribe('articles', {
get (attrs, payload, done) {
const queries = ( attrs.uid ) ? { uid : attrs.uid } : {}
Articles.find(queries, (err, res) => {
done(res, err)
})
}
})
// client side
fetch('http://localhost:8080/qlink', {
method : 'GET',
body : JSON.stringify({
articles : {
title : ""
}
})
headers: { 'Content-Type': 'application/json' }
})
Computed Method
// server side
qlink.subscribe('articles', {
computed : {
isLikedFromUser (payload, article, done) {
done( article.likes.includes( payload.user._id ) )
}
}
})
// client side
fetch('http://localhost:8080/qlink', {
method : 'GET',
body : JSON.stringify({
articles : {
$attrs : { uid : 'Sj834j' },
isLikedFromUser : '',
... // rest of query
}
}),
headers: { 'Content-Type': 'application/json' }
})
Mutations Method
// server side
qlink.subscribe('articles', {
mutations : {
otherMutaion(data, article, done) {},
addArticle (data, article, done) {
const article = new Atricle(data);
article.save();
this.otherMutaion(data, article, done) // you can call other mutations
}
}
})
// client side
fetch('http://localhost:8080/qlink', {
method : 'GET',
body : JSON.stringify({
articles : {
addArticle : {
$attrs : { ...data },
title : '',
isLikedFromUser : ''
}
}
}),
headers: { 'Content-Type': 'application/json' }
})
Middlewares Method
// server side
qlink.subscribe('articles', {
middlewares : {
default (article, subscibes, next) {}, // middleware for endpoint articles
anotherMiddleware (id, subscribes, next) {},
author : (id, subscribes, next) => {
Users.find({ id }, (err, user) => {
next( user )
})
// OR
// you can get any middleware by call it with this keyword
// like :
this.anotherMiddleware(id, subscribes, next)
}
}
})
// client side
fetch('http://localhost:8080/qlink', {
method : 'GET',
body : JSON.stringify({
articles : {
author : {
name : ''
}
}
}),
headers: { 'Content-Type': 'application/json' }
})