1
0
silverbullet/website/🔌 Query.md

199 lines
11 KiB
Markdown
Raw Normal View History

2022-07-28 17:47:36 +00:00
```meta
type: plug
uri: core:query
repo: https://github.com/silverbulletmd/silverbullet
author: Silver Bullet Authors
```
2022-07-30 11:11:24 +00:00
### 1. What?
2022-07-30 15:42:25 +00:00
The query plug is a built-in plug implementing the `<!-- #query -->` mechanism. You can use query plug to automatically receive information from your pages.
2022-07-29 16:05:43 +00:00
2022-07-30 11:11:24 +00:00
### 2. Syntax
2022-07-30 11:52:41 +00:00
1. _start with_: `<!-- #query [QUERY GOES HERE] -->`
2. _end with_: `<!-- /query -->`
3. _write your query_: replace `[QUERY GOES HERE]` with any query you want using options below
2022-08-21 20:23:30 +00:00
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.
2022-07-29 16:05:43 +00:00
* `where`
2022-07-30 15:42:25 +00:00
* `order by`
2022-07-29 16:05:43 +00:00
* `limit`
* `select`
* `render`
2022-07-30 11:52:41 +00:00
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).
2022-07-29 16:05:43 +00:00
2022-08-21 20:23:30 +00:00
#### 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”`.
2022-07-30 11:11:24 +00:00
### 3. How to run a query?
2022-07-30 11:52:41 +00:00
After writing the query, there are three options:
2022-07-29 16:05:43 +00:00
* Open the **command palette** and run **Materialized Queries: Update**
2022-07-30 11:52:41 +00:00
* Use shortcut: hit **Alt-q** (Windows, Linux) or **Option-q** (Mac)
2022-07-30 11:03:57 +00:00
* Go to another page and come back to the page where query is located
2022-07-29 16:05:43 +00:00
2022-07-30 15:42:25 +00:00
After using one of the options, the “body” of the query is replaced with the new results of the query data will be displayed.
2022-07-30 11:11:24 +00:00
### 4. Data sources
2022-07-30 11:52:41 +00:00
Available data sources can be categorized as:
2022-07-30 11:11:24 +00:00
1. Builtin data sources
2. Data that can be inserted by users
2022-07-30 11:13:43 +00:00
3. Plugs data sources
2022-07-30 11:11:24 +00:00
2022-07-30 11:52:41 +00:00
Best part about data sources: there is an auto completion. 🎉
2022-07-30 15:42:25 +00:00
Start writing `<!— #query ` or simply use `/query` slash command, it will show you all available data sources. 🤯
2022-07-30 11:11:24 +00:00
2022-07-30 15:42:25 +00:00
#### 4.1. Available data sources
2022-07-30 11:52:41 +00:00
* `page`: list of all pages 📄
* `task`: list of all tasks created with `[]` syntax ✅
* `full-text`: use it with `where phrase = "SOME_TEXT"`. List of all pages where `SOME_TEXT` is mentioned ✍️
* `item`: list of ordered and unordered items such as bulleted lists ⏺️
* `tags`: list of all hashtags used in all pages ⚡
* `link`: list of all pages giving a link to the page where query is written 🔗
2022-07-30 15:42:25 +00:00
* `data`: You can insert a data using the syntax below 🖥️. You can query the data using `data` option.
2022-07-30 11:13:43 +00:00
```data
name: John
age: 50
city: Milan
country: Italy
---
name: Jane
age: 53
city: Rome
country: Italy
---
name: Francesco
age: 28
city: Berlin
country: Germany
```
<!-- #query data where age > 20 and country = "Italy" -->
2022-08-09 13:37:47 +00:00
|name|age|city |country|page |pos |
|----|--|-----|-----|--------|----|
2022-08-21 20:45:32 +00:00
|John|50|Milan|Italy|🔌 Query|2696|
|Jane|53|Rome |Italy|🔌 Query|2742|
2022-07-30 11:11:24 +00:00
<!-- /query -->
2022-07-30 15:42:25 +00:00
#### 4.2 Plugs data sources
2022-08-21 20:23:30 +00:00
Certain plugs can also provide special data sources to query a certain data. Some examples are:
2022-07-30 11:16:40 +00:00
* [[🔌 Github]] provides `gh-pull` to query PRs for selected repo
* [[🔌 Mattermost]] provides `mm-saved` to fetch (by default 15) saved posts in Mattermost
2022-07-30 11:52:41 +00:00
For a complete list of data sources, please check plugs own pages.
2022-07-30 11:22:41 +00:00
2022-08-21 20:35:32 +00:00
### 5. Templates
Templates are predefined formats to render the body of the query.
#### 5.1 How to create a template?
2022-08-21 20:49:49 +00:00
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.
2022-08-21 20:35:32 +00:00
2022-08-21 20:45:32 +00:00
#### 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}}))`
2022-08-21 20:49:49 +00:00
Let me break it down for you
2022-08-21 20:45:32 +00:00
* `* ` 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/).
2022-08-21 20:35:32 +00:00
2022-08-21 20:49:49 +00:00
#### 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.
2022-08-21 20:35:32 +00:00
### 6. Examples
2022-07-30 11:52:41 +00:00
We will walk you through a set of examples starting from very basic one until to format the data using templates.
2022-07-30 11:22:41 +00:00
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.
2022-08-21 20:35:32 +00:00
#### 6.1 Simple query without any condition
2022-07-30 11:29:23 +00:00
**Goal:** We would like to get the list of all pages.
2022-07-30 11:22:41 +00:00
2022-07-30 11:52:41 +00:00
**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.
2022-07-30 11:22:41 +00:00
<!-- #query page limit 10 -->
2022-08-09 13:37:47 +00:00
|name |lastModified |perm|tags |type|uri |repo |author |
2022-07-30 11:22:41 +00:00
|--|--|--|--|--|--|--|--|
2022-08-21 20:23:30 +00:00
|SETTINGS |1661112513714|rw|undefined|undefined|undefined |undefined |undefined |
|Silver Bullet |1661112513714|rw|undefined|undefined|undefined |undefined |undefined |
|CHANGELOG |1661112513714|rw|undefined|undefined|undefined |undefined |undefined |
|Mattermost Plugin|1661112513714|rw|undefined|undefined|undefined |undefined |undefined |
|PLUGS |1661112513714|rw|undefined|undefined|undefined |undefined |undefined |
|index |1661112513714|rw|undefined|undefined|undefined |undefined |undefined |
|template/plug |1661112513718|rw|undefined|undefined|undefined |undefined |undefined |
|template/tasks |1661112513718|rw|#each|undefined|undefined |undefined |undefined |
|💡 Inspiration |1661112513718|rw|undefined|undefined|undefined |undefined |undefined |
|🔌 Backlinks |1661112513718|rw|undefined|plug|ghr:Willyfrog/silverbullet-backlinks|https://github.com/Willyfrog/silverbullet-backlinks|Guillermo Vayá|
2022-07-30 11:22:41 +00:00
<!-- /query -->
2022-08-21 20:35:32 +00:00
#### 6.2 Simple query with a condition
2022-07-30 11:52:41 +00:00
**Goal:** We would like to get all plug pages and sorted by last modified time.
2022-07-30 11:29:23 +00:00
2022-07-30 11:52:41 +00:00
**Result:** Okay, this what we wanted but there are also information such as perm, type and lastModified that we don't need.
2022-07-30 11:24:23 +00:00
<!-- #query page where type = "plug" order by lastModified desc limit 5 -->
2022-08-21 20:23:30 +00:00
|name |lastModified |perm|type|uri |repo |author |
2022-07-30 11:24:23 +00:00
|--|--|--|--|--|--|--|
2022-08-21 20:45:32 +00:00
|🔌 Query |1661114193972|rw|plug|core:query |https://github.com/silverbulletmd/silverbullet |Silver Bullet Authors|
2022-08-21 20:23:30 +00:00
|🔌 Backlinks|1661112513718|rw|plug|ghr:Willyfrog/silverbullet-backlinks |https://github.com/Willyfrog/silverbullet-backlinks |Guillermo Vayá |
|🔌 Core |1661112513718|rw|plug|builtin:core |https://github.com/silverbulletmd/silverbullet |Silver Bullet Authors|
|🔌 Ghost |1661112513718|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 |
2022-07-30 11:24:23 +00:00
<!-- /query -->
2022-07-30 11:29:23 +00:00
2022-08-21 20:35:32 +00:00
#### 6.3 Query to select only certain fields
2022-07-30 11:52:41 +00:00
**Goal:** We would like to get all plug pages, select only `name`, `author` and `repo` columns and sort by last modified time.
2022-07-30 11:29:23 +00:00
2022-07-30 11:52:41 +00:00
**Result:** Okay, this is much better. However, I believe this needs a touch from a visual perspective.
2022-07-30 11:29:23 +00:00
2022-07-30 11:36:25 +00:00
<!-- #query page select name author repo uri where type = "plug" order by lastModified desc limit 5 -->
2022-08-21 20:23:30 +00:00
|name |author |repo |
2022-07-30 11:36:25 +00:00
|--|--|--|
2022-08-21 20:45:32 +00:00
|🔌 Query |Silver Bullet Authors|https://github.com/silverbulletmd/silverbullet |
2022-08-21 20:23:30 +00:00
|🔌 Backlinks|Guillermo Vayá |https://github.com/Willyfrog/silverbullet-backlinks |
|🔌 Core |Silver Bullet Authors|https://github.com/silverbulletmd/silverbullet |
|🔌 Ghost |Zef Hemel |https://github.com/silverbulletmd/silverbullet-ghost |
|🔌 Git |Zef Hemel |https://github.com/silverbulletmd/silverbullet-github|
2022-07-30 11:29:23 +00:00
<!-- /query -->
2022-08-21 20:35:32 +00:00
#### 6.4 Display the data in a format defined by a template
2022-07-30 11:36:25 +00:00
2022-07-30 11:52:41 +00:00
**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.
2022-07-30 11:36:25 +00:00
2022-07-30 11:52:41 +00:00
**Result:** Here you go. This is the result we would like to achieve 🎉. Did you see how I used `render` and `template/plug` in a query? 🚀
2022-07-30 11:36:25 +00:00
2022-08-09 13:37:47 +00:00
<!-- #query page select name author repo uri where type = "plug" order by lastModified desc limit 5 render [[template/plug]] -->
2022-08-21 20:45:32 +00:00
* [[🔌 Query]] by **Silver Bullet Authors** ([repo](https://github.com/silverbulletmd/silverbullet))
2022-08-21 20:23:30 +00:00
* [[🔌 Backlinks]] by **Guillermo Vayá** ([repo](https://github.com/Willyfrog/silverbullet-backlinks))
* [[🔌 Core]] by **Silver Bullet Authors** ([repo](https://github.com/silverbulletmd/silverbullet))
2022-08-09 13:37:47 +00:00
* [[🔌 Ghost]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-ghost))
2022-08-21 20:23:30 +00:00
* [[🔌 Git]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-github))
2022-07-30 11:52:41 +00:00
<!-- /query -->
2022-08-21 20:23:30 +00:00
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.
2022-07-30 11:52:41 +00:00
Therefore, following queries are same in terms of end result when using the templates.
```yaml
2022-08-09 13:37:47 +00:00
<!-- #query page select name author repo uri where type = "plug" order by lastModified desc limit 5 render [[template/plug]] -->
2022-07-30 11:52:41 +00:00
```
```yaml
2022-08-09 13:37:47 +00:00
<!-- #query page where type = "plug" order by lastModified desc limit 5 render [[template/plug]] -->
2022-07-30 11:52:41 +00:00
```