@openhps/mongodb
Version:
Open Hybrid Positioning System - MongoDB Database component
163 lines • 5.67 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongoDataServiceDriver = void 0;
const core_1 = require("@openhps/core");
const mongodb_1 = require("mongodb");
class MongoDataServiceDriver extends core_1.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) => {
mongodb_1.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(core_1.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(core_1.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(core_1.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 = core_1.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.`));
}
}
}
exports.MongoDataServiceDriver = MongoDataServiceDriver;
//# sourceMappingURL=MongoDataServiceDriver.js.map