UNPKG

blow-data-service

Version:

Observable data service for Blow.

55 lines (54 loc) 1.94 kB
'use strict'; const blow_collection_1 = require('blow-collection'); const DataConnector_1 = require('./DataConnector'); function copyObject(obj) { return Object.assign({}, obj); } class MemoryConnector extends DataConnector_1.DataConnector { constructor(settings) { super(settings); this._db = new Map(); } _collection(collectionName) { if (!this._db.has(collectionName)) { this._db.set(collectionName, new blow_collection_1.Collection({ idKey: '_id' })); } return this._db.get(collectionName); } find(collectionName, query) { return this._collection(collectionName) .find(this._prepareQuery(query)).map(copyObject); } count(collectionName, query) { return this._collection(collectionName) .count(this._prepareQuery(query).where); } delete(collectionName, query) { return this._collection(collectionName) .destroy(this._prepareQuery(query).where); } deleteById(collectionName, id) { return this._collection(collectionName).destroyById(id); } get(collectionName, id) { return this._collection(collectionName).findById(id).map(copyObject); } save(collectionName, doc) { const hasId = Object.keys(doc).indexOf('_id') > -1 && doc['_id']; if (!hasId) { return this._collection(collectionName).create(doc).map(copyObject); } else { return this._collection(collectionName) .update(this._buildQueryWhereForId(doc['_id']), doc) .mapTo(doc); } } updateAttributes(collectionName, id, attributes) { delete attributes['_id']; return this.get(collectionName, id) .map(result => Object.assign(result, attributes)) .mergeMap(doc => this.save(collectionName, doc)); } } exports.MemoryConnector = MemoryConnector;