-
Notifications
You must be signed in to change notification settings - Fork 7
Templated datalog queries #54
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| const getAllBlockUids = (): string[] => | ||
| window.roamAlphaAPI | ||
| .q(`[:find ?u :where [?e :block/uid ?u] [?e :block/string]]`) | ||
| .map((f) => f[0] as string); | ||
| .q<[string]>(`[:find ?u :where [?e :block/uid ?u] [?e :block/string]]`) | ||
| .map((f) => f[0]); | ||
|
|
||
| export default getAllBlockUids; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| const getAllBlockUidsAndTexts = (): { uid: string; text: string }[] => | ||
| window.roamAlphaAPI | ||
| .q(`[:find ?u ?s :where [?e :block/uid ?u] [?e :block/string ?s]]`) | ||
| .map((f) => ({ uid: f[0] as string, text: f[1] as string })); | ||
| .q<[string, string]>( | ||
| `[:find ?u ?s :where [?e :block/uid ?u] [?e :block/string ?s]]` | ||
| ) | ||
| .map((f) => ({ uid: f[0], text: f[1] })); | ||
|
|
||
| export default getAllBlockUidsAndTexts; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| const getAllPageNames = (): string[] => | ||
| window.roamAlphaAPI | ||
| .q("[:find ?s :where [?e :node/title ?s]]") | ||
| .map((b) => b[0] as string); | ||
| .q<[string]>("[:find ?s :where [?e :node/title ?s]]") | ||
| .map((b) => b[0]); | ||
|
|
||
| export default getAllPageNames; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| import { PullBlock } from "../types"; | ||
|
|
||
| const getBlockUidsReferencingBlock = (uid: string): string[] => | ||
| ( | ||
| window.roamAlphaAPI.data.fast.q( | ||
| window.roamAlphaAPI.data.fast | ||
| .q<[PullBlock]>( | ||
| `[:find (pull ?r [:block/uid]) :where [?b :block/uid "${uid}"] [?r :block/refs ?b]]` | ||
| ) as [PullBlock][] | ||
| ).map((s) => s[0][":block/uid"] || ""); | ||
| ) | ||
| .map((s) => s[0][":block/uid"] || ""); | ||
|
|
||
| export default getBlockUidsReferencingBlock; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,8 @@ | ||
|
|
||
|
|
||
| const getBlockUidsWithParentUid = (uid: string): string[] => | ||
| window.roamAlphaAPI | ||
| .q( | ||
| .q<[string]>( | ||
| `[:find ?u :where [?c :block/uid ?u] [?c :block/parents ?b] [?b :block/uid "${uid}"]]` | ||
| ) | ||
| .map((r) => r[0] as string); | ||
| .map((r) => r[0]); | ||
|
|
||
| export default getBlockUidsWithParentUid; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| const getDisplayNameByEmail = (email: string): string => | ||
| (window.roamAlphaAPI.q( | ||
| window.roamAlphaAPI.q<[string]>( | ||
| `[:find ?name :where[?e :user/display-name ?name] [?e :user/email "${email}"]]` | ||
| )?.[0]?.[0] as string) || ""; | ||
| )?.[0]?.[0] || ""; | ||
|
|
||
| export default getDisplayNameByEmail; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| const getDisplayNameByUid = (uid: string): string => | ||
| (window.roamAlphaAPI.q( | ||
| window.roamAlphaAPI.q<[string]>( | ||
| `[:find ?s :where [?e :user/uid "${uid}"] [?e :user/display-page ?p] [?p :node/title ?s]]` | ||
| )?.[0]?.[0] as string) || ""; | ||
| )?.[0]?.[0] || ""; | ||
|
|
||
| export default getDisplayNameByUid; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| const getEditedUserEmailByBlockUid = (blockUid: string): string => | ||
| (window.roamAlphaAPI.q( | ||
| window.roamAlphaAPI.q<[string]>( | ||
| `[:find ?e :where [?u :user/email ?e] [?b :edit/user ?u] [?b :block/uid "${blockUid}"]]` | ||
| )?.[0]?.[0] as string) || ""; | ||
| )?.[0]?.[0] || ""; | ||
|
|
||
| export default getEditedUserEmailByBlockUid; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,6 @@ | ||
|
|
||
|
|
||
| const getFirstChildTextByBlockUid = (blockUid: string): string => | ||
| window.roamAlphaAPI.q( | ||
| window.roamAlphaAPI.q<[string]>( | ||
| `[:find ?s :where [?c :block/string ?s] [?c :block/order 0] [?p :block/children ?c] [?p :block/uid "${blockUid}"]]` | ||
| )?.[0]?.[0] as string; | ||
| )?.[0]?.[0]; | ||
|
|
||
| export default getFirstChildTextByBlockUid; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| const getLinkedPageTitlesUnderUid = (uid: string): string[] => | ||
| window.roamAlphaAPI | ||
| .q( | ||
| .q<[string]>( | ||
| `[:find ?t :where [?r :node/title ?t] [?c :block/refs ?r] [?c :block/parents ?b] [?b :block/uid "${uid}"]]` | ||
| ) | ||
| .map((r) => r[0] as string); | ||
| .map((r) => r[0]); | ||
|
|
||
| export default getLinkedPageTitlesUnderUid; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| const getOrderByBlockUid = (blockUid: string): number => | ||
| window.roamAlphaAPI.q( | ||
| window.roamAlphaAPI.q<[number]>( | ||
| `[:find ?o :where [?r :block/order ?o] [?r :block/uid "${blockUid}"]]` | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can return undefined. |
||
| )?.[0]?.[0] as number; | ||
| )?.[0]?.[0]; | ||
|
|
||
| export default getOrderByBlockUid; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,8 +1,6 @@ | ||||||||||||||
| const getPageTitleByBlockUid = (blockUid: string): string => | ||||||||||||||
| ( | ||||||||||||||
| window.roamAlphaAPI.q( | ||||||||||||||
| `[:find (pull ?p [:node/title]) :where [?e :block/uid "${blockUid}"] [?e :block/page ?p]]` | ||||||||||||||
| )?.[0]?.[0] as { title?: string } | ||||||||||||||
| )?.title || ""; | ||||||||||||||
| window.roamAlphaAPI.q<[{ title: string }]>( | ||||||||||||||
| `[:find (pull ?p [:node/title]) :where [?e :block/uid "${blockUid}"] [?e :block/page ?p]]` | ||||||||||||||
| )?.[0]?.[0]?.title || ""; | ||||||||||||||
|
Comment on lines
+2
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type mismatch: pull result uses The Roam pull expression 🔎 Proposed fix const getPageTitleByBlockUid = (blockUid: string): string =>
- window.roamAlphaAPI.q<[{ title: string }]>(
+ window.roamAlphaAPI.q<[{ ":node/title": string }]>(
`[:find (pull ?p [:node/title]) :where [?e :block/uid "${blockUid}"] [?e :block/page ?p]]`
- )?.[0]?.[0]?.title || "";
+ )?.[0]?.[0]?.[":node/title"] || "";Alternatively, use +import type { PullBlock } from "../types";
+
const getPageTitleByBlockUid = (blockUid: string): string =>
- window.roamAlphaAPI.q<[{ title: string }]>(
+ window.roamAlphaAPI.q<[PullBlock]>(
`[:find (pull ?p [:node/title]) :where [?e :block/uid "${blockUid}"] [?e :block/page ?p]]`
- )?.[0]?.[0]?.title || "";
+ )?.[0]?.[0]?.[":node/title"] || "";📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
|
|
||||||||||||||
| export default getPageTitleByBlockUid; | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,6 @@ | ||
| import { PullBlock } from "../types"; | ||
|
|
||
| export const getPageTitleByPageUid = (pageUid: string): string => | ||
| ( | ||
| window.roamAlphaAPI.pull(`[:node/title]`, [ | ||
| ":block/uid", | ||
| pageUid, | ||
| ]) as PullBlock | ||
| )?.[":node/title"] || ""; | ||
| window.roamAlphaAPI.pull(`[:node/title]`, [":block/uid", pageUid])?.[ | ||
| ":node/title" | ||
| ] || ""; | ||
|
|
||
| export default getPageTitleByPageUid; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,13 @@ | ||
| import { PullBlock } from "../types"; | ||
| import type { PullBlock } from "../types"; | ||
| import normalizePageTitle from "./normalizePageTitle"; | ||
|
|
||
| const getPageTitleReferencesByPageTitle = (title: string): string[] => | ||
| ( | ||
| window.roamAlphaAPI.data.fast.q( | ||
| window.roamAlphaAPI.data.fast | ||
| .q<[PullBlock]>( | ||
| `[:find (pull ?b [:node/title]) :where [?r :node/title "${normalizePageTitle( | ||
| title | ||
| )}"] [?c :block/refs ?r] [?c :block/page ?b]]` | ||
| ) as [PullBlock][] | ||
| ).map((p) => p[0][":node/title"] as string); | ||
| ) | ||
| .map((p) => p[0][":node/title"] || ""); | ||
|
|
||
| export default getPageTitleReferencesByPageTitle; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,18 @@ | ||
| import { PullBlock } from "../types"; | ||
| import type { PullBlock } from "../types"; | ||
| import normalizePageTitle from "./normalizePageTitle"; | ||
|
|
||
| const getPageTitlesAndBlockUidsReferencingPage = ( | ||
| pageName: string | ||
| ): { title: string; uid: string }[] => | ||
| ( | ||
| window.roamAlphaAPI.data.fast.q( | ||
| window.roamAlphaAPI.data.fast | ||
| .q<[PullBlock, PullBlock]>( | ||
| `[:find (pull ?pr [:node/title]) (pull ?r [:block/uid]) :where [?p :node/title "${normalizePageTitle( | ||
| pageName | ||
| )}"] [?r :block/refs ?p] [?r :block/page ?pr]]` | ||
| ) as [PullBlock, PullBlock][] | ||
| ).map(([pr, r]) => ({ | ||
| title: pr?.[":node/title"] || "", | ||
| uid: r?.[":block/uid"] || "", | ||
| })); | ||
| ) | ||
| .map(([pr, r]) => ({ | ||
| title: pr[":node/title"] || "", | ||
| uid: r[":block/uid"] || "", | ||
| })); | ||
|
|
||
| export default getPageTitlesAndBlockUidsReferencingPage; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| import { PullBlock } from "../types"; | ||
| import type { PullBlock } from "../types"; | ||
|
|
||
| const getPageTitlesReferencingBlockUid = (uid: string): string[] => | ||
| ( | ||
| window.roamAlphaAPI.data.fast.q( | ||
| window.roamAlphaAPI.data.fast | ||
| .q<[PullBlock]>( | ||
| `[:find (pull ?p [:node/title]) :where [?r :block/uid "${uid}"] [?b :block/refs ?r] [?b :block/page ?p]]` | ||
| ) as [PullBlock][] | ||
| ).map((s) => s[0]?.[":node/title"] || ""); | ||
| ) | ||
| .map((s) => s[0]?.[":node/title"] || ""); | ||
|
|
||
| export default getPageTitlesReferencingBlockUid; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can return undefined.