BVB Source Codes

parse-server Show MongoCollection.js Source code

Return Download parse-server: download MongoCollection.js Source code - Download parse-server Source code - Type:.js
  1. const mongodb = require('mongodb');
  2. const Collection = mongodb.Collection;
  3.  
  4. export default class MongoCollection {
  5.   _mongoCollection:Collection;
  6.  
  7.   constructor(mongoCollection:Collection) {
  8.     this._mongoCollection = mongoCollection;
  9.   }
  10.  
  11.   // Does a find with "smart indexing".
  12.   // Currently this just means, if it needs a geoindex and there is
  13.   // none, then build the geoindex.
  14.   // This could be improved a lot but it's not clear if that's a good
  15.   // idea. Or even if this behavior is a good idea.
  16.   find(query, { skip, limit, sort, keys, maxTimeMS } = {}) {
  17.     return this._rawFind(query, { skip, limit, sort, keys, maxTimeMS })
  18.       .catch(error => {
  19.         // Check for "no geoindex" error
  20.         if (error.code != 17007 && !error.message.match(/unable to find index for .geoNear/)) {
  21.           throw error;
  22.         }
  23.         // Figure out what key needs an index
  24.         const key = error.message.match(/field=([A-Za-z_0-9]+) /)[1];
  25.         if (!key) {
  26.           throw error;
  27.         }
  28.  
  29.         var index = {};
  30.         index[key] = '2d';
  31.         return this._mongoCollection.createIndex(index)
  32.           // Retry, but just once.
  33.           .then(() => this._rawFind(query, { skip, limit, sort, keys, maxTimeMS }));
  34.       });
  35.   }
  36.  
  37.   _rawFind(query, { skip, limit, sort, keys, maxTimeMS } = {}) {
  38.     let findOperation = this._mongoCollection
  39.       .find(query, { skip, limit, sort })
  40.  
  41.     if (keys) {
  42.       findOperation = findOperation.project(keys);
  43.     }
  44.  
  45.     if (maxTimeMS) {
  46.       findOperation = findOperation.maxTimeMS(maxTimeMS);
  47.     }
  48.  
  49.     return findOperation.toArray();
  50.   }
  51.  
  52.   count(query, { skip, limit, sort, maxTimeMS } = {}) {
  53.     const countOperation = this._mongoCollection.count(query, { skip, limit, sort, maxTimeMS });
  54.  
  55.     return countOperation;
  56.   }
  57.  
  58.   insertOne(object) {
  59.     return this._mongoCollection.insertOne(object);
  60.   }
  61.  
  62.   // Atomically updates data in the database for a single (first) object that matched the query
  63.   // If there is nothing that matches the query - does insert
  64.   // Postgres Note: `INSERT ... ON CONFLICT UPDATE` that is available since 9.5.
  65.   upsertOne(query, update) {
  66.     return this._mongoCollection.update(query, update, { upsert: true })
  67.   }
  68.  
  69.   updateOne(query, update) {
  70.     return this._mongoCollection.updateOne(query, update);
  71.   }
  72.  
  73.   updateMany(query, update) {
  74.     return this._mongoCollection.updateMany(query, update);
  75.   }
  76.  
  77.   deleteOne(query) {
  78.     return this._mongoCollection.deleteOne(query);
  79.   }
  80.  
  81.   deleteMany(query) {
  82.     return this._mongoCollection.deleteMany(query);
  83.   }
  84.  
  85.   _ensureSparseUniqueIndexInBackground(indexRequest) {
  86.     return new Promise((resolve, reject) => {
  87.       this._mongoCollection.ensureIndex(indexRequest, { unique: true, background: true, sparse: true }, (error) => {
  88.         if (error) {
  89.           reject(error);
  90.         } else {
  91.           resolve();
  92.         }
  93.       });
  94.     });
  95.   }
  96.  
  97.   drop() {
  98.     return this._mongoCollection.drop();
  99.   }
  100. }
  101.  
downloadMongoCollection.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top