-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal_db.js
57 lines (49 loc) · 1.39 KB
/
external_db.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
// Represent new mongo db cluster
let mongoose = require('mongoose')
let Schema = mongoose.Schema
const Account = new Schema({
account_id: Number,
limit: Number,
products: Schema.Types.Mixed
})
function getExternalDBUrl(){
const USER = 'get_your_own'
const PASSWORD = 'get_your_own'
const HOST = 'get_your_own'
const DB = 'get_your_own'
const url = `mongodb+srv://${USER}:${PASSWORD}@${HOST}/${DB}?retryWrites=true&w=majority`
return url
}
const tableName = 'accounts'
const condition = { account_id: 371138 }
async function connect_ext(url) {
// Here we make use of createConnection method, instead of connect method
// connect method would directly update mongoose object, where as
// createConnection method would not update mongoose object, it creates a new and returs it
const conn = await mongoose.createConnection(url)
return conn
}
async function printRecord_ext(conn) {
const MyModel = conn.model(tableName, Account)
const res = await MyModel.findOne(condition)
console.log(res)
}
async function insertRecs_ext(conn) {
const recs = [{
account_id: 999901,
limit: 10000,
products: ['a', 'b']
}]
const MyModel = conn.model(tableName, Account)
const res = await MyModel.insertMany(recs)
console.log(res)
}
module.exports = {
url: getExternalDBUrl(),
schema: Account,
condition,
tableName,
connect_ext,
printRecord_ext,
insertRecs_ext
}