<img src="https://www.vocsel.com/cube.svg" width="100">
# vocsel.com

## JavaScript API

[![NPM Version][npm-version-image]][npm-url]
[![NPM Install Size][npm-install-size-image]][npm-install-size-url]
[![NPM Downloads][npm-downloads-image]][npm-downloads-url]

## Installation

```sh
  npm i vocsel-api --save
```

OR

```sh
  yarn add vocsel-api
```

## License

MIT

## API

### Authorization

```js
import VocselApi from "vocsel-api/dist/Api"

const vocselClient = await VocselApi.auth({
  projectId: <PROJECT_ID>,
  auth: { ... },
})
```

Currently we support following types of authorization:

1. Private (secured access with specified abilities)
2. Public (unsecured access with no abilities)

##### Private authorization

1. Using email and password

```js
auth: {
  email: <EMAIL>,
  password: <PASSWORD>,
}
```

2. Using Bearer JWT-token from https://vocsel.com/auth/login API call

```js
auth: {
  token: <TOKEN>
}
```

3. Using User Public Key

```js
auth: {
  publicKey: <PUBLIC_KEY>
}
```

##### Public authorization

```js
import VocselApi from "vocsel-api/dist/Api"

const vocselClient = await VocselApi.auth({
  projectId: <PROJECT_ID>,
  auth: {
    publicKey: <PUBLIC_KEY> // Optional
  },
})
```

## API

### Database API

#### Key-Value Database

##### Set

```js
/**
 * Function that sets key-value
 * @param    {String} key
 * @param    {String} value
 * @return   {Promise<Void>}
 */
await vocselClient.db.set(key, value);
```

##### Get

```js
/**
 * Function that gets a value by a key
 * @param    {String} key
 * @return   {Promise<String>}
 */
const value = await vocselClient.db.get(key);
```

#### Document Database

Mongodb compatible API

##### Create document

```js
/**
* Function that creates a document model
* @param    {String} name
* @return   {Promise<Document>}
*/
const doc = await vocselClient.db.useDocument(name)

/**
* Function that creates a document
* @param    {Boolean} force  - flushed existing document first (false, by default)
* @return   {Void}
*/
await doc.create({ reset: <Boolean> })
```

##### Insert data into the document

```js
const doc = await vocselClient.db.useDocument("test-document");

const data = {
  userName: "Johny",
  userAge: 48,
};

await doc.insert(data);
```

##### Select data from document

```js
const doc = await vocselClient.db.useDocument("test-document");

const request = {
  userName: "Johny",
};

const users = await doc.findMany(request);
// or
const user = await doc.findOne(request);
```

#### Events

Emit and subscribe on events

##### Subscribe on event

```js
await api.event.on(eventName, message => {
  expect(message).to.equal(eventMessage);
});
```

##### Emit an event

```js
await api.event.emit(eventName, eventMessage);
```

[npm-downloads-image]: https://badgen.net/npm/dm/vocsel-api
[npm-downloads-url]: https://npmcharts.com/compare/vocsel-api?minimal=true
[npm-install-size-image]: https://badgen.net/packagephobia/install/vocsel-api
[npm-install-size-url]: https://packagephobia.com/result?p=vocsel-api
[npm-url]: https://npmjs.org/package/vocsel-api
[npm-version-image]: https://badgen.net/npm/v/vocsel-api
