{"id":13150,"date":"2026-10-30T09:06:23","date_gmt":"2026-10-30T00:06:23","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=13150"},"modified":"2026-10-30T11:59:17","modified_gmt":"2026-10-30T02:59:17","slug":"nextjs-sns-server-52","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/10\/30\/nextjs-sns-server-52\/","title":{"rendered":"[nextjs]SNS Server-52(myapp62)[DB \uc5b4\ub311\ud130\/DB Adapter]"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \uc5ec\ub7ec\uac00\uc9c0 \ub370\uc774\ud130\ubca0\uc774\uc2a4\ub97c \uc0ac\uc6a9\ud558\uae30 \uc704\ud574\uc11c DB \uc5b4\ub311\ud130\ub97c \ub9cc\ub4ed\ub2c8\ub2e4.<br>We create a DB adapter to work with various databases.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb DB\uc5b4\ub311\ud130\ub97c \uc704\ud574\uc11c \uc0ac\uc6a9\ub418\ub294 \ub370\uc774\ud130\ubca0\uc774\uc2a4\ub294 sqlite\uc640 mysql\uc785\ub2c8\ub2e4.<br>The databases used for the DB adapter are SQLite and MySQL.<\/p>\n\n\n\n<p>\ud83d\udcc1 \ud504\ub85c\uc81d\ud2b8 \uc2dc\uc791 \/ Project Start<\/p>\n\n\n\n<p>\u2714\ufe0f Nextjs \ud504\ub85c\uc81d\ud2b8 \uc124\uce58<br>Setting up a Next.js project<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npx create-next-app@latest<\/code><\/pre>\n\n\n\n<p>\ud83d\udcc1 \ubaa8\ub4c8 \uc124\uce58 \/ Module Installation<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:14px\"><code># db\nnpm install mysql2        \nnpm install better-sqlite3\n\n# type\nnpm i --save-dev @types\/better-sqlite3 <\/code><\/pre>\n\n\n\n<p>\ud83d\udcc1 .env.local<\/p>\n\n\n\n<p>\u2714\ufe0f \ub370\uc774\ud130\ubca0\uc774\uc2a4 \uc120\ud0dd \/ Select Database<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:14px\"><code>DB_TYPE=sqlite                              # mysql,sqlite\nDB_HOST=localhost\nDB_USER=mysns\nDB_PASSWORD=\"\"\nDB_DATABASE=test\nDOMAIN=aloy-horizon.duckdns.org             # only server\nNEXT_PUBLIC_DOMAIN=aloy-horizon.duckdns.org # client\nJWT_SECRET=your-secret-key<\/code><\/pre>\n\n\n\n<p>\ud83d\udcc1 \ud14c\uc774\ube14 \uc0dd\uc131 \/ Create table<\/p>\n\n\n\n<p>\u2714\ufe0f SQLite<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:13px\"><code>CREATE TABLE IF NOT EXISTS user (\n  id TEXT PRIMARY KEY,\n  name TEXT NOT NULL,\n  email TEXT NOT NULL\n);<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f Mysql<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:14px\"><code>CREATE TABLE IF NOT EXISTS `user` (\n  id VARCHAR(255) PRIMARY KEY,\n  name VARCHAR(255) NOT NULL,\n  email VARCHAR(255) NOT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;<\/code><\/pre>\n\n\n\n<p>\ud83d\udcc1 \ub77c\uc6b0\ud2b8 \/ route<\/p>\n\n\n\n<p>\u2714\ufe0f api\/users\/route.ts<\/p>\n\n\n\n<p>&#8212; list ,insert<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:14px\"><code>\/\/ app\/api\/users\/route.ts\nimport { NextRequest, NextResponse } from 'next\/server';\nimport { getDb } from '@\/lib\/db';\n\nexport async function GET() {\n  const db = getDb();\n  await db.init();\n  const users = await db.listUsers();\n  return NextResponse.json(users);\n}\n\nexport async function POST(req: NextRequest) {\n  const db = getDb();\n  await db.init();\n  const { id, name, email } = await req.json();\n  \n  const user = await db.createUser({ id, name, email });\n  return NextResponse.json(user);\n}<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f api\/users\/[id]\/route.ts<\/p>\n\n\n\n<p>&#8212; delete<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:14px\"><code>\/\/ app\/api\/users\/&#91;id]\/route.ts\nimport { NextResponse } from 'next\/server';\nimport { getDb } from '@\/lib\/db';\n\nexport async function DELETE(_: Request, { params }: { params: { id: string } }) {\n  const db = getDb();\n  await db.init();\n  await db.deleteUser(params.id);\n  return NextResponse.json({ ok: true });\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udcc1 \ud14c\uc2a4\ud2b8 \/ Test<\/p>\n\n\n\n<p>\u2714\ufe0f \ub370\uc774\ud130 \ubca0\uc774\uc2a4 \uc804\ud658 \/ Database Migration<\/p>\n\n\n\n<p>&#8212; \ub370\uc774\ud130\ubca0\uc774\uc2a4 \uc804\ud658\uc740 .env.local\ud30c\uc77c\uc758 DB_TYPE\uc744 mysql\uc774\ub098 sqlite\ub85c \uc804\ud658\ud558\uace0 \uc11c\ubc84 \uc7ac\uc2dc\uc791\ud558\uba74 \ub429\ub2c8\ub2e4.<br>To switch databases, change the <code>DB_TYPE<\/code> in the <code>.env.local<\/code> file to either <code>mysql<\/code> or <code>sqlite<\/code>, then restart the server.<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:14px\"><code>DB_TYPE=                                   # mysql,sqlite\nDB_HOST=localhost\nDB_USER=mysns\nDB_PASSWORD=\"\"\nDB_DATABASE=test\nDOMAIN=aloy-horizon.duckdns.org             # only server\nNEXT_PUBLIC_DOMAIN=aloy-horizon.duckdns.org # client\nJWT_SECRET=your-secret-key\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ub370\uc774\ud130 \uc785\ub825 \/ Data Entry<\/p>\n\n\n\n<p>&#8212; \ud130\ubbf8\ub110\uc5d0\uc11c \uc2e4\ud589\ud558\uba74 \ub429\ub2c8\ub2e4.<br>You can run it from the terminal.<\/p>\n\n\n\n<p>1)mysql<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:14px\"><code># mysql\ncurl -X POST https:\/\/aloy-horizon.duckdns.org\/api\/users \\\n  -H \"Content-Type: application\/json\" \\\n  -d '{\"id\":\"u1\",\"name\":\"Hello\",\"email\":\"test@test.com\"}' \n\ncurl -X POST https:\/\/aloy-horizon.duckdns.org\/api\/users \\\n  -H \"Content-Type: application\/json\" \\\n  -d '{\"id\":\"u2\",\"name\":\"Hello mysql\",\"email\":\"test@test.com\"}' <\/code><\/pre>\n\n\n\n<p>2)sqlite<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:14px\"><code># sqlite\ncurl -X POST https:\/\/aloy-horizon.duckdns.org\/api\/users \\\n  -H \"Content-Type: application\/json\" \\\n  -d '{\"id\":\"u1\",\"name\":\"Hello sqlite\",\"email\":\"test@test.com\"}'<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f sqlite<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:14px\"><code>sqlite&gt; .tables\nuser\nsqlite&gt; .schema\nCREATE TABLE user (\n  id TEXT PRIMARY KEY,\n  name TEXT NOT NULL,\n  email TEXT NOT NULL\n);\nsqlite&gt; select * from user;\nu1|Hello sqlite|test@test.com\nsqlite&gt; <\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f mysql<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:14px\"><code>mysql&gt; select * from user;\n+----+-------------+---------------+\n| id | name        | email         |\n+----+-------------+---------------+\n| u1 | Hello       | test@test.com |\n| u2 | Hello mysql | test@test.com |\n+----+-------------+---------------+\n2 rows in set (0.001 sec)\n\nmysql&gt; <\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"800\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/10\/adapter-jpg.jpg\" alt=\"\" class=\"wp-image-13182\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/10\/adapter-jpg.jpg 800w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/10\/adapter-jpg-300x300.jpg 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/10\/adapter-jpg-150x150.jpg 150w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/10\/adapter-jpg-768x768.jpg 768w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2026\/10\/adapter-jpg-400x400.jpg 400w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ub9c8\ud0dc\ubcf5\uc74c 21\uc7a5 22\uc808 \/ Matthew 21: 22<\/p>\n\n\n\n<p>&#8220;\ub108\ud76c\uac00 \uae30\ub3c4\ud560 \ub54c\uc5d0 \ubb34\uc5c7\uc774\ub4e0\uc9c0 \ubbff\uace0 \uad6c\ud558\ub294 \uac83\uc740 \ub2e4 \ubc1b\uc73c\ub9ac\ub77c \ud558\uc2dc\ub2c8\ub77c&#8221;<\/p>\n\n\n\n<p>&#8220;And everything you ask in prayer, believing, you will receive.&#8221;<\/p>\n<\/blockquote>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \uc5ec\ub7ec\uac00\uc9c0 \ub370\uc774\ud130\ubca0\uc774\uc2a4\ub97c \uc0ac\uc6a9\ud558\uae30 \uc704\ud574\uc11c DB \uc5b4\ub311\ud130\ub97c \ub9cc\ub4ed\ub2c8\ub2e4.We create a DB adapter to work with various databases. \ud83d\udc49\ud83c\udffb DB\uc5b4\ub311\ud130\ub97c \uc704\ud574\uc11c \uc0ac\uc6a9\ub418\ub294 \ub370\uc774\ud130\ubca0\uc774\uc2a4\ub294 sqlite\uc640 mysql\uc785\ub2c8\ub2e4.The databases used for the DB adapter are SQLite and MySQL. \ud83d\udcc1 \ud504\ub85c\uc81d\ud2b8 \uc2dc\uc791 \/ Project Start \u2714\ufe0f Nextjs \ud504\ub85c\uc81d\ud2b8 \uc124\uce58Setting up a Next.js project \ud83d\udcc1 \ubaa8\ub4c8 \uc124\uce58 \/ Module Installation \ud83d\udcc1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":13182,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,27,1,7],"tags":[],"class_list":["post-13150","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","category-nextjs","category-uncategorized","category-website","has-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/13150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=13150"}],"version-history":[{"count":33,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/13150\/revisions"}],"predecessor-version":[{"id":13185,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/13150\/revisions\/13185"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media\/13182"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=13150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=13150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=13150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}