Référence API (Sdk)

Installation

Installation du package JS pour vos rest API .

bun i @dnax/sdk@latest

Instance REST API

Front-End

import {useRest} from "@dnax/sdk"
const rest = new useRest({
  key:"_API_", // Unique Key for each application
  tenant_id:"0001", // Tenant ID
  server:"http://localhost:4000" // Server url
})

Les opérateur de recherche

$match & $matchInclude

les relations (Propagation)

$include

Inclusion des relations

rest.find(collection,{
    $include:Array<string,$lookup>
})

Plus de détails

Fecthing options

$limit

La taille d'élément à retourner par défaut 100 premiers éléments matches.

$skip

Nombre d'élément à omettre

Ordering options

$sort

Affichage des données ASC/DESC

$project

$ (projection)

Méthodes

authCollection

Authentification à partir d'une collection

var data ={
  email:"[email protected]",
  password:"onlyshow01"
}
await rest.authCollection("users",data)

aggregate

Function d'aggregation pour performer des statistiques .

  • collection

  • pipeline

var pipeline =[]
await rest.aggregate("users",pipeline)

find

Fetching des données

  • $match

  • $matchInclude

  • $limit

  • $project

  • $skip

  • $sort

var findParams ={
  $match:{}, // operateur de filtre de MongoDB
  $include:[], // Operateur lookup de mongoDB 
  $matchInclude:{}, 
  $limit:100,// Defaut :100
  $project:{}, // Projection de MongoDB
  $skip:0, 
  $sort:{} 
}
await rest.find("users",findParams)

findOne

Retrouver un document spécifique

  • id (Id du document)

  • $include

  • $project

var ID="" // Id 
await rest.findOne("users",ID,{
  $include:{},
  $project:{}
})

insertOne

  • data

  • options : InsertOneOption

var data ={
  fullname:'Jhon Doe',
  email:"[email protected]"
}

await rest.insertOne("users",data,{})

await rest.insertOne("users",data,{
  cleanDeep:true  // Auto clean null and empty array or undefined element
})

insertMany

  • data : Array<object>

var data =[
  {
  fullname:'Jhon Doe',
  email:"[email protected]"
  },{
  fullname:'Edguard Lenois',
    email:'[email protected]'
}
]

await rest.insertMany("users",data)

updateOne

  • id

  • update: updateOption

var userId ="66b10b98cb5e238d20002674"
var updateOption ={
  $set:{
    email:"[email protected]"
  },
  $addToSet:{},
  $currentDate:{},
  $inc:{},
  $mul:{},
  $pop:{},
  $pull:{},
  $pullAll:{},
  $push:{}
}
await rest.updateOne("users",userId,upateOption)

updateMany

var userIds =[id1,id2]
var updateOption ={
  $set:{
    role:"admin"
  },
}
await rest.updateOne("users",userIds,upateOption)

deleteOne

await rest.deleteOne("users",id)

deleteMany

await rest.deleteMany("users",ids)

setToken

await rest.setToken(token)

clearToken

Suppression du token du navigateur

await rest.clearToken()

logout

Déconnection de l'utilisateur

await rest.logout() // This method clear token automatically

service

  • service_name:string

  • data:any

await rest.service("request-otp",data) 

endpoint

await rest.endpoint(path,{
  headers,
  body,
  method
})

dropIndexes

// Local API only
await rest.dropIndexes(collectionName) // removes all indexes

socketIO

// emit event to server
rest.socket.emit(event:string,data:any)

// listenning
rest.socket.on(event:string,(data)=>{

    // DO something
})

Last updated