|
| 1 | +module.exports.Context = class { |
| 2 | + |
| 3 | +constructor(config) { |
| 4 | + if (!config.config.url) { |
| 5 | + throw new Error('url is required for mongodb connection') |
| 6 | + } |
| 7 | + this._config = config.config |
| 8 | +} |
| 9 | + |
| 10 | +getConnection() { |
| 11 | + return new MongoDB(this._config) |
| 12 | +} |
| 13 | + |
| 14 | +} |
| 15 | + |
| 16 | +class MongoDB { |
| 17 | + |
| 18 | +constructor(config) { |
| 19 | + this._config = config |
| 20 | + this._readonly = config.readonly |
| 21 | + this._released = false |
| 22 | + this._conn = monk(config.url) |
| 23 | + log.trace('mongodb connection established') |
| 24 | +} |
| 25 | + |
| 26 | +__destroy() { |
| 27 | + if (this._conn._state === 'closed') { |
| 28 | + return |
| 29 | + } |
| 30 | + this._conn.close() |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * MongoDB.insert() |
| 35 | + * |
| 36 | + * @param {string} table |
| 37 | + * @param {object|Array} sets |
| 38 | + * @param {function} callback |
| 39 | + */ |
| 40 | +insert(table, sets, callback) { |
| 41 | + if (sets.constructor !== Object && sets.constructor !== Array) { |
| 42 | + return callback(new Error('invalid sets')) |
| 43 | + } |
| 44 | + if (this._readonly) { |
| 45 | + return errNotAllowd(callback) |
| 46 | + } |
| 47 | + this._conn.collection(table).insert(sets, (err, result) => { |
| 48 | + if (result.constructor === Object) { |
| 49 | + result = [result] |
| 50 | + } |
| 51 | + return callback(err, { |
| 52 | + affected_rows: result.length, |
| 53 | + docs: result, |
| 54 | + }, null) |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +update(table, sets, where, callback) { |
| 59 | + if (sets.constructor !== Object && sets.constructor !== Array) { |
| 60 | + return callback(new Error('invalid sets')) |
| 61 | + } |
| 62 | + if (this._readonly) { |
| 63 | + return errNotAllowd(callback) |
| 64 | + } |
| 65 | + this._conn.collection(table) |
| 66 | + .update(where, {$set: sets}, {multi: true}, (err, result) => { |
| 67 | + return callback(err, { |
| 68 | + affected_rows: result.n, |
| 69 | + changed_rows: result.nModified, |
| 70 | + }, null) |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Mongodb.remove() |
| 76 | + * |
| 77 | + * @param {string} table |
| 78 | + * @param {object} where |
| 79 | + * @param {function} callback |
| 80 | + */ |
| 81 | +delete(table, where, callback) { |
| 82 | + if (this._readonly) { |
| 83 | + return errNotAllowd(callback) |
| 84 | + } |
| 85 | + this._conn.collection(table).remove(where, (err, result) => { |
| 86 | + return callback(err, { |
| 87 | + affected_rows: result.deletedCount, |
| 88 | + }, null) |
| 89 | + }) |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Mongodb.find() |
| 94 | + * |
| 95 | + * @param {string} table |
| 96 | + * @param {string|array} options |
| 97 | + * @param {string|object_id|object} where |
| 98 | + * @param {function} callback |
| 99 | + */ |
| 100 | +select(table, fields, where, callback) { |
| 101 | + let _fields = fields === '*' ? {} : fields |
| 102 | + this._conn.collection(table).find(where, _fields, (err, docs) => { |
| 103 | + return callback(err, docs, null) |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Mongodb.find() |
| 109 | + * support sort/fileds/limit/skip/rawCursor in options |
| 110 | + * |
| 111 | + * @param {string} table |
| 112 | + * @param {object} options |
| 113 | + * @param {string|object_id|object} where |
| 114 | + * @param {function} callback |
| 115 | + */ |
| 116 | +find(table, options, where, callback) { |
| 117 | + this._conn.collection(table).find(where, options, (err, docs) => { |
| 118 | + return callback(err, docs, null) |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Mongodb.close() |
| 124 | + */ |
| 125 | +release() { |
| 126 | + this._released = true |
| 127 | + if (!this._conn._state === 'open') return |
| 128 | + this._conn.close() |
| 129 | + this._conn = null |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Mongodb.aggregate() |
| 134 | + * |
| 135 | + * @param {string} table |
| 136 | + * @param {object} options |
| 137 | + * @param {array} pipeline |
| 138 | + * @param {function} callback |
| 139 | + */ |
| 140 | +aggregate(table, options, pipeline, callback) { |
| 141 | + this._conn.collection(table).aggregate(pipeline, options, (err, docs) => { |
| 142 | + return callback(err, docs, null) |
| 143 | + }) |
| 144 | +} |
| 145 | + |
| 146 | +} |
| 147 | + |
| 148 | +const util = require('util') |
| 149 | +const debug = require('debug')('sqlx') |
| 150 | +const monk = require('monk') |
| 151 | +function __log() { |
| 152 | + debug(util.format.apply(null, arguments)) |
| 153 | +} |
| 154 | +const log = { |
| 155 | + trace: __log, |
| 156 | + error: __log, |
| 157 | + debug: __log, |
| 158 | + info : __log, |
| 159 | +} |
| 160 | + |
| 161 | +function errNotAllowd(callback) { |
| 162 | + return callback(new Error('not allowed')) |
| 163 | +} |
0 commit comments