Giter Site home page Giter Site logo

dynamic-model-with-a-schema's Introduction

https://gist.github.com/huynhsamha/4dcf00a1fba96ae7f186b606b33b7e9c

.env

MONGO_USER=<User Name>
MONGO_PW=<Password>
MONGO_HOST=<Host Domain>
MONGO_PORT=<DB Port>
MONGO_DB=<Database Name>

MONGO_URI="mongodb://${MONGO_USER}:${MONGO_PW}@${MONGO_HOST}:${MONGO_PORT}/${MONGO_DB}?authSource=admin"

Run

$ npm run local:up
$ npm start

Test

Channel ID는 path param으로 처리하고,
User ID는 query string으로 다루었습니다.

$ curl -X POST http://localhost:8080/channel/1?userId=1 \
  -H 'Content-Type: application/json' \
  -d '{"message": "message1"}'

$ curl -X POST http://localhost:8080/channel/1?userId=2 \
  -H 'Content-Type: application/json' \
  -d '{"message": "message2"}'

$ curl -X POST http://localhost:8080/channel/2?userId=3 \
  -H 'Content-Type: application/json' \
  -d '{"message": "message3"}'

$ curl -X POST http://localhost:8080/channel/2?userId=3 \
  -H 'Content-Type: application/json' \
  -d '{"message": "message4"}'

Result

> use test

test> db.getCollectionNames()
< [ 'channel1_user1', 'channel2_user3', 'channel1_user2' ]

test> db.channel1_user1.find()
< {
  _id: ObjectId("64c9a68aa266b5453a2a2f32"),
  message: 'message1',
  timeline: 2023-08-02T00:42:50.989Z,
  __v: 0
}

test> db.channel1_user2.find()
< {
  _id: ObjectId("64c9a69aa266b5453a2a2f34"),
  message: 'message2',
  timeline: 2023-08-02T00:43:06.235Z,
  __v: 0
}

test> db.channel2_user3.find()
< {
  _id: ObjectId("64c9a69ea266b5453a2a2f36"),
  message: 'message3',
  timeline: 2023-08-02T00:43:10.272Z,
  __v: 0
}
{
  _id: ObjectId("64c9a6a3a266b5453a2a2f38"),
  message: 'message4',
  timeline: 2023-08-02T00:43:15.625Z,
  __v: 0
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.