한컴AI 2기

[스나이퍼팩토리] 한컴AI 2기 - npm, mysql, 몽고DB CRUD

싱커 2025. 9. 3. 15:36

npm init

npm init을 하면, 패키지를 구성하기 위한 속성들을 커맨드라인에서 입력하게된다.

패키지명, 버전, 설명, 진입점, 테스트커맨드, 깃주소, 키워드, 작성자, 라이센스 등을 설정할 수 있다.

C:\한컴개발\노드js>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (노드js) hancom-node
version: (1.0.0)
description: 한컴 AI 2기 과정 노드 패키지
entry point: (index.js)
test command: hancom test
git repository: https://github.com/xinKer-Kim/hancom-node
keywords: 한컴
author: XinKer-Kim
license: (ISC)                                                                                                                
About to write to C:\한컴개발\노드js\package.json:

{
  "name": "hancom-node",
  "version": "1.0.0",
  "description": "한컴 AI 2기 과정 노드 패키지",
  "main": "index.js",
  "scripts": {
    "test": "hancom test",
    "start": "node server.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/xinKer-Kim/hancom-node.git"
  },
  "keywords": [
    "한컴"
  ],
  "author": "XinKer-Kim",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/xinKer-Kim/hancom-node/issues"
  },
  "homepage": "https://github.com/xinKer-Kim/hancom-node#readme"
}


Is this OK? (yes) y

완성된 패키지 내용은 json형태로 정리된다.

mysql @ CLI

mysql> create table nodejs.users(
    -> id int not null auto_increment,
    -> name varchar(20) not null,
    -> age int unsigned not null,
    -> married tinyint not null,
    -> comment text null,
    -> created_at datetime not null default now(),
    -> primary key(id),
    -> unique index name_unique (name ASC))
    -> comment = '사용자 정보'
    -> engine = InnoDB;

 

배포서버에서 mysql을 조작할 때 CLI에서 작업을 하게되는데, 한 줄 한 줄 입력하는 것이 다른 툴로 작업할 때와는 많이 달라 어색했다.


몽고DB

admin 설정하기

test> use admin
switched to db admin
admin> db.createUser({user:"xinker", pwd: "0000", roles: ["root"]})
{ ok: 1 }

use admin으로 스위치한 후, createUser를 사용하여 유저를 만든다.

이후, mongoshadmin –u 이름–p 비밀번호 형태로 접속하면 된다.

users에 데이터 추가

몽고DB에서는 테이블 대신 컬렉션이라는 용어를 사용한다.

데이터를 json 포맷으로 insert하면 컬렉션이 자동 생성된다.

 

컬렉션 생성하기 

admin> use nodejs
switched to db nodejs
nodejs> show dbs
admin   172.00 KiB
config   84.00 KiB
local    40.00 KiB
nodejs> db.createCollection("test1")
{ ok: 1 }
nodejs> db.createCollection("test2")
{ ok: 1 }
nodejs> show collections
test1
test2

 

Create : insertOne 메서드

nodejs> db.test1.insertOne({name:'json',age:20});
{
  acknowledged: true,
  insertedId: ObjectId('68afbc35f948db2cc8eec4aa')
}
nodejs> db.test2.insertOne({name:'kong',age:22});
{
  acknowledged: true,
  insertedId: ObjectId('68afbc49f948db2cc8eec4ab')
}

 

Read : find로 모두조회, 조건조회

nodejs> db.test2.find({})
[
  { _id: ObjectId('68afbc49f948db2cc8eec4ab'), name: 'kong', age: 22 }
]

nodejs> db.test2.find({age:22})
[
  { _id: ObjectId('68afbc49f948db2cc8eec4ab'), name: 'kong', age: 22 }
]

Update : updateOne 메서드,  $set

nodejs> db.test1.updateOne(
... {name:'json'},
... {$set:{name:'LimRaynor'}}
... );
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}
nodejs> db.test1.find({});
[
  {
    _id: ObjectId('68afbc35f948db2cc8eec4aa'),
    name: 'LimRaynor',
    age: 20
  }
]

Delete : delteOne 메서드

첫번째 인수로 삭제할 대상 조건 제공

성공시 삭제된 개수가 반환됨


본 후기는 [한글과컴퓨터x한국생산성본부x스나이퍼팩토리] 한컴 AI 아카데미 2기 (B-log) 리뷰로 작성 되었습니다.