0.19.3 • Published 4 years ago

space-api v0.19.3

Weekly downloads
11
License
Apache-2.0
Repository
github
Last release
4 years ago

Client API for Space Cloud

Installation

Install via. npm

$ npm install space-api --save

or import as a stand alone library

<script src="https://spaceuptech.com/downloads/libraries/space-api.js"></script>

Documentation

The complete documentation can be found here.

Documentation for specific databases is given below:

Documentation for real time feature is given below:

Quick Start

Create Client Instance

import { API, and, or, cond } from 'space-api';

const api = new API('demo-project', 'http://localhost:4122');
const db = api.DB("mongo") // Put your database alias name here to create DB instance

Note: Multiple databases may be used simultaneously.

Insert a document into the database

const doc = {_id: 1, title: "Title 1", content: "My first record"};
db.insert('COLLECTION_NAME').doc(doc).apply().then(res => {
  if (res.status === 200) {
    // Document inserted successfully
    return;
  }
}).catch(ex => {
  // Exception occured while processing request
});

Note: Always insert documents with a id in order to use the realtime feature.

Query documents in database

const find = and(cond('title', '==', 'Title1'), cond('author', '==', 'Jon'));
db.get('COLLECTION_NAME').where(find)
  .skip(10).limit(10)
  .apply()
  .then(res => {
    if (res.status === 200) {
      // res.data contains the documents returned by the database
      console.log('Response:', res.data);
      return;
    }
  }).catch(ex => {
    // Exception occured while processing request
  });

Update documents in database

const find = and(cond('author', '==', 'Jon'));
db.update('COLLECTION_NAME').where(find)
  .set({ author: 'John' })
  .apply()
  .then(res => {
    if (res.status === 200) {
      // Document updated successfully
      return;
    }
  }).catch(ex => {
    // Exception occured while processing request
  });

Delete documents in database

const find = and(cond('author', '==', 'John'));
db.delete('COLLECTION_NAME').where(find)
  .apply()
  .then(res => {
    if (res.status === 200) {
      // Document deleted successfully
      return;
    }
  }).catch(ex => {
    // Exception occured while processing request
  });

Get real time updates

const onSnapshot  = (docs, type, find, changedDoc) => {
  if (type === 'initial') {
    console.log('Initial docs ', docs)
    return
  }
  console.log(docs, type, find, changedDoc)
}
 
const onError = (err) => {
  console.log('Monitor error', err)
}
 
let subscription = db.liveQuery('posts').where().subscribe(onSnapshot, onError) 
 
subscription.unsubscribe()

Upload file

const fileInput = document.querySelector('#your-file-input');
api.FileStore().uploadFile("/some-path", fileInput.files[0])
  .then(res => {
    if (res.status === 200) {
        console.log("File uploaded successfully");
    }
  }).catch(ex => {
    // Exception occured while uploading file
  })

Call functions directly (Function as a Service)

api.call('my-engine', 'my-func', { msg: 'Function as a Service is awesome!' }, 1000)
  .then(res => {
    if (res.status === 200) {
      console.log('Response: ', res.data)
    }
  }).catch(ex => {
    // Exception occured while processing request
  })

License

Copyright 2018 Noorain Panjwani

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

0.19.3

4 years ago

0.18.2

4 years ago

0.18.1

4 years ago

0.18.0

4 years ago

0.17.4

4 years ago

0.17.3

4 years ago

0.17.2

4 years ago

0.17.1

4 years ago

0.17.0

4 years ago

0.16.0

4 years ago

0.15.3

4 years ago

0.15.0

4 years ago

0.12.1

5 years ago

0.12.0

5 years ago

0.11.0

5 years ago

0.10.3

5 years ago

0.10.2

5 years ago

0.10.1

5 years ago

0.10.0

5 years ago

0.9.2

5 years ago

0.9.1

5 years ago

0.9.0

5 years ago

0.8.0

5 years ago

0.7.0

5 years ago

0.6.2

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.0

5 years ago

0.4.7

5 years ago

0.4.6

5 years ago

0.4.5

5 years ago

0.4.4

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.29

5 years ago

0.3.28

5 years ago

0.3.27

5 years ago

0.3.26

5 years ago

0.3.24

6 years ago

0.3.23

6 years ago

0.3.22

6 years ago

0.3.21

6 years ago

0.3.20

6 years ago

0.3.18

6 years ago

0.3.17

6 years ago

0.3.16

6 years ago

0.3.15

6 years ago

0.3.14

6 years ago

0.3.13

6 years ago

0.3.12

6 years ago

0.3.11

6 years ago

0.3.10

6 years ago

0.3.9

6 years ago

0.3.8

6 years ago

0.3.7

6 years ago

0.3.6

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.1.0

6 years ago