RESTでMySQLを操作する

JSON Serverのインストール


最近RESTという言葉をよく聞きます。
そもそもREST(ソフトウェアアーキテクチャのスタイル)とHTTP(プロトコルの名前)の関係が正確には分かっていませんが、
自分流に解釈すると、「RESTの考え方に従ってHTTP(GET POST PUT DELETE)を使う」ということになるのでしょうか??

RESTでMySQLを操作する
RESTスタイルでMySQLを操作する
プロトコルは規定しない

HTTPでMySQLを操作する
HTTPを使ってMySQLを操作するは
スタイルは規定しない

だから厳密にはこのページのタイトルは、「RESTに従ってHTTPでMySQLを操作する」が正しいのかもしれませんが、
回りくどいので「RESTでMySQLを操作する」にしました。



そもそもRESTとMySQLを調べ始めたきっかけは、ESP8266やESP32のWiFiを使ってMySQLを扱うことができれば
面白いかも....という興味からでした。

調べるうちにphp-crud-apiにたどり着き、さらに調べるうちにJSON Serverにたどり着きました。

esp8266やesp32でMySQLを扱いたいな....

どうやらRESTというものがあるみたいだ

php-crud-apiにたどり着く

esp8266でJSONを扱うにはどうするの??

ArduinoJsonライブラリにたどり着く

esp32(ESP-IDF)でJSONを扱うにはどうするの??

JSONコンポーネントにたどり着く

これらのライブラリやコンポーネントはどうやってテストするの??

JSON Serverにたどり着く

このWEBページは上記とは逆順にまとめています。



RESTでMySQLを操作する場合、データのやり取りは全てJSONフォーマットで行われます。
そこで、いきなりMySQLを操作する前に、こちらのJSON Serverをインストールし、JSONフォーマットの特徴とハンドリング方法を確認します。

JSON Serverは手軽にJSONフォーマットを応答してくれるツールで、正式なサーバーが完成するまでの
ダミーサーバー(公式ページではfake REST API)として使うことを想定しています。

JSON Serverのインストール方法は色々なところで公開されています。
今回は以下の手順でグローバルインストールしました。
$ sudo apt-get install npm

$ sudo npm install -g json-server
/usr/local/bin/json-server -> /usr/local/lib/node_modules/json-server/lib/cli/bin.js
/usr/local/lib
mqw json-server@0.15.1
  tqw body-parser@1.19.0
  x tqq bytes@3.1.0
  x tqq content-type@1.0.4
  x tqw debug@2.6.9
  x x mqq ms@2.0.0
  x tqq depd@1.1.2
  x tqw http-errors@1.7.2
  x x tqq inherits@2.0.3
  x x mqq toidentifier@1.0.0
  x tqw iconv-lite@0.4.24
  x x mqq safer-buffer@2.1.2
  x tqw on-finished@2.3.0
  x x mqq ee-first@1.1.1
  x tqq qs@6.7.0
  x tqw raw-body@2.4.0
  x x mqq unpipe@1.0.0
  x mqw type-is@1.6.18
  x   mqq media-typer@0.3.0
  tqw chalk@2.4.2
  x tqw ansi-styles@3.2.1
  x x mqw color-convert@1.9.3
  x x   mqq color-name@1.1.3
  x tqq escape-string-regexp@1.0.5
  x mqw supports-color@5.5.0
  x   mqq has-flag@3.0.0
  tqw compression@1.7.4
  x tqw accepts@1.3.7
  x x mqq negotiator@0.6.2
  x tqq bytes@3.0.0
  x tqw compressible@2.0.17
  x x mqq mime-db@1.42.0
  x tqq on-headers@1.0.2
  x tqq safe-buffer@5.1.2
  x mqq vary@1.1.2
  tqq connect-pause@0.1.1
  tqq cors@2.8.5
  tqw errorhandler@1.5.1
  x mqq escape-html@1.0.3
  tqw express@4.17.1
  x tqq array-flatten@1.1.1
  x tqq content-disposition@0.5.3
  x tqq cookie@0.4.0
  x tqq cookie-signature@1.0.6
  x tqq encodeurl@1.0.2
  x tqq etag@1.8.1
  x tqq finalhandler@1.1.2
  x tqq fresh@0.5.2
  x tqq merge-descriptors@1.0.1
  x tqq methods@1.1.2
  x tqq parseurl@1.3.3
  x tqq path-to-regexp@0.1.7
  x tqw proxy-addr@2.0.5
  x x tqq forwarded@0.1.2
  x x mqq ipaddr.js@1.9.0
  x tqq range-parser@1.2.1
  x tqw send@0.17.1
  x x tqq destroy@1.0.4
  x x tqq mime@1.6.0
  x x mqq ms@2.1.1
  x tqq serve-static@1.14.1
  x tqq setprototypeof@1.1.1
  x tqq statuses@1.5.0
  x mqq utils-merge@1.0.1
  tqw express-urlrewrite@1.2.0
  x mqw path-to-regexp@1.8.0
  x   mqq isarray@0.0.1
  tqw json-parse-helpfulerror@1.0.3
  x mqq jju@1.4.0
  tqq lodash@4.17.15
  tqq lodash-id@0.14.0
  tqw lowdb@1.0.0
  x tqq graceful-fs@4.2.3
  x tqq is-promise@2.1.0
  x tqq pify@3.0.0
  x mqq steno@0.4.4
  tqw method-override@3.0.0
  x mqq debug@3.1.0
  tqw morgan@1.9.1
  x mqq basic-auth@2.0.1
  tqq nanoid@2.1.8
  tqq object-assign@4.1.1
  tqw please-upgrade-node@3.2.0
  x mqq semver-compare@1.0.0
  tqq pluralize@8.0.0
  tqw request@2.88.0
  x tqq aws-sign2@0.7.0
  x tqq aws4@1.9.0
  x tqq caseless@0.12.0
  x tqw combined-stream@1.0.8
  x x mqq delayed-stream@1.0.0
  x tqq extend@3.0.2
  x tqq forever-agent@0.6.1
  x tqw form-data@2.3.3
  x x mqq asynckit@0.4.0
  x tqw har-validator@5.1.3
  x x tqw ajv@6.10.2
  x x x tqq fast-deep-equal@2.0.1
  x x x tqq fast-json-stable-stringify@2.1.0
  x x x tqq json-schema-traverse@0.4.1
  x x x mqw uri-js@4.2.2
  x x x   mqq punycode@2.1.1
  x x mqq har-schema@2.0.0
  x tqw http-signature@1.2.0
  x x tqq assert-plus@1.0.0
  x x tqw jsprim@1.4.1
  x x x tqq extsprintf@1.3.0
  x x x tqq json-schema@0.2.3
  x x x mqw verror@1.10.0
  x x x   mqq core-util-is@1.0.2
  x x mqw sshpk@1.16.1
  x x   tqq asn1@0.2.4
  x x   tqq bcrypt-pbkdf@1.0.2
  x x   tqq dashdash@1.14.1
  x x   tqq ecc-jsbn@0.1.2
  x x   tqq getpass@0.1.7
  x x   tqq jsbn@0.1.1
  x x   mqq tweetnacl@0.14.5
  x tqq is-typedarray@1.0.0
  x tqq isstream@0.1.2
  x tqq json-stringify-safe@5.0.1
  x tqq mime-types@2.1.25
  x tqq oauth-sign@0.9.0
  x tqq performance-now@2.1.0
  x tqq qs@6.5.2
  x tqw tough-cookie@2.4.3
  x x tqq psl@1.6.0
  x x mqq punycode@1.4.1
  x tqq tunnel-agent@0.6.0
  x mqq uuid@3.3.3
  tqq server-destroy@1.0.1
  tqw update-notifier@3.0.1
  x tqw boxen@3.2.0
  x x tqq ansi-align@3.0.0
  x x tqq camelcase@5.3.1
  x x tqq cli-boxes@2.2.0
  x x tqw term-size@1.2.0
  x x x mqw execa@0.7.0
  x x x   tqw cross-spawn@5.1.0
  x x x   x tqw lru-cache@4.1.5
  x x x   x x tqq pseudomap@1.0.2
  x x x   x x mqq yallist@2.1.2
  x x x   x tqw shebang-command@1.2.0
  x x x   x x mqq shebang-regex@1.0.0
  x x x   x mqw which@1.3.1
  x x x   x   mqq isexe@2.0.0
  x x x   tqq get-stream@3.0.0
  x x x   tqq is-stream@1.1.0
  x x x   tqw npm-run-path@2.0.2
  x x x   x mqq path-key@2.0.1
  x x x   tqq p-finally@1.0.0
  x x x   mqq strip-eof@1.0.0
  x x tqq type-fest@0.3.1
  x x mqw widest-line@2.0.1
  x x   mqw string-width@2.1.1
  x x     mqw strip-ansi@4.0.0
  x x       mqq ansi-regex@3.0.0
  x tqw configstore@4.0.0
  x x tqw dot-prop@4.2.0
  x x x mqq is-obj@1.0.1
  x x tqq make-dir@1.3.0
  x x tqw unique-string@1.0.0
  x x x mqq crypto-random-string@1.0.0
  x x mqw write-file-atomic@2.4.3
  x x   tqq imurmurhash@0.1.4
  x x   mqq signal-exit@3.0.2
  x tqq has-yarn@2.1.0
  x tqq import-lazy@2.1.0
  x tqw is-ci@2.0.0
  x x mqq ci-info@2.0.0
  x tqw is-installed-globally@0.1.0
  x x tqw global-dirs@0.1.1
  x x x mqq ini@1.3.5
  x x mqw is-path-inside@1.0.1
  x x   mqq path-is-inside@1.0.2
  x tqq is-npm@3.0.0
  x tqq is-yarn-global@0.3.0
  x tqw latest-version@5.1.0
  x x mqw package-json@6.5.0
  x x   tqw got@9.6.0
  x x   x tqq @sindresorhus/is@0.14.0
  x x   x tqw @szmarczak/http-timer@1.1.2
  x x   x x mqq defer-to-connect@1.1.1
  x x   x tqw cacheable-request@6.1.0
  x x   x x tqq clone-response@1.0.2
  x x   x x tqq get-stream@5.1.0
  x x   x x tqq http-cache-semantics@4.0.3
  x x   x x tqw keyv@3.1.0
  x x   x x x mqq json-buffer@3.0.0
  x x   x x tqq lowercase-keys@2.0.0
  x x   x x tqq normalize-url@4.5.0
  x x   x x mqq responselike@1.0.2
  x x   x tqq decompress-response@3.3.0
  x x   x tqq duplexer3@0.1.4
  x x   x tqw get-stream@4.1.0
  x x   x x mqw pump@3.0.0
  x x   x x   tqq end-of-stream@1.4.4
  x x   x x   mqw once@1.4.0
  x x   x x     mqq wrappy@1.0.2
  x x   x tqq lowercase-keys@1.0.1
  x x   x tqq mimic-response@1.0.1
  x x   x tqq p-cancelable@1.1.0
  x x   x tqq to-readable-stream@1.0.0
  x x   x mqw url-parse-lax@3.0.0
  x x   x   mqq prepend-http@2.0.0
  x x   tqw registry-auth-token@4.0.0
  x x   x mqw rc@1.2.8
  x x   x   tqq deep-extend@0.6.0
  x x   x   tqq minimist@1.2.0
  x x   x   mqq strip-json-comments@2.0.1
  x x   tqq registry-url@5.1.0
  x x   mqq semver@6.3.0
  x tqw semver-diff@2.1.0
  x x mqq semver@5.7.1
  x mqq xdg-basedir@3.0.0
  mqw yargs@14.2.2
    tqw cliui@5.0.0
    x tqw strip-ansi@5.2.0
    x x mqq ansi-regex@4.1.0
    x mqq wrap-ansi@5.1.0
    tqq decamelize@1.2.0
    tqw find-up@3.0.0
    x mqw locate-path@3.0.0
    x   tqw p-locate@3.0.0
    x   x mqw p-limit@2.2.1
    x   x   mqq p-try@2.2.0
    x   mqq path-exists@3.0.0
    tqq get-caller-file@2.0.5
    tqq require-directory@2.1.1
    tqq require-main-filename@2.0.0
    tqq set-blocking@2.0.0
    tqw string-width@3.1.0
    x tqq emoji-regex@7.0.3
    x mqq is-fullwidth-code-point@2.0.0
    tqq which-module@2.0.0
    tqq y18n@4.0.0
    mqq yargs-parser@15.0.0



JSON Serverの動作確認のためにdb.jsonを以下の内容で作成します。
{
  "todos": [
    {
      "id": 1,
      "title": "JSON ServerでダミーAPIを作成",
      "time":1351824120,
      "data":[1.00, 2.00, 3.00]
    },
    {
      "id": 2,
      "title": "Vue.jsで基本機能を実装",
      "time":1351824240,
      "data":[1.11, 2.22]
    }
  ]
}


JSON Serverを起動します。
ネットワーク越しにアクセスできるように起動時のオプションでhostを指定し、自分のIPアドレスを指定します。
このあたりのことはこちらに 詳しく紹介されています。
$ json-server --watch --host 192.168.10.43 db.json

  \{^_^}/ hi!

  Loading db.json
  Done

  Resources
  http://192.168.10.43:3000/todos

  Home
  http://192.168.10.43:3000

  Type s + enter at any time to create a snapshot of the database
  Watching...

ネットワーク上にある別のマシンから、curlを使って以下のコマンドを実行します。
$ curl http://192.168.10.43:3000/todos
[
  {
    "id": 1,
    "title": "JSON ServerでダミーAPIを作成",
    "time": 1351824120,
    "data": [
      1,
      2,
      3
    ]
  },
  {
    "id": 2,
    "title": "Vue.jsで基本機能を実装",
    "time": 1351824240,
    "data": [
      1.11,
      2.22
    ]
  }
]


$ curl http://192.168.10.43:3000/todos/2
{
  "id": 2,
  "title": "Vue.jsで基本機能を実装",
  "time": 1351824240,
  "data": [
    1.11,
    2.22
  ]
}

続く...