Skip to content

feat: add ParseSchema to SDK #370

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

Merged
merged 55 commits into from
May 30, 2022
Merged

feat: add ParseSchema to SDK #370

merged 55 commits into from
May 30, 2022

Conversation

cbaker6
Copy link
Contributor

@cbaker6 cbaker6 commented May 22, 2022

New Pull Request Checklist

Issue Description

Schema and Class Level Permissions are current not implemented in the SDK, preventing the developers for creating/updating/deleting Parse schemas directly in Swift.

Related issue: #n/a

Approach

Add ParseSchema, ParseCLP, ParseFieldOptions types to the SDK. Since ParseSchema requires the use of the masterKey, this should only be used when using the Swift SDK on a server.

Example usage can be found in the Playgrounds:

//: First lets create a new CLP for the new schema.
let clp = ParseCLP(requiresAuthentication: true, publicAccess: false)
.setAccessPublic(true, on: .get)
.setAccessPublic(true, on: .find)
//: Next we use the CLP to create the new schema and add fields to it.
var gameScoreSchema = ParseSchema<GameScore2>(classLevelPermissions: clp)
.addField("points",
type: .number,
options: ParseFieldOptions<Int>(required: false, defauleValue: nil))
.addField("level",
type: .number,
options: ParseFieldOptions<Int>(required: false, defauleValue: nil))
.addField("data",
type: .bytes,
options: ParseFieldOptions<String>(required: false, defauleValue: nil))
do {
gameScoreSchema = try gameScoreSchema
.addField("owner",
type: .pointer,
options: ParseFieldOptions<User>(required: false, defauleValue: nil))
.addField("rivals",
type: .array,
options: ParseFieldOptions<[User]>(required: false, defauleValue: nil))
} catch {
print("Can't add field: \(gameScoreSchema)")
}
//: Now lets create the schema on the server.
gameScoreSchema.create { result in
switch result {
case .success(let savedSchema):
print("Check GameScore2 in Dashboard. \nThe created schema: \(savedSchema)")
case .failure(let error):
print("Couldn't save schema: \(error)")
}
}
//: We can update the CLP to only allow access to users specified in the "owner" field.
let clp2 = clp.setPointerFields(Set(["owner"]), on: .get)
gameScoreSchema.classLevelPermissions = clp2
//: In addition, we can add an index.
gameScoreSchema = gameScoreSchema.addIndex("myIndex", field: "level", index: 1)
//: Next, we need to update the schema on the server with the changes.
gameScoreSchema.update { result in
switch result {
case .success(let updatedSchema):
print("Check GameScore2 in Dashboard. \nThe updated schema: \(updatedSchema)")
/*:
Updated the current gameScoreSchema with the newest.
*/
gameScoreSchema = updatedSchema
case .failure(let error):
print("Couldn't update schema: \(error)")
}
}
//: Indexes can also be deleted.
gameScoreSchema = gameScoreSchema.deleteIndex("myIndex")
//: Next, we need to update the schema on the server with the changes.
gameScoreSchema.update { result in
switch result {
case .success(let updatedSchema):
print("Check GameScore2 in Dashboard. \nThe updated schema: \(updatedSchema)")
/*:
Updated the current gameScoreSchema with the newest.
*/
gameScoreSchema = updatedSchema
case .failure(let error):
print("Couldn't update schema: \(error)")
}
}
/*:
Fields can also be deleted on a schema. Lets remove
the **data** field since it's not going being used.
*/
gameScoreSchema = gameScoreSchema.deleteField("data")
//: Next, we need to update the schema on the server with the changes.
gameScoreSchema.update { result in
switch result {
case .success(let updatedSchema):
print("Check GameScore2 in Dashboard. \nThe updated schema: \(updatedSchema)")
/*:
Updated the current gameScoreSchema with the newest.
*/
gameScoreSchema = updatedSchema
case .failure(let error):
print("Couldn't update schema: \(error)")
}
}
/*:
Sets of fields can also be protected from access. Lets protect
some fields from access.
*/
var clp3 = gameScoreSchema.classLevelPermissions
clp3 = clp3?
.setProtectedFieldsPublic(["owner"])
.setProtectedFields(["level"], userField: "rivals")
gameScoreSchema.classLevelPermissions = clp3
//: Next, we need to update the schema on the server with the changes.
gameScoreSchema.update { result in
switch result {
case .success(let updatedSchema):
print("Check GameScore2 in Dashboard. \nThe updated schema: \(updatedSchema)")
/*:
Updated the current gameScoreSchema with the newest.
*/
gameScoreSchema = updatedSchema
case .failure(let error):
print("Couldn't update schema: \(error)")
}
}
//: Now lets save a new object to the new schema.
var gameScore = GameScore2()
gameScore.points = 120
gameScore.owner = User.current
gameScore.save { result in
switch result {
case .success(let savedGameScore):
print("The saved GameScore is: \(savedGameScore)")
case .failure(let error):
print("Couldn't save schema: \(error)")
}
}
//: You can delete all objects your schema by purging them.
gameScoreSchema.purge { result in
switch result {
case .success:
print("All objects have been purged from this schema.")
case .failure(let error):
print("Couldn't purge schema: \(error)")
}
}
/*:
As long as there's no data in your `ParseSchema` you can
delete the schema.
*/
gameScoreSchema.delete { result in
switch result {
case .success:
print("The schema has been deleted.")
case .failure(let error):
print("Couldn't delete the schema: \(error)")
}
}

TODOs before merging

  • Add tests
  • Add entry to changelog
  • Add changes to documentation (guides, repository pages, in-code descriptions)

@parse-github-assistant
Copy link

parse-github-assistant bot commented May 22, 2022

Thanks for opening this pull request!

  • 🎉 We are excited about your hands-on contribution!

@cbaker6 cbaker6 marked this pull request as draft May 22, 2022 17:23
@codecov
Copy link

codecov bot commented May 22, 2022

Codecov Report

Merging #370 (ae88e8e) into main (56ec801) will increase coverage by 0.58%.
The diff coverage is 97.65%.

@@            Coverage Diff             @@
##             main     #370      +/-   ##
==========================================
+ Coverage   87.92%   88.51%   +0.58%     
==========================================
  Files         114      125      +11     
  Lines       12214    12970     +756     
==========================================
+ Hits        10739    11480     +741     
- Misses       1475     1490      +15     
Impacted Files Coverage Δ
...thentication/3rd Party/ParseApple/ParseApple.swift 86.25% <ø> (ø)
...cation/3rd Party/ParseFacebook/ParseFacebook.swift 100.00% <ø> (ø)
...entication/3rd Party/ParseGithub/ParseGitHub.swift 100.00% <ø> (ø)
...entication/3rd Party/ParseGoogle/ParseGoogle.swift 100.00% <ø> (ø)
...Authentication/3rd Party/ParseLDAP/ParseLDAP.swift 100.00% <ø> (ø)
...cation/3rd Party/ParseLinkedIn/ParseLinkedIn.swift 100.00% <ø> (ø)
...tication/3rd Party/ParseTwitter/ParseTwitter.swift 100.00% <ø> (ø)
...Authentication/Protocols/ParseAuthentication.swift 77.04% <ø> (ø)
Sources/ParseSwift/Coding/ParseEncoder.swift 77.77% <ø> (ø)
...es/ParseSwift/LiveQuery/ParseLiveQuery+async.swift 81.81% <ø> (ø)
... and 45 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 56ec801...ae88e8e. Read the comment docs.

@cbaker6 cbaker6 marked this pull request as ready for review May 28, 2022 20:10
@cbaker6 cbaker6 merged commit c32e269 into parse-community:main May 30, 2022
@cbaker6 cbaker6 deleted the schema branch May 30, 2022 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant