Skip to content

Commit b27ff60

Browse files
dplewisflovilmart
authored andcommitted
Support for Parse Schema (parse-community#489)
* Support for Parse Schema * multiple update * test fix * bump node to 6.11.4 * docs * improve docs * jest tests
1 parent a712abb commit b27ff60

File tree

6 files changed

+1184
-0
lines changed

6 files changed

+1184
-0
lines changed

integration/test/ParseSchemaTest.js

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
const assert = require('assert');
2+
const clear = require('./clear');
3+
const Parse = require('../../node');
4+
5+
describe('Schema', () => {
6+
before(() => {
7+
Parse.initialize('integration');
8+
Parse.CoreManager.set('SERVER_URL', 'http://localhost:1337/parse');
9+
Parse.CoreManager.set('MASTER_KEY', 'notsosecret');
10+
Parse.Storage._clear();
11+
});
12+
13+
beforeEach((done) => {
14+
clear().then(() => {
15+
done();
16+
});
17+
});
18+
19+
it('invalid get all no schema', (done) => {
20+
Parse.Schema.all().then(() => {}).fail((e) => {
21+
done();
22+
});
23+
});
24+
25+
it('invalid get no schema', (done) => {
26+
const testSchema = new Parse.Schema('SchemaTest');
27+
testSchema.get().then(() => {}).fail((e) => {
28+
done();
29+
});
30+
});
31+
32+
it('save', (done) => {
33+
const testSchema = new Parse.Schema('SchemaTest');
34+
testSchema.save().then((result) => {
35+
assert.equal(result.className, 'SchemaTest');
36+
done();
37+
});
38+
});
39+
40+
it('get', (done) => {
41+
const testSchema = new Parse.Schema('SchemaTest');
42+
testSchema
43+
.addField('defaultFieldString')
44+
.addString('stringField')
45+
.addNumber('numberField')
46+
.addBoolean('booleanField')
47+
.addDate('dateField')
48+
.addFile('fileField')
49+
.addGeoPoint('geoPointField')
50+
.addArray('arrayField')
51+
.addObject('objectField')
52+
.addPointer('pointerField', '_User')
53+
.addRelation('relationField', '_User');
54+
55+
testSchema.save().then(() => {
56+
return testSchema.get();
57+
}).then((result) => {
58+
assert.equal(result.fields.defaultFieldString.type, 'String');
59+
assert.equal(result.fields.stringField.type, 'String');
60+
assert.equal(result.fields.numberField.type, 'Number');
61+
assert.equal(result.fields.booleanField.type, 'Boolean');
62+
assert.equal(result.fields.dateField.type, 'Date');
63+
assert.equal(result.fields.fileField.type, 'File');
64+
assert.equal(result.fields.geoPointField.type, 'GeoPoint');
65+
assert.equal(result.fields.arrayField.type, 'Array');
66+
assert.equal(result.fields.objectField.type, 'Object');
67+
assert.equal(result.fields.pointerField.type, 'Pointer');
68+
assert.equal(result.fields.relationField.type, 'Relation');
69+
assert.equal(result.fields.pointerField.targetClass, '_User');
70+
assert.equal(result.fields.relationField.targetClass, '_User');
71+
done();
72+
});
73+
});
74+
75+
it('all', (done) => {
76+
const testSchema = new Parse.Schema('SchemaTest');
77+
testSchema.save().then(() => {
78+
return Parse.Schema.all();
79+
}).then((results) => {
80+
assert.equal(results.length, 1);
81+
done();
82+
});
83+
});
84+
85+
it('update', (done) => {
86+
const testSchema = new Parse.Schema('SchemaTest');
87+
testSchema.addString('name');
88+
testSchema.save().then(() => {
89+
testSchema.deleteField('name');
90+
testSchema.addNumber('quantity');
91+
testSchema.addBoolean('status');
92+
return testSchema.update();
93+
}).then((result) => {
94+
assert.equal(result.fields.status.type, 'Boolean');
95+
assert.equal(result.fields.quantity.type, 'Number');
96+
assert.equal(result.fields.name, undefined);
97+
done();
98+
});
99+
});
100+
101+
it('multiple update', (done) => {
102+
const testSchema = new Parse.Schema('SchemaTest');
103+
testSchema.save().then(() => {
104+
testSchema.addString('name');
105+
return testSchema.update();
106+
}).then(() => {
107+
return testSchema.update();
108+
}).then(() => {
109+
return testSchema.get();
110+
}).then((result) => {
111+
assert.equal(Object.keys(result.fields).length, 5);
112+
done();
113+
});
114+
});
115+
116+
it('delete', (done) => {
117+
const testSchema1 = new Parse.Schema('SchemaTest1');
118+
const testSchema2 = new Parse.Schema('SchemaTest2');
119+
testSchema1.save().then(() => {
120+
return testSchema2.save();
121+
}).then(() => {
122+
return Parse.Schema.all();
123+
}).then((results) => {
124+
assert.equal(results.length, 2);
125+
return testSchema1.delete();
126+
}).then(() => {
127+
return Parse.Schema.all();
128+
}).then((results) => {
129+
assert.equal(results.length, 1);
130+
assert.equal(results[0].className, 'SchemaTest2');
131+
done();
132+
});
133+
});
134+
135+
it('save index', (done) => {
136+
const testSchema = new Parse.Schema('SchemaTest');
137+
const index = {
138+
name: 1
139+
};
140+
testSchema.addString('name');
141+
testSchema.addIndex('test_index', index);
142+
testSchema.save().then((result) => {
143+
assert.notEqual(result.indexes.test_index, undefined);
144+
done();
145+
});
146+
});
147+
148+
it('update index', (done) => {
149+
const testSchema = new Parse.Schema('SchemaTest');
150+
testSchema.save().then((result) => {
151+
const index = {
152+
name: 1
153+
};
154+
testSchema.addString('name');
155+
testSchema.addIndex('test_index', index);
156+
return testSchema.update();
157+
}).then((result) => {
158+
assert.notEqual(result.indexes.test_index, undefined);
159+
done();
160+
});
161+
});
162+
163+
it('delete index', (done) => {
164+
const testSchema = new Parse.Schema('SchemaTest');
165+
testSchema.save().then((result) => {
166+
const index = {
167+
name: 1
168+
};
169+
testSchema.addString('name');
170+
testSchema.addIndex('test_index', index);
171+
return testSchema.update();
172+
}).then((result) => {
173+
assert.notEqual(result.indexes.test_index, undefined);
174+
testSchema.deleteIndex('test_index');
175+
return testSchema.update();
176+
}).then((result) => {
177+
assert.equal(result.indexes.test_index, undefined);
178+
done();
179+
});
180+
});
181+
182+
it('invalid field name', (done) => {
183+
const testSchema = new Parse.Schema('SchemaTest');
184+
try {
185+
testSchema.addField(null);
186+
} catch (e) {
187+
done();
188+
}
189+
});
190+
191+
it('invalid field type', (done) => {
192+
const testSchema = new Parse.Schema('SchemaTest');
193+
try {
194+
testSchema.addField('name', 'UnknownType');
195+
} catch (e) {
196+
done();
197+
}
198+
});
199+
200+
it('invalid index name', (done) => {
201+
const testSchema = new Parse.Schema('SchemaTest');
202+
try {
203+
testSchema.addIndex(null);
204+
} catch (e) {
205+
done();
206+
}
207+
});
208+
209+
it('invalid index', (done) => {
210+
const testSchema = new Parse.Schema('SchemaTest');
211+
try {
212+
testSchema.addIndex('name', null);
213+
} catch (e) {
214+
done();
215+
}
216+
});
217+
218+
it('invalid pointer name', (done) => {
219+
const testSchema = new Parse.Schema('SchemaTest');
220+
try {
221+
testSchema.addPointer(null);
222+
} catch (e) {
223+
done();
224+
}
225+
});
226+
227+
it('invalid pointer class', (done) => {
228+
const testSchema = new Parse.Schema('SchemaTest');
229+
try {
230+
testSchema.addPointer('name', null);
231+
} catch (e) {
232+
done();
233+
}
234+
});
235+
236+
it('invalid relation name', (done) => {
237+
const testSchema = new Parse.Schema('SchemaTest');
238+
try {
239+
testSchema.addRelation(null);
240+
} catch (e) {
241+
done();
242+
}
243+
});
244+
245+
it('invalid relation class', (done) => {
246+
const testSchema = new Parse.Schema('SchemaTest');
247+
try {
248+
testSchema.addRelation('name', null);
249+
} catch (e) {
250+
done();
251+
}
252+
});
253+
254+
it('assert class name', (done) => {
255+
const testSchema = new Parse.Schema();
256+
try {
257+
testSchema.assertClassName();
258+
} catch (e) {
259+
done();
260+
}
261+
});
262+
});

src/CoreManager.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ type RESTController = {
7777
request: (method: string, path: string, data: mixed) => ParsePromise;
7878
ajax: (method: string, url: string, data: any, headers?: any) => ParsePromise;
7979
};
80+
type SchemaController = {
81+
get: (className: string, options: RequestOptions) => ParsePromise;
82+
delete: (className: string, options: RequestOptions) => ParsePromise;
83+
create: (className: string, params: any, options: RequestOptions) => ParsePromise;
84+
update: (className: string, params: any, options: RequestOptions) => ParsePromise;
85+
send(className: string, method: string, params: any, options: RequestOptions): ParsePromise;
86+
};
8087
type SessionController = {
8188
getSession: (token: RequestOptions) => ParsePromise;
8289
};
@@ -132,6 +139,7 @@ type Config = {
132139
PushController?: PushController,
133140
QueryController?: QueryController,
134141
RESTController?: RESTController,
142+
SchemaController?: SchemaController,
135143
SessionController?: SessionController,
136144
StorageController?: StorageController,
137145
UserController?: UserController,
@@ -286,6 +294,15 @@ module.exports = {
286294
return config['RESTController'];
287295
},
288296

297+
setSchemaController(controller: SchemaController) {
298+
requireMethods('SchemaController', ['get', 'create', 'update', 'delete', 'send'], controller);
299+
config['SchemaController'] = controller;
300+
},
301+
302+
getSchemaController(): SchemaController {
303+
return config['SchemaController'];
304+
},
305+
289306
setSessionController(controller: SessionController) {
290307
requireMethods('SessionController', ['getSession'], controller);
291308
config['SessionController'] = controller;

src/Parse.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Parse.Push = require('./Push');
145145
Parse.Query = require('./ParseQuery').default;
146146
Parse.Relation = require('./ParseRelation').default;
147147
Parse.Role = require('./ParseRole').default;
148+
Parse.Schema = require('./ParseSchema').default;
148149
Parse.Session = require('./ParseSession').default;
149150
Parse.Storage = require('./Storage');
150151
Parse.User = require('./ParseUser').default;

0 commit comments

Comments
 (0)