-
-
Notifications
You must be signed in to change notification settings - Fork 516
Update JS Docs #497
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
Update JS Docs #497
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
# Schema | ||
|
||
Schema is the structure representing classes in your app. You can use the schema | ||
of an app to verify operations in a unit test, generate test data, generate test | ||
classes and then clean up after tests. The schema API can also be used to create | ||
custom views of your data. We use the schema API to display column names and | ||
types in the databrowser. | ||
|
||
This API allows you to access the schemas of your app. | ||
|
||
* Parse Server 2.7.1+ | ||
* `MasterKey` is required. | ||
|
||
Schema will return an object similar to the following: | ||
|
||
<pre><code class="javascript"> | ||
{ | ||
className: 'MyClass', | ||
fields: { | ||
// Default fields | ||
ACL: {type: 'ACL'}, | ||
createdAt: {type: 'Date'}, | ||
updatedAt: {type: 'Date'}, | ||
objectId: {type: 'String'}, | ||
// Custom fields | ||
aNumber: {type: 'Number'}, | ||
aString: {type: 'String'}, | ||
aBool: {type: 'Boolean'}, | ||
aDate: {type: 'Date'}, | ||
aObject: {type: 'Object'}, | ||
aArray: {type: 'Array'}, | ||
aGeoPoint: {type: 'GeoPoint'}, | ||
aPolygon: {type: 'Polygon'}, | ||
aFile: {type: 'File'} | ||
}, | ||
classLevelPermissions: { | ||
find: { | ||
'*': true | ||
}, | ||
create: { | ||
'*': true | ||
}, | ||
get: { | ||
'*': true | ||
}, | ||
update: { | ||
'*': true | ||
}, | ||
addField: { | ||
'*': true | ||
}, | ||
delete: { | ||
'*': true | ||
} | ||
}, | ||
indexes: { | ||
indexName: { aString: 1 }, | ||
} | ||
} | ||
</code></pre> | ||
|
||
Direct manipulation of the classes that are on your server is possible through ParseSchema. Although fields and classes can be automatically generated (the latter assuming client class creation is enabled) ParseSchema gives you explicit control over these classes and their fields. | ||
|
||
*With great power comes great responsibility. Altering the schema directly should be done with care, you can't go back to retrieve data if you remove a field and it's associated values.* | ||
|
||
<pre><code class="javascript"> | ||
// create an instance to manage your class | ||
const mySchema = new Parse.Schema('MyClass'); | ||
|
||
// gets the current schema data | ||
mySchema.get(); | ||
|
||
// returns schema for all classes | ||
Parse.Schema.all() | ||
|
||
// add any # of fields, without having to create any objects | ||
mySchema | ||
.addString('stringField') | ||
.addNumber('numberField') | ||
.addBoolean('booleanField') | ||
.addDate('dateField') | ||
.addFile('fileField') | ||
.addGeoPoint('geoPointField') | ||
.addPolygon('polygonField') | ||
.addArray('arrayField') | ||
.addObject('objectField') | ||
.addPointer('pointerField', '_User') | ||
.addRelation('relationField', '_User'); | ||
|
||
// new types can be added as they are available | ||
mySchema.addField('newField', 'ANewDataType') | ||
|
||
// save/update this schema to persist your field changes | ||
mySchema.save().then((result) => { | ||
// returns save new schema | ||
}); | ||
// or | ||
mySchema.update().then((result) => { | ||
// updates existing schema | ||
}); | ||
</code></pre> | ||
|
||
Assuming you want to remove a field you can simply call `deleteField` and `save/update` to clear it out. | ||
|
||
<pre><code class="javascript"> | ||
mySchema.deleteField('stringField'); | ||
mySchema.save(); | ||
// or for an existing schema... | ||
mySchema.update(); | ||
</code></pre> | ||
|
||
## Indexes | ||
|
||
Indexes support efficient execution of queries from the database. Keep in mind that the `masterKey` is required for these operations, so be sure it's set in your initialization code before you use this feature. | ||
|
||
<pre><code class="javascript"> | ||
// To add an index, the field must exist before you create an index | ||
mySchema.addString('stringField'); | ||
const index = { | ||
stringField: 1 | ||
}; | ||
mySchema.addIndex('stringFieldIndex', index); | ||
mySchema.save().then((result) => { | ||
// returns schema including index stringFieldIndex and field stringField | ||
}); | ||
|
||
// Delete an index | ||
testSchema.deleteIndex('indexName'); | ||
mySchema.save().then((result) => { | ||
// returns schema without indexName index | ||
}); | ||
|
||
// If indexes exist, you can retrieve them | ||
mySchema.get().then((result) => { | ||
// result.indexes | ||
}); | ||
</code></pre> | ||
|
||
## Purge | ||
|
||
All objects can be purged from a schema (class) via purge. But *be careful*! This can be considered an irreversible action. Only do this if you really need to delete all objects from a class, such as when you need to delete the class (as in the code example above). | ||
|
||
<pre><code class="javascript"> | ||
// delete all objects in the schema | ||
mySchema.purge(); | ||
</code></pre> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ types in the databrowser. | |
This API allows you to access the schemas of your app. | ||
Note: This API can be only accessed using the `master key`. | ||
|
||
* Starting with Parse-Server 3.0 Index support added. | ||
* Starting with Parse Server 2.7.1 Index support added. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't even realize the 3.0 thing there...nice catch |
||
|
||
## Fetch the schema | ||
To fetch the Schema for all the classes of your app, run: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ sections: | |
- "js/analytics.md" | ||
- "common/data.md" | ||
- "common/relations.md" | ||
- "js/schema.md" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're just adding schema.md you might want to drop schemas.md. In the php section I have it setup to reference schema.md already, might as well be consistent. |
||
- "js/handling-errors.md" | ||
- "common/security.md" | ||
- "common/performance.md" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably would be good to add a little warning with this as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like