Database

A secure client library for MongoDB & MySQL API

Install

npm i @appstitch/database

Install peer dependencies

npm i @appstitch/core

Usage

API

Method

Type

Description

Platform

collection

Collection Name

Interchangeable with table

MongoDB & MySQL

table

Table Name

Interchangeable with collection

MongoDB & MySQL

id

ID, FieldName (Optional)

Set the ID for a specific document/record. FieldName is only required for MySQL

MongoDB & MySQL

where

Field, OperatorType, Value

A query object used to filter documents

MongoDB & MySQL

limit

number

Limits the number of documents. Default 50

MongoDB & MySQL

include

Field Array

Return specific fields.

MongoDB & MySQL

exclude

Field Array

Prevent specific fields from being returned

MongoDB & MySQL

startAfter

number

Skips the first n documents/records

MongoDB & MySQL

orderBy

Field, Direction

Specified order for results

MongoDB & MySQL

join

Source Field, Destination Table, Destination Field, JoinType

Used to combine rows from two or more tables

MySQL

Read Operations

Get by ID

// MongoDB

db.collection("Users")
  .id("7b5f87cfb6ce44f98c82a1c6")
  .then(result => {
     if(result.success)
     {
        const user = result.doc;
     }
     else
     {
        // handle error
     }
  })
  
// MySQL

db.table("Users")
  .id("7b5f87cfb6ce44f98c82a1c6", "UserID")
  .then(result => {
     if(result.success)
     {
        const user = result.doc;
     }
     else
     {
        // handle error
     }
  })

collection("users") and table("users") is interchangable and has the same effect. Appstitch offers a linguistic convention for both the NoSQL and SQL world

Get multiple documents

// MongoDB

db.collection("Users")
  .where("City", "==", "NewYork")  
  .then(result => {
     if(result.success)
     {
        const user = result.doc;
     }
     else
     {
        // handle error
     }
  })

Write Operations

Insert

db.collection("Users")
    .insert({name : "Bruce", city : "Gotham"})
    .then(result => {
        if (result.success) {
            const userID = result.id;
        } else {
            // handle error
        }
    })

Update

const result =  db
               .collection("users")
               .doc(userID)
               .update({city : "New York"})
               .then(result => {
                 if (result.success) {
                   // success 
                 } else {
                     // handle error
                 }
               })

Delete

const result =  db
               .collection("users")
               .doc(userID)
               .update({city : "New York"})
               .then(result => {
                 if (result.success) {
                   // success 
                 } else {
                     // handle error
                 }
               })

Algolia Integration

Appstitch can be used to keep your Algolia indices in sync. Just pass syncData with any write operation.

The Algolia index will need to have the identical name (and case) as your collection/table

Algolia example

db.collection("Users")
    .insert(
    {name : "Bruce", city : "Gotham"}, 
    {syncData : true} // <-- User will be created in Algolia 
    )
    .then(result => {
        if (result.success) {
            const userID = result.id;
        } else {
            // handle error
        }
    })

Last updated

Was this helpful?