Skip to content

Commit

Permalink
feat: Add Bytes type to Parse.Schema (#2001)
Browse files Browse the repository at this point in the history
  • Loading branch information
dplewis authored Aug 27, 2023
1 parent 9c0733f commit 343d0d7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
9 changes: 9 additions & 0 deletions integration/test/ParseSchemaTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe('Schema', () => {
.addString('stringField')
.addNumber('numberField')
.addBoolean('booleanField')
.addBytes('bytesField')
.addDate('dateField')
.addFile('fileField')
.addGeoPoint('geoPointField')
Expand All @@ -91,6 +92,7 @@ describe('Schema', () => {
assert.equal(result.fields.stringField.type, 'String');
assert.equal(result.fields.numberField.type, 'Number');
assert.equal(result.fields.booleanField.type, 'Boolean');
assert.equal(result.fields.bytesField.type, 'Bytes');
assert.equal(result.fields.dateField.type, 'Date');
assert.equal(result.fields.fileField.type, 'File');
assert.equal(result.fields.geoPointField.type, 'GeoPoint');
Expand Down Expand Up @@ -182,6 +184,7 @@ describe('Schema', () => {
required: true,
defaultValue: '2000-01-01T00:00:00.000Z',
})
.addBytes('bytesField', { required: true, defaultValue: 'ParseA==' })
.addFile('fileField', { required: true, defaultValue: file })
.addGeoPoint('geoPointField', { required: true, defaultValue: point })
.addPolygon('polygonField', { required: true, defaultValue: polygon })
Expand All @@ -204,6 +207,11 @@ describe('Schema', () => {
stringField: { type: 'String', required: true, defaultValue: 'world' },
numberField: { type: 'Number', required: true, defaultValue: 10 },
booleanField: { type: 'Boolean', required: true, defaultValue: false },
bytesField: {
type: 'Bytes',
required: true,
defaultValue: { __type: 'Bytes', base64: 'ParseA==' },
},
dateField: {
type: 'Date',
required: true,
Expand Down Expand Up @@ -245,6 +253,7 @@ describe('Schema', () => {
stringField: 'world',
numberField: 10,
booleanField: false,
bytesField: { __type: 'Bytes', base64: 'ParseA==' },
dateField: { __type: 'Date', iso: '2000-01-01T00:00:00.000Z' },
dateStringField: { __type: 'Date', iso: '2000-01-01T00:00:00.000Z' },
fileField: file.toJSON(),
Expand Down
20 changes: 20 additions & 0 deletions src/ParseSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const FIELD_TYPES = [
'String',
'Number',
'Boolean',
'Bytes',
'Date',
'File',
'GeoPoint',
Expand Down Expand Up @@ -242,6 +243,14 @@ class ParseSchema {
};
}
}
if (type === 'Bytes') {
if (options && options.defaultValue) {
fieldOptions.defaultValue = {
__type: 'Bytes',
base64: options.defaultValue,
};
}
}
this._fields[name] = fieldOptions;
return this;
}
Expand Down Expand Up @@ -303,6 +312,17 @@ class ParseSchema {
return this.addField(name, 'Boolean', options);
}

/**
* Adding Bytes Field
*
* @param {string} name Name of the field that will be created on Parse
* @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
* @returns {Parse.Schema} Returns the schema, so you can chain this call.
*/
addBytes(name: string, options: FieldOptions) {
return this.addField(name, 'Bytes', options);
}

/**
* Adding Date Field
*
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/ParseSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('ParseSchema', () => {
.addString('stringField')
.addNumber('numberField')
.addBoolean('booleanField')
.addBytes('bytesField')
.addDate('dateField')
.addFile('fileField')
.addGeoPoint('geoPointField')
Expand All @@ -70,6 +71,7 @@ describe('ParseSchema', () => {
expect(schema._fields.stringField.type).toEqual('String');
expect(schema._fields.numberField.type).toEqual('Number');
expect(schema._fields.booleanField.type).toEqual('Boolean');
expect(schema._fields.bytesField.type).toEqual('Bytes');
expect(schema._fields.dateField.type).toEqual('Date');
expect(schema._fields.fileField.type).toEqual('File');
expect(schema._fields.geoPointField.type).toEqual('GeoPoint');
Expand Down Expand Up @@ -103,6 +105,10 @@ describe('ParseSchema', () => {
required: true,
defaultValue: 'hello',
})
.addBytes('bytesField', {
required: true,
defaultValue: 'ParseA==',
})
.addDate('dateField', {
required: true,
defaultValue: '2000-01-01T00:00:00.000Z',
Expand Down Expand Up @@ -131,6 +137,14 @@ describe('ParseSchema', () => {
iso: new Date('2000-01-01T00:00:00.000Z'),
},
});
expect(schema._fields.bytesField).toEqual({
type: 'Bytes',
required: true,
defaultValue: {
__type: 'Bytes',
base64: 'ParseA==',
},
});
});

it('can create schema indexes', done => {
Expand Down

0 comments on commit 343d0d7

Please sign in to comment.