Search code examples
mongodbstronglooploopback

How to make a post API for mongodb with loopback


I am newbie with mongodb and loopback. I want to send and save data from my app to database. how can I do that?

Update shops model:

{
  "shopname": "string",
  "tel": "string",
  "latlng": "string",
  "address": "string",
  "id": "string",
  "personId": "string"
}

CURL:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ 
   "shopname": "spring", \ 
   "tel": "12345678", \ 
   "latlng": "52.1106986,21.7768998", \ 
   "address": "05-319 Skwarne, Poland" \ 
 }' 'http://localhost:3000/api/shops'

Now what should I write in shops.js to give an api for using in app to send data to database ?

'use strict';

    module.exports = function(Shops) {

    };

Solution

  • you should have provided more info about the steps you have already done. let me begin with the first step:

    1. download and install mongodb on your server: link

    2. after running mongodb, add your desired database info to datasources.json file. e.g.

      {
          "db": {
          "name": "db",
          "connector": "memory"
          },
          "shopDB": {
          "host": "localhost",
          "port": 27017,
          "url": "mongodb://localhost:27017/shopDB",
          "database": "shopDB",
          "password": "",
          "name": "shopDB",
          "user": "",
          "connector": "mongodb"
          }
      }
      
    3. add loopback-connector-mongodb to your project via npm.

    4. now define your model(you can utilize loopback's user friendly command line interface to do so. call command "slc loopback:model" in your projects root folder)

    5. after you finish the step 4, loopback will create 2files for you: shop.js and shop.json => these files are located in your projectFolder/common/models/ directory. note that it's a good practice to follow the loopback's convention in naming models and name your model in singular form(shop). (it uses the plural forms of the model names in other parts of the project). your shop.json should look like the below code:

      {
          "name": "shop",
          "plural": "shops",
          "base": "PersistedModel",
          "idInjection": true,
          "options": {
              "validateUpsert": true
          },
          "properties": {
              "shopname": {
                  "type": "string",
                  "required": true
              },
              "tel": {
                  "type": "string",
                  "required": true
              },
              "latlng": {
                  "type": "string",
                  "required": true
              },
              "address": {
                  "type": "string"
              },
              "personId": {
                  "type": "string",
                  "required": true
              }
          },
          "validations": [],
          "relations": {},
          "acls": [],
          "methods": {}
      }
      
    6. now you can post your shop json to http://localhost:3000/api/shops/ . note that our shop model inherits from PersistedModel base model and has some built-in functions to do crud operations. and if you want just create some shop instances in your db, you won't need to add anything to your shop.js file!