Meteor.js(2)

0

"Meteor.js 라이브러리 이해를 하기"

/.meteor/packages

autopublish@1.0.7             # Publish all data to the clients (for prototyping)
insecure@1.0.7                # Allow all DB writes from clients (for prototyping)

Meteor 라이브러리 패키지들을 확인할 수 있는 폴더에 자동으로 몽고DB를 미니몽고DB로 프론트에서 활용할 수 있게 작업하는 코드이다.

말그대로 모든 데이터를 클라이언에 보내주고 클라이언에 DB를 작성할 수 있다는 설명이 있다.

지난번 만든 프로젝트에 이 모듈들을 지우면 DB작업을 프론트에서 할 수 없다.

이것을 Meteor 매서드로 해결가능하다!!!

https://guide.meteor.com/methods.html 참고

RPC프로토콜로 API,메서드 등을 제공한다. 메서드는 DB데이터 로딩 시스템으로
클라이언트에서 사용한다.

"you can define a Method on the server and its counterpart on the client, then call it with some data, write to the database, and get the return value in a callback. —the ability to simulate server-side actions on the client to make your app feel faster than it actually is."

위에 내용을 다시한번 숙지하자!

이제 Meteor 코드에 improving해보자

Api로. DB작업 후에 Meteor콜로 클라이언트에서 작업을 하였음

1.프론트에서 Meteor.Call(“method_name”)

Meteor.methods({
  "contacts.insert"({ name, email, imageUrl }) {
    // check(name, string); //에러 다루기 => meteor 함수 check로 문자열인지 확인, 클라이언트에서
    // check(email, string);
    // check(imageUrl, string);

    if (!name) {
      throw new Meteor.Error("Required name!!!");
    }
    return ContactCollection.insert({
      name,
      email,
      imageUrl,
      createdAt: new Date(),
    });
  },

2.매서드만들고 api에서 DB수정

  const removeContact = (event, _id) => {
    event.preventDefault(); // remove 버튼눌렀을때 안움직이게 => event객체 사용
    Meteor.call("contacts.remove", { contactId: _id });
  };

🚀 원리 이해

DDP라는 웹소켓으로 프론트에서 몽고 DB작업을 할 수 있고, 미니몽고는 프론트에서만으로 DB작업을 렌더링할 수 있게 도와준다 !

0개의 댓글