Skip to content

Commit

Permalink
Merge pull request #80 from ceramicnetwork/feature/ws2-3149-put-all-t…
Browse files Browse the repository at this point in the history
…he-docs-to-docusaurus-repo-from-notion

Feature/ws2 3149 put all the docs to docusaurus repo from notion
  • Loading branch information
JulissaDantes authored Apr 9, 2024
2 parents 9105e6e + 956cad5 commit dc364e0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
26 changes: 19 additions & 7 deletions docs/composedb/guides/data-modeling/relations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ Where:

---

Model to model relations enable you to link data to and query data from another piece of data. These relations can be uni-directional (e.g. query a post from a comment) or bi-directional (e.g. query a post from a comment and query all comments from a post).
Model to model relations enable you to link data to and query data from another piece of data. These relations can be uni-directional (e.g. query a post from a comment) or bi-directional (e.g. query a post from a comment and query all comments from a post).

### Example: Post with comments
There is type of model-to-model relation that includes the user as part of the relationship. This is achieved by using the `SET` account relation type, which allows users to enforce a constraint where each user account (or DID) can create only one instance of a model for a specific record of another model.

Heres a model that allows many comments to be made on a single post. It supports unlimited comments per user, and bi-directional queries from any comment to the original post and from the original post to all of its comments.
### Example: Post with comments and likes

Heres a model that allows many comments from the same or different account to be made on a single post. It supports unlimited comments per user, and bi-directional queries from any comment or like to the original post and from the original post to all of its comments and likes. The model schema also creates a relation between posts and likes enabling a single like per post by an account, meaning a single account will only be able to like the post once

```graphql
# Load post model (using streamID)
Expand All @@ -86,14 +88,22 @@ type Post @loadModel(id: "kjzl6hvfrbw6c99mdfpjx1z3fue7sesgua6gsl1vu97229lq56344z
}

# New comment model
# Set relationship to original post
# Set reference to original post
# Enable querying comment to get original post

type Comment @createModel(accountRelation: LIST, description: "A comment on a Post") {
postID: StreamID! @documentReference(model: "Post")
post: Post! @relationDocument(property: "postID")
text: String! @string(maxLength: 500)
}

# New like model
# Set relationship to original post
# Enable querying comment to get original post
type Like @createModel(description: "A like on a post", accountRelation: SET, accountRelationFields: ["postID"]) {
postID: StreamID! @documentReference(model: "Post")
post: Post! @relationDocument(property: "postID")
}
```

Relations can also be created between models loaded from known streamIDs
Expand All @@ -106,12 +116,13 @@ type Comment @loadModel(id: "kjzl6hvfrbw6c9oo2ync09y6z5c9mas9u49lfzcowepuzxmcn3p
}

# Load post model
# Extend post model with comments
# Set relationships to all comments
# Enable querying post to get all comments
# Extend post model with comments and likes
# Set relationships to all comments and likes
# Enable querying post to get all comments and likes

type Post @loadModel(id: "kjzl6hvfrbw6c99mdfpjx1z3fue7sesgua6gsl1vu97229lq56344zu9bawnf96") {
comments: [Comment] @relationFrom(model: "Comment", property: "postID")
likes: [Like] @relationFrom(model: "Like", property: "postID")
}
```

Expand All @@ -122,6 +133,7 @@ Where:
- `post` allows accessing the original post from the comment, using `@relationDocument`
- `text` defines a string for the comment
- `comments` defines the relationships from a post to a collection of comments, using `@relationFrom`; requires specifying the model relation (`Comment`) and the specific property that stores the relation (`postID`)
- `likes` defines the relationships from a post to a collection of comments, using `@relationFrom`; requires specifying the model relation (`Like`) and the specific property that stores the relation (`postID`)

### Using interfaces

Expand Down
5 changes: 3 additions & 2 deletions docs/composedb/guides/data-modeling/schemas.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ type Post implements TextContent @createModel(description: "Post model") {

### Directives

ComposeDB comes with [a list of different directives](https://composedb.js.org/docs/0.5.x/api/sdl/directives) that can be used to create or load data models, define type validation rules, and create indices
ComposeDB comes with [a list of different directives](https://composedb.js.org/docs/0.7.x/api/sdl/directives) that can be used to create or load data models, define type validation rules, and create indices
for specific fields which enables them to be used for document filtering and sorting.

#### Type validation directives
Expand All @@ -125,7 +125,7 @@ type EducationModule {
type Learner {
first_name: String! @string(minLength: 10, maxLength: 30)
last_name: String! @string(maxLength: 30)
username: String! @string(maxLength: 32)
username: String! @string(maxLength: 32) @immutable
}
```

Expand All @@ -135,6 +135,7 @@ Where:
- `@string` adds validation rules to values that are strings, e.g. minimum and maximum length
- `@int` adds validation rules to values that are integers, e.g. minimum and maximum values
- `@list` adds validation rules to an array, e.g. maximum length
- `@immutable` ensures that after a field value is set it won't be updated

#### Directives for creating indices

Expand Down
46 changes: 45 additions & 1 deletion docs/composedb/interact-with-data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ query{


## Mutations
There are two types of mutations you can perform on ComposeDB data: creating and updating records.
There are three types of mutations you can perform on ComposeDB data: creating, and updating records, or change wether the records is indexed or not.

### Creating records
Let’s say, you would like to create a post and add it to the graph. To do that, you will have to run a mutation as shown below and pass the actual text as a variable:
Expand Down Expand Up @@ -232,6 +232,50 @@ This mutation will update the record with ID `kjzl6kcym7w8y9xlffqruh3v7ou1vn11t8
}
```

### Remove/Restore record from index
If instead of updating the created post record we want to stop indexing said record we would need to use the `enableIndexingPost`
mutation with the `shouldIndex` option set to `false`, or if we want to index an un-indexed record we can call the `enableIndexingPost`
mutation with the `shouldIndex` option set to `true`, and the post ID as variable.


**Query:**

```graphql
mutation EnableIndexingPost($input: EnableIndexingPostInput!) {
enableIndexingPost(input: $input) {
document {
id
}
}
}
```



**Variables:**

```json
{
"i": {
"id": "kjzl6kcym7w8y9xlffqruh3v7ou1vn11t8203i6te2i3pliizt65ad3vdh5nl4l",
"shouldIndex": false
}
}
```

This mutation will un-index the record with ID `kjzl6kcym7w8y9xlffqruh3v7ou1vn11t8203i6te2i3pliizt65ad3vdh5nl4l`.

**Response:**
```json
{
"data": {
"enableIndexingPost": {
"document": null
}
}
}
```

## Authentication
Although you can query records created by other accounts, you can only perform mutations on records controlled by your account. This guide did not require your authentication because you previously did that in the [Set up your environment](./set-up-your-environment.mdx) guide.

Expand Down

0 comments on commit dc364e0

Please sign in to comment.