# Associate nodes with database entries, inspect changes

**URL:** https://discuss.prosemirror.net/t/associate-nodes-with-database-entries-inspect-changes/3423
**Category:** Uncategorized
**Created:** [January 21, 2021, 5:41am UTC](https://discuss.prosemirror.net/t/associate-nodes-with-database-entries-inspect-changes/3423 "2021-01-21T05:41:16Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![byw](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@byw](https://discuss.prosemirror.net/u/byw)
#### Post date: [January 21, 2021, 5:41am UTC](https://discuss.prosemirror.net/t/associate-nodes-with-database-entries-inspect-changes/3423/1 "2021-01-21T05:41:16Z")

</div>

Hi,

So right now I have a `hashTag` node type, and every time a `hashTag` is created and deleted, I’d like to insert/delete the corresponding database entry.

After looking around, I added a `dispatchTransaction` callback for the view, which passes in a `Transaction`.

In Prosemirror Dev Tool, I seemed to have access to the node through `tx > steps > 0 > slice > content > content > hashTag node`. But in code (TypeScript), I can’t seem to access `slice` on the `Step`. I tried converting it to `ReplaceStep` to no avail. Is there a way to get access, or am I doing it wrong?

Or maybe taking a step back, am I approaching this problem correctly?

Thanks in advance!

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [January 21, 2021, 10:40am UTC](https://discuss.prosemirror.net/t/associate-nodes-with-database-entries-inspect-changes/3423/2 "2021-01-21T10:40:30Z")

</div>

Not all step types have slices. You could do an `s instanceof ReplaceStep || s instanceof ReplaceAroundStep` check to narrow the type, or you could iterate over the parts of the new doc covered by the step (with the step map’s `forEach` method).

---

<div class="post-metadata">

### Author: ![byw](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@byw](https://discuss.prosemirror.net/u/byw)
#### Post date: [January 22, 2021, 12:46am UTC](https://discuss.prosemirror.net/t/associate-nodes-with-database-entries-inspect-changes/3423/3 "2021-01-22T00:46:50Z")

</div>

Thanks for the pointers.

It looks like `slice` is not exposed in `ReplaceStep` and `ReplaceAroundStep` though: [https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/prosemirror-transform/index.d.ts#L347](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/prosemirror-transform/index.d.ts#L347) Is there another way to access them?

But I guess to detect node removal, I still need to compare docs right? (I couldn’t find any info in the slice about what node was removed) So for that would the last doc in `tr.docs` give me a point of comparison?

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [January 22, 2021, 8:06am UTC](https://discuss.prosemirror.net/t/associate-nodes-with-database-entries-inspect-changes/3423/5 "2021-01-22T08:06:41Z")

</div>

Oh, good point, those are indeed not public it seems.

But indeed, to detect removal you’ll also need to iterate over the deleted range. The step maps also give you the information you need there.

---

<div class="post-metadata">

### Author: ![byw](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@byw](https://discuss.prosemirror.net/u/byw)
#### Post date: [January 23, 2021, 12:23am UTC](https://discuss.prosemirror.net/t/associate-nodes-with-database-entries-inspect-changes/3423/6 "2021-01-23T00:23:23Z")

</div>

Good to know.

So I’m using `step.getMap()` like this:

```
          const lastDoc = tr.docs[tr.docs.length - 1]
          const thisDoc = tr.doc

          tr.steps.forEach(step => {
            step.getMap().forEach((oldStart, oldEnd, newStart, newEnd) => {  

              const newNodes: Node[] = []
              thisDoc.nodesBetween(newStart, newEnd, newNode => { newNodes.push(newNode) })
              lastDoc.nodesBetween(oldStart, oldEnd, oldNode => {
                if (oldNode.type === schema.nodes.hashTag && !newNodes.includes(oldNode)) {
                  console.log('hash tag deleted', oldNode)
                }
              })
            })
          }

```

But it seems `oldEnd` was out of bound in `lastDoc.nodesBetween(oldStart, oldEnd, ...`

I assumed `oldStart` and `oldEnd` refers to the previous doc, and `newStart` and `newEnd` refers to the current doc, is that correct?

---

<div class="post-metadata">

### Author: ![byw](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@byw](https://discuss.prosemirror.net/u/byw)
#### Post date: [January 23, 2021, 6:18am UTC](https://discuss.prosemirror.net/t/associate-nodes-with-database-entries-inspect-changes/3423/7 "2021-01-23T06:18:39Z")

</div>

Oh, I think I got it. Looks like each step should use its own doc through `tr.docs[stepIndex]`.

---

<div class="post-metadata">

### Author: ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)
#### Post date: [January 23, 2021, 12:31pm UTC](https://discuss.prosemirror.net/t/associate-nodes-with-database-entries-inspect-changes/3423/8 "2021-01-23T12:31:36Z")

</div>

Yes, that’s the idea — a step map indicates a difference between the docs before and after that step.

---

<div class="post-metadata">

### Author: ![byw](https://discuss.prosemirror.net/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@byw](https://discuss.prosemirror.net/u/byw)
#### Post date: [January 24, 2021, 10:02pm UTC](https://discuss.prosemirror.net/t/associate-nodes-with-database-entries-inspect-changes/3423/9 "2021-01-24T22:02:10Z")

</div>

Thanks for the help. Finally things are coming together.
