1.1.0 ā€¢ Published 5 years ago

jsonbox-js v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

A TypeScript friendly jsonbox.io API wrapper

šŸ  Homepage

Prerequisites

  • node >=10

Install

yarn add jsonbox-js

Usage

Example
import { jsonbox } from 'jsonbox-js'

type User = {
  name: string
  age: string
}

const { create, read, remove, update } = jsonbox<User>(process.env.BOX_ID)

// `create(data)` is return data. Able to pass array or object.
await create([{ name: 'john', age: 20 }, { name: 'doe', age: 30 }])
/* =>
[
  {"_id":"5d88e9a02bd38a0017ce2ac9","name":"doe","age":30,"_createdOn":"2019-09-23T15:49:52.943Z"},
  {"_id":"5d88e9a02bd38a0017ce2ac8","name":"john","age":20,"_createdOn":"2019-09-23T15:49:52.942Z"}
]
*/

// `read()` get all data. 
await read()
/* //=>
[
  {"_id":"5d88e9a02bd38a0017ce2ac9","name":"doe","age":30,"_createdOn":"2019-09-23T15:49:52.943Z"},
  {"_id":"5d88e9a02bd38a0017ce2ac8","name":"john","age":20,"_createdOn":"2019-09-23T15:49:52.942Z"}
]
*/

// `read(option)` get filtered data. 
await read({ limit: '1' })
/* //=>
[
  {"_id":"5d88e9a02bd38a0017ce2ac9","name":"doe","age":30,"_createdOn":"2019-09-23T15:49:52.943Z"}
]
*/

// `update(valueToUpdate)` is not return a value. Need to include `_id`
await update({ ...doe, age: 40 })
/* =>
[
  {
    "_id":" 5d88e9a02bd38a0017ce2ac9",
    "name": "doe",
    "age": 40,
    "_createdOn": "2019-09-23T15:49:52.943Z", 
    "_updatedOn": "2019-09-23T16:01:30.322Z"
  },
  {
    "_id": "5d88e9a02bd38a0017ce2ac8",
    "name": "john",
    "age": 20,
    "_createdOn": "2019-09-23T15:49:52.942Z"
  }
]
*/

// `remove(id)` is not return a value.
await remove(john._id)
/* =>
[
  {"_id":"5d88e9a02bd38a0017ce2ac9","name":"doe","age":30,"_createdOn":"2019-09-23T15:49:52.943Z"}
]
*/
Response is typed with JsonboxRecord.
import { jsonbox, JsonboxRecord } from 'jsonbox-js'

type User = {
  name: string
  age: string
}

type Props = {
  users: JsonboxRecord<User>[]
}

const { read } = jsonbox<User>(process.env.BOX_ID)

const Index: NextPage<Props> = ({ users }) => {
  return (
    <React.Fragment>
      {users.map(user => (
        <div key={user._id}>{user.name}</div>
      ))}
    </React.Fragment>
  )
}

Index.getInitialProps = async () => {
  const users = await read()
  return { users }
}

export default Index

Author

šŸ‘¤ ymkz

šŸ¤ Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

Show your support

Give a ā­ļø if this project helped you!

šŸ“ License

Copyright Ā© 2019 ymkz. This project is MIT licensed.


This README was generated with ā¤ļø by readme-md-generator

1.1.0

5 years ago

1.0.1

5 years ago