Skip to content

Commit 343d0d7

Browse files
authored
feat: Add Bytes type to Parse.Schema (#2001)
1 parent 9c0733f commit 343d0d7

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

integration/test/ParseSchemaTest.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ describe('Schema', () => {
7272
.addString('stringField')
7373
.addNumber('numberField')
7474
.addBoolean('booleanField')
75+
.addBytes('bytesField')
7576
.addDate('dateField')
7677
.addFile('fileField')
7778
.addGeoPoint('geoPointField')
@@ -91,6 +92,7 @@ describe('Schema', () => {
9192
assert.equal(result.fields.stringField.type, 'String');
9293
assert.equal(result.fields.numberField.type, 'Number');
9394
assert.equal(result.fields.booleanField.type, 'Boolean');
95+
assert.equal(result.fields.bytesField.type, 'Bytes');
9496
assert.equal(result.fields.dateField.type, 'Date');
9597
assert.equal(result.fields.fileField.type, 'File');
9698
assert.equal(result.fields.geoPointField.type, 'GeoPoint');
@@ -182,6 +184,7 @@ describe('Schema', () => {
182184
required: true,
183185
defaultValue: '2000-01-01T00:00:00.000Z',
184186
})
187+
.addBytes('bytesField', { required: true, defaultValue: 'ParseA==' })
185188
.addFile('fileField', { required: true, defaultValue: file })
186189
.addGeoPoint('geoPointField', { required: true, defaultValue: point })
187190
.addPolygon('polygonField', { required: true, defaultValue: polygon })
@@ -204,6 +207,11 @@ describe('Schema', () => {
204207
stringField: { type: 'String', required: true, defaultValue: 'world' },
205208
numberField: { type: 'Number', required: true, defaultValue: 10 },
206209
booleanField: { type: 'Boolean', required: true, defaultValue: false },
210+
bytesField: {
211+
type: 'Bytes',
212+
required: true,
213+
defaultValue: { __type: 'Bytes', base64: 'ParseA==' },
214+
},
207215
dateField: {
208216
type: 'Date',
209217
required: true,
@@ -245,6 +253,7 @@ describe('Schema', () => {
245253
stringField: 'world',
246254
numberField: 10,
247255
booleanField: false,
256+
bytesField: { __type: 'Bytes', base64: 'ParseA==' },
248257
dateField: { __type: 'Date', iso: '2000-01-01T00:00:00.000Z' },
249258
dateStringField: { __type: 'Date', iso: '2000-01-01T00:00:00.000Z' },
250259
fileField: file.toJSON(),

src/ParseSchema.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const FIELD_TYPES = [
1212
'String',
1313
'Number',
1414
'Boolean',
15+
'Bytes',
1516
'Date',
1617
'File',
1718
'GeoPoint',
@@ -242,6 +243,14 @@ class ParseSchema {
242243
};
243244
}
244245
}
246+
if (type === 'Bytes') {
247+
if (options && options.defaultValue) {
248+
fieldOptions.defaultValue = {
249+
__type: 'Bytes',
250+
base64: options.defaultValue,
251+
};
252+
}
253+
}
245254
this._fields[name] = fieldOptions;
246255
return this;
247256
}
@@ -303,6 +312,17 @@ class ParseSchema {
303312
return this.addField(name, 'Boolean', options);
304313
}
305314

315+
/**
316+
* Adding Bytes Field
317+
*
318+
* @param {string} name Name of the field that will be created on Parse
319+
* @param {object} options See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Schema.html#addField addField}
320+
* @returns {Parse.Schema} Returns the schema, so you can chain this call.
321+
*/
322+
addBytes(name: string, options: FieldOptions) {
323+
return this.addField(name, 'Bytes', options);
324+
}
325+
306326
/**
307327
* Adding Date Field
308328
*

src/__tests__/ParseSchema-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ describe('ParseSchema', () => {
5757
.addString('stringField')
5858
.addNumber('numberField')
5959
.addBoolean('booleanField')
60+
.addBytes('bytesField')
6061
.addDate('dateField')
6162
.addFile('fileField')
6263
.addGeoPoint('geoPointField')
@@ -70,6 +71,7 @@ describe('ParseSchema', () => {
7071
expect(schema._fields.stringField.type).toEqual('String');
7172
expect(schema._fields.numberField.type).toEqual('Number');
7273
expect(schema._fields.booleanField.type).toEqual('Boolean');
74+
expect(schema._fields.bytesField.type).toEqual('Bytes');
7375
expect(schema._fields.dateField.type).toEqual('Date');
7476
expect(schema._fields.fileField.type).toEqual('File');
7577
expect(schema._fields.geoPointField.type).toEqual('GeoPoint');
@@ -103,6 +105,10 @@ describe('ParseSchema', () => {
103105
required: true,
104106
defaultValue: 'hello',
105107
})
108+
.addBytes('bytesField', {
109+
required: true,
110+
defaultValue: 'ParseA==',
111+
})
106112
.addDate('dateField', {
107113
required: true,
108114
defaultValue: '2000-01-01T00:00:00.000Z',
@@ -131,6 +137,14 @@ describe('ParseSchema', () => {
131137
iso: new Date('2000-01-01T00:00:00.000Z'),
132138
},
133139
});
140+
expect(schema._fields.bytesField).toEqual({
141+
type: 'Bytes',
142+
required: true,
143+
defaultValue: {
144+
__type: 'Bytes',
145+
base64: 'ParseA==',
146+
},
147+
});
134148
});
135149

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

0 commit comments

Comments
 (0)