8000 feat: BrowserDb query cursor and v3 core (v3) by adityamukho · Pull Request #4 · project-minigraf/minigraf-wasm · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: WASM CI

on:
push:
branches: [main]
branches: [main, v3]
pull_request:
branches: [main]
branches: [main, v3]
workflow_call:
inputs:
ref:
Expand All @@ -13,7 +13,7 @@ on:
type: string
default: ''
core-ref:
description: 'minigraf core ref to build the WASI binary from (defaults to main)'
description: 'minigraf core ref to build the WASI binary from (defaults to v3 on this branch)'
required: false
type: string
default: ''
Expand Down Expand Up @@ -41,7 +41,7 @@ jobs:
- uses: actions/checkout@v4
with:
repository: project-minigraf/minigraf
ref: ${{ inputs.core-ref || 'main' }}
ref: ${{ inputs.core-ref || 'v3' }}
- uses: dtolnay/rust-toolchain@stable
with:
targets: wasm32-wasip1
Expand Down
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ description = "wasm-pack build shim for Minigraf browser and WASI targets"
crate-type = ["cdylib", "rlib"]

[dependencies]
minigraf = { version = "=2.0.3", features = ["browser"] }
# Unreleased v3 core (format v8). Switches to version = "=3.0.0" at the v3.0.0 release.
minigraf = { git = "https://github.com/project-minigraf/minigraf", rev = "97cd4a2a85301ea3478250dd654139d45a1f7cd0", features = ["browser"] }

[features]
browser = ["minigraf/browser"]
Expand Down
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,45 @@ Two packages are published from this repo:
## Browser (`@minigraf/browser`)

```js
import init, { MiniGrafDb } from '@minigraf/browser'
import init, { BrowserDb } from '@minigraf/browser'

await init()

const db = MiniGrafDb.inMemory()
db.execute('(transact [[:alice :name "Alice"]])')
const db = await BrowserDb.open('mydb') // IndexedDB-backed; BrowserDb.openInMemory() for tests
await db.execute('(transact [[:alice :name "Alice"]])')

const result = JSON.parse(db.execute('(query [:find ?n :where [?e :name ?n]])'))
const result = JSON.parse(await db.execute('(query [:find ?n :where [?e :name ?n]])'))
console.log(result.results[0][0]) // "Alice"
```

### Query cursors

`db.query(datalog)` returns a `BrowserCursor` whose answer is fixed when it opens.
`nextBatch(n)` returns up to `n` rows as a JSON array (encoded like `execute()`'s
`results`), or `undefined` at the end; `close()` releases it. A `transact`, `retract`
or `rule` throws `[API-012]`.

```js
const cursor = db.query('(query [:find ?n :where [?e :name ?n]])')
try {
let batch
while ((batch = cursor.nextBatch(1000)) !== undefined) {
for (const row of JSON.parse(batch)) console.log(row)
}
} finally {
cursor.close()
}
```

### Not available in the browser

`BrowserDb` keeps its pages in memory and writes them to IndexedDB; it has no `.graph`
file, WAL or file lock. So the file-backed options of the other bindings
(`read_only`, `allow_unlocked`, `page_cache_size`, the WAL options) do not apply, and
the raw log writer (`LogWriter`) is not built for wasm32. The fact log is not exposed
in the browser. To move data between a browser and a native database, use
`exportGraph()` / `importGraph()`.

## WASI (`@minigraf/wasi`)

```js
Expand Down
Loading
0