Skip to content

Commit

Permalink
Fixes tests, improves flow typing
Browse files Browse the repository at this point in the history
  • Loading branch information
flovilmart committed Dec 30, 2017
1 parent 277bdec commit ef882d3
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 25 deletions.
2 changes: 1 addition & 1 deletion spec/MongoStorageAdapter.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { MongoStorageAdapter } = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter');
import MongoStorageAdapter from '../src/Adapters/Storage/Mongo/MongoStorageAdapter';
const { MongoClient } = require('mongodb');
const databaseURI = 'mongodb://localhost:27017/parseServerMongoAdapterTestDatabase';

Expand Down
2 changes: 1 addition & 1 deletion spec/ParsePolygon.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const TestObject = Parse.Object.extend('TestObject');
const MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter').MongoStorageAdapter;
import MongoStorageAdapter from '../src/Adapters/Storage/Mongo/MongoStorageAdapter';
const mongoURI = 'mongodb://localhost:27017/parseServerMongoAdapterTestDatabase';
const rp = require('request-promise');
const defaultHeaders = {
Expand Down
4 changes: 2 additions & 2 deletions spec/ParseQuery.FullTextSearch.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter').MongoStorageAdapter;
import MongoStorageAdapter from '../src/Adapters/Storage/Mongo/MongoStorageAdapter';
const mongoURI = 'mongodb://localhost:27017/parseServerMongoAdapterTestDatabase';
const PostgresStorageAdapter = require('../src/Adapters/Storage/Postgres/PostgresStorageAdapter').PostgresStorageAdapter;
import PostgresStorageAdapter from '../src/Adapters/Storage/Postgres/PostgresStorageAdapter';
const postgresURI = 'postgres://localhost:5432/parse_server_postgres_adapter_test_database';
const Parse = require('parse/node');
const rp = require('request-promise');
Expand Down
2 changes: 1 addition & 1 deletion spec/PostgresInitOptions.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Parse = require('parse/node').Parse;
const PostgresStorageAdapter = require('../src/Adapters/Storage/Postgres/PostgresStorageAdapter').PostgresStorageAdapter;
import PostgresStorageAdapter from '../src/Adapters/Storage/Postgres/PostgresStorageAdapter';
const postgresURI = 'postgres://localhost:5432/parse_server_postgres_adapter_test_database';
const ParseServer = require("../src/index");
const express = require('express');
Expand Down
2 changes: 1 addition & 1 deletion spec/PostgresStorageAdapter.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const PostgresStorageAdapter = require('../src/Adapters/Storage/Postgres/PostgresStorageAdapter');
import PostgresStorageAdapter from '../src/Adapters/Storage/Postgres/PostgresStorageAdapter';
const databaseURI = 'postgres://localhost:5432/parse_server_postgres_adapter_test_database';

describe_only_db('postgres')('PostgresStorageAdapter', () => {
Expand Down
4 changes: 2 additions & 2 deletions spec/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ var cache = require('../src/cache').default;
var ParseServer = require('../src/index').ParseServer;
var path = require('path');
var TestUtils = require('../src/TestUtils');
var MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter').MongoStorageAdapter;
const GridStoreAdapter = require('../src/Adapters/Files/GridStoreAdapter').GridStoreAdapter;
const FSAdapter = require('@parse/fs-files-adapter');
const PostgresStorageAdapter = require('../src/Adapters/Storage/Postgres/PostgresStorageAdapter').PostgresStorageAdapter;
import PostgresStorageAdapter from '../src/Adapters/Storage/Postgres/PostgresStorageAdapter';
import MongoStorageAdapter from '../src/Adapters/Storage/Mongo/MongoStorageAdapter';
const RedisCacheAdapter = require('../src/Adapters/Cache/RedisCacheAdapter').default;

const mongoURI = 'mongodb://localhost:27017/parseServerMongoAdapterTestDatabase';
Expand Down
2 changes: 1 addition & 1 deletion spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var ParseServer = require("../src/index");
var Config = require('../src/Config');
var express = require('express');

const MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter').MongoStorageAdapter;
import MongoStorageAdapter from '../src/Adapters/Storage/Mongo/MongoStorageAdapter';

describe('server', () => {
it('requires a master key and app id', done => {
Expand Down
12 changes: 12 additions & 0 deletions src/Adapters/Storage/Mongo/MongoSchemaCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ class MongoSchemaCollection {
return this._collection._mongoCollection.findAndRemove(_mongoSchemaQueryFromNameQuery(name), []);
}

insertSchema(schema: any) {
return this._collection.insertOne(schema)
.then(result => mongoSchemaToParseSchema(result.ops[0]))
.catch(error => {
if (error.code === 11000) { //Mongo's duplicate key error
throw new Parse.Error(Parse.Error.DUPLICATE_VALUE, 'Class already exists.');
} else {
throw error;
}
})
}

updateSchema(name: string, update) {
return this._collection.updateOne(_mongoSchemaQueryFromNameQuery(name), update);
}
Expand Down
29 changes: 13 additions & 16 deletions src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import MongoSchemaCollection from './MongoSchemaCollection';
import { StorageAdapter } from '../StorageAdapter';
import type { SchemaType,
QueryType,
StorageClass,
QueryOptions } from '../StorageAdapter';
import {
parse as parseUrl,
Expand Down Expand Up @@ -89,6 +90,10 @@ const mongoSchemaFromFieldsAndClassNameAndCLP = (fields, className, classLevelPe
mongoObject._metadata.indexes = indexes;
}

if (!mongoObject._metadata) { // cleanup the unused _metadata
delete mongoObject._metadata;
}

return mongoObject;
}

Expand Down Expand Up @@ -168,7 +173,7 @@ export class MongoStorageAdapter implements StorageAdapter {
.then(rawCollection => new MongoCollection(rawCollection));
}

_schemaCollection() {
_schemaCollection(): Promise<MongoSchemaCollection> {
return this.connect()
.then(() => this._adaptiveCollection(MongoSchemaCollectionName))
.then(collection => new MongoSchemaCollection(collection));
Expand All @@ -182,7 +187,7 @@ export class MongoStorageAdapter implements StorageAdapter {
});
}

setClassLevelPermissions(className: string, CLPs: any) {
setClassLevelPermissions(className: string, CLPs: any): Promise<void> {
return this._schemaCollection()
.then(schemaCollection => schemaCollection.updateSchema(className, {
$set: { '_metadata.class_permissions': CLPs }
Expand Down Expand Up @@ -258,24 +263,16 @@ export class MongoStorageAdapter implements StorageAdapter {
});
}

createClass(className: string, schema: SchemaType) {
createClass(className: string, schema: SchemaType): Promise<void> {
schema = convertParseSchemaToMongoSchema(schema);
const mongoObject = mongoSchemaFromFieldsAndClassNameAndCLP(schema.fields, className, schema.classLevelPermissions, schema.indexes);
mongoObject._id = className;
return this.setIndexesWithSchemaFormat(className, schema.indexes, {}, schema.fields)
.then(() => this._schemaCollection())
.then(schemaCollection => schemaCollection._collection.insertOne(mongoObject))
.then(result => MongoSchemaCollection._TESTmongoSchemaToParseSchema(result.ops[0]))
.catch(error => {
if (error.code === 11000) { //Mongo's duplicate key error
throw new Parse.Error(Parse.Error.DUPLICATE_VALUE, 'Class already exists.');
} else {
throw error;
}
})
.then(schemaCollection => schemaCollection.insertSchema(mongoObject));
}

addFieldIfNotExists(className: string, fieldName: string, type: any) {
addFieldIfNotExists(className: string, fieldName: string, type: any): Promise<void> {
return this._schemaCollection()
.then(schemaCollection => schemaCollection.addFieldIfNotExists(className, fieldName, type))
.then(() => this.createIndexesIfNeeded(className, fieldName, type));
Expand Down Expand Up @@ -351,14 +348,14 @@ export class MongoStorageAdapter implements StorageAdapter {
// Return a promise for all schemas known to this adapter, in Parse format. In case the
// schemas cannot be retrieved, returns a promise that rejects. Requirements for the
// rejection reason are TBD.
getAllClasses() {
getAllClasses(): Promise<StorageClass[]> {
return this._schemaCollection().then(schemasCollection => schemasCollection._fetchAllSchemasFrom_SCHEMA());
}

// Return a promise for the schema with the given name, in Parse format. If
// this adapter doesn't know about the schema, return a promise that rejects with
// undefined as the reason.
getClass(className: string) {
getClass(className: string): Promise<StorageClass> {
return this._schemaCollection()
.then(schemasCollection => schemasCollection._fetchOneSchemaFrom_SCHEMA(className))
}
Expand Down Expand Up @@ -626,7 +623,7 @@ export class MongoStorageAdapter implements StorageAdapter {
.then(collection => collection._mongoCollection.dropIndexes());
}

updateSchemaWithIndexes() {
updateSchemaWithIndexes(): Promise<any> {
return this.getAllClasses()
.then((classes) => {
const promises = classes.map((schema) => {
Expand Down

0 comments on commit ef882d3

Please sign in to comment.