@feltdb/core 0.12.1 → 0.12.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/build-identity.json +13 -4
- package/dist/cli/commands.js +88 -43
- package/dist/collection.d.ts +41 -4
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +116 -47
- package/dist/composition.d.ts.map +1 -1
- package/dist/composition.js +11 -1
- package/dist/create/build-identity.json +13 -4
- package/dist/create/cli.js +3 -3
- package/dist/create/create.js +17 -19
- package/dist/create/package-versions.js +1 -1
- package/dist/create/server-source/Cargo.lock +18 -3
- package/dist/create/server-source/Cargo.toml +4 -2
- package/dist/create/server-source/crates/feltdb/Cargo.toml +20 -0
- package/dist/create/server-source/crates/feltdb/LICENSE +21 -0
- package/dist/create/server-source/crates/feltdb/README.md +75 -0
- package/dist/create/server-source/crates/feltdb/src/agent_state.rs +320 -0
- package/dist/create/server-source/crates/feltdb/src/agent_transition_contract.rs +1545 -0
- package/dist/create/server-source/crates/feltdb/src/authority.rs +145 -15
- package/dist/create/server-source/crates/feltdb/src/lib.rs +227 -23
- package/dist/create/server-source/crates/feltdb/src/state_model.rs +1 -5
- package/dist/create/server-source/crates/feltdb/tests/agent_state_native.rs +1188 -0
- package/dist/create/server-source/crates/feltdb/tests/agent_transition_contract_conformance.rs +2884 -0
- package/dist/create/server-source/crates/feltdb/tests/authority_transaction_identity.rs +534 -0
- package/dist/create/server-source/crates/feltdb/tests/fixtures/agent_transition_corpus.json +23340 -0
- package/dist/create/server-source/crates/feltdb-client/Cargo.toml +29 -0
- package/dist/create/server-source/crates/feltdb-client/LICENSE +21 -0
- package/dist/create/server-source/crates/feltdb-client/README.md +70 -0
- package/dist/create/server-source/crates/feltdb-client/src/application.rs +527 -0
- package/dist/create/server-source/crates/feltdb-client/src/authority.rs +679 -0
- package/dist/create/server-source/crates/feltdb-client/src/control.rs +292 -0
- package/dist/create/server-source/crates/feltdb-client/src/error.rs +227 -0
- package/dist/create/server-source/crates/feltdb-client/src/events.rs +146 -0
- package/dist/create/server-source/crates/feltdb-client/src/lib.rs +466 -0
- package/dist/create/server-source/crates/feltdb-client/tests/fixtures/README.md +4 -0
- package/dist/create/server-source/crates/feltdb-client/tests/fixtures/private-ca.pem +20 -0
- package/dist/create/server-source/crates/feltdb-client/tests/fixtures/server-key.pem +28 -0
- package/dist/create/server-source/crates/feltdb-client/tests/fixtures/server.pem +20 -0
- package/dist/create/server-source/crates/feltdb-client/tests/private_ca.rs +95 -0
- package/dist/create/server-source/crates/feltdb-server/Cargo.toml +20 -1
- package/dist/create/server-source/crates/feltdb-server/LICENSE +21 -0
- package/dist/create/server-source/crates/feltdb-server/README.md +12 -1
- package/dist/create/server-source/crates/feltdb-server/src/backup.rs +6 -2
- package/dist/create/server-source/crates/feltdb-server/src/lib.rs +20 -0
- package/dist/create/server-source/crates/feltdb-server/src/main.rs +1 -3
- package/dist/create/server-source/crates/feltdb-server/tests/rust_client.rs +683 -0
- package/dist/create/server-source/crates/feltdb-wasm/Cargo.toml +3 -1
- package/dist/create/template/default-project/README.md +12 -7
- package/dist/create/template/default-project/src/pages/Dashboard.tsx +1 -1
- package/dist/create/template/dot-feltdb-README.md +3 -3
- package/dist/db.d.ts.map +1 -1
- package/dist/db.js +43 -17
- package/dist/development-runtime-bridge.d.ts.map +1 -1
- package/dist/development-runtime-bridge.js +22 -1
- package/dist/embedded-transaction.d.ts +75 -8
- package/dist/embedded-transaction.d.ts.map +1 -1
- package/dist/embedded-transaction.js +124 -18
- package/dist/error-codes.d.ts +2 -0
- package/dist/error-codes.d.ts.map +1 -1
- package/dist/error-codes.js +2 -0
- package/dist/feltdb.d.ts +1 -11
- package/dist/feltdb.d.ts.map +1 -1
- package/dist/file-db.d.ts +6 -0
- package/dist/file-db.d.ts.map +1 -1
- package/dist/file-db.js +43 -11
- package/dist/indexeddb-db.d.ts.map +1 -1
- package/dist/indexeddb-db.js +65 -41
- package/dist/memory-db.d.ts.map +1 -1
- package/dist/memory-db.js +29 -8
- package/dist/studio/{KeyManagementPanel-DZuSeWBK.js → KeyManagementPanel-D5FMvvU9.js} +1 -0
- package/dist/studio/app.d.ts.map +1 -1
- package/dist/studio/components/KeyManagementPanel.d.ts.map +1 -1
- package/dist/studio/components/KeyManagementPanel.js +1 -1
- package/dist/studio/components/RouteErrorBoundary.d.ts +5 -0
- package/dist/studio/components/RouteErrorBoundary.d.ts.map +1 -0
- package/dist/studio/components/index.js +2 -2
- package/dist/studio/{components-Bivm2lX6.js → components-B4eKIUN5.js} +1 -1
- package/dist/studio/control-plane/Agents.d.ts.map +1 -1
- package/dist/studio/control-plane/Data.d.ts.map +1 -1
- package/dist/studio/control-plane/Instance.d.ts.map +1 -1
- package/dist/studio/control-plane/Overview.d.ts.map +1 -1
- package/dist/studio/control-plane/Schema.d.ts.map +1 -1
- package/dist/studio/control-plane/context.d.ts +4 -1
- package/dist/studio/control-plane/context.d.ts.map +1 -1
- package/dist/studio/control-plane/index.js +2 -2
- package/dist/studio/control-plane/primitives.d.ts +8 -0
- package/dist/studio/control-plane/primitives.d.ts.map +1 -1
- package/dist/studio/{control-plane-CLm69XwX.js → control-plane-fxMcJn74.js} +1371 -1319
- package/dist/studio/index.js +146 -105
- package/dist/studio/utils/control-plane-api.d.ts.map +1 -1
- package/dist/studio/utils/control-plane-api.js +4 -3
- package/dist/studio/utils/service-api.d.ts.map +1 -1
- package/dist/studio/utils/service-api.js +5 -4
- package/dist/studio-app/assets/{feltdb_wasm-DACcD-zs.js → feltdb_wasm-BatwwogY.js} +1 -1
- package/dist/studio-app/assets/feltdb_wasm_bg-BX0newOr.wasm +0 -0
- package/dist/studio-app/assets/index-DoVvbIay.js +30 -0
- package/dist/studio-app/index.html +1 -1
- package/dist/telemetry.d.ts +1 -0
- package/dist/telemetry.d.ts.map +1 -1
- package/dist/telemetry.js +4 -3
- package/dist/transaction-identity.d.ts +62 -0
- package/dist/transaction-identity.d.ts.map +1 -0
- package/dist/transaction-identity.js +138 -0
- package/dist/transaction.d.ts +50 -5
- package/dist/transaction.d.ts.map +1 -1
- package/dist/transaction.js +28 -0
- package/dist/wasm/feltdb_wasm_bg.wasm +0 -0
- package/dist/wasm/package.json +1 -1
- package/package.json +1 -1
- package/dist/create/server-source/crates/feltdb-wasm/test.db +0 -0
- package/dist/studio-app/assets/feltdb_wasm_bg-BejThWID.wasm +0 -0
- package/dist/studio-app/assets/index-CoZaxnlm.js +0 -30
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "feltdb-client"
|
|
3
|
+
version.workspace = true
|
|
4
|
+
edition.workspace = true
|
|
5
|
+
# The oldest toolchain CI builds and tests this workspace with (.github/workflows/ci.yml).
|
|
6
|
+
rust-version = "1.96"
|
|
7
|
+
description = "Native async Rust client for a FeltDB server: records, atomic and idempotent transactions, compare-and-set, bounded queries, read-basis fences, events, and the canonical application API"
|
|
8
|
+
license = "MIT"
|
|
9
|
+
repository = "https://github.com/rkendel1/flow_db"
|
|
10
|
+
readme = "README.md"
|
|
11
|
+
keywords = ["database", "client", "transactions", "feltdb", "http"]
|
|
12
|
+
categories = ["database", "api-bindings", "asynchronous"]
|
|
13
|
+
exclude = ["tests/", "target/"]
|
|
14
|
+
|
|
15
|
+
[dependencies]
|
|
16
|
+
# The wire types are the server's own: the request and response documents are defined once, in
|
|
17
|
+
# `feltdb`, and both sides serialize the same structs. Exact, because the modules they come from are
|
|
18
|
+
# not yet covered by `feltdb`'s compatibility policy; the workspace releases both at one version.
|
|
19
|
+
feltdb = { version = "=0.2.1", path = "../feltdb" }
|
|
20
|
+
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
|
|
21
|
+
serde = { version = "1", features = ["derive"] }
|
|
22
|
+
serde_json = "1"
|
|
23
|
+
uuid = { version = "1.6", features = ["v4"] }
|
|
24
|
+
|
|
25
|
+
[dev-dependencies]
|
|
26
|
+
serde_json = "1"
|
|
27
|
+
tokio = { version = "1", features = ["io-util", "macros", "net", "rt-multi-thread"] }
|
|
28
|
+
# A TLS server for the private-CA test; the same rustls reqwest uses.
|
|
29
|
+
tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12"] }
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 FeltDB Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# feltdb-client
|
|
2
|
+
|
|
3
|
+
A native async Rust client for a [FeltDB](https://github.com/rkendel1/flow_db) server: the Rust
|
|
4
|
+
counterpart of `@feltdb/core`'s server runtime. It holds no state of its own; the server is the
|
|
5
|
+
authority for every read and write.
|
|
6
|
+
|
|
7
|
+
```toml
|
|
8
|
+
[dependencies]
|
|
9
|
+
feltdb-client = "0.2"
|
|
10
|
+
serde_json = "1"
|
|
11
|
+
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```rust
|
|
15
|
+
use feltdb_client::{Client, Operation, Precondition, Transaction};
|
|
16
|
+
use serde_json::json;
|
|
17
|
+
|
|
18
|
+
#[tokio::main]
|
|
19
|
+
async fn main() -> feltdb_client::Result<()> {
|
|
20
|
+
let client = Client::builder("http://127.0.0.1:7700")
|
|
21
|
+
.token(std::env::var("FELTDB_TOKEN").unwrap_or_default())
|
|
22
|
+
.build()?;
|
|
23
|
+
|
|
24
|
+
// One atomic commit. The id makes a retry after an uncertain outcome safe.
|
|
25
|
+
let tx = Transaction::new("ship-o-1")
|
|
26
|
+
.operation(Operation::put("orders", "o-1", json!({ "status": "shipped" })).expect_version(1))
|
|
27
|
+
.operation(Operation::create("audit", "a-1", json!({ "order": "o-1" })))
|
|
28
|
+
.precondition(Precondition::absent("locks", "o-1"));
|
|
29
|
+
match client.commit(&tx).await {
|
|
30
|
+
Ok(receipt) => println!("committed at revision {}", receipt.commit_revision),
|
|
31
|
+
Err(error) if error.is_conflict() => println!("lost a race: {:?}", error.precondition_failure()),
|
|
32
|
+
Err(error) if error.is_outcome_unknown() => { /* resubmit `tx` unchanged */ }
|
|
33
|
+
Err(error) => return Err(error),
|
|
34
|
+
}
|
|
35
|
+
Ok(())
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## What it covers
|
|
40
|
+
|
|
41
|
+
| Server surface | Client |
|
|
42
|
+
| --- | --- |
|
|
43
|
+
| Records: `GET/POST /collections/…`, `PATCH`, `DELETE` | `get`, `list`, `create`, `update`, `delete` |
|
|
44
|
+
| Atomic, idempotent transactions with preconditions and fences: `POST /transactions` | `commit` with `Transaction`, `Operation`, `Precondition` |
|
|
45
|
+
| Read bases: `POST /transactions/read-basis` | `read_basis`, then `Transaction::fence` |
|
|
46
|
+
| Compare-and-set: `POST /collections/{c}/{id}/cas` | `compare_and_set` → `CasOutcome` |
|
|
47
|
+
| Bounded query: `POST /query` | `query` with `BoundedQuery` |
|
|
48
|
+
| Committed revision: `GET /revision` | `revision` |
|
|
49
|
+
| Live events: `GET /events` (SSE) | `events` → `EventStream` |
|
|
50
|
+
| Canonical application API under `/v1` | `client.application(id)`: `discover`, `query`, `commit` (per-operation `if_version` via `update_if_version`/`delete_if_version`), `read_basis`, `events`, `replay`, `provenance`, `sign_in`/`sign_up`/`session`/`sign_out` |
|
|
51
|
+
| Committed state version: `GET /v1/state/version` | `application.state_version()` |
|
|
52
|
+
| Control plane: `/api/tenants`, `/api/applications/…` drafts, revisions, promotion; `POST /admin/backups` | `client.control()`: `create_tenant`, `create_application`, `create_draft`, `update_draft`, `validate_draft`, `commit_draft`, `revision`, `promote`, `backup` |
|
|
53
|
+
| Health: `GET /health` | `health` |
|
|
54
|
+
| Capability discovery | `capabilities` |
|
|
55
|
+
| Anything else | `request(Method, path, body)` |
|
|
56
|
+
|
|
57
|
+
Errors carry the server's stable `code`, its `request_id`, and for a refused transaction the
|
|
58
|
+
structured `PreconditionFailure`. The canonical query and transaction documents are `feltdb`'s
|
|
59
|
+
own types (re-exported under `feltdb_client::wire`), so the client and server serialize the same
|
|
60
|
+
structs.
|
|
61
|
+
|
|
62
|
+
A server behind a private CA is trusted with
|
|
63
|
+
`Client::builder(url).add_root_certificate_pem(pem)`, in addition to the bundled public roots; verification is never
|
|
64
|
+
disabled.
|
|
65
|
+
|
|
66
|
+
The event stream does not reconnect by itself; reopen it from `EventStream::last_event_id()`.
|
|
67
|
+
For an embedded store in your own process, use the [`feltdb`](https://crates.io/crates/feltdb)
|
|
68
|
+
crate instead; to run the server, `cargo install feltdb-server`.
|
|
69
|
+
|
|
70
|
+
License: MIT.
|
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
//! The canonical application Service API (`/v1`), scoped to one application and environment.
|
|
2
|
+
|
|
3
|
+
use std::{
|
|
4
|
+
collections::BTreeSet,
|
|
5
|
+
sync::{Arc, Mutex},
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
use feltdb::{
|
|
9
|
+
state_contract::{
|
|
10
|
+
AuthorizationContext, CanonicalQuery, QueryResult, TransactionOperation,
|
|
11
|
+
TransactionOperationKind, TransactionPrecondition, TransactionRequest, TransactionResult,
|
|
12
|
+
},
|
|
13
|
+
ReadBasis, ReadSet,
|
|
14
|
+
};
|
|
15
|
+
use serde::{Deserialize, Serialize};
|
|
16
|
+
use serde_json::{json, Value};
|
|
17
|
+
|
|
18
|
+
use crate::{encode, Client, Error, EventStream, Method, Result};
|
|
19
|
+
|
|
20
|
+
/// The active contract of an application environment, as discovery reports it.
|
|
21
|
+
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
|
22
|
+
pub struct ApplicationInfo {
|
|
23
|
+
pub application_id: String,
|
|
24
|
+
#[serde(default)]
|
|
25
|
+
pub application_name: Option<String>,
|
|
26
|
+
pub environment: String,
|
|
27
|
+
/// The contract revision every canonical request is evaluated against.
|
|
28
|
+
pub revision_id: String,
|
|
29
|
+
#[serde(default)]
|
|
30
|
+
pub version: Option<u64>,
|
|
31
|
+
pub contract_hash: String,
|
|
32
|
+
#[serde(default)]
|
|
33
|
+
pub flow_hash: Option<String>,
|
|
34
|
+
#[serde(default)]
|
|
35
|
+
pub dsl_version: Option<u32>,
|
|
36
|
+
/// The contract snapshot: schema, indexes, policies, workflows, agents and capabilities.
|
|
37
|
+
#[serde(default)]
|
|
38
|
+
pub snapshot: Value,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/// A canonical transaction: operations and preconditions the application contract validates and
|
|
42
|
+
/// authorizes, committed atomically.
|
|
43
|
+
#[derive(Debug, Clone)]
|
|
44
|
+
pub struct AppTransaction {
|
|
45
|
+
transaction_id: String,
|
|
46
|
+
operations: Vec<TransactionOperation>,
|
|
47
|
+
preconditions: Vec<TransactionPrecondition>,
|
|
48
|
+
fences: Vec<ReadBasis>,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
impl AppTransaction {
|
|
52
|
+
/// A transaction with this identity; resubmitting it after an uncertain outcome is safe.
|
|
53
|
+
pub fn new(transaction_id: impl Into<String>) -> Self {
|
|
54
|
+
Self {
|
|
55
|
+
transaction_id: transaction_id.into(),
|
|
56
|
+
operations: Vec::new(),
|
|
57
|
+
preconditions: Vec::new(),
|
|
58
|
+
fences: Vec::new(),
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/// A transaction with a fresh random identity.
|
|
63
|
+
pub fn generated() -> Self {
|
|
64
|
+
Self::new(format!("tx-{}", uuid::Uuid::new_v4()))
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
pub fn id(&self) -> &str {
|
|
68
|
+
&self.transaction_id
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
fn push(
|
|
72
|
+
self,
|
|
73
|
+
kind: TransactionOperationKind,
|
|
74
|
+
collection: String,
|
|
75
|
+
id: String,
|
|
76
|
+
value: Value,
|
|
77
|
+
) -> Self {
|
|
78
|
+
self.operation(TransactionOperation {
|
|
79
|
+
kind,
|
|
80
|
+
collection,
|
|
81
|
+
id,
|
|
82
|
+
value,
|
|
83
|
+
if_version: None,
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/// Create a record that must not exist.
|
|
88
|
+
pub fn insert(
|
|
89
|
+
self,
|
|
90
|
+
collection: impl Into<String>,
|
|
91
|
+
id: impl Into<String>,
|
|
92
|
+
value: Value,
|
|
93
|
+
) -> Self {
|
|
94
|
+
self.push(
|
|
95
|
+
TransactionOperationKind::Insert,
|
|
96
|
+
collection.into(),
|
|
97
|
+
id.into(),
|
|
98
|
+
value,
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/// Write a record.
|
|
103
|
+
pub fn update(
|
|
104
|
+
self,
|
|
105
|
+
collection: impl Into<String>,
|
|
106
|
+
id: impl Into<String>,
|
|
107
|
+
value: Value,
|
|
108
|
+
) -> Self {
|
|
109
|
+
self.push(
|
|
110
|
+
TransactionOperationKind::Update,
|
|
111
|
+
collection.into(),
|
|
112
|
+
id.into(),
|
|
113
|
+
value,
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
pub fn delete(self, collection: impl Into<String>, id: impl Into<String>) -> Self {
|
|
118
|
+
self.push(
|
|
119
|
+
TransactionOperationKind::Delete,
|
|
120
|
+
collection.into(),
|
|
121
|
+
id.into(),
|
|
122
|
+
Value::Null,
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/// Merge `value` into a record only if its `__version` is still `version`. Refused with
|
|
127
|
+
/// `PRECONDITION_FAILED` otherwise; nothing in the transaction is written.
|
|
128
|
+
pub fn update_if_version(
|
|
129
|
+
self,
|
|
130
|
+
collection: impl Into<String>,
|
|
131
|
+
id: impl Into<String>,
|
|
132
|
+
value: Value,
|
|
133
|
+
version: u64,
|
|
134
|
+
) -> Self {
|
|
135
|
+
self.operation(TransactionOperation {
|
|
136
|
+
kind: TransactionOperationKind::Update,
|
|
137
|
+
collection: collection.into(),
|
|
138
|
+
id: id.into(),
|
|
139
|
+
value,
|
|
140
|
+
if_version: Some(version),
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/// Delete a record only if its `__version` is still `version`. With an
|
|
145
|
+
/// [`AppTransaction::insert`] of the same id after it, an atomic, fenced replace.
|
|
146
|
+
pub fn delete_if_version(
|
|
147
|
+
self,
|
|
148
|
+
collection: impl Into<String>,
|
|
149
|
+
id: impl Into<String>,
|
|
150
|
+
version: u64,
|
|
151
|
+
) -> Self {
|
|
152
|
+
self.operation(TransactionOperation {
|
|
153
|
+
kind: TransactionOperationKind::Delete,
|
|
154
|
+
collection: collection.into(),
|
|
155
|
+
id: id.into(),
|
|
156
|
+
value: Value::Null,
|
|
157
|
+
if_version: Some(version),
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/// Any operation of the canonical contract, as the wire document states it.
|
|
162
|
+
pub fn operation(mut self, operation: TransactionOperation) -> Self {
|
|
163
|
+
self.operations.push(operation);
|
|
164
|
+
self
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/// Any precondition of the canonical contract, e.g. a record's `__version`:
|
|
168
|
+
///
|
|
169
|
+
/// ```
|
|
170
|
+
/// # use feltdb_client::{AppTransaction, wire::TransactionPrecondition};
|
|
171
|
+
/// let tx = AppTransaction::new("tx-1").precondition(TransactionPrecondition::Record {
|
|
172
|
+
/// collection: "Order".into(),
|
|
173
|
+
/// id: "o-1".into(),
|
|
174
|
+
/// require_absent: false,
|
|
175
|
+
/// expected_version: Some(3),
|
|
176
|
+
/// expected_epoch: None,
|
|
177
|
+
/// expected_lease_id: None,
|
|
178
|
+
/// });
|
|
179
|
+
/// # let _ = tx;
|
|
180
|
+
/// ```
|
|
181
|
+
pub fn precondition(mut self, precondition: TransactionPrecondition) -> Self {
|
|
182
|
+
self.preconditions.push(precondition);
|
|
183
|
+
self
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/// Commit only if the record's `__version` is still `version`.
|
|
187
|
+
pub fn expect_version(
|
|
188
|
+
self,
|
|
189
|
+
collection: impl Into<String>,
|
|
190
|
+
id: impl Into<String>,
|
|
191
|
+
version: u64,
|
|
192
|
+
) -> Self {
|
|
193
|
+
self.precondition(TransactionPrecondition::Record {
|
|
194
|
+
collection: collection.into(),
|
|
195
|
+
id: id.into(),
|
|
196
|
+
require_absent: false,
|
|
197
|
+
expected_version: Some(version),
|
|
198
|
+
expected_epoch: None,
|
|
199
|
+
expected_lease_id: None,
|
|
200
|
+
})
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/// Commit only if the record does not exist.
|
|
204
|
+
pub fn expect_absent(self, collection: impl Into<String>, id: impl Into<String>) -> Self {
|
|
205
|
+
self.precondition(TransactionPrecondition::Record {
|
|
206
|
+
collection: collection.into(),
|
|
207
|
+
id: id.into(),
|
|
208
|
+
require_absent: true,
|
|
209
|
+
expected_version: None,
|
|
210
|
+
expected_epoch: None,
|
|
211
|
+
expected_lease_id: None,
|
|
212
|
+
})
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/// Commit only if the state `basis` covers is unchanged since [`Application::read_basis`].
|
|
216
|
+
pub fn fence(mut self, basis: ReadBasis) -> Self {
|
|
217
|
+
self.fences.push(basis);
|
|
218
|
+
self
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/// What a canonical transaction committed.
|
|
223
|
+
#[derive(Debug, Clone, Deserialize)]
|
|
224
|
+
pub struct AppCommit {
|
|
225
|
+
#[serde(flatten)]
|
|
226
|
+
pub result: TransactionResult,
|
|
227
|
+
pub revision_id: String,
|
|
228
|
+
#[serde(default)]
|
|
229
|
+
pub operation_ids: Vec<String>,
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/// The application's committed state version: [`Application::state_version`].
|
|
233
|
+
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
|
234
|
+
pub struct StateVersion {
|
|
235
|
+
pub application_id: String,
|
|
236
|
+
pub revision_id: String,
|
|
237
|
+
pub schema_version: u64,
|
|
238
|
+
/// Advances inside the commit critical section, so it totally orders commits.
|
|
239
|
+
pub state_version: u64,
|
|
240
|
+
/// The state namespace the version counts. Versions of two namespaces are not comparable.
|
|
241
|
+
#[serde(default)]
|
|
242
|
+
pub state_namespace: Option<String>,
|
|
243
|
+
/// Per-operation versions, for a causal read.
|
|
244
|
+
#[serde(default)]
|
|
245
|
+
pub causal_cursor: Value,
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/// One page of committed history.
|
|
249
|
+
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
|
250
|
+
pub struct ReplayPage {
|
|
251
|
+
pub contract_version: String,
|
|
252
|
+
pub cursor_format: String,
|
|
253
|
+
pub application_id: String,
|
|
254
|
+
pub environment: String,
|
|
255
|
+
/// Committed events in order; each carries its own resumable cursor in `metadata.cursor`.
|
|
256
|
+
pub events: Vec<Value>,
|
|
257
|
+
/// Where the next page starts. Bound to this authority, tenant and application.
|
|
258
|
+
pub next_cursor: String,
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/// One application environment on a FeltDB server. Cheap to clone.
|
|
262
|
+
#[derive(Debug, Clone)]
|
|
263
|
+
pub struct Application {
|
|
264
|
+
client: Client,
|
|
265
|
+
application_id: String,
|
|
266
|
+
environment: String,
|
|
267
|
+
/// The active revision, cached after a successful discovery. A failure is never cached.
|
|
268
|
+
revision: Arc<Mutex<Option<String>>>,
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
#[derive(Serialize)]
|
|
272
|
+
struct Envelope<'a, T: Serialize> {
|
|
273
|
+
application_id: &'a str,
|
|
274
|
+
revision_id: &'a str,
|
|
275
|
+
environment: &'a str,
|
|
276
|
+
#[serde(flatten)]
|
|
277
|
+
payload: T,
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
impl Application {
|
|
281
|
+
pub(crate) fn new(client: Client, application_id: String) -> Self {
|
|
282
|
+
Self {
|
|
283
|
+
client,
|
|
284
|
+
application_id,
|
|
285
|
+
environment: "production".into(),
|
|
286
|
+
revision: Arc::default(),
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/// The same application in another environment.
|
|
291
|
+
pub fn environment(mut self, environment: impl Into<String>) -> Self {
|
|
292
|
+
self.environment = environment.into();
|
|
293
|
+
self.revision = Arc::default();
|
|
294
|
+
self
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
pub fn id(&self) -> &str {
|
|
298
|
+
&self.application_id
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
pub fn environment_name(&self) -> &str {
|
|
302
|
+
&self.environment
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
fn scoped(&self, path: &str) -> String {
|
|
306
|
+
let separator = if path.contains('?') { '&' } else { '?' };
|
|
307
|
+
format!(
|
|
308
|
+
"/v1{path}{separator}application_id={}&environment={}",
|
|
309
|
+
encode(&self.application_id),
|
|
310
|
+
encode(&self.environment)
|
|
311
|
+
)
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/// Discover the environment's active contract, and remember its revision for later requests.
|
|
315
|
+
pub async fn discover(&self) -> Result<ApplicationInfo> {
|
|
316
|
+
let info: ApplicationInfo = self
|
|
317
|
+
.client
|
|
318
|
+
.send::<_, ()>(Method::GET, &self.scoped("/application"), None)
|
|
319
|
+
.await?;
|
|
320
|
+
*self
|
|
321
|
+
.revision
|
|
322
|
+
.lock()
|
|
323
|
+
.unwrap_or_else(|poisoned| poisoned.into_inner()) = Some(info.revision_id.clone());
|
|
324
|
+
Ok(info)
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/// The revision canonical requests are evaluated against: cached after the first discovery.
|
|
328
|
+
/// Call [`Application::discover`] again after a promotion to move to the new revision.
|
|
329
|
+
pub async fn revision_id(&self) -> Result<String> {
|
|
330
|
+
let cached = self
|
|
331
|
+
.revision
|
|
332
|
+
.lock()
|
|
333
|
+
.unwrap_or_else(|poisoned| poisoned.into_inner())
|
|
334
|
+
.clone();
|
|
335
|
+
match cached {
|
|
336
|
+
Some(revision) => Ok(revision),
|
|
337
|
+
None => Ok(self.discover().await?.revision_id),
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
async fn canonical<T: serde::de::DeserializeOwned>(
|
|
342
|
+
&self,
|
|
343
|
+
path: &str,
|
|
344
|
+
payload: impl Serialize,
|
|
345
|
+
) -> Result<T> {
|
|
346
|
+
let revision_id = self.revision_id().await?;
|
|
347
|
+
let body = Envelope {
|
|
348
|
+
application_id: &self.application_id,
|
|
349
|
+
revision_id: &revision_id,
|
|
350
|
+
environment: &self.environment,
|
|
351
|
+
payload,
|
|
352
|
+
};
|
|
353
|
+
self.client
|
|
354
|
+
.send(Method::POST, &self.scoped(path), Some(&body))
|
|
355
|
+
.await
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/// The committed state version of the environment's state, at its active revision.
|
|
359
|
+
pub async fn state_version(&self) -> Result<StateVersion> {
|
|
360
|
+
let revision_id = self.revision_id().await?;
|
|
361
|
+
let path = self.scoped(&format!(
|
|
362
|
+
"/state/version?revision_id={}",
|
|
363
|
+
encode(&revision_id)
|
|
364
|
+
));
|
|
365
|
+
self.client.send::<_, ()>(Method::GET, &path, None).await
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/// Run a canonical query, authorized by the contract's read policy for the collection.
|
|
369
|
+
pub async fn query(&self, query: &CanonicalQuery) -> Result<QueryResult> {
|
|
370
|
+
self.canonical("/query", json!({ "query": query })).await
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/// Commit a canonical transaction.
|
|
374
|
+
pub async fn commit(&self, transaction: &AppTransaction) -> Result<AppCommit> {
|
|
375
|
+
if transaction.operations.is_empty() {
|
|
376
|
+
return Err(Error::InvalidRequest(
|
|
377
|
+
"a transaction requires at least one operation".into(),
|
|
378
|
+
));
|
|
379
|
+
}
|
|
380
|
+
let revision_id = self.revision_id().await?;
|
|
381
|
+
// Tenant, revision, schema version and authorization are the authority's to assign: it
|
|
382
|
+
// overwrites them from the authenticated principal and the active contract. They are sent
|
|
383
|
+
// because the document requires them, never as a claim.
|
|
384
|
+
let tenant_id = match &self.client.inner.scope {
|
|
385
|
+
Some(crate::AuthorityScope::Tenant { tenant_id, .. }) => tenant_id.clone(),
|
|
386
|
+
_ => String::new(),
|
|
387
|
+
};
|
|
388
|
+
let request = TransactionRequest {
|
|
389
|
+
transaction_id: Some(transaction.transaction_id.clone()),
|
|
390
|
+
tenant_id: tenant_id.clone(),
|
|
391
|
+
application_id: self.application_id.clone(),
|
|
392
|
+
revision_id: revision_id.clone(),
|
|
393
|
+
schema_version: 0,
|
|
394
|
+
state_namespace: None,
|
|
395
|
+
causal_parent: None,
|
|
396
|
+
authorization: AuthorizationContext {
|
|
397
|
+
subject: String::new(),
|
|
398
|
+
tenant_id,
|
|
399
|
+
application_id: self.application_id.clone(),
|
|
400
|
+
revision_id,
|
|
401
|
+
capabilities: BTreeSet::new(),
|
|
402
|
+
readable_collections: BTreeSet::new(),
|
|
403
|
+
writable_collections: BTreeSet::new(),
|
|
404
|
+
field_projections: Default::default(),
|
|
405
|
+
},
|
|
406
|
+
operations: transaction.operations.clone(),
|
|
407
|
+
preconditions: transaction.preconditions.clone(),
|
|
408
|
+
fences: transaction.fences.clone(),
|
|
409
|
+
};
|
|
410
|
+
self.canonical("/transactions", json!({ "transaction": request }))
|
|
411
|
+
.await
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/// Capture a read basis over the application's state, under the caller's read authorization.
|
|
415
|
+
pub async fn read_basis(&self, reads: &ReadSet) -> Result<ReadBasis> {
|
|
416
|
+
self.canonical("/transactions/read-basis", json!({ "reads": reads }))
|
|
417
|
+
.await
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/// Committed changes to this application as they happen, from `cursor` or from now.
|
|
421
|
+
pub async fn events(&self, cursor: Option<&str>) -> Result<EventStream> {
|
|
422
|
+
self.client
|
|
423
|
+
.open_events(&self.scoped("/events"), cursor)
|
|
424
|
+
.await
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/// A bounded page of committed history from `cursor` (the start when `None`), at most `limit`
|
|
428
|
+
/// events (the server caps it at 256). Replay, not the live stream, is the authority on history.
|
|
429
|
+
pub async fn replay(&self, cursor: Option<&str>, limit: Option<usize>) -> Result<ReplayPage> {
|
|
430
|
+
let mut path = self.scoped("/public/events/replay");
|
|
431
|
+
if let Some(cursor) = cursor {
|
|
432
|
+
path.push_str(&format!("&cursor={}", encode(cursor)));
|
|
433
|
+
}
|
|
434
|
+
if let Some(limit) = limit {
|
|
435
|
+
path.push_str(&format!("&limit={limit}"));
|
|
436
|
+
}
|
|
437
|
+
self.client.send::<_, ()>(Method::GET, &path, None).await
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/// The complete mutation chain of one record.
|
|
441
|
+
pub async fn provenance(&self, collection: &str, id: &str) -> Result<Value> {
|
|
442
|
+
let path = self.scoped(&format!(
|
|
443
|
+
"/control-plane/provenance/{}/{}",
|
|
444
|
+
encode(collection),
|
|
445
|
+
encode(id)
|
|
446
|
+
));
|
|
447
|
+
self.client.send::<_, ()>(Method::GET, &path, None).await
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/// Sign an end user in to this application; the session's access token becomes this client's
|
|
451
|
+
/// bearer token.
|
|
452
|
+
pub async fn sign_in(&self, email: &str, password: &str) -> Result<Value> {
|
|
453
|
+
let body =
|
|
454
|
+
json!({ "application_id": self.application_id, "email": email, "password": password });
|
|
455
|
+
self.authenticate("/v1/auth/signin", &body).await
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/// Register an end user of this application and sign them in.
|
|
459
|
+
pub async fn sign_up(
|
|
460
|
+
&self,
|
|
461
|
+
email: &str,
|
|
462
|
+
password: &str,
|
|
463
|
+
display_name: Option<&str>,
|
|
464
|
+
) -> Result<Value> {
|
|
465
|
+
let body = json!({
|
|
466
|
+
"application_id": self.application_id,
|
|
467
|
+
"email": email,
|
|
468
|
+
"password": password,
|
|
469
|
+
"display_name": display_name,
|
|
470
|
+
});
|
|
471
|
+
self.authenticate("/v1/auth/signup", &body).await
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
async fn authenticate(&self, path: &str, body: &Value) -> Result<Value> {
|
|
475
|
+
let session: Value = self.client.send(Method::POST, path, Some(body)).await?;
|
|
476
|
+
if let Some(token) = session.get("access_token").and_then(Value::as_str) {
|
|
477
|
+
self.client.set_token(token);
|
|
478
|
+
}
|
|
479
|
+
Ok(session)
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/// The current session's principal.
|
|
483
|
+
pub async fn session(&self) -> Result<Value> {
|
|
484
|
+
self.client
|
|
485
|
+
.send::<_, ()>(Method::GET, "/v1/auth/session", None)
|
|
486
|
+
.await
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/// End the current session and clear this client's token.
|
|
490
|
+
pub async fn sign_out(&self) -> Result<()> {
|
|
491
|
+
self.client
|
|
492
|
+
.exchange::<()>(Method::POST, "/v1/auth/signout", None)
|
|
493
|
+
.await?;
|
|
494
|
+
self.client.set_token("");
|
|
495
|
+
Ok(())
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
impl Client {
|
|
500
|
+
/// The server's versioned capability declaration.
|
|
501
|
+
pub async fn capabilities(&self) -> Result<Value> {
|
|
502
|
+
self.send::<_, ()>(Method::GET, "/v1/control-plane/capabilities", None)
|
|
503
|
+
.await
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
#[cfg(test)]
|
|
508
|
+
mod tests {
|
|
509
|
+
use super::*;
|
|
510
|
+
|
|
511
|
+
#[test]
|
|
512
|
+
fn scoped_paths_carry_the_application_and_environment() {
|
|
513
|
+
let app = Client::builder("http://localhost:1")
|
|
514
|
+
.build()
|
|
515
|
+
.unwrap()
|
|
516
|
+
.application("app 1")
|
|
517
|
+
.environment("staging");
|
|
518
|
+
assert_eq!(
|
|
519
|
+
app.scoped("/application"),
|
|
520
|
+
"/v1/application?application_id=app%201&environment=staging"
|
|
521
|
+
);
|
|
522
|
+
assert_eq!(
|
|
523
|
+
app.scoped("/x?y=1"),
|
|
524
|
+
"/v1/x?y=1&application_id=app%201&environment=staging"
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
}
|