This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
7,196 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.cache/ | ||
node_modules/ | ||
lib/ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,171 @@ | ||
# backend-js | ||
StaticBackend JavaScript client library | ||
# @staticbackend/js | ||
[StaticBackend](https://staticbackend.com)'s JavaScript client library. | ||
|
||
### Installation | ||
|
||
```shell | ||
$> npm install @staticbackend/js | ||
``` | ||
|
||
### Usage | ||
|
||
```javascript | ||
import { Backend } from "@staticbackend/js"; | ||
|
||
const bkn = new Backend("your-public-token", "na1"); | ||
``` | ||
|
||
**Parameters**: | ||
|
||
* Your public key | ||
* The region (we only supports `na1` or `dev` for now) | ||
|
||
**If using the development server**: | ||
|
||
You may initiate the client like this for development: | ||
|
||
```javascript | ||
const bkn = new Backend("anything", "dev"); | ||
``` | ||
|
||
When using the development server you don't need a valid public key. | ||
|
||
### Available helpers | ||
|
||
**register(email: string, password: string)** | ||
|
||
All you users will need a session token to perform CRUD operations against your | ||
database. *Note that it's possible to have public repository*. | ||
|
||
```javascript | ||
const registerButton = document.getElementById("registerButton"); | ||
registerButton.addEventListener("click", async (e) => { | ||
const result = await bkn.register("[email protected]", "their-password"); | ||
if (!result.ok) { | ||
// could not register that user | ||
console.error(result.content); | ||
return | ||
} | ||
|
||
// you must use this token on all sub-sequent requests: | ||
const token = result.content; | ||
|
||
// Ideally you'd store that somewhere for that user to retrieve like | ||
// SessionStorage for instance. | ||
}); | ||
``` | ||
|
||
**login(email: string, password: string)** | ||
|
||
The `login` function is identical to the `register`. You'll receive a session | ||
token on successful login. | ||
|
||
```javascript | ||
const result = await bkn.login(email, pass); | ||
// handle result.ok == false or result.content contains their token. | ||
``` | ||
|
||
#### Database | ||
|
||
**create(token: string, repo: string, doc)** | ||
|
||
|
||
|
||
```javascript | ||
createProjectButton.addEventListener("click", async (e) => { | ||
// thos values would come from <input> or something from your app | ||
const project = { | ||
name: "New project name", | ||
active: false, | ||
created: new Date() | ||
}; | ||
|
||
const result = await bkn.create(token, "projects", project); | ||
// handle !result.ok or created document in result.content | ||
}); | ||
``` | ||
|
||
**list(token: string, repo: string)** | ||
|
||
```javascript | ||
const result = await bkn.list(token, "projects"); | ||
// handle !result.ok or result.content is an object | ||
/* | ||
{ | ||
"page": 1, | ||
"total": 159, | ||
"results": [{ | ||
id: "an id", | ||
name: "New project name", | ||
active: false, | ||
created: "20201223T07:48:24" | ||
}] | ||
} | ||
*/ | ||
``` | ||
|
||
**getById(token: string, repo: string, id: string)** | ||
|
||
This return the document by ID. | ||
|
||
**query(token: string, repo: string, filters)** | ||
|
||
```javascript | ||
const filters = [ | ||
["active", "==", false] | ||
]; | ||
|
||
const result = await bkn.query(token, "projects", filters) | ||
// handle !result.ok or result.content contains same as list(). | ||
``` | ||
|
||
The `filters` is an array or array having the following format: | ||
|
||
```javascript | ||
["field-name", "operator", value] | ||
``` | ||
|
||
The supported operators are: | ||
|
||
```javascript | ||
"==", "!=", ">", "<", ">=", "<=", "in", "!in" | ||
``` | ||
|
||
**update(token: string, repo: string, id: string, doc)** | ||
|
||
Update a document by its ID, and only for the properties in the `doc` parameter. | ||
|
||
**delete(token: string, repo: string, id: string)** | ||
|
||
Delete a document by its ID | ||
|
||
#### Upload file | ||
|
||
```html | ||
<form enctype="multipart/form-data"> | ||
<input type="file" name="file" /> | ||
</form> | ||
<p> | ||
<button id="upbtn">upload file</button> | ||
</p> | ||
``` | ||
|
||
```javascript | ||
const upbtn = document.getElementById("upbtn"); | ||
upbtn.addEventListener("click", async (e) => { | ||
// you can grab your form by ID or whatever | ||
const form = document.forms[0]; | ||
const result = await bkn.storeFile(token, form); | ||
if (!result.ok) { | ||
console.error(result.content); | ||
return; | ||
} | ||
|
||
console.log("file url", result.content); | ||
}) | ||
``` | ||
|
||
*When using the development server the URLs return will not be fully qualified | ||
domain name*. | ||
|
||
In production our CDN URL are returned. |
Oops, something went wrong.