1.0.26 • Published 4 years ago

@appstitch/database v1.0.26

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

Appstitch MongoDB

Serverless Mongo DB Integrations for Appstitch

Frameworks

  • React
  • React Native

Usage

Fetch one document

const getUser = async (id) => {
    db.collection("users")
      .id(id)
      .get()
      .then((result) => {
        if (result.success) {

          setUser(result.doc);
        }
      })
  };

Fetch multiple documents

   const getUsers = () => {
    db.collection("users")
      .limit(5)

      .get()
      .then((result) => {
        if (result.success) {
          setUsers(result.docs);
        }
      })
  };

Read Options

OptionTypeDescription
collectionStringRequired. Collection name
idStringReturn a specific document. Either 1 document is returned or null
wherestring, OperatorType, objectA query object used filter documents
orderBystring, objectA query object used filter documents
includeString[]Return specific document fields. The _id field is, by default, included in the output documents.
excludeString[]Prevent specific document fields from being return. The _id field is, by default
limitnumberLimits the number of documents. Default 50 returned
startAfternumberSkips the first n documents where n is the specified skip number and passes the remaining documents

Create a document

const createUser = async () => {
    const newUser = {
      firstName: "Bruce",
      lastName: "Banner",
      email: "bruce@avengers.com",
      dateOfBirth: new Date(1970, 12, 8),
    };
    db.collection("Users")
      .insert(newUser)
      .then((result) => {
        if (result.success) {
          setUser(result.doc);
        }
      })
  };

Update a document

  const updateUser = (id) => {
    db.collection("users")
      .id(id)
      .update(
        {
          email: "hulk@revengers.com",
        }
      )
      .then((result) => {
          if(result.success)
          {
            // Deleted successfully 
          }
        
      })

  };

Delete a document

 const deleteUser = (id) => {
    db.collection("users")
      .id(id)
      .delete()
      .then((result) => {
        if(result.success)
          {
            // Updated successfully 
          }
      })
  };

Write Options

OptionTypeDescription
collectionStringRequired. Collection name
idStringWrites to a specific document. Throws an error if document is not found
wherestring, OperatorType, objectA query object used filter documents

Algolia Integration

The Algolia integration keeps your MongoDB & Algolia data in sync. The syncData option is available on all write operations (insert, update, delete).

Example

    final result = await db
    .collection("users")
    .insert(user, {syncData: true})

Install

npm i @appstitch/core @appstitch/types @appstitch/database

Initialize

import appsticth from "@appstitch/core";
import database from "@appstitch/database";

const db = new database();


  useEffect(() => {
    appsticth.initialize({
      clientID: "client",
      appStitchKey:
        "key",
    });
  }, []);
1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago