UNPKG

@openhps/mongodb

Version:

Open Hybrid Positioning System - MongoDB Database component

159 lines 5.51 kB
import { DataSerializer, DataServiceDriver, DataSerializerUtils, } from '@openhps/core'; import { MongoClient } from 'mongodb'; export class MongoDataServiceDriver extends DataServiceDriver { constructor(dataType, options) { super(dataType); this.options = options; this.options.collectionName = this.options.collectionName || this.uid.toLowerCase(); this.once('build', this.connect.bind(this)); this.once('destroy', this.disconnect.bind(this)); } connect() { if (this._client !== undefined) { return Promise.resolve(); } return new Promise((resolve, reject) => { MongoClient.connect(this.options.dbURL, { auth: this.options.auth, }) .then((client) => { this._client = client; this._db = client.db(this.options.dbName); this._collection = this._db.collection(this.options.collectionName); const indexes = Array.from(DataSerializerUtils.getRootMetadata(this.dataType).dataMembers.values()) .filter((dataMember) => dataMember.index) .map(this.createIndex.bind(this)); return Promise.all(indexes); }) .then(() => resolve()) .catch(reject); }); } createIndex(dataMember) { return new Promise((resolve, reject) => { this._collection .createIndex(dataMember.key, { unique: dataMember.unique ? true : false, }) .then(() => { resolve(); }) .catch(reject); }); } disconnect() { if (this._client === undefined) { return Promise.resolve(); } return this._client.close(); } findByUID(id) { return new Promise((resolve, reject) => { this.findOne({ _id: id }) .then((object) => { if (object === undefined) { return reject(`${this.dataType.name} with identifier #${id} not found!`); } resolve(object); }) .catch(reject); }); } findOne(query, options) { return new Promise((resolve, reject) => { this._checkIfReady(reject); this._collection .findOne(query, options) .then((serializedObject) => { if (!serializedObject) { return resolve(undefined); } resolve(DataSerializer.deserialize(serializedObject, this.dataType)); }) .catch(reject); }); } findAll(query, options) { return new Promise((resolve, reject) => { this._checkIfReady(reject); this._collection .find(query, options) .toArray() .then((result) => { const deserializedResults = []; result.forEach((r) => { deserializedResults.push(DataSerializer.deserialize(r, this.dataType)); }); resolve(deserializedResults); }) .catch(reject); }); } insert(id, object) { return new Promise((resolve, reject) => { this._checkIfReady(reject); this._collection .findOne({ _id: id }) .then((existingObject) => { const preparedObject = DataSerializer.serialize(object); preparedObject._id = id; if (!existingObject) { this._collection .insertOne(preparedObject) .then(() => { resolve(object); }) .catch(() => { resolve(object); }); } else { this._collection .updateOne({ _id: id }, { $set: preparedObject }) .then(() => { resolve(object); }) .catch(reject); } }) .catch((ex) => { reject(ex); }); }); } count(query) { return new Promise((resolve, reject) => { this._checkIfReady(reject); this._collection.count(query).then(resolve).catch(reject); }); } delete(id) { return new Promise((resolve, reject) => { this._checkIfReady(reject); this._collection .deleteOne({ _id: id }) .then(() => { resolve(); }) .catch(reject); }); } deleteAll(query) { return new Promise((resolve, reject) => { this._checkIfReady(reject); this._collection .deleteMany(query) .then(() => { resolve(); }) .catch(reject); }); } _checkIfReady(reject) { if (this._collection === undefined) { return reject(new Error(`MongoDB connection not ready! Most likely the service was accessed before the connection was completed.`)); } } } //# sourceMappingURL=MongoDataServiceDriver.js.map