Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions spec/GridFSBucketStorageAdapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,14 @@ describe_only_db('mongo')('GridFSBucket', () => {
expect(gfsResult.toString('utf8')).toBe(twoMegabytesFile);
});

it('properly upload a file when disableIndexFieldValidation exist in databaseOptions', async () => {
const gfsAdapter = new GridFSBucketAdapter(databaseURI, { disableIndexFieldValidation: true });
const twoMegabytesFile = randomString(2048 * 1024);
const res = await gfsAdapter.createFile('myFileName', twoMegabytesFile);
expect(res._id).toBeTruthy();
expect(res.filename).toEqual('myFileName');
});

it('properly deletes a file from GridFS', async () => {
const gfsAdapter = new GridFSBucketAdapter(databaseURI);
await gfsAdapter.createFile('myFileName', 'a simple file');
Expand Down
24 changes: 24 additions & 0 deletions spec/schemas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2932,6 +2932,7 @@ describe('schemas', () => {
beforeEach(async () => {
await TestUtils.destroyAllDataPermanently(false);
await config.database.adapter.performInitialization({ VolatileClassesSchemas: [] });
databaseAdapter.disableIndexFieldValidation = false;
});

it('cannot create index if field does not exist', done => {
Expand Down Expand Up @@ -2960,6 +2961,29 @@ describe('schemas', () => {
});
});

it('can create index if field does not exist with disableIndexFieldValidation true ', async () => {
databaseAdapter.disableIndexFieldValidation = true;
await request({
url: 'http://localhost:8378/1/schemas/NewClass',
method: 'POST',
headers: masterKeyHeaders,
json: true,
body: {},
});
const response = await request({
url: 'http://localhost:8378/1/schemas/NewClass',
method: 'PUT',
headers: masterKeyHeaders,
json: true,
body: {
indexes: {
name1: { aString: 1 },
},
},
});
expect(response.data.indexes.name1).toEqual({ aString: 1 });
});

it('can create index on default field', done => {
request({
url: 'http://localhost:8378/1/schemas/NewClass',
Expand Down
1 change: 1 addition & 0 deletions src/Adapters/Files/GridFSBucketAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class GridFSBucketAdapter extends FilesAdapter {
useUnifiedTopology: true,
};
this._mongoOptions = Object.assign(defaultMongoOptions, mongoOptions);
delete this._mongoOptions.disableIndexFieldValidation;
}

_connect() {
Expand Down
4 changes: 4 additions & 0 deletions src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export class MongoStorageAdapter implements StorageAdapter {
_maxTimeMS: ?number;
canSortOnJoinTables: boolean;
enableSchemaHooks: boolean;
disableIndexFieldValidation: boolean;

constructor({ uri = defaults.DefaultMongoURI, collectionPrefix = '', mongoOptions = {} }: any) {
this._uri = uri;
Expand All @@ -152,7 +153,9 @@ export class MongoStorageAdapter implements StorageAdapter {
this._maxTimeMS = mongoOptions.maxTimeMS;
this.canSortOnJoinTables = true;
this.enableSchemaHooks = !!mongoOptions.enableSchemaHooks;
this.disableIndexFieldValidation = !!mongoOptions.disableIndexFieldValidation;
delete mongoOptions.enableSchemaHooks;
delete mongoOptions.disableIndexFieldValidation;
delete mongoOptions.maxTimeMS;
}

Expand Down Expand Up @@ -287,6 +290,7 @@ export class MongoStorageAdapter implements StorageAdapter {
} else {
Object.keys(field).forEach(key => {
if (
!this.disableIndexFieldValidation &&
!Object.prototype.hasOwnProperty.call(
fields,
key.indexOf('_p_') === 0 ? key.replace('_p_', '') : key
Expand Down
19 changes: 16 additions & 3 deletions src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -851,11 +851,14 @@ export class PostgresStorageAdapter implements StorageAdapter {
_pgp: any;
_stream: any;
_uuid: any;
disableIndexFieldValidation: boolean;

constructor({ uri, collectionPrefix = '', databaseOptions = {} }: any) {
this._collectionPrefix = collectionPrefix;
this.enableSchemaHooks = !!databaseOptions.enableSchemaHooks;
this.disableIndexFieldValidation = !!databaseOptions.disableIndexFieldValidation;
delete databaseOptions.enableSchemaHooks;
delete databaseOptions.disableIndexFieldValidation;

const { client, pgp } = createClient(uri, databaseOptions);
this._client = client;
Expand Down Expand Up @@ -975,7 +978,10 @@ export class PostgresStorageAdapter implements StorageAdapter {
delete existingIndexes[name];
} else {
Object.keys(field).forEach(key => {
if (!Object.prototype.hasOwnProperty.call(fields, key)) {
if (
!this.disableIndexFieldValidation &&
!Object.prototype.hasOwnProperty.call(fields, key)
) {
throw new Parse.Error(
Parse.Error.INVALID_QUERY,
`Field ${key} does not exist, cannot add index.`
Expand All @@ -990,8 +996,15 @@ export class PostgresStorageAdapter implements StorageAdapter {
}
});
await conn.tx('set-indexes-with-schema-format', async t => {
if (insertedIndexes.length > 0) {
await self.createIndexes(className, insertedIndexes, t);
try {
if (insertedIndexes.length > 0) {
await self.createIndexes(className, insertedIndexes, t);
}
} catch (e) {
const columnDoesNotExistError = e.errors?.[0]?.code === '42703';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional chaining is available from Node 14; Parse Server still supports Node 12.

Copy link
Member

@mtrezza mtrezza Nov 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use optional changing now, Parse Server 6 supports Node >=14; does lint on the latest alpha commit still report an error if you use optional chaining?

if (columnDoesNotExistError && !this.disableIndexFieldValidation) {
throw e;
}
}
if (deletedIndexes.length > 0) {
await self.dropIndexes(className, deletedIndexes, t);
Expand Down