Skip to content

Commit

Permalink
feat: allow creating of unique indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
dblythy committed Jun 27, 2023
1 parent f8b5a99 commit ad62422
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 2 deletions.
100 changes: 100 additions & 0 deletions spec/schemas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3051,6 +3051,42 @@ describe('schemas', () => {
});
});

it('allows add unique index when you create a class', async () => {
await reconfigureServer({ silent: false });
const response = await request({
url: 'http://localhost:8378/1/schemas',
method: 'POST',
headers: masterKeyHeaders,
json: true,
body: {
className: 'NewClass',
fields: {
aString: { type: 'String' },
},
indexes: {
name1: { aString: 1, _options: { unique: true } },
},
},
});
expect(response.data).toEqual({
className: 'NewClass',
fields: {
ACL: { type: 'ACL' },
createdAt: { type: 'Date' },
updatedAt: { type: 'Date' },
objectId: { type: 'String' },
aString: { type: 'String' },
},
classLevelPermissions: defaultClassLevelPermissions,
indexes: {
name1: { aString: 1, _options: { unique: true } },
},
});
const indexes = await config.database.adapter.getIndexes('NewClass');
expect(indexes.length).toBe(2);
expect(indexes.filter(row => row.unique).length).toEqual(1);
});

it('empty index returns nothing', done => {
request({
url: 'http://localhost:8378/1/schemas',
Expand Down Expand Up @@ -3148,6 +3184,70 @@ describe('schemas', () => {
});
});

it('lets you add unique indexes', async () => {
await request({
url: 'http://localhost:8378/1/schemas/NewClass',
method: 'POST',
headers: masterKeyHeaders,
json: true,
body: {},
});
let response = await request({
url: 'http://localhost:8378/1/schemas/NewClass',
method: 'PUT',
headers: masterKeyHeaders,
json: true,
body: {
fields: {
aString: { type: 'String' },
},
indexes: {
name1: { aString: 1, _options: { unique: true } },
},
},
});
expect(
dd(response.data, {
className: 'NewClass',
fields: {
ACL: { type: 'ACL' },
createdAt: { type: 'Date' },
updatedAt: { type: 'Date' },
objectId: { type: 'String' },
aString: { type: 'String' },
},
classLevelPermissions: defaultClassLevelPermissions,
indexes: {
_id_: { _id: 1 },
name1: { aString: 1, _options: { unique: true } },
},
})
).toEqual(undefined);
response = await request({
url: 'http://localhost:8378/1/schemas/NewClass',
headers: masterKeyHeaders,
json: true,
});
expect(response.data).toEqual({
className: 'NewClass',
fields: {
ACL: { type: 'ACL' },
createdAt: { type: 'Date' },
updatedAt: { type: 'Date' },
objectId: { type: 'String' },
aString: { type: 'String' },
},
classLevelPermissions: defaultClassLevelPermissions,
indexes: {
_id_: { _id: 1 },
name1: { aString: 1, _options: { unique: true } },
},
});
const indexes = await config.database.adapter.getIndexes('NewClass');
expect(indexes.length).toEqual(2);
expect(indexes.filter(row => row.unique).length).toEqual(1);
});

it_only_db('mongo')('lets you add index with with pointer like structure', done => {
request({
url: 'http://localhost:8378/1/schemas/NewClass',
Expand Down
16 changes: 14 additions & 2 deletions src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import MongoCollection from './MongoCollection';
import MongoSchemaCollection from './MongoSchemaCollection';
import { StorageAdapter } from '../StorageAdapter';
import deepcopy from 'deepcopy';
import type { SchemaType, QueryType, StorageClass, QueryOptions } from '../StorageAdapter';
import { parse as parseUrl, format as formatUrl } from '../../../vendor/mongodbUrl';
import {
Expand Down Expand Up @@ -275,7 +276,13 @@ export class MongoStorageAdapter implements StorageAdapter {
const deletePromises = [];
const insertedIndexes = [];
Object.keys(submittedIndexes).forEach(name => {
const field = submittedIndexes[name];
const field = deepcopy(submittedIndexes[name]);
let indexOptions = {};
if (field._options) {
indexOptions = field._options;
delete field._options;
}
console.log(existingIndexes[name], field);
if (existingIndexes[name] && field.__op !== 'Delete') {
throw new Parse.Error(Parse.Error.INVALID_QUERY, `Index ${name} exists, cannot update.`);
}
Expand Down Expand Up @@ -303,11 +310,16 @@ export class MongoStorageAdapter implements StorageAdapter {
);
}
});
existingIndexes[name] = field;
insertedIndexes.push({
key: field,
name,
...indexOptions,
});
const fieldCopy = deepcopy(field);
if (indexOptions) {
fieldCopy._options = indexOptions;
}
existingIndexes[name] = fieldCopy;
}
});
let insertPromise = Promise.resolve();
Expand Down

0 comments on commit ad62422

Please sign in to comment.