{"id":359,"date":"2017-04-22T12:05:28","date_gmt":"2017-04-22T03:05:28","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2017\/04\/22\/mysqljstest"},"modified":"2017-04-22T12:05:28","modified_gmt":"2017-04-22T03:05:28","slug":"mysqljstest","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2017\/04\/22\/mysqljstest\/","title":{"rendered":"mysqljs\u3068\u30c6\u30b9\u30c8\u306b\u3064\u3044\u3066"},"content":{"rendered":"

\u6700\u8fd1Lambda\uff0bnode\u3092\u9811\u5f35\u3063\u3066\u3044\u308b\u3001\u9577\u3067\u3059\u3002\u6700\u8fd1mysqljs\u3068\u8a00\u3046\u3082\u306e\u3092\u4f7f\u7528\u3057\u305f\u306e\u3067\u3001\u4f7f\u3044\u65b9\u3068\u30c6\u30b9\u30c8\u306b\u3064\u3044\u3066\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

\u307e\u305a\u306f\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/p>\n

npm install mysql<\/code><\/pre>\n

\u4f7f\u3044\u65b9<\/p>\n

import mysql from 'mysql';\n\nconst connection = mysql.createConnection({\n   host: 'localhost',\n   user: 'user',\n   password: 'password',\n   database: 'database'\n});\n\nconnection.connect((error) => {\n  if (error) {\n    console.error('error connecting: ' + error.stack);\n    return;\n  }\n  console.log('connected as id ' + connection.threadId);\n});\n\nconnection.query(SELECT * FROM table, (error, results, fields) => {\n  if (error) {\n    console.log(error);\n    return;\n  } else {\n    console.log('results[0]);\n  }\n});\n<\/code><\/pre>\n

mysql.createConnection\u306e\u5024\u306f\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u5909\u66f4\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n

\u305d\u3057\u3066\u3001\u30c6\u30b9\u30c8\u3067\u3059\u304c\u30c6\u30b9\u30c8\u3067\u5b9f\u969b\u306eDB\u306b\u5bfe\u3057\u3066\u30af\u30a8\u30ea\u5b9f\u884c\u3057\u3066\u3057\u307e\u3046\u306e\u306f\u3088\u308d\u3057\u304f\u306a\u3044\u306e\u3067\u3001\u4eca\u56de\u300cmockery\u300d\u3092\u4f7f\u7528\u3057\u3001mysqljs\u3092\u30e2\u30c3\u30af\u3057\u3066\u30c6\u30b9\u30c8\u3092\u884c\u306a\u3046\u5f62\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n

\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/p>\n

npm install mockery<\/code><\/pre>\n

setup.js<\/p>\n

import mockery from 'mockery';\nimport m from 'module';\n\nconst connectionMock = {\n  connect: () => {\n  },\n  query: (query) => {\n    let result;\n    switch (query) {\n      case 'SELECT * FROM  table;':\n        result = 'result'\n        break;\n      default:\n    }\n    return result;\n  }\n}\n\nconst mysqlMock = {\n   createConnection: () => {\n     return connectionMock;\n   }\n};\n\nmockery.enable();\n\nmockery.warnOnUnregistered(false);\n\nmockery.registerMock('mysql', mysqlMock);\n\nconst originalLoader = m._load;\nm._load = (request, parent, isMain) => {\n  return originalLoader(request, parent, isMain);\n}<\/code><\/pre>\n

\u4e0a\u8a18\u306ejs\u3092package.json\u306b\u8ffd\u8a18\u3057\u3001\u30c6\u30b9\u30c8\u306e\u969b\u306b\u5b9f\u884c\u3055\u308c\u308b\u3088\u3046\u306b\u3059\u308c\u3070OK\u3067\u3059\u3002<\/p>\n

\u3053\u3046\u3044\u3063\u305f\u5b9f\u88c5\u306f\u521d\u3081\u3066\u3060\u3063\u305f\u306e\u3067\u3059\u304c\u3001mockery\u306f\u3044\u308d\u3093\u306a\u5834\u9762\u3067\u5f79\u306b\u305f\u3061\u305d\u3046\u3060\u306a\u3068\u611f\u3058\u307e\u3057\u305f(\u309c\u309c)<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6700\u8fd1Lambda\uff0bnode\u3092\u9811\u5f35\u3063\u3066\u3044\u308b\u3001\u9577\u3067\u3059\u3002\u6700\u8fd1mysqljs\u3068\u8a00\u3046\u3082\u306e\u3092\u4f7f\u7528\u3057\u305f\u306e\u3067\u3001\u4f7f\u3044\u65b9\u3068\u30c6\u30b9\u30c8\u306b\u3064\u3044\u3066\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 \u307e\u305a\u306f\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb npm install mysql \u4f7f\u3044\u65b9 import mysql from 'mysql'; const connection = mysql.createConnection({ host: 'localhos […]<\/p>\n","protected":false},"author":1,"featured_media":826,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/359"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=359"}],"version-history":[{"count":0,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/359\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media\/826"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}