Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Parse Schema #489

Merged
merged 7 commits into from
Nov 30, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
language: node_js
node_js:
- '5'
- '6.11.4'

branches:
only:
Expand Down
2 changes: 1 addition & 1 deletion integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"dependencies": {
"express": "^4.13.4",
"mocha": "^2.4.5",
"parse-server": "^2.6.0"
"parse-server": "^2.7.0"
},
"scripts": {
"test": "mocha --reporter dot -t 5000"
Expand Down
262 changes: 262 additions & 0 deletions integration/test/ParseSchemaTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
const assert = require('assert');
const clear = require('./clear');
const Parse = require('../../node');

describe('Schema', () => {
before(() => {
Parse.initialize('integration');
Parse.CoreManager.set('SERVER_URL', 'http://localhost:1337/parse');
Parse.CoreManager.set('MASTER_KEY', 'notsosecret');
Parse.Storage._clear();
});

beforeEach((done) => {
clear().then(() => {
done();
});
});

it('invalid get all no schema', (done) => {
Parse.Schema.all().then(() => {}).fail((e) => {
done();
});
});

it('invalid get no schema', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
testSchema.get().then(() => {}).fail((e) => {
done();
});
});

it('save', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
testSchema.save().then((result) => {
assert.equal(result.className, 'SchemaTest');
done();
});
});

it('get', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
testSchema
.addField('defaultFieldString')
.addString('stringField')
.addNumber('numberField')
.addBoolean('booleanField')
.addDate('dateField')
.addFile('fileField')
.addGeoPoint('geoPointField')
.addArray('arrayField')
.addObject('objectField')
.addPointer('pointerField', '_User')
.addRelation('relationField', '_User');

testSchema.save().then(() => {
return testSchema.get();
}).then((result) => {
assert.equal(result.fields.defaultFieldString.type, 'String');
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.dateField.type, 'Date');
assert.equal(result.fields.fileField.type, 'File');
assert.equal(result.fields.geoPointField.type, 'GeoPoint');
assert.equal(result.fields.arrayField.type, 'Array');
assert.equal(result.fields.objectField.type, 'Object');
assert.equal(result.fields.pointerField.type, 'Pointer');
assert.equal(result.fields.relationField.type, 'Relation');
assert.equal(result.fields.pointerField.targetClass, '_User');
assert.equal(result.fields.relationField.targetClass, '_User');
done();
});
});

it('all', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
testSchema.save().then(() => {
return Parse.Schema.all();
}).then((results) => {
assert.equal(results.length, 1);
done();
});
});

it('update', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
testSchema.addString('name');
testSchema.save().then(() => {
testSchema.deleteField('name');
testSchema.addNumber('quantity');
testSchema.addBoolean('status');
return testSchema.update();
}).then((result) => {
assert.equal(result.fields.status.type, 'Boolean');
assert.equal(result.fields.quantity.type, 'Number');
assert.equal(result.fields.name, undefined);
done();
});
});

it('multiple update', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
testSchema.save().then(() => {
testSchema.addString('name');
return testSchema.update();
}).then(() => {
return testSchema.update();
}).then(() => {
return testSchema.get();
}).then((result) => {
assert.equal(Object.keys(result.fields).length, 5);
done();
});
});

it('delete', (done) => {
const testSchema1 = new Parse.Schema('SchemaTest1');
const testSchema2 = new Parse.Schema('SchemaTest2');
testSchema1.save().then(() => {
return testSchema2.save();
}).then(() => {
return Parse.Schema.all();
}).then((results) => {
assert.equal(results.length, 2);
return testSchema1.delete();
}).then(() => {
return Parse.Schema.all();
}).then((results) => {
assert.equal(results.length, 1);
assert.equal(results[0].className, 'SchemaTest2');
done();
});
});

it('save index', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
const index = {
name: 1
};
testSchema.addString('name');
testSchema.addIndex('test_index', index);
testSchema.save().then((result) => {
assert.notEqual(result.indexes.test_index, undefined);
done();
});
});

it('update index', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
testSchema.save().then((result) => {
const index = {
name: 1
};
testSchema.addString('name');
testSchema.addIndex('test_index', index);
return testSchema.update();
}).then((result) => {
assert.notEqual(result.indexes.test_index, undefined);
done();
});
});

it('delete index', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
testSchema.save().then((result) => {
const index = {
name: 1
};
testSchema.addString('name');
testSchema.addIndex('test_index', index);
return testSchema.update();
}).then((result) => {
assert.notEqual(result.indexes.test_index, undefined);
testSchema.deleteIndex('test_index');
return testSchema.update();
}).then((result) => {
assert.equal(result.indexes.test_index, undefined);
done();
});
});

it('invalid field name', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
try {
testSchema.addField(null);
} catch (e) {
done();
}
});

it('invalid field type', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
try {
testSchema.addField('name', 'UnknownType');
} catch (e) {
done();
}
});

it('invalid index name', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
try {
testSchema.addIndex(null);
} catch (e) {
done();
}
});

it('invalid index', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
try {
testSchema.addIndex('name', null);
} catch (e) {
done();
}
});

it('invalid pointer name', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
try {
testSchema.addPointer(null);
} catch (e) {
done();
}
});

it('invalid pointer class', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
try {
testSchema.addPointer('name', null);
} catch (e) {
done();
}
});

it('invalid relation name', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
try {
testSchema.addRelation(null);
} catch (e) {
done();
}
});

it('invalid relation class', (done) => {
const testSchema = new Parse.Schema('SchemaTest');
try {
testSchema.addRelation('name', null);
} catch (e) {
done();
}
});

it('assert class name', (done) => {
const testSchema = new Parse.Schema();
try {
testSchema.assertClassName();
} catch (e) {
done();
}
});
});
17 changes: 17 additions & 0 deletions src/CoreManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ type RESTController = {
request: (method: string, path: string, data: mixed) => ParsePromise;
ajax: (method: string, url: string, data: any, headers?: any) => ParsePromise;
};
type SchemaController = {
get: (className: string, options: RequestOptions) => ParsePromise;
delete: (className: string, options: RequestOptions) => ParsePromise;
create: (className: string, params: any, options: RequestOptions) => ParsePromise;
update: (className: string, params: any, options: RequestOptions) => ParsePromise;
send(className: string, method: string, params: any, options: RequestOptions): ParsePromise;
};
type SessionController = {
getSession: (token: RequestOptions) => ParsePromise;
};
Expand Down Expand Up @@ -131,6 +138,7 @@ type Config = {
PushController?: PushController,
QueryController?: QueryController,
RESTController?: RESTController,
SchemaController?: SchemaController,
SessionController?: SessionController,
StorageController?: StorageController,
UserController?: UserController,
Expand Down Expand Up @@ -285,6 +293,15 @@ module.exports = {
return config['RESTController'];
},

setSchemaController(controller: SchemaController) {
requireMethods('SchemaController', ['get', 'create', 'update', 'delete', 'send'], controller);
config['SchemaController'] = controller;
},

getSchemaController(): SchemaController {
return config['SchemaController'];
},

setSessionController(controller: SessionController) {
requireMethods('SessionController', ['getSession'], controller);
config['SessionController'] = controller;
Expand Down
1 change: 1 addition & 0 deletions src/Parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Parse.Push = require('./Push');
Parse.Query = require('./ParseQuery').default;
Parse.Relation = require('./ParseRelation').default;
Parse.Role = require('./ParseRole').default;
Parse.Schema = require('./ParseSchema').default;
Parse.Session = require('./ParseSession').default;
Parse.Storage = require('./Storage');
Parse.User = require('./ParseUser').default;
Expand Down
Loading