```meta type: plug uri: core:query repo: https://github.com/silverbulletmd/silverbullet author: Silver Bullet Authors ``` ### 1. What? The query plug is a built-in plug implementing the `` mechanism. You can use query plug to automatically receive information from your notes. ### 2. Syntax 1. start with: `` 2. end with: `` 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 general query language except special render option. Render option is to use display the data in a format that you created in a separate template * `where` * `order` * `limit` * `select` * `render` 5. 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) ### 3. How to run a query? After writing the query, there are two options * Open the **command palette** and run **Materialized Queries: Update** * Use shortcut: for windows **Alt-q** and for mac **Option-q** * Go to another page and come back to the page where query is located After using one of the options, the data will be displayed. ### 4. Data sources Available data sources can be categorized as 1. Builtin data sources 2. Data that can be inserted by users 3. Plugโ€™s data sources Best part about data sources: there is an auto completion ๐ŸŽ‰. Start writing ` 20 and country = "Italy" --> |name|age|city |country|page |pos | |----|--|-----|-----|---------------|----| |John|50|Milan|Italy|Test Data Query|0 | |Jane|53|Rome |Italy|Test Data Query|46 | |John|50|Milan|Italy|๐Ÿ”Œ Query |2148| |Jane|53|Rome |Italy|๐Ÿ”Œ Query |2194| #### 4.3 Plugsโ€™ data sources Certain plugs can also give you special options to query a certain data. Some examples are * [[๐Ÿ”Œ Github]] provides `gh-pull` to query PRs for selected repo * [[๐Ÿ”Œ Mattermost]] provides `mm-saved` to fetch (by default 15) saved posts in Mattermost For complete list of data sources, please check plugsโ€™ pages ### 5. Examples We will walk you through a set of example starting from very basic one all the way 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. 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 **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 |name |lastModified |perm|tags |type|uri |repo |author | |--|--|--|--|--|--|--|--| |index |1659178324609|rw|undefined|undefined|undefined |undefined |undefined | |Mattermost Plugin|1659108035000|rw|undefined|undefined|undefined |undefined |undefined | |PLUGS |1659108634000|rw|undefined|undefined|undefined |undefined |undefined | |Test Data Query |1659179547936|rw|undefined|undefined|undefined |undefined |undefined | |template/plug |1659108035000|rw|undefined|undefined|undefined |undefined |undefined | |template/tasks |1659108035000|rw|#each|undefined|undefined |undefined |undefined | |๐Ÿ’ก Inspiration |1659108035000|rw|undefined|undefined|undefined |undefined |undefined | |๐Ÿ”Œ Backlinks |1659108035000|rw|undefined|plug|ghr:Willyfrog/silverbullet-backlinks |https://github.com/Willyfrog/silverbullet-backlinks |Guillermo Vayรก| |๐Ÿ”Œ Ghost |1659108035000|rw|undefined|plug|github:silverbulletmd/silverbullet-ghost/ghost.plug.json |https://github.com/silverbulletmd/silverbullet-ghost |Zef Hemel | |๐Ÿ”Œ Git |1659108035000|rw|undefined|plug|github:silverbulletmd/silverbullet-github/github.plug.json|https://github.com/silverbulletmd/silverbullet-github|Zef Hemel | #### 5.2 Simple query without a condition **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 want to show in the page |name |lastModified |perm|type|uri |repo |author | |--|--|--|--|--|--|--| |๐Ÿ”Œ Query |1659180816719|rw|plug|core:query |https://github.com/silverbulletmd/silverbullet |Silver Bullet Authors| |๐Ÿ”Œ Mattermost|1659111156000|rw|plug|github:silverbulletmd/silverbullet-mattermost/mattermost.plug.json|https://github.com/silverbulletmd/silverbullet-mattermost|Zef Hemel | |๐Ÿ”Œ Backlinks |1659108035000|rw|plug|ghr:Willyfrog/silverbullet-backlinks |https://github.com/Willyfrog/silverbullet-backlinks |Guillermo Vayรก | |๐Ÿ”Œ Ghost |1659108035000|rw|plug|github:silverbulletmd/silverbullet-ghost/ghost.plug.json |https://github.com/silverbulletmd/silverbullet-ghost |Zef Hemel | |๐Ÿ”Œ Git |1659108035000|rw|plug|github:silverbulletmd/silverbullet-github/github.plug.json |https://github.com/silverbulletmd/silverbullet-github |Zef Hemel | #### 5.3 Query to select only certain fields **Goal:** We would like to get all plug pages, select only `name` and `uri` column and sort by last modified time and **Result:** Okay, this is much better. However, I believe this needs a touch from a visual perspective |name |author |repo | |--|--|--| |๐Ÿ”Œ Query |Silver Bullet Authors|https://github.com/silverbulletmd/silverbullet | |๐Ÿ”Œ Mattermost|Zef Hemel |https://github.com/silverbulletmd/silverbullet-mattermost| |๐Ÿ”Œ Backlinks |Guillermo Vayรก |https://github.com/Willyfrog/silverbullet-backlinks | |๐Ÿ”Œ Ghost |Zef Hemel |https://github.com/silverbulletmd/silverbullet-ghost | |๐Ÿ”Œ Git |Zef Hemel |https://github.com/silverbulletmd/silverbullet-github | #### 5.4 Display the data in a format defined by a template **Goal:** We would like to display the data from 5.3 in a nice format using bullet points with links to Plug pages, with author name and link to their GitHub repo. **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? ๐Ÿš€ * [[๐Ÿ”Œ Query]] by **Silver Bullet Authors** ([repo](https://github.com/silverbulletmd/silverbullet)) * [[๐Ÿ”Œ Mattermost]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-mattermost)) * [[๐Ÿ”Œ Backlinks]] by **Guillermo Vayรก** ([repo](https://github.com/Willyfrog/silverbullet-backlinks)) * [[๐Ÿ”Œ Ghost]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-ghost)) * [[๐Ÿ”Œ Git]] by **Zef Hemel** ([repo](https://github.com/silverbulletmd/silverbullet-github))