8000 feat: prepared queries in @minigraf/browser (minigraf#181) by adityamukho · Pull Request #18 · 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
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
# 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 = "949813ce27273d0c2820690df844e76951cb7996", features = ["browser"] }
minigraf = { git = "https://github.com/project-minigraf/minigraf", rev = "5f2ba46e0c5a9954d90e52f60daaad2128d91973", features = ["browser"] }

[features]
browser = ["minigraf/browser"]
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ try {
}
```

### Prepared queries

`db.prepare(datalog)` parses a query once and returns a `BrowserPreparedQuery`;
`execute(bindings)` returns the JSON `execute()` returns, and `query(bindings)` a
`BrowserCursor`. Both are synchronous. Bindings are an object of slot name (without the
`$`) to a boolean, a string, `null`, a `bigint` or a `number` (an integer when
`Number.isInteger` accepts it, else a float), or a typed `{ type, value }` form:
`{ type: 'float', value: 2 }`, `'keyword'`, `'ref'`, `{ type: 'entity', value: uuid }`,
`{ type: 'txCount' | 'timestamp', value }` for `:as-of`, and `{ type: 'anyValidTime' }`
for `:valid-at`. A value that is not a bind value throws a `TypeError`; a missing slot
throws `[API-022]`, a slot in an attribute position `[API-023]`, and a value of the wrong
kind `[API-024]`.

```js
const pq = db.prepare('(query [:find ?n :where [?e :age $age] [?e :name ?n]])')
JSON.parse(pq.execute({ age: 30 })).results
```

### Not available in the browser

`BrowserDb` keeps its pages in memory and writes them to IndexedDB; it has no `.graph`
Expand Down
14 changes: 14 additions & 0 deletions consumer-test/browser/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ async function run() {
const rows = JSON.parse(await mem.execute('(query [:find ?n ?a :where [?e :name ?n] [?e :age ?a]])')).results
check(JSON.stringify(rows) === '[["Alice",30]]', 'in-memory transact and query')

// Prepared query (minigraf#181): parse once, run with plain and typed values.
await mem.execute('(transact [[:bob :name "Bob"] [:bob :age 41]])')
const pq = mem.prepare('(query [:find ?n :where [?e :age $age] [?e :name ?n]])')
check(JSON.stringify(JSON.parse(pq.execute({ age: 30 })).results) === '[["Alice"]]', 'prepared query with a number')
check(JSON.stringify(JSON.parse(pq.execute({ age: 41n })).results) === '[["Bob"]]', 'prepared query with a bigint')
check(JSON.stringify(JSON.parse(pq.execute({ age: { type: 'integer', value: 30 } })).results) === '[["Alice"]]', 'prepared query with a typed value')
let missing
try {
pq.execute({})
} catch (e) {
missing = e
}
check(typeof missing === 'string' && missing.startsWith('[API-022] '), `a missing slot is API-022 (${missing})`)

// Error type: a rejected promise whose reason starts with the error code,
// as on every other API ("[PRS-011] Unknown command: nosuchcommand").
let reason
Expand Down
Loading
0