Skip to content

Commit d7b226e

Browse files
committed
Fixes tests, improves flow typing
1 parent 79dd066 commit d7b226e

9 files changed

+34
-25
lines changed

spec/MongoStorageAdapter.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

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

spec/ParsePolygon.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const TestObject = Parse.Object.extend('TestObject');
2-
const MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter').MongoStorageAdapter;
2+
import MongoStorageAdapter from '../src/Adapters/Storage/Mongo/MongoStorageAdapter';
33
const mongoURI = 'mongodb://localhost:27017/parseServerMongoAdapterTestDatabase';
44
const rp = require('request-promise');
55
const defaultHeaders = {

spec/ParseQuery.FullTextSearch.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
const MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter').MongoStorageAdapter;
3+
import MongoStorageAdapter from '../src/Adapters/Storage/Mongo/MongoStorageAdapter';
44
const mongoURI = 'mongodb://localhost:27017/parseServerMongoAdapterTestDatabase';
5-
const PostgresStorageAdapter = require('../src/Adapters/Storage/Postgres/PostgresStorageAdapter').PostgresStorageAdapter;
5+
import PostgresStorageAdapter from '../src/Adapters/Storage/Postgres/PostgresStorageAdapter';
66
const postgresURI = 'postgres://localhost:5432/parse_server_postgres_adapter_test_database';
77
const Parse = require('parse/node');
88
const rp = require('request-promise');

spec/PostgresInitOptions.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const Parse = require('parse/node').Parse;
2-
const PostgresStorageAdapter = require('../src/Adapters/Storage/Postgres/PostgresStorageAdapter').PostgresStorageAdapter;
2+
import PostgresStorageAdapter from '../src/Adapters/Storage/Postgres/PostgresStorageAdapter';
33
const postgresURI = 'postgres://localhost:5432/parse_server_postgres_adapter_test_database';
44
const ParseServer = require("../src/index");
55
const express = require('express');

spec/PostgresStorageAdapter.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const PostgresStorageAdapter = require('../src/Adapters/Storage/Postgres/PostgresStorageAdapter');
1+
import PostgresStorageAdapter from '../src/Adapters/Storage/Postgres/PostgresStorageAdapter';
22
const databaseURI = 'postgres://localhost:5432/parse_server_postgres_adapter_test_database';
33

44
describe_only_db('postgres')('PostgresStorageAdapter', () => {

spec/helper.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ var cache = require('../src/cache').default;
2727
var ParseServer = require('../src/index').ParseServer;
2828
var path = require('path');
2929
var TestUtils = require('../src/TestUtils');
30-
var MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter').MongoStorageAdapter;
3130
const GridStoreAdapter = require('../src/Adapters/Files/GridStoreAdapter').GridStoreAdapter;
3231
const FSAdapter = require('@parse/fs-files-adapter');
33-
const PostgresStorageAdapter = require('../src/Adapters/Storage/Postgres/PostgresStorageAdapter').PostgresStorageAdapter;
32+
import PostgresStorageAdapter from '../src/Adapters/Storage/Postgres/PostgresStorageAdapter';
33+
import MongoStorageAdapter from '../src/Adapters/Storage/Mongo/MongoStorageAdapter';
3434
const RedisCacheAdapter = require('../src/Adapters/Cache/RedisCacheAdapter').default;
3535

3636
const mongoURI = 'mongodb://localhost:27017/parseServerMongoAdapterTestDatabase';

spec/index.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var ParseServer = require("../src/index");
66
var Config = require('../src/Config');
77
var express = require('express');
88

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

1111
describe('server', () => {
1212
it('requires a master key and app id', done => {

src/Adapters/Storage/Mongo/MongoSchemaCollection.js

+12
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ class MongoSchemaCollection {
137137
return this._collection._mongoCollection.findAndRemove(_mongoSchemaQueryFromNameQuery(name), []);
138138
}
139139

140+
insertSchema(schema: any) {
141+
return this._collection.insertOne(schema)
142+
.then(result => mongoSchemaToParseSchema(result.ops[0]))
143+
.catch(error => {
144+
if (error.code === 11000) { //Mongo's duplicate key error
145+
throw new Parse.Error(Parse.Error.DUPLICATE_VALUE, 'Class already exists.');
146+
} else {
147+
throw error;
148+
}
149+
})
150+
}
151+
140152
updateSchema(name: string, update) {
141153
return this._collection.updateOne(_mongoSchemaQueryFromNameQuery(name), update);
142154
}

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

+13-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import MongoSchemaCollection from './MongoSchemaCollection';
44
import { StorageAdapter } from '../StorageAdapter';
55
import type { SchemaType,
66
QueryType,
7+
StorageClass,
78
QueryOptions } from '../StorageAdapter';
89
import {
910
parse as parseUrl,
@@ -89,6 +90,10 @@ const mongoSchemaFromFieldsAndClassNameAndCLP = (fields, className, classLevelPe
8990
mongoObject._metadata.indexes = indexes;
9091
}
9192

93+
if (!mongoObject._metadata) { // cleanup the unused _metadata
94+
delete mongoObject._metadata;
95+
}
96+
9297
return mongoObject;
9398
}
9499

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

171-
_schemaCollection() {
176+
_schemaCollection(): Promise<MongoSchemaCollection> {
172177
return this.connect()
173178
.then(() => this._adaptiveCollection(MongoSchemaCollectionName))
174179
.then(collection => new MongoSchemaCollection(collection));
@@ -182,7 +187,7 @@ export class MongoStorageAdapter implements StorageAdapter {
182187
});
183188
}
184189

185-
setClassLevelPermissions(className: string, CLPs: any) {
190+
setClassLevelPermissions(className: string, CLPs: any): Promise<void> {
186191
return this._schemaCollection()
187192
.then(schemaCollection => schemaCollection.updateSchema(className, {
188193
$set: { '_metadata.class_permissions': CLPs }
@@ -258,24 +263,16 @@ export class MongoStorageAdapter implements StorageAdapter {
258263
});
259264
}
260265

261-
createClass(className: string, schema: SchemaType) {
266+
createClass(className: string, schema: SchemaType): Promise<void> {
262267
schema = convertParseSchemaToMongoSchema(schema);
263268
const mongoObject = mongoSchemaFromFieldsAndClassNameAndCLP(schema.fields, className, schema.classLevelPermissions, schema.indexes);
264269
mongoObject._id = className;
265270
return this.setIndexesWithSchemaFormat(className, schema.indexes, {}, schema.fields)
266271
.then(() => this._schemaCollection())
267-
.then(schemaCollection => schemaCollection._collection.insertOne(mongoObject))
268-
.then(result => MongoSchemaCollection._TESTmongoSchemaToParseSchema(result.ops[0]))
269-
.catch(error => {
270-
if (error.code === 11000) { //Mongo's duplicate key error
271-
throw new Parse.Error(Parse.Error.DUPLICATE_VALUE, 'Class already exists.');
272-
} else {
273-
throw error;
274-
}
275-
})
272+
.then(schemaCollection => schemaCollection.insertSchema(mongoObject));
276273
}
277274

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

358355
// Return a promise for the schema with the given name, in Parse format. If
359356
// this adapter doesn't know about the schema, return a promise that rejects with
360357
// undefined as the reason.
361-
getClass(className: string) {
358+
getClass(className: string): Promise<StorageClass> {
362359
return this._schemaCollection()
363360
.then(schemasCollection => schemasCollection._fetchOneSchemaFrom_SCHEMA(className))
364361
}
@@ -628,7 +625,7 @@ export class MongoStorageAdapter implements StorageAdapter {
628625
.then(collection => collection._mongoCollection.dropIndexes());
629626
}
630627

631-
updateSchemaWithIndexes() {
628+
updateSchemaWithIndexes(): Promise<any> {
632629
return this.getAllClasses()
633630
.then((classes) => {
634631
const promises = classes.map((schema) => {

0 commit comments

Comments
 (0)