Dataview: reuse DQL queries?

Hi @anon63144152, great question. I’m generally a fan of more atomic notes, so I would probably keep it to one file per query and then refer to them individually.

Of course, if the two queries depend on each other, or would always be together, then it might make sense to keep them in the same file.

1 Like

I think I am going to have to go for one query in each JS file as I can’t get two queries in the same file to run. I would ideally like to add some inline JS snippets in the JS file as well but not been able to make those work either. Baby steps. Lots of reading to do.

But brilliant idea. Loving the paths it is taking my mind along. :heart_eyes_cat::exploding_head::person_in_lotus_position:t3:

Inline JS snippets don’t work correctly in live preview from memory, have a look in reading mode - you might find they’re working there.

1 Like

Something like this works when placed directly in a MD note in both live preview and reading mode:

`$= moment(dv.current().file.day.toISODate()).diff(moment('2022-11-14'), 'years').toLocaleString('en-en') + " years"` / `$= moment(dv.current().file.day.toISODate()).diff(moment('2022-11-14'), 'days').toLocaleString('en-en') + " days"`   

But if I put it in its own JS file—exactly as it is written above—and then call it in a MD note it doesn’t work:

> [!INFO] Quote  
> ```dataviewjs
>dv.view("/js/t1");
>```

I am also trying to put two DQL tables in one JS file but can’t get that to work as yet.

dv.execute(`

TABLE
file.tags as Tags
WHERE file.cday = this.file.day
or file.mday = this.file.day
SORT file.mtime desc
LIMIT 10

`);

dv.execute(`

dataview
TABLE
file.tags as Tags
FROM #obsidian
SORT file.mtime desc
LIMIT 10

`);

Busy reading the effing manual :smiling_face_with_tear:

The following example is given on the Dataview help pages:

If I create a JS file (called t1) with:

dv.execute("LIST FROM #obsidian");
dv.execute("TABLE title, author FROM #css");

…only the second query is executed when t1 is embedded in a MD file:

```dataviewjs
dv.view("/_inbox/t1");
```

Shouldn’t both execute in line with the help pages example?

Thanks for any suggestions.

1 Like

Tried a couple of different solutions but always get some (varying) error messages.

I have the following dataview query:

TABLE WITHOUT ID Schlagwort, map(rows, (r) ="* " + link(r.file.link, r.title) + "<br>" + " (" + r.kategorie + ")") AS Einträge
FROM !"bibliothek"
WHERE file.path !=this.file.path
FLATTEN file.tags as Schlagwort
GROUP BY Schlagwort
WHERE econtains(this.file.etags, Schlagwort)

It works absolutely fine when put directly into a note. When put into a separate note and called (in the active note) via

$= dv.execute(await dv.io.load("path/to/file.md"))

(as @orand suggested here), it says “No implementation of ‘map’ found for arguments: array, boolean”.

What am I doing wrong?

This part needs to become

map(rows,  (r) => "* "

for starters. Don’t know if there are other errors, as well.

2 Likes

That’s it - you’re my hero! :partying_face:

Essentially that code is:

`$= moment(....) + " text " ` / `$= moment( ... ) + "text"`

Which will not execute within javascript either, or more correct it’ll not produce any sensible output. Remember that inline dataviewjs query is kind of surrounded by dv.span() in order to produce its output.

So if you change the file to include to something like:

dv.span(moment( ...) + "text " + moment( ... ) + "text")

It’ll happily execute it.

Regarding the double DQL queries to be executed, that’s another matter it seems. There is an extra dataview before the second query, which makes it not compile, and give an error message on the console. But even with that fixed, it’s a little tricky to get it to work properly.

There are concurrency issues related to what runs when, and also related to how the dv.execute (or dv.query which I also tested) and the other dv.functions() calls are executed and hook into the DOM. At least, when I added the javascript above, two dv.header() calls, and the two dv.execute() the order got messed up, and only one of the two queries (seemingly random which one) was executed.

My full javascript-file
dv.span(
  moment(dv.current().file.day.toISODate())
    .diff(moment('2022-11-14'), 'years')
    .toLocaleString('en-en') + 
  " years / " +
  moment(dv.current().file.day.toISODate())
    .diff(moment('2022-11-14'), 'days')
    .toLocaleString('en-en')
  + " days" )
                 
dv.header(2, "First query")

dv.execute(`
  TABLE
    file.tags as Tags1
  WHERE file.tags 
  WHERE file.cday = this.file.day
     OR file.mday = this.file.day
  SORT file.mtime desc
  LIMIT 10
`); /* */

dv.header(2, "Second query")

dv.execute(`
  TABLE 
    file.tags as Tags2 , file.cday
  WHERE file.tags
  SORT file.mtime desc 
  LIMIT 10
`);          

So I’m thinking it’s not wise to include too much of this stuff, especially in the context of calling through dv.view(). I’ve never experience this order issue, or stuff being left out when doing it directly within dataviewjs.

Apologies for the tardy reply—not been online for a few days. :scream:

Thanks for the help. Realize that this is beyond what I can work with at the moment. Either need to learn more or to just keep things simple.

Right now the fun of Obsidian is bleeding away over the frustration of things I can’t tame. Time to take a break. :stop_sign: :wave:t4: :crying_cat_face:

1 Like