-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (58 loc) · 1.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const express = require('express');
const app = express();
const mongoose = require("mongoose");
/**
* FIRST DB CONNECTION
* ===================
* */
// UPDATE YOUR DEFAULTS
const firstDB = {
name: "First", // for demo
connStr: "your first db connection url",
db: "mydb",
coll: "mycoll"
};
// CONNECT DB
const firstConn = require("./connect")(firstDB.name, firstDB.connStr);
// CREATE MODEL & SCHEMA
const FirstSchema = firstConn.useDb(firstDB.db).model(
firstDB.coll,
new mongoose.Schema({}, { strict: false, collection: firstDB.coll })
);
// DO TRANSACTIONS WITH SCHEMA
async function firstInsert() {
let doc = await FirstSchema.create({ name: "test", calledAt: new Date() });
console.log(doc);
}
firstInsert();
/**
* SECOND DB CONNECTION
* ====================
* */
// UPDATE YOUR DEFAULTS
const secondDB = {
name: "Second", // for demo
connStr: "your second db connection url",
db: "mydb",
coll: "mycoll"
};
// CONNECT DB
const secondConn = require("./connect")(secondDB.name, secondDB.connStr);
// CREATE MODEL & SCHEMA
const SecondSchema = secondConn.useDb(secondDB.db).model(
secondDB.coll,
new mongoose.Schema({}, { strict: false, collection: secondDB.coll })
);
// DO TRANSACTIONS WITH SCHEMA
async function secondInsert() {
let doc = await SecondSchema.create({ name: "test", calledAt: new Date() });
console.log(doc);
}
secondInsert();
// CONNET SERVER
const port = 3001;
app.listen(port, () => {
console.info(`Listening on port ${port}...`);
}).on("error", (err) => {
console.error(err.message);
});