From d78c2746e9c8f37fbe80b5b129a32d30b0eaf0d3 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Sat, 27 Feb 2016 02:02:33 -0800 Subject: [PATCH] Rename ExportAdapter to DatabaseController. --- spec/DatabaseController.spec.js | 15 ++++++ spec/ExportAdapter.spec.js | 15 ------ src/Adapters/Files/FilesAdapter.js | 2 +- .../DatabaseController.js} | 52 +++++++++---------- src/DatabaseAdapter.js | 6 +-- src/Schema.js | 2 +- src/index.js | 2 +- 7 files changed, 46 insertions(+), 48 deletions(-) create mode 100644 spec/DatabaseController.spec.js delete mode 100644 spec/ExportAdapter.spec.js rename src/{ExportAdapter.js => Controllers/DatabaseController.js} (90%) diff --git a/spec/DatabaseController.spec.js b/spec/DatabaseController.spec.js new file mode 100644 index 00000000..6ec8bfc0 --- /dev/null +++ b/spec/DatabaseController.spec.js @@ -0,0 +1,15 @@ +var DatabaseController = require('../src/Controllers/DatabaseController'); + +describe('DatabaseController', () => { + it('can be constructed', (done) => { + var database = new DatabaseController('mongodb://localhost:27017/test', + { + collectionPrefix: 'test_' + }); + database.connect().then(done, (error) => { + console.log('error', error.stack); + fail(); + }); + }); + +}); diff --git a/spec/ExportAdapter.spec.js b/spec/ExportAdapter.spec.js deleted file mode 100644 index a4f3f9b6..00000000 --- a/spec/ExportAdapter.spec.js +++ /dev/null @@ -1,15 +0,0 @@ -var ExportAdapter = require('../src/ExportAdapter'); - -describe('ExportAdapter', () => { - it('can be constructed', (done) => { - var database = new ExportAdapter('mongodb://localhost:27017/test', - { - collectionPrefix: 'test_' - }); - database.connect().then(done, (error) => { - console.log('error', error.stack); - fail(); - }); - }); - -}); diff --git a/src/Adapters/Files/FilesAdapter.js b/src/Adapters/Files/FilesAdapter.js index a1d5955f..2ff9fdb2 100644 --- a/src/Adapters/Files/FilesAdapter.js +++ b/src/Adapters/Files/FilesAdapter.js @@ -8,7 +8,7 @@ // * getFileLocation(config, request, filename) // // Default is GridStoreAdapter, which requires mongo -// and for the API server to be using the ExportAdapter +// and for the API server to be using the DatabaseController with Mongo // database adapter. export class FilesAdapter { diff --git a/src/ExportAdapter.js b/src/Controllers/DatabaseController.js similarity index 90% rename from src/ExportAdapter.js rename to src/Controllers/DatabaseController.js index 821c69cd..ed6241ce 100644 --- a/src/ExportAdapter.js +++ b/src/Controllers/DatabaseController.js @@ -5,12 +5,12 @@ var mongodb = require('mongodb'); var MongoClient = mongodb.MongoClient; var Parse = require('parse/node').Parse; -var Schema = require('./Schema'); -var transform = require('./transform'); +var Schema = require('./../Schema'); +var transform = require('./../transform'); // options can contain: // collectionPrefix: the string to put in front of every collection name. -function ExportAdapter(mongoURI, options = {}) { +function DatabaseController(mongoURI, options = {}) { this.mongoURI = mongoURI; this.collectionPrefix = options.collectionPrefix; @@ -27,7 +27,7 @@ function ExportAdapter(mongoURI, options = {}) { // connection is successful. // this.db will be populated with a Mongo "Db" object when the // promise resolves successfully. -ExportAdapter.prototype.connect = function() { +DatabaseController.prototype.connect = function() { if (this.connectionPromise) { // There's already a connection in progress. return this.connectionPromise; @@ -43,7 +43,7 @@ ExportAdapter.prototype.connect = function() { // Returns a promise for a Mongo collection. // Generally just for internal use. -ExportAdapter.prototype.collection = function(className) { +DatabaseController.prototype.collection = function(className) { if (!Schema.classNameIsValid(className)) { throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, 'invalid className: ' + className); @@ -51,7 +51,7 @@ ExportAdapter.prototype.collection = function(className) { return this.rawCollection(className); }; -ExportAdapter.prototype.rawCollection = function(className) { +DatabaseController.prototype.rawCollection = function(className) { return this.connect().then(() => { return this.db.collection(this.collectionPrefix + className); }); @@ -64,7 +64,7 @@ function returnsTrue() { // Returns a promise for a schema object. // If we are provided a acceptor, then we run it on the schema. // If the schema isn't accepted, we reload it at most once. -ExportAdapter.prototype.loadSchema = function(acceptor = returnsTrue) { +DatabaseController.prototype.loadSchema = function(acceptor = returnsTrue) { if (!this.schemaPromise) { this.schemaPromise = this.collection('_SCHEMA').then((coll) => { @@ -88,8 +88,8 @@ ExportAdapter.prototype.loadSchema = function(acceptor = returnsTrue) { // Returns a promise for the classname that is related to the given // classname through the key. -// TODO: make this not in the ExportAdapter interface -ExportAdapter.prototype.redirectClassNameForKey = function(className, key) { +// TODO: make this not in the DatabaseController interface +DatabaseController.prototype.redirectClassNameForKey = function(className, key) { return this.loadSchema().then((schema) => { var t = schema.getExpectedType(className, key); var match = t.match(/^relation<(.*)>$/); @@ -105,7 +105,7 @@ ExportAdapter.prototype.redirectClassNameForKey = function(className, key) { // Returns a promise that resolves to the new schema. // This does not update this.schema, because in a situation like a // batch request, that could confuse other users of the schema. -ExportAdapter.prototype.validateObject = function(className, object, query) { +DatabaseController.prototype.validateObject = function(className, object, query) { return this.loadSchema().then((schema) => { return schema.validateObject(className, object, query); }); @@ -113,7 +113,7 @@ ExportAdapter.prototype.validateObject = function(className, object, query) { // Like transform.untransformObject but you need to provide a className. // Filters out any data that shouldn't be on this REST-formatted object. -ExportAdapter.prototype.untransformObject = function( +DatabaseController.prototype.untransformObject = function( schema, isMaster, aclGroup, className, mongoObject) { var object = transform.untransformObject(schema, className, mongoObject); @@ -138,7 +138,7 @@ ExportAdapter.prototype.untransformObject = function( // acl: a list of strings. If the object to be updated has an ACL, // one of the provided strings must provide the caller with // write permissions. -ExportAdapter.prototype.update = function(className, query, update, options) { +DatabaseController.prototype.update = function(className, query, update, options) { var acceptor = function(schema) { return schema.hasKeys(className, Object.keys(query)); }; @@ -196,7 +196,7 @@ ExportAdapter.prototype.update = function(className, query, update, options) { // Returns a promise that resolves successfully when these are // processed. // This mutates update. -ExportAdapter.prototype.handleRelationUpdates = function(className, +DatabaseController.prototype.handleRelationUpdates = function(className, objectId, update) { var pending = []; @@ -243,7 +243,7 @@ ExportAdapter.prototype.handleRelationUpdates = function(className, // Adds a relation. // Returns a promise that resolves successfully iff the add was successful. -ExportAdapter.prototype.addRelation = function(key, fromClassName, +DatabaseController.prototype.addRelation = function(key, fromClassName, fromId, toId) { var doc = { relatedId: toId, @@ -258,7 +258,7 @@ ExportAdapter.prototype.addRelation = function(key, fromClassName, // Removes a relation. // Returns a promise that resolves successfully iff the remove was // successful. -ExportAdapter.prototype.removeRelation = function(key, fromClassName, +DatabaseController.prototype.removeRelation = function(key, fromClassName, fromId, toId) { var doc = { relatedId: toId, @@ -277,7 +277,7 @@ ExportAdapter.prototype.removeRelation = function(key, fromClassName, // acl: a list of strings. If the object to be updated has an ACL, // one of the provided strings must provide the caller with // write permissions. -ExportAdapter.prototype.destroy = function(className, query, options = {}) { +DatabaseController.prototype.destroy = function(className, query, options = {}) { var isMaster = !('acl' in options); var aclGroup = options.acl || []; @@ -320,7 +320,7 @@ ExportAdapter.prototype.destroy = function(className, query, options = {}) { // Inserts an object into the database. // Returns a promise that resolves successfully iff the object saved. -ExportAdapter.prototype.create = function(className, object, options) { +DatabaseController.prototype.create = function(className, object, options) { var schema; var isMaster = !('acl' in options); var aclGroup = options.acl || []; @@ -346,7 +346,7 @@ ExportAdapter.prototype.create = function(className, object, options) { // This should only be used for testing - use 'find' for normal code // to avoid Mongo-format dependencies. // Returns a promise that resolves to a list of items. -ExportAdapter.prototype.mongoFind = function(className, query, options = {}) { +DatabaseController.prototype.mongoFind = function(className, query, options = {}) { return this.collection(className).then((coll) => { return coll.find(query, options).toArray(); }); @@ -355,7 +355,7 @@ ExportAdapter.prototype.mongoFind = function(className, query, options = {}) { // Deletes everything in the database matching the current collectionPrefix // Won't delete collections in the system namespace // Returns a promise. -ExportAdapter.prototype.deleteEverything = function() { +DatabaseController.prototype.deleteEverything = function() { this.schemaPromise = null; return this.connect().then(() => { @@ -390,7 +390,7 @@ function keysForQuery(query) { // Returns a promise for a list of related ids given an owning id. // className here is the owning className. -ExportAdapter.prototype.relatedIds = function(className, key, owningId) { +DatabaseController.prototype.relatedIds = function(className, key, owningId) { var joinTable = '_Join:' + key + ':' + className; return this.collection(joinTable).then((coll) => { return coll.find({owningId: owningId}).toArray(); @@ -401,7 +401,7 @@ ExportAdapter.prototype.relatedIds = function(className, key, owningId) { // Returns a promise for a list of owning ids given some related ids. // className here is the owning className. -ExportAdapter.prototype.owningIds = function(className, key, relatedIds) { +DatabaseController.prototype.owningIds = function(className, key, relatedIds) { var joinTable = '_Join:' + key + ':' + className; return this.collection(joinTable).then((coll) => { return coll.find({relatedId: {'$in': relatedIds}}).toArray(); @@ -414,7 +414,7 @@ ExportAdapter.prototype.owningIds = function(className, key, relatedIds) { // equal-to-pointer constraints on relation fields. // Returns a promise that resolves when query is mutated // TODO: this only handles one of these at a time - make it handle more -ExportAdapter.prototype.reduceInRelation = function(className, query, schema) { +DatabaseController.prototype.reduceInRelation = function(className, query, schema) { // Search for an in-relation or equal-to-relation for (var key in query) { if (query[key] && @@ -442,7 +442,7 @@ ExportAdapter.prototype.reduceInRelation = function(className, query, schema) { // Modifies query so that it no longer has $relatedTo // Returns a promise that resolves when query is mutated -ExportAdapter.prototype.reduceRelationKeys = function(className, query) { +DatabaseController.prototype.reduceRelationKeys = function(className, query) { var relatedTo = query['$relatedTo']; if (relatedTo) { return this.relatedIds( @@ -461,7 +461,7 @@ ExportAdapter.prototype.reduceRelationKeys = function(className, query) { // none, then build the geoindex. // This could be improved a lot but it's not clear if that's a good // idea. Or even if this behavior is a good idea. -ExportAdapter.prototype.smartFind = function(coll, where, options) { +DatabaseController.prototype.smartFind = function(coll, where, options) { return coll.find(where, options).toArray() .then((result) => { return result; @@ -502,7 +502,7 @@ ExportAdapter.prototype.smartFind = function(coll, where, options) { // TODO: make userIds not needed here. The db adapter shouldn't know // anything about users, ideally. Then, improve the format of the ACL // arg to work like the others. -ExportAdapter.prototype.find = function(className, query, options = {}) { +DatabaseController.prototype.find = function(className, query, options = {}) { var mongoOptions = {}; if (options.skip) { mongoOptions.skip = options.skip; @@ -568,4 +568,4 @@ ExportAdapter.prototype.find = function(className, query, options = {}) { }); }; -module.exports = ExportAdapter; +module.exports = DatabaseController; diff --git a/src/DatabaseAdapter.js b/src/DatabaseAdapter.js index 5c957418..f4d43771 100644 --- a/src/DatabaseAdapter.js +++ b/src/DatabaseAdapter.js @@ -13,11 +13,9 @@ // * destroy(className, query, options) // * This list is incomplete and the database process is not fully modularized. // -// Default is ExportAdapter, which uses mongo. +// Default is DatabaseController, which uses mongo at this time. -var ExportAdapter = require('./ExportAdapter'); - -var adapter = ExportAdapter; +var adapter = require('./Controllers/DatabaseController'); var dbConnections = {}; var databaseURI = 'mongodb://localhost:27017/parse'; var appDatabaseURIs = {}; diff --git a/src/Schema.js b/src/Schema.js index 63dc6f37..7f7d4701 100644 --- a/src/Schema.js +++ b/src/Schema.js @@ -10,7 +10,7 @@ // keeping it this way for now. // // In API-handling code, you should only use the Schema class via the -// ExportAdapter. This will let us replace the schema logic for +// DatabaseController. This will let us replace the schema logic for // different databases. // TODO: hide all schema logic inside the database adapter. diff --git a/src/index.js b/src/index.js index dae92ee4..5062b6b0 100644 --- a/src/index.js +++ b/src/index.js @@ -45,7 +45,7 @@ addParseCloud(); // ParseServer works like a constructor of an express app. // The args that we understand are: -// "databaseAdapter": a class like ExportAdapter providing create, find, +// "databaseAdapter": a class like DatabaseController providing create, find, // update, and delete // "filesAdapter": a class like GridStoreAdapter providing create, get, // and delete