jsonbox-client
This is an isomorphic JavaScript module which includes all of the tools you need to help you interact with a jsonbox server (like jsonbox.io). It has first-class TypeScript support.
Install
npm install jsonbox-client
Use
Import
Your module specifier depends on your environment. Examples are included below.
import
(ES modules)
Environments: browser, Node.js >= 13.x.x using ES modules
;
Specifier examples:
- Browser (local):
./node_modules/jsonbox-client/dist/index.js
(or another path to where you've placed the module file) - Browser (CDN):
https://unpkg.com/jsonbox-client
(or another URL which resolves the module code) - Node:
jsonbox-client
require
(CommonJS)
Environments: Node.js >=12.x.x using CommonJS modules
;
Specifier examples:
- Node >13.x.x:
jsonbox-client
- Node 12.x.x:
jsonbox-client/commonjs
Imports
Jsonbox
This is the main import. It helps you to send and receive data from your jsonbox server. To get started, pass your configuration options to create a new Jsonbox
instance:
;
Signatures
new JsonboxboxId: string; // -> Jsonbox class instance new JsonboxboxId: string, ; // -> Jsonbox class instance
Examples
At minimum, you must provide a box ID:
;;
You can also provide some options:
; ;
If provided, the
apiKey
must be a valid UUID. To generate one, use thegenerateApiKey
import.
Jsonbox
methods
These are all async. In order to reduce boilerplate code in the documentation examples, they don't show the async context, but please take note of the use of
await
keywords.In the event of bad responses (no network connection /
status.ok === false
/ etc.), these methods willreject
with an error object containing an additionalresponse
property. The use oftry
/catch
is recommended when using these methods in order to handle the possibility of such errors.
;
create
Create one or more records (objects) in a jsonbox (an array of objects)
;
Signatures
createrecord: object, collection?: string; // -> Promise resolving the record with metadata createrecords: object, collection?: string; // -> Promise resolving the array of records with metadata
Examples
Create a single record:
;;
// result _id: '5e7ac65299ed160017dc2c81' name: 'Jacob' _createdOn: '2020-03-25T02:47:46.525Z'
Create a single record in a collection:
;;;
// result _id: '5e7ac67499ed160017dc2c82' name: 'Harry' house: 'Gryffindor' born: 1980 _createdOn: '2020-03-25T02:48:20.278Z'
Create multiple records at once in a collection:
;;;
// result _id: '5e7ac69799ed160017dc2c83' name: 'Ron' house: 'Gryffindor' born: 1980 _createdOn: '2020-03-25T02:48:55.777Z' _id: '5e7ac69799ed160017dc2c84' name: 'Draco' house: 'Slytherin' born: 1980 _createdOn: '2020-03-25T02:48:55.778Z' _id: '5e7ac69799ed160017dc2c85' name: 'Cedric' house: 'Hufflepuff' born: 1977 _createdOn: '2020-03-25T02:48:55.778Z'
update
Update a record
;
Signatures
updateid: string, record: object; // -> Promise resolving an object with a status message
Examples
;;;
// result message: 'Record updated.'
read
Read one or more records
;
Signatures
readid: string; // -> Promise resolving a record with metadata read; // -> Promise resolving an array of records with metadata read; // -> Promise resolving an array of records with metadata
Examples
Read a single record by its ID:
;;
// result _id: '5e7ac69799ed160017dc2c83' name: 'Ron' house: 'Gryffindor' born: 1980 _createdOn: '2020-03-25T02:48:55.777Z' _collection: 'wizards'
Read all records:
;
// result _id: '5e7ac69799ed160017dc2c84' name: 'Draco' house: 'Slytherin' born: 1980 _createdOn: '2020-03-25T02:48:55.778Z' _collection: 'wizards' _id: '5e7ac69799ed160017dc2c85' name: 'Cedric' house: 'Hufflepuff' born: 1977 died: 1995 _createdOn: '2020-03-25T02:48:55.778Z' _updatedOn: '2020-03-25T03:01:59.698Z' _collection: 'wizards' _id: '5e7ac69799ed160017dc2c83' name: 'Ron' house: 'Gryffindor' born: 1980 _createdOn: '2020-03-25T02:48:55.777Z' _collection: 'wizards' _id: '5e7ac67499ed160017dc2c82' name: 'Harry' house: 'Gryffindor' born: 1980 _createdOn: '2020-03-25T02:48:20.278Z' _collection: 'wizards' _id: '5e7ac65299ed160017dc2c81' name: 'Jacob' _createdOn: '2020-03-25T02:47:46.525Z'
Read all records in a collection:
;;
// result _id: '5e7ac69799ed160017dc2c84' name: 'Draco' house: 'Slytherin' born: 1980 _createdOn: '2020-03-25T02:48:55.778Z' _id: '5e7ac69799ed160017dc2c85' name: 'Cedric' house: 'Hufflepuff' born: 1977 died: 1995 _createdOn: '2020-03-25T02:48:55.778Z' _updatedOn: '2020-03-25T03:01:59.698Z' _id: '5e7ac69799ed160017dc2c83' name: 'Ron' house: 'Gryffindor' born: 1980 _createdOn: '2020-03-25T02:48:55.777Z' _id: '5e7ac67499ed160017dc2c82' name: 'Harry' house: 'Gryffindor' born: 1980 _createdOn: '2020-03-25T02:48:20.278Z'
Limited number of records:
;;
// result _id: '5e7ac69799ed160017dc2c84' name: 'Draco' house: 'Slytherin' born: 1980 _createdOn: '2020-03-25T02:48:55.778Z' _collection: 'wizards' _id: '5e7ac69799ed160017dc2c85' name: 'Cedric' house: 'Hufflepuff' born: 1977 died: 1995 _createdOn: '2020-03-25T02:48:55.778Z' _updatedOn: '2020-03-25T03:01:59.698Z' _collection: 'wizards'
Skip some records:
;;
// result _id: '5e7ac69799ed160017dc2c83' name: 'Ron' house: 'Gryffindor' born: 1980 _createdOn: '2020-03-25T02:48:55.777Z' _collection: 'wizards' _id: '5e7ac67499ed160017dc2c82' name: 'Harry' house: 'Gryffindor' born: 1980 _createdOn: '2020-03-25T02:48:20.278Z' _collection: 'wizards' _id: '5e7ac65299ed160017dc2c81' name: 'Jacob' _createdOn: '2020-03-25T02:47:46.525Z'
Sort records by field:
;;
// result _id: '5e7ac65299ed160017dc2c81' name: 'Jacob' _createdOn: '2020-03-25T02:47:46.525Z' _id: '5e7ac67499ed160017dc2c82' name: 'Harry' house: 'Gryffindor' born: 1980 _createdOn: '2020-03-25T02:48:20.278Z' _collection: 'wizards' _id: '5e7ac69799ed160017dc2c83' name: 'Ron' house: 'Gryffindor' born: 1980 _createdOn: '2020-03-25T02:48:55.777Z' _collection: 'wizards' _id: '5e7ac69799ed160017dc2c85' name: 'Cedric' house: 'Hufflepuff' born: 1977 died: 1995 _createdOn: '2020-03-25T02:48:55.778Z' _updatedOn: '2020-03-25T03:01:59.698Z' _collection: 'wizards' _id: '5e7ac69799ed160017dc2c84' name: 'Draco' house: 'Slytherin' born: 1980 _createdOn: '2020-03-25T02:48:55.778Z' _collection: 'wizards'
Filter records by value(s):
(See jsonbox#filtering for syntax details). For easier filter matching, use the provided imports combineFilters
and valueOf
.
;;
// result _id: '5e7ac69799ed160017dc2c84' name: 'Draco' house: 'Slytherin' born: 1980 _createdOn: '2020-03-25T02:48:55.778Z' _collection: 'wizards' _id: '5e7ac67499ed160017dc2c82' name: 'Harry' house: 'Gryffindor' born: 1980 _createdOn: '2020-03-25T02:48:20.278Z' _collection: 'wizards'
Combine read options:
;;
// result _id: '5e7ac67499ed160017dc2c82' name: 'Harry' house: 'Gryffindor' born: 1980 _createdOn: '2020-03-25T02:48:20.278Z' _collection: 'wizards' _id: '5e7ac69799ed160017dc2c83' name: 'Ron' house: 'Gryffindor' born: 1980 _createdOn: '2020-03-25T02:48:55.777Z' _collection: 'wizards'
delete
/ remove
Delete one or more records
jsonbox.delete
and jsonbox.remove
are the same method. jsonbox.remove
is offered because the delete
operator already exists in the global scope.
;
Signatures
removeid: string; // -> Promise resolving an object with a status message removeids: string; // -> Promise resolving an array of objects with properties related to individual delete requests remove; // -> Promise resolving an object with a status message
Examples
Delete a single record by its ID:
;;
// result message: 'Record removed.'
Delete multiple records by their IDs:
This actually makes multiple concurrent delete requests using Promise.allSettled
and returns the mapped results in an array. This is a unique call which does not reject
even on network failure. Check the results for the status of the individual delete requests.
;;
// result id: '5e7ac65299ed160017dc2c81' success: true id: '5e7ac69799ed160017dc2c84' success: true
Delete one or more records using a filter:
;;
// result message: '2 Records removed.'
meta
Read the metadata for your box
;
Signatures
meta; // -> Promise resolving an object with the box's metadata
Examples
;
// result _count: 5 _createdOn: '2020-03-25T02:47:46.525Z' _updatedOn: '2020-03-25T03:01:59.698Z'
combineFilters
and valueOf
These are functions to help you compose filters using natural language instead of having to remember the jsonbox-specific syntax.
;
Signatures
combineFilters...filters: string; // -> A combined, jsonbox-formatted filter string
// All of these return a jsonbox-formatted filter string valueOfkey: string.endsWithvalue: string; valueOfkey: string.includesvalue: string; valueOfkey: string.isvalue: boolean | number | string; valueOfkey: string.isGreaterThanvalue: number; valueOfkey: string.isGreaterThanOrEqualTovalue: number; valueOfkey: string.isLessThanvalue: number; valueOfkey: string.isLessThanOrEqualTovalue: number; valueOfkey: string.startsWithvalue: string;
Examples
Single filter:
; filter === 'house:Gryffindor'; // -> true
Combine multiple filters:
; filter === 'house:*in,name:*a*,born:>=1980'; // -> true ;;
// result _id: '5e7ac69799ed160017dc2c84' name: 'Draco' house: 'Slytherin' born: 1980 _createdOn: '2020-03-25T02:48:55.778Z' _collection: 'wizards'
generateApiKey
When using an API key, jsonbox requires it to be a valid UUID. This function generates a randomized, RFC 4122-compliant version-4 UUID.
;
Signatures
generateApiKey; // -> random UUID in the format 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
Examples
// apiKey 'fef67c1a-845a-4b7b-8a86-f6d70faadfd0'