|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const IDb = require('../idb') |
| 4 | +const Hoek = require('@hapi/hoek') |
| 5 | +const zango = require('zangodb') |
| 6 | +const reach = require('../../utils/reach') |
| 7 | +const ObjectId = require('bson-objectid') |
| 8 | + |
| 9 | +const MongoQuery = require('../mongo-query') |
| 10 | +const { promisfy } = require('promisfy') |
| 11 | +const debug = require('debug')('bouncer.db.zango-db') |
| 12 | + |
| 13 | + |
| 14 | +module.exports = class ZangoDb extends IDb { |
| 15 | + |
| 16 | + constructor ({dbname, factory}) { |
| 17 | + super(factory) |
| 18 | + debug('constructor') |
| 19 | + this.zango = null |
| 20 | + this.dbname = dbname |
| 21 | + this.error = null |
| 22 | + } |
| 23 | + |
| 24 | + |
| 25 | + async start(){ |
| 26 | + |
| 27 | + debug('starting') |
| 28 | + |
| 29 | + |
| 30 | + let collectionSettings = {} |
| 31 | + |
| 32 | + for(const name of this.factory.getValidators()){ |
| 33 | + debug('creating collection', name) |
| 34 | + |
| 35 | + const indexSettings = reach(this.factory, 'model.IndexSettings.'+name) |
| 36 | + |
| 37 | + const indices = ['$meta.id'].concat(indexSettings.unique).concat(indexSettings.indices) |
| 38 | + |
| 39 | + collectionSettings[this.prefix+name] = indices.length > 0 ? indices : true |
| 40 | + } |
| 41 | + |
| 42 | + debug('dbname',this.dbname, collectionSettings) |
| 43 | + |
| 44 | + this.zango = new zango.Db(this.dbname, collectionSettings) |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + async getCollectionNames(){ |
| 49 | + |
| 50 | + const names = this.factory.getValidators() |
| 51 | + |
| 52 | + return names.map(name=>{return name.replace(this.prefix,'')}) |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + async getCollection(name){ |
| 57 | + let collection = this.zango.collection(this.prefix+name) |
| 58 | + |
| 59 | + return collection |
| 60 | + } |
| 61 | + |
| 62 | + /** convert db documnet to plain object with $meta field */ |
| 63 | + documentToObject(doc){ |
| 64 | + let obj = Object.assign({},doc) |
| 65 | + obj.$meta = { |
| 66 | + id: Hoek.reach(obj,'meta.id', {default: obj._id}), |
| 67 | + type: Hoek.reach(doc,'meta.type'), |
| 68 | + created: Hoek.reach(obj,'meta.created', {default: (new Date()).toISOString()}), |
| 69 | + revision: Hoek.reach(obj,'meta.revision', {default: 1}), |
| 70 | + removed: Hoek.reach(obj,'meta.removed') |
| 71 | + } |
| 72 | + |
| 73 | + delete obj.meta |
| 74 | + delete obj._id |
| 75 | + |
| 76 | + return obj |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + /** convert object with $meta field to db representation*/ |
| 81 | + documentFromObject(obj){ |
| 82 | + let doc = Object.assign({},obj) |
| 83 | + doc._id = Hoek.reach(obj,'$meta.id', {default: obj._id}), |
| 84 | + doc.meta = { |
| 85 | + id: Hoek.reach(obj,'$meta.id', {default: obj._id}), |
| 86 | + type: Hoek.reach(obj,'$meta.type'), |
| 87 | + created: Hoek.reach(obj,'$meta.created', {default: (new Date()).toISOString()}), |
| 88 | + revision: Hoek.reach(obj,'$meta.revision', {default: 1}), |
| 89 | + removed: Hoek.reach(obj,'$meta.removed') |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + delete doc.$meta |
| 94 | + |
| 95 | + return doc |
| 96 | + } |
| 97 | + |
| 98 | + ensureId(obj){ |
| 99 | + let temp = {...obj} |
| 100 | + if(!reach(temp,'$meta.id')){ |
| 101 | + temp.$meta.id = (new ObjectId()).toHexString() |
| 102 | + } |
| 103 | + |
| 104 | + let dbDoc = this.documentFromObject(temp) |
| 105 | + |
| 106 | + return dbDoc |
| 107 | + } |
| 108 | + |
| 109 | + async find(collectionName, mongoQuery){ |
| 110 | + |
| 111 | + let query = mongoQuery.getQueryDoc() |
| 112 | + |
| 113 | + debug('find collection=', collectionName, ' query=', JSON.stringify(query,null,2)) |
| 114 | + let collection = await this.getCollection(collectionName) |
| 115 | + let resultSet = collection.find(query) |
| 116 | + |
| 117 | + |
| 118 | + if(mongoQuery.hasLimit()){ |
| 119 | + resultSet = resultSet.limit(mongoQuery.getLimit()) |
| 120 | + } |
| 121 | + |
| 122 | + if(mongoQuery.hasSort()){ |
| 123 | + resultSet = resultSet.sort( mongoQuery.getSort() ) |
| 124 | + } |
| 125 | + |
| 126 | + return (await resultSet.toArray()).map(this.documentToObject) || [] |
| 127 | + } |
| 128 | + |
| 129 | + async insertMany(collectionName, docs){ |
| 130 | + debug('insert collection=', collectionName, ' docs=', JSON.stringify(docs,null,2)) |
| 131 | + let collection = await this.getCollection(collectionName) |
| 132 | + |
| 133 | + let resultSet = [] |
| 134 | + |
| 135 | + for(let obj of docs){ |
| 136 | + let temp = {...obj} |
| 137 | + if(temp._id===undefined){ temp._id = (new ObjectId()).toString(); temp.$meta.id=temp._id; } |
| 138 | + |
| 139 | + let dbDoc = this.documentFromObject(temp) |
| 140 | + |
| 141 | + const stripped = this.stripMeta(temp) |
| 142 | + |
| 143 | + debug('validating', stripped,'from', temp) |
| 144 | + |
| 145 | + await this.factory.validate(collectionName, stripped) |
| 146 | + |
| 147 | + debug('its good, inserting', dbDoc) |
| 148 | + |
| 149 | + await collection.insert( dbDoc ) |
| 150 | + |
| 151 | + debug('inserted', dbDoc) |
| 152 | + |
| 153 | + const finalObj = this.documentToObject(dbDoc) |
| 154 | + |
| 155 | + debug('returning', finalObj) |
| 156 | + |
| 157 | + this.emitChange(finalObj, 'create') |
| 158 | + |
| 159 | + resultSet.push(finalObj) |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + return resultSet |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | + async update(collectionName, docs){ |
| 168 | + debug('update collection', collectionName, ' docs', docs) |
| 169 | + |
| 170 | + let collection = await this.getCollection(collectionName) |
| 171 | + |
| 172 | + let objs = [] |
| 173 | + |
| 174 | + for(let obj of docs){ |
| 175 | + let dbDoc = this.documentFromObject(obj) |
| 176 | + |
| 177 | + debug('updating',obj, 'to', dbDoc) |
| 178 | + |
| 179 | + const stripped = this.stripMeta(dbDoc) |
| 180 | + const meta = this.onlyMeta(obj) |
| 181 | + |
| 182 | + debug('validating', stripped,'from', dbDoc) |
| 183 | + |
| 184 | + await this.factory.validate(collectionName, stripped) |
| 185 | + |
| 186 | + debug('its good, updating', dbDoc) |
| 187 | + |
| 188 | + let old = await collection.findOne( {'_id': dbDoc._id}) |
| 189 | + |
| 190 | + debug('found old', old) |
| 191 | + dbDoc.meta.revision = old.meta.revision++ |
| 192 | + |
| 193 | + |
| 194 | + let mergedDoc = {...old, ...dbDoc} |
| 195 | + |
| 196 | + await collection.update({'_id': dbDoc._id}, mergedDoc) |
| 197 | + |
| 198 | + const finalObj = this.documentToObject(mergedDoc) |
| 199 | + |
| 200 | + this.emitChange(finalObj, 'update') |
| 201 | + |
| 202 | + objs.push( finalObj ) |
| 203 | + |
| 204 | + } |
| 205 | + |
| 206 | + return objs |
| 207 | + } |
| 208 | + |
| 209 | + async findAndRemove(collectionName, obj){ |
| 210 | + debug('findAndRemove collection', collectionName, ' obj', obj) |
| 211 | + |
| 212 | + let collection = await this.getCollection(collectionName) |
| 213 | + |
| 214 | + const dbDoc = await collection.findOne( {'_id': obj.$meta.id}) |
| 215 | + |
| 216 | + debug('found old doc', dbDoc) |
| 217 | + |
| 218 | + await collection.remove( { '_id': obj.$meta.id } ) |
| 219 | + |
| 220 | + debug('dbDoc', dbDoc) |
| 221 | + |
| 222 | + let finalObj = this.documentToObject(dbDoc) |
| 223 | + |
| 224 | + finalObj.$meta.removed = true |
| 225 | + |
| 226 | + this.emitChange(finalObj, 'remove') |
| 227 | + |
| 228 | + debug('obj', finalObj) |
| 229 | + |
| 230 | + return finalObj |
| 231 | + } |
| 232 | +} |
0 commit comments