2018-03-03 11:39:56 +09:00
|
|
|
import { DatabaseConnectors } from '../connectors.js';
|
2018-02-11 12:57:51 +09:00
|
|
|
|
2018-06-17 12:30:37 +09:00
|
|
|
// convert GraphQL selector into Mongo-compatible selector
|
|
|
|
// TODO: add support for more than just documentId/_id and slug, potentially making conversion unnecessary
|
|
|
|
// see https://github.com/VulcanJS/Vulcan/issues/2000
|
|
|
|
const convertSelector = selector => {
|
|
|
|
return selector;
|
|
|
|
};
|
|
|
|
const convertUniqueSelector = selector => {
|
|
|
|
if (selector.documentId) {
|
|
|
|
selector._id = selector.documentId;
|
|
|
|
delete selector.documentId;
|
|
|
|
}
|
|
|
|
return selector;
|
|
|
|
};
|
|
|
|
|
2018-03-03 11:39:56 +09:00
|
|
|
DatabaseConnectors.mongo = {
|
2018-02-18 15:27:25 +09:00
|
|
|
get: async (collection, selector = {}, options = {}) => {
|
2018-06-17 12:30:37 +09:00
|
|
|
return await collection.findOne(convertUniqueSelector(selector), options);
|
2018-02-11 12:57:51 +09:00
|
|
|
},
|
2018-02-18 15:27:25 +09:00
|
|
|
find: async (collection, selector = {}, options = {}) => {
|
2018-06-17 12:30:37 +09:00
|
|
|
return await collection.find(convertSelector(selector), options).fetch();
|
2018-02-11 12:57:51 +09:00
|
|
|
},
|
2018-02-18 15:27:25 +09:00
|
|
|
count: async (collection, selector = {}, options = {}) => {
|
2018-06-17 12:30:37 +09:00
|
|
|
return await collection.find(convertSelector(selector), options).count();
|
2018-02-11 12:57:51 +09:00
|
|
|
},
|
2018-02-18 15:27:25 +09:00
|
|
|
create: async (collection, document, options = {}) => {
|
2018-02-11 12:57:51 +09:00
|
|
|
return await collection.insert(document);
|
|
|
|
},
|
2018-02-18 15:27:25 +09:00
|
|
|
update: async (collection, selector, modifier, options = {}) => {
|
2018-06-17 12:30:37 +09:00
|
|
|
return await collection.update(convertUniqueSelector(selector), modifier, options);
|
2018-02-11 12:57:51 +09:00
|
|
|
},
|
2018-02-18 15:27:25 +09:00
|
|
|
delete: async (collection, selector, options = {}) => {
|
2018-06-17 12:30:37 +09:00
|
|
|
return await collection.remove(convertUniqueSelector(selector));
|
2018-02-11 12:57:51 +09:00
|
|
|
},
|
|
|
|
}
|