Skip to content

Commit e544c91

Browse files
committed
error handling
1 parent ad9998a commit e544c91

File tree

2 files changed

+82
-12
lines changed

2 files changed

+82
-12
lines changed

spec/schemas.spec.js

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,10 @@ describe('schemas', () => {
17841784
name2: { field2: 1},
17851785
},
17861786
});
1787-
done();
1787+
config.database.adapter.getIndexes('NewClass').then((indexes) => {
1788+
expect(indexes.length).toBe(3);
1789+
done();
1790+
});
17881791
});
17891792
});
17901793

@@ -1860,7 +1863,7 @@ describe('schemas', () => {
18601863
indexes: {
18611864
name1: { field1: 1 },
18621865
name2: { field2: 1 },
1863-
name3: { field3: 1 },
1866+
name3: { field3: 1, field4: 1 },
18641867
}
18651868
}
18661869
}, (error, response, body) => {
@@ -1876,7 +1879,7 @@ describe('schemas', () => {
18761879
indexes: {
18771880
name1: { field1: 1 },
18781881
name2: { field2: 1 },
1879-
name3: { field3: 1 },
1882+
name3: { field3: 1, field4: 1 },
18801883
}
18811884
})).toEqual(undefined);
18821885
request.get({
@@ -1896,7 +1899,7 @@ describe('schemas', () => {
18961899
indexes: {
18971900
name1: { field1: 1 },
18981901
name2: { field2: 1 },
1899-
name3: { field3: 1 },
1902+
name3: { field3: 1, field4: 1 },
19001903
},
19011904
});
19021905
config.database.adapter.getIndexes('NewClass').then((indexes) => {
@@ -2102,4 +2105,63 @@ describe('schemas', () => {
21022105
});
21032106
})
21042107
});
2108+
2109+
it_exclude_dbs(['postgres'])('cannot delete index that does not exist', done => {
2110+
request.post({
2111+
url: 'http://localhost:8378/1/schemas/NewClass',
2112+
headers: masterKeyHeaders,
2113+
json: true,
2114+
body: {},
2115+
}, () => {
2116+
request.put({
2117+
url: 'http://localhost:8378/1/schemas/NewClass',
2118+
headers: masterKeyHeaders,
2119+
json: true,
2120+
body: {
2121+
indexes: {
2122+
unknownIndex: { __op: 'Delete' }
2123+
}
2124+
}
2125+
}, (error, response, body) => {
2126+
expect(body.code).toBe(Parse.Error.INVALID_QUERY);
2127+
expect(body.error).toBe('Index unknownIndex does not exist, cannot delete.');
2128+
done();
2129+
});
2130+
})
2131+
});
2132+
2133+
it_exclude_dbs(['postgres'])('cannot update index that exist', done => {
2134+
request.post({
2135+
url: 'http://localhost:8378/1/schemas/NewClass',
2136+
headers: masterKeyHeaders,
2137+
json: true,
2138+
body: {},
2139+
}, () => {
2140+
request.put({
2141+
url: 'http://localhost:8378/1/schemas/NewClass',
2142+
headers: masterKeyHeaders,
2143+
json: true,
2144+
body: {
2145+
indexes: {
2146+
name1: { field1: 1 }
2147+
}
2148+
}
2149+
}, () => {
2150+
request.put({
2151+
url: 'http://localhost:8378/1/schemas/NewClass',
2152+
headers: masterKeyHeaders,
2153+
json: true,
2154+
body: {
2155+
indexes: {
2156+
name1: { field2: 1 }
2157+
}
2158+
}
2159+
}, (error, response, body) => {
2160+
expect(body.code).toBe(Parse.Error.INVALID_QUERY);
2161+
expect(body.error).toBe('Index name1 exists, cannot update.');
2162+
done();
2163+
});
2164+
});
2165+
})
2166+
});
21052167
});

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,23 @@ export class MongoStorageAdapter {
179179
}
180180
const deletePromises = [];
181181
const insertedIndexes = [];
182-
Object.keys(submittedIndexes).forEach(indexName => {
183-
if (submittedIndexes[indexName].__op === 'Delete') {
184-
const promise = this.dropIndex(className, indexName);
182+
Object.keys(submittedIndexes).forEach(name => {
183+
const field = submittedIndexes[name];
184+
if (existingIndexes[name] && field.__op !== 'Delete') {
185+
throw new Parse.Error(Parse.Error.INVALID_QUERY, `Index ${name} exists, cannot update.`);
186+
}
187+
if (!existingIndexes[name] && field.__op === 'Delete') {
188+
throw new Parse.Error(Parse.Error.INVALID_QUERY, `Index ${name} does not exist, cannot delete.`);
189+
}
190+
if (field.__op === 'Delete') {
191+
const promise = this.dropIndex(className, name);
185192
deletePromises.push(promise);
186-
delete existingIndexes[indexName];
193+
delete existingIndexes[name];
187194
} else {
188-
existingIndexes[indexName] = submittedIndexes[indexName];
195+
existingIndexes[name] = field;
189196
insertedIndexes.push({
190-
key: submittedIndexes[indexName],
191-
name: indexName,
197+
key: field,
198+
name,
192199
});
193200
}
194201
});
@@ -208,7 +215,8 @@ export class MongoStorageAdapter {
208215
schema = convertParseSchemaToMongoSchema(schema);
209216
const mongoObject = mongoSchemaFromFieldsAndClassNameAndCLP(schema.fields, className, schema.classLevelPermissions, schema.indexes);
210217
mongoObject._id = className;
211-
return this._schemaCollection()
218+
return this.setIndexes(className, schema.indexes, {}, schema)
219+
.then(() => this._schemaCollection())
212220
.then(schemaCollection => schemaCollection._collection.insertOne(mongoObject))
213221
.then(result => MongoSchemaCollection._TESTmongoSchemaToParseSchema(result.ops[0]))
214222
.catch(error => {

0 commit comments

Comments
 (0)