1
0

Merge pull request #64 from sinansonmez/improve-query-docs

This commit is contained in:
Zef Hemel 2022-08-22 19:57:57 +02:00 committed by GitHub
commit 51d57e0648
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,7 +12,7 @@ The query plug is a built-in plug implementing the `<!-- #query -->` mechanism.
1. _start with_: `<!-- #query [QUERY GOES HERE] -->` 1. _start with_: `<!-- #query [QUERY GOES HERE] -->`
2. _end with_: `<!-- /query -->` 2. _end with_: `<!-- /query -->`
3. _write your query_: replace `[QUERY GOES HERE]` with any query you want using options below 3. _write your query_: replace `[QUERY GOES HERE]` with any query you want using options below
4. _available query options_: Usage of options is similar to SQL except special `render` option. Render option is to use display the data in a format that you created in a separate template 4. _available query options_: Usage of options is similar to SQL except special `render` option. Render option is to use display the data in a format that you created in a separate template.
* `where` * `where`
* `order by` * `order by`
* `limit` * `limit`
@ -21,6 +21,18 @@ The query plug is a built-in plug implementing the `<!-- #query -->` mechanism.
P.S.: If you are a developer or have a technical knowledge to read a code and would like to know more about syntax, please check out [query grammar](https://github.com/silverbulletmd/silverbullet/blob/main/packages/plugs/query/query.grammar). P.S.: If you are a developer or have a technical knowledge to read a code and would like to know more about syntax, please check out [query grammar](https://github.com/silverbulletmd/silverbullet/blob/main/packages/plugs/query/query.grammar).
#### 2.1. Available query operators:
* `=` equals
* `!=` not equals
* `<` less than
* `<=` less than or equals
* `>` greater than
* `>=` greater than or equals
* `=~` to match against a regular expression
* `!=~` does not match this regular expression
Further, you can combine multiple of these with `and`. Example `prop =~ /something/ and prop != “something”`.
### 3. How to run a query? ### 3. How to run a query?
After writing the query, there are three options: After writing the query, there are three options:
* Open the **command palette** and run **Materialized Queries: Update** * Open the **command palette** and run **Materialized Queries: Update**
@ -66,44 +78,70 @@ country: Germany
<!-- #query data where age > 20 and country = "Italy" --> <!-- #query data where age > 20 and country = "Italy" -->
|name|age|city |country|page |pos | |name|age|city |country|page |pos |
|----|--|-----|-----|--------|----| |----|--|-----|-----|--------|----|
|John|50|Milan|Italy|🔌 Query|2277| |John|50|Milan|Italy|🔌 Query|2696|
|Jane|53|Rome |Italy|🔌 Query|2323| |Jane|53|Rome |Italy|🔌 Query|2742|
<!-- /query --> <!-- /query -->
#### 4.2 Plugs data sources #### 4.2 Plugs data sources
Certain plugs can also provide special data sources to query a certain data. Some examples are Certain plugs can also provide special data sources to query a certain data. Some examples are:
* [[🔌 Github]] provides `gh-pull` to query PRs for selected repo * [[🔌 Github]] provides `gh-pull` to query PRs for selected repo
* [[🔌 Mattermost]] provides `mm-saved` to fetch (by default 15) saved posts in Mattermost * [[🔌 Mattermost]] provides `mm-saved` to fetch (by default 15) saved posts in Mattermost
For complete list of data sources, please check plugs pages. For a complete list of data sources, please check plugs own pages.
### 5. Examples ### 5. Templates
Templates are predefined formats to render the body of the query.
#### 5.1 How to create a template?
It is pretty easy. You just need to create a new page. However, it is recommended to create your templates using `template/[TEMPLATE_NAME]` convention. For this guide, we will create `template/plug` to display list of Plugs available in Silver Bullet. We will use this template in the Examples section below.
#### 5.2 What is the syntax?
We are using Handlebars which is a simple templating language. It is using double curly braces and name of parameter to be injected. For our `template/plug`, we are using simple template like below.
`* [[{{name}}]] by **{{author}}** ([repo]({{repo}}))`
Let me break it down for you
* `* ` is creating a bullet point for each item in Silver Bullet
* `[[{{name}}]]` is injecting the name of Plug and creating an internal link to the page of the Plug
* `**{{author}}**` is injecting the author of the Plug and making it bold
* `([repo]({{repo}}))` is injecting the name of the Plug and creating an external link to GitHub page of the Plug
For more information on the Handlebars syntax, you can read the [official documentation](https://handlebarsjs.com/).
#### 5.3 How to use the template?
You just need to add `render` keyword followed by the link of the template to the query like below:
`#query page where type = "plug" render [[template/plug]]`
You can see the usage of our template in the example 6.4 below.
### 6. Examples
We will walk you through a set of examples starting from very basic one until to format the data using templates. We will walk you through a set of examples starting from very basic one until to format the data using templates.
Our goal in this exercise is to (i) get all plug pages (ii) ordered by last modified time and (iii) display in a nice format. Our goal in this exercise is to (i) get all plug pages (ii) ordered by last modified time and (iii) display in a nice format.
For the sake of simplicity, we will use `page` data source and limit the results not to spoil the page. For the sake of simplicity, we will use `page` data source and limit the results not to spoil the page.
#### 5.1 Simple query without any condition #### 6.1 Simple query without any condition
**Goal:** We would like to get the list of all pages. **Goal:** We would like to get the list of all pages.
**Result:** Look at the data. This is more than we need. The query even gives us template pages. Lets try to limit it in the next step. **Result:** Look at the data. This is more than we need. The query even gives us template pages. Lets try to limit it in the next step.
<!-- #query page limit 10 --> <!-- #query page limit 10 -->
|name |lastModified |perm|tags |type|uri |repo |author | |name |lastModified |perm|tags |type|uri |repo |author |
|--|--|--|--|--|--|--|--| |--|--|--|--|--|--|--|--|
|SETTINGS |1659437160849|rw|undefined|undefined|undefined |undefined |undefined | |SETTINGS |1661112513714|rw|undefined|undefined|undefined |undefined |undefined |
|Silver Bullet |1660051168973|rw|undefined|undefined|undefined |undefined |undefined | |Silver Bullet |1661112513714|rw|undefined|undefined|undefined |undefined |undefined |
|CHANGELOG |1660050383951|rw|undefined|undefined|undefined |undefined |undefined | |CHANGELOG |1661112513714|rw|undefined|undefined|undefined |undefined |undefined |
|Mattermost Plugin|1658755340866|rw|undefined|undefined|undefined |undefined |undefined | |Mattermost Plugin|1661112513714|rw|undefined|undefined|undefined |undefined |undefined |
|PLUGS |1659437423367|rw|undefined|undefined|undefined |undefined |undefined | |PLUGS |1661112513714|rw|undefined|undefined|undefined |undefined |undefined |
|index |1659440751554|rw|undefined|undefined|undefined |undefined |undefined | |index |1661112513714|rw|undefined|undefined|undefined |undefined |undefined |
|template/plug |1658751100952|rw|undefined|undefined|undefined |undefined |undefined | |template/plug |1661112513718|rw|undefined|undefined|undefined |undefined |undefined |
|template/tasks |1657890041936|rw|#each|undefined|undefined |undefined |undefined | |template/tasks |1661112513718|rw|#each|undefined|undefined |undefined |undefined |
|💡 Inspiration |1658133917441|rw|undefined|undefined|undefined |undefined |undefined | |💡 Inspiration |1661112513718|rw|undefined|undefined|undefined |undefined |undefined |
|🔌 Backlinks |1658760465195|rw|undefined|plug|ghr:Willyfrog/silverbullet-backlinks|https://github.com/Willyfrog/silverbullet-backlinks|Guillermo Vayá| |🔌 Backlinks |1661112513718|rw|undefined|plug|ghr:Willyfrog/silverbullet-backlinks|https://github.com/Willyfrog/silverbullet-backlinks|Guillermo Vayá|
<!-- /query --> <!-- /query -->
#### 5.2 Simple query with a condition #### 6.2 Simple query with a condition
**Goal:** We would like to get all plug pages and sorted by last modified time. **Goal:** We would like to get all plug pages and sorted by last modified time.
**Result:** Okay, this what we wanted but there are also information such as perm, type and lastModified that we don't need. **Result:** Okay, this what we wanted but there are also information such as perm, type and lastModified that we don't need.
@ -111,15 +149,15 @@ For the sake of simplicity, we will use `page` data source and limit the results
<!-- #query page where type = "plug" order by lastModified desc limit 5 --> <!-- #query page where type = "plug" order by lastModified desc limit 5 -->
|name |lastModified |perm|type|uri |repo |author | |name |lastModified |perm|type|uri |repo |author |
|--|--|--|--|--|--|--| |--|--|--|--|--|--|--|
|🔌 Query |1660051209241|rw|plug|core:query |https://github.com/silverbulletmd/silverbullet |Silver Bullet Authors| |🔌 Query |1661114193972|rw|plug|core:query |https://github.com/silverbulletmd/silverbullet |Silver Bullet Authors|
|🔌 Github|1660050280511|rw|plug|github:silverbulletmd/silverbullet-github/github.plug.json|https://github.com/silverbulletmd/silverbullet-github|Zef Hemel | |🔌 Backlinks|1661112513718|rw|plug|ghr:Willyfrog/silverbullet-backlinks |https://github.com/Willyfrog/silverbullet-backlinks |Guillermo Vayá |
|🔌 Mount |1658760601369|rw|plug|github:silverbulletmd/silverbullet-mount/mount.plug.json |https://github.com/silverbulletmd/silverbullet-mount |Zef Hemel | |🔌 Core |1661112513718|rw|plug|builtin:core |https://github.com/silverbulletmd/silverbullet |Silver Bullet Authors|
|🔌 Git |1658760545612|rw|plug|github:silverbulletmd/silverbullet-github/github.plug.json|https://github.com/silverbulletmd/silverbullet-github|Zef Hemel | |🔌 Ghost |1661112513718|rw|plug|github:silverbulletmd/silverbullet-ghost/ghost.plug.json |https://github.com/silverbulletmd/silverbullet-ghost |Zef Hemel |
|🔌 Ghost |1658760515320|rw|plug|github:silverbulletmd/silverbullet-ghost/ghost.plug.json |https://github.com/silverbulletmd/silverbullet-ghost |Zef Hemel | |🔌 Git |1661112513718|rw|plug|github:silverbulletmd/silverbullet-github/github.plug.json|https://github.com/silverbulletmd/silverbullet-github|Zef Hemel |
<!-- /query --> <!-- /query -->
#### 5.3 Query to select only certain fields #### 6.3 Query to select only certain fields
**Goal:** We would like to get all plug pages, select only `name`, `author` and `repo` columns and sort by last modified time. **Goal:** We would like to get all plug pages, select only `name`, `author` and `repo` columns and sort by last modified time.
**Result:** Okay, this is much better. However, I believe this needs a touch from a visual perspective. **Result:** Okay, this is much better. However, I believe this needs a touch from a visual perspective.
@ -128,13 +166,13 @@ For the sake of simplicity, we will use `page` data source and limit the results
|name |author |repo | |name |author |repo |
|--|--|--| |--|--|--|
|🔌 Query |Silver Bullet Authors|https://github.com/silverbulletmd/silverbullet | |🔌 Query |Silver Bullet Authors|https://github.com/silverbulletmd/silverbullet |
|🔌 Github|Zef Hemel |https://github.com/silverbulletmd/silverbullet-github| |🔌 Backlinks|Guillermo Vayá |https://github.com/Willyfrog/silverbullet-backlinks |
|🔌 Mount |Zef Hemel |https://github.com/silverbulletmd/silverbullet-mount | |🔌 Core |Silver Bullet Authors|https://github.com/silverbulletmd/silverbullet |
|🔌 Git |Zef Hemel |https://github.com/silverbulletmd/silverbullet-github|
|🔌 Ghost |Zef Hemel |https://github.com/silverbulletmd/silverbullet-ghost | |🔌 Ghost |Zef Hemel |https://github.com/silverbulletmd/silverbullet-ghost |
|🔌 Git |Zef Hemel |https://github.com/silverbulletmd/silverbullet-github|
<!-- /query --> <!-- /query -->
#### 5.4 Display the data in a format defined by a template #### 6.4 Display the data in a format defined by a template
**Goal:** We would like to display the data from step 5.3 in a nice format using bullet points with links to Plug pages, with author name and link to their GitHub repo. **Goal:** We would like to display the data from step 5.3 in a nice format using bullet points with links to Plug pages, with author name and link to their GitHub repo.
@ -142,13 +180,13 @@ For the sake of simplicity, we will use `page` data source and limit the results
<!-- #query page select name author repo uri where type = "plug" order by lastModified desc limit 5 render [[template/plug]] --> <!-- #query page select name author repo uri where type = "plug" order by lastModified desc limit 5 render [[template/plug]] -->
* [[🔌 Query]] by **Silver Bullet Authors** ([repo](https://github.com/silverbulletmd/silverbullet)) * [[🔌 Query]] by **Silver Bullet Authors** ([repo](https://github.com/silverbulletmd/silverbullet))
* [[🔌 Github]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-github)) * [[🔌 Backlinks]] by **Guillermo Vayá** ([repo](https://github.com/Willyfrog/silverbullet-backlinks))
* [[🔌 Mount]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-mount)) * [[🔌 Core]] by **Silver Bullet Authors** ([repo](https://github.com/silverbulletmd/silverbullet))
* [[🔌 Git]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-github))
* [[🔌 Ghost]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-ghost)) * [[🔌 Ghost]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-ghost))
* [[🔌 Git]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-github))
<!-- /query --> <!-- /query -->
PS: You don't need select only certain fields to use templates. Templates are smart enough to get only the information needed to render the data. PS: You don't need to select only certain fields to use templates. Templates are smart enough to get only the information needed to render the data.
Therefore, following queries are same in terms of end result when using the templates. Therefore, following queries are same in terms of end result when using the templates.
```yaml ```yaml