|
1 |
| -# backend-js |
2 |
| -StaticBackend JavaScript client library |
| 1 | +# @staticbackend/js |
| 2 | +[StaticBackend](https://staticbackend.com)'s JavaScript client library. |
| 3 | + |
| 4 | +### Installation |
| 5 | + |
| 6 | +```shell |
| 7 | +$> npm install @staticbackend/js |
| 8 | +``` |
| 9 | + |
| 10 | +### Usage |
| 11 | + |
| 12 | +```javascript |
| 13 | +import { Backend } from "@staticbackend/js"; |
| 14 | + |
| 15 | +const bkn = new Backend("your-public-token", "na1"); |
| 16 | +``` |
| 17 | + |
| 18 | +**Parameters**: |
| 19 | + |
| 20 | +* Your public key |
| 21 | +* The region (we only supports `na1` or `dev` for now) |
| 22 | + |
| 23 | +**If using the development server**: |
| 24 | + |
| 25 | +You may initiate the client like this for development: |
| 26 | + |
| 27 | +```javascript |
| 28 | +const bkn = new Backend("anything", "dev"); |
| 29 | +``` |
| 30 | + |
| 31 | +When using the development server you don't need a valid public key. |
| 32 | + |
| 33 | +### Available helpers |
| 34 | + |
| 35 | +**register(email: string, password: string)** |
| 36 | + |
| 37 | +All you users will need a session token to perform CRUD operations against your |
| 38 | +database. *Note that it's possible to have public repository*. |
| 39 | + |
| 40 | +```javascript |
| 41 | +const registerButton = document.getElementById("registerButton"); |
| 42 | +registerButton.addEventListener("click", async (e) => { |
| 43 | + const result = await bkn.register("user@company.com", "their-password"); |
| 44 | + if (!result.ok) { |
| 45 | + // could not register that user |
| 46 | + console.error(result.content); |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + // you must use this token on all sub-sequent requests: |
| 51 | + const token = result.content; |
| 52 | + |
| 53 | + // Ideally you'd store that somewhere for that user to retrieve like |
| 54 | + // SessionStorage for instance. |
| 55 | +}); |
| 56 | +``` |
| 57 | + |
| 58 | +**login(email: string, password: string)** |
| 59 | + |
| 60 | +The `login` function is identical to the `register`. You'll receive a session |
| 61 | +token on successful login. |
| 62 | + |
| 63 | +```javascript |
| 64 | +const result = await bkn.login(email, pass); |
| 65 | +// handle result.ok == false or result.content contains their token. |
| 66 | +``` |
| 67 | + |
| 68 | +#### Database |
| 69 | + |
| 70 | +**create(token: string, repo: string, doc)** |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +```javascript |
| 75 | +createProjectButton.addEventListener("click", async (e) => { |
| 76 | + // thos values would come from <input> or something from your app |
| 77 | + const project = { |
| 78 | + name: "New project name", |
| 79 | + active: false, |
| 80 | + created: new Date() |
| 81 | + }; |
| 82 | + |
| 83 | + const result = await bkn.create(token, "projects", project); |
| 84 | + // handle !result.ok or created document in result.content |
| 85 | +}); |
| 86 | +``` |
| 87 | + |
| 88 | +**list(token: string, repo: string)** |
| 89 | + |
| 90 | +```javascript |
| 91 | +const result = await bkn.list(token, "projects"); |
| 92 | +// handle !result.ok or result.content is an object |
| 93 | +/* |
| 94 | +{ |
| 95 | + "page": 1, |
| 96 | + "total": 159, |
| 97 | + "results": [{ |
| 98 | + id: "an id", |
| 99 | + name: "New project name", |
| 100 | + active: false, |
| 101 | + created: "20201223T07:48:24" |
| 102 | + }] |
| 103 | +} |
| 104 | +*/ |
| 105 | +``` |
| 106 | + |
| 107 | +**getById(token: string, repo: string, id: string)** |
| 108 | + |
| 109 | +This return the document by ID. |
| 110 | + |
| 111 | +**query(token: string, repo: string, filters)** |
| 112 | + |
| 113 | +```javascript |
| 114 | +const filters = [ |
| 115 | + ["active", "==", false] |
| 116 | +]; |
| 117 | + |
| 118 | +const result = await bkn.query(token, "projects", filters) |
| 119 | +// handle !result.ok or result.content contains same as list(). |
| 120 | +``` |
| 121 | + |
| 122 | +The `filters` is an array or array having the following format: |
| 123 | + |
| 124 | +```javascript |
| 125 | +["field-name", "operator", value] |
| 126 | +``` |
| 127 | + |
| 128 | +The supported operators are: |
| 129 | + |
| 130 | +```javascript |
| 131 | +"==", "!=", ">", "<", ">=", "<=", "in", "!in" |
| 132 | +``` |
| 133 | + |
| 134 | +**update(token: string, repo: string, id: string, doc)** |
| 135 | + |
| 136 | +Update a document by its ID, and only for the properties in the `doc` parameter. |
| 137 | + |
| 138 | +**delete(token: string, repo: string, id: string)** |
| 139 | + |
| 140 | +Delete a document by its ID |
| 141 | + |
| 142 | +#### Upload file |
| 143 | + |
| 144 | +```html |
| 145 | +<form enctype="multipart/form-data"> |
| 146 | + <input type="file" name="file" /> |
| 147 | +</form> |
| 148 | +<p> |
| 149 | + <button id="upbtn">upload file</button> |
| 150 | +</p> |
| 151 | +``` |
| 152 | + |
| 153 | +```javascript |
| 154 | +const upbtn = document.getElementById("upbtn"); |
| 155 | +upbtn.addEventListener("click", async (e) => { |
| 156 | + // you can grab your form by ID or whatever |
| 157 | + const form = document.forms[0]; |
| 158 | + const result = await bkn.storeFile(token, form); |
| 159 | + if (!result.ok) { |
| 160 | + console.error(result.content); |
| 161 | + return; |
| 162 | + } |
| 163 | + |
| 164 | + console.log("file url", result.content); |
| 165 | +}) |
| 166 | +``` |
| 167 | + |
| 168 | +*When using the development server the URLs return will not be fully qualified |
| 169 | +domain name*. |
| 170 | + |
| 171 | +In production our CDN URL are returned. |
0 commit comments