iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 12
0
自我挑戰組

Some thing with Reason系列 第 12

Express-Mongodb

  • 分享至 

  • xImage
  •  

MongoDB

之前有提過如何使用 NPM 上的 package

今天來試試看串接一下 MongoDB

做最簡單的 CRUD

Install

  $ npm install mongodb

Nodejs version

const express = require('express');
const userRouter = require('./routes/usersRoutes');

const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
const dbName = 'myproject';

MongoClient.connect(url, function(err, client) {
  const db = client.db(dbName);
  const collection = db.collection('documents');
  collection.insertMany([
    {a : 1}, {a : 2}, {a : 3}
  ], function(err, result) {
    console.log("Inserted 3 documents into the collection");
    console.log(result);
  });
  client.close();
});

const app = express();

const port = 5000;

app.use('/user', userRouter);

app.get('/', (req, res) => res.send('Hello World!'));

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

可以看到在 mongodb 中有一個 MongoClient 他還有一個 connect 的 function

connect 有兩個參數 (string, (err, client) => unit)

以此類推

轉譯成 Reason

type res = {.
  [@bs.meth] "send": string => string
 };

type nextFun = unit => string;
type handler = (string, res, nextFun) => string;

type expressApp = {.
  [@bs.meth] "use": handler => unit,
  [@bs.meth] "listen": (int) => unit,
  [@bs.meth] "get": (string, handler, handler) => string
};

type router = {.
  [@bs.meth] "get": (string, handler) => string,
  [@bs.meth] "use": handler => unit
};

type dbErrorType = Js.t({. message: string });
type jsObjectType = {. };
type insertManyResultType = {. };
type insertRespType = (dbErrorType, insertManyResultType) => unit;
type collectionType = {.
  [@bs.meth] "insertMany": (array({. "a": int}), insertRespType) => unit
};
type dbType = {.
  [@bs.meth] "collection": (string) => collectionType
};

type clientType = Js.t({.
  [@bs.meth] db: string => dbType,
  [@bs.meth] close: unit => unit
});

type mongoClientType = {.
  [@bs.meth] "connect": (string, (dbErrorType, clientType) => unit) => unit
};

[@bs.module] external express: unit => expressApp = "express";
[@bs.module "mongodb"] external mongoClient: mongoClientType = "MongoClient";
[@bs.module "./routes/UserRoutes.bs"] external userRoutes: router = "routes";

let url = "mongodb://localhost:27017";
let dbName = "myproject";
mongoClient##connect(url, (error, client) => {
  let db = client##db(dbName);
  let collection = db##collection("documents");

  let inputData = [|{"a": 1}|];

  collection##insertMany(inputData, (_, result) => {
    Js.log("Inserted 3 documents into the collection");
    Js.log(result);
  });
  client##close();
});

let app = express();

[@bs.val] external use: handler => unit = "app.use";
[@bs.val] external useWithString: (string, router) => unit = "app.use";

use((_, _, next) => {
  Js.log("here is use function");
  next();
});

useWithString("/user", userRoutes);

app##get("/", (_, _, next) => {
  Js.log("hello next");
  next();
}, (_, res, _) => res##send("Hello World!"));

app##listen(5000);

再重寫的時候會複習一下之前討論過的很多資料型態

發現不夠熟練

let list1 = [0,1];
let array1 = [|0, 1|];

這兩種的轉譯結果依序是

var list1 = /* :: */[
  0,
  /* :: */[
    1,
    /* [] */0
  ]
];

var array1 = /* array */[
  0,
  1
];

可以看到這兩個的結果和原本預計的會有所不同

type obj2Type = Js.t({
  .
  a: string
});

let obj1: {. "a": string} = {"a": "a"};
let obj2 = {"a": "a", "b": "b"};

上述的範例中 obj1 如果不定長度的話會引起錯誤

obj2 可以是任意長度 (Json)

轉譯結果

var obj1 = {
  a: "a"
};

var obj2 = {
  a: "a",
  b: "b"
};

結果如上

今天光處理 mongodb 的部分就已經花了好多時間跟腦力

先這樣吧

明天再來處理 CRUD


上一篇
Express-Router
下一篇
Express-MongoDB-PartII
系列文
Some thing with Reason30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言