2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jsonbox.ioをJavaScriptから使う

Last updated at Posted at 2019-09-20

jsonを無料で読み書きできるサービス、便利そう!
ということで試してみました。

導入

console
% npm i -S jsonbox-node

BOX_IDを準備しておく

https://jsonbox.io/ にアクセスして、https://jsonbox.io/より後ろの値をコピーしておきます。
これも生成できたら良いのに…

実装

demo.js
const {JsonBox} = require('jsonbox-node')
const BOX_ID = 'box_id'; // コピーしたBOX_ID

(async () => {
  const jbn = await new JsonBox()

  // レコード作成
  const get = await jbn.create({name: 'Pickles', age: 18}, BOX_ID)
  console.table(get)

  // 更新
  await jbn.update({age: 43}, BOX_ID, get._id)

  // 全レコード取得
  const records = await jbn.read(BOX_ID)

  // 検索
  // const records = await jbn.read(BOX_ID, {id: get.id})
  console.table(records[0])

  // 1リクエストで全件削除
  const recordIds = await Promise.all(records.map(async el => await el._id))
  await jbn.deleteMany(BOX_ID, recordIds)

  // 1件ずつ削除
  // await Promise.all(records.map(async (el) => await jbn.delete(BOX_ID, el._id)))
})()

実行結果

更新時に nameを指定しなかったから消えてしまった…

console
% node demo.js
┌────────────┬────────────────────────────┐
│  (index)   │           Values           │
├────────────┼────────────────────────────┤
│    _id     │ '5d84848d9d1f330017f8f2f3' │
│    name    │         'Pickles'          │
│    age     │             18             │
│ _createdOn │ '2019-09-20T07:49:33.338Z' │
└────────────┴────────────────────────────┘
┌────────────┬────────────────────────────┐
│  (index)   │           Values           │
├────────────┼────────────────────────────┤
│    _id     │ '5d84848d9d1f330017f8f2f3' │
│    age     │             43             │
│ _createdOn │ '2019-09-20T07:49:33.338Z' │
│ _updatedOn │ '2019-09-20T07:49:33.835Z' │
└────────────┴────────────────────────────┘

雑感

検索とか細かくできる必要ないと思うので、試した範囲でも結構遊べる印象です。

参考

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?