@romedb/client 0.0.0-stage → 0.2.15

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/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright 2024 the Turso authors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,147 @@
1
- # Temporary Holding Version
1
+ # RomeDB client
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ `@romedb/client` connects to a RomeDB database by URL, behind `romedb serve`, `romedb
4
+ serve --directory` or RomeDB Cloud. It runs in Node.js, Cloudflare Workers, browsers
5
+ and any other runtime with `fetch`.
6
+
7
+ The package is about 30 KB to download, has no dependencies and has no native addon. To
8
+ open a local database file, use [`@romedb/node`](https://www.npmjs.com/package/@romedb/node),
9
+ which depends on this package.
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ npm install @romedb/client
15
+ ```
16
+
17
+ ## A first request
18
+
19
+ ```js
20
+ import { connect, Float64 } from '@romedb/client';
21
+
22
+ const rql = await connect({ url: process.env.ROMEDB_URL, token: process.env.ROMEDB_TOKEN });
23
+ const snapshot = await rql.read([{ sql: 'SELECT * FROM posts LIMIT 20' }]);
24
+ await rql.writeAtomic([
25
+ { sql: 'UPDATE posts:p1 MERGE {score: $score}', params: { $score: new Float64(1) } },
26
+ ]);
27
+ await rql.close();
28
+ ```
29
+
30
+ `require('@romedb/client')` works the same way in Node.js.
31
+
32
+ ## In a Cloudflare Worker
33
+
34
+ Workers need no `nodejs_compat`.
35
+
36
+ ```js
37
+ import { connect } from '@romedb/client';
38
+
39
+ export default {
40
+ async fetch(request, env) {
41
+ const rql = await connect({ url: env.ROMEDB_URL, token: env.ROMEDB_TOKEN });
42
+ const { results } = await rql.read([{ sql: "SELECT title FROM posts WHERE array::contains(tags, 'news')" }]);
43
+ return Response.json({ posts: results[0].rows.length });
44
+ },
45
+ };
46
+ ```
47
+
48
+ ## Value types only
49
+
50
+ `@romedb/client/values` exports `Float64`, `Record`, `Vector`, `encodeValue` and
51
+ `decodeValue`. Use it for code that never connects, such as a browser bundle.
52
+
53
+ ## Using it with `@romedb/node`
54
+
55
+ `@romedb/node` depends on this package at the same version. Its `client`, `values` and
56
+ `embedded-client` entries use the classes from here, so a `Record` or `Float64` made by
57
+ one package works in the other.
58
+
59
+ That needs one installed copy of `@romedb/client`, so keep both packages at the same
60
+ version. If the versions differ, your package manager installs a second copy, and a
61
+ client refuses values made by the other copy with `unsupported parameter value`.
62
+
63
+ The `Database` and `AsyncDatabase` classes of the main `@romedb/node` entry keep their
64
+ own `Record` and `Vector`. Use the types of that entry with them.
65
+
66
+ ## Saved requests
67
+
68
+ `saveRequest` and `loadRequest` keep a prepared write in a private file, so a write with
69
+ an unknown outcome can be recovered. They need Node.js file access and reject in other
70
+ runtimes. There, store the `PreparedWrite` object yourself, for example in a Durable
71
+ Object.
72
+
73
+ ## URLs and tokens
74
+
75
+ A local service URL is `http://127.0.0.1:4321`. A Cloud URL names the organization
76
+ and database by stable UUIDs, and a dashboard URL that ends in `/query` also works.
77
+ Use HTTPS for every host that is not local.
78
+
79
+ `romedb://` is another name for `https://`. The client replaces the scheme and applies
80
+ the same rules, so `romedb://cloud.romedb.com/v1/organizations/{org}/databases/{db}`
81
+ and the `https://` form are the same database. Requests use `redirect: 'manual'`, and
82
+ any redirect fails with `RDB_PROTOCOL`, so the token never goes to another URL. Pass
83
+ the bearer token separately. The client refuses a token in the URL.
84
+
85
+ ## Cloud URLs
86
+
87
+ For a Cloud URL, `connect()` returns without a network round trip. The database ID
88
+ comes from the URL, and the Cloud capabilities are documented. Reads start from
89
+ sequence 0 as their lower bound and update the sequence from each reply.
90
+
91
+ A write needs a current sequence. If none is known yet, `writeAtomic` first fetches
92
+ the database details, and `prepareWrite` throws `RDB_SEQUENCE_UNKNOWN` (outcome
93
+ `not-sent`) until a read, `info()` or an explicit `afterSequence` supplies one. To
94
+ fetch the details at connect, pass `deferInfo: false`. Other URLs always fetch the
95
+ details at connect and refuse `deferInfo: true`.
96
+
97
+ `connect()` on a Cloud URL also sends a warm request in the background and does not
98
+ wait for it. Cloud then starts the container of the database and opens it, so a query
99
+ a second or more later does not wait for that start. Errors of the background request
100
+ are ignored. To skip it, pass `warm: false`.
101
+
102
+ `await rql.warm()` sends the same request and returns `{ state }`. The state is
103
+ `ready` when the database is already open, `starting` when Cloud began to open it, or
104
+ `skipped` when Cloud declined because another database is busy. Call it when your
105
+ application knows that a query is coming, for example when a page or a function
106
+ starts. Local URLs refuse `warm()`.
107
+
108
+ ## Local directories
109
+
110
+ An organization URL on a loopback host, such as a database of `romedb serve
111
+ --directory` (`http://127.0.0.1:4321/v1/organizations/{org}/databases/{db}`), always
112
+ fetches the details at connect. A reply with `serviceVersion: 1` is a local service.
113
+ The client then negotiates its capabilities and uses the 64 KiB service request
114
+ format, with no warm request. Any other reply is Cloud running locally, which keeps
115
+ the Cloud request format. So an application can use the same organization URLs for a
116
+ local directory and for Cloud.
117
+
118
+ ## Values and results
119
+
120
+ Results use positional rows and keep duplicate columns. Integer results are `bigint`.
121
+ Number results are `Float64` instances with a `.value` number. They keep their exact
122
+ float type when you bind them again, integral floats and negative zero included.
123
+
124
+ Encoded vectors and the dense float32, float64 and sparse-entry factories
125
+ work without native code. `null` is a present field. `undefined` and array holes are
126
+ refused, so no data disappears without an error.
127
+
128
+ ## Errors and timeouts
129
+
130
+ Requests and responses have finite byte and row limits. Each call times out after 20
131
+ seconds by default and accepts `signal` and `timeoutMs`. An HTTP error reply with a
132
+ `Retry-After` header sets `ClientError.retryAfterMs`. A transport or response failure
133
+ without a database reply keeps the underlying error as `ClientError.cause`, for
134
+ example a `fetch failed` TypeError with `ECONNREFUSED`.
135
+
136
+ `rql.sequenceKnown` is `true` once a reply has given the current sequence. `rql.cloud`
137
+ tells a Cloud database from a local service. A write cancelled after it was sent stays
138
+ unknown until a receipt confirms it. Cloud capabilities are marked as documented,
139
+ because its API does not negotiate the local capability object. See the
140
+ [service contract](https://github.com/romedb/romedb/blob/main/romedb/docs/service-protocol.md) for limits, receipt rules and
141
+ compatibility gaps.
142
+
143
+ ## More
144
+
145
+ The [Node.js guide](https://github.com/romedb/romedb/blob/main/romedb/bindings/node/README.md#url-client)
146
+ has the full client reference, including saved requests, query versions and file
147
+ exports. The client methods are the same in both packages.
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+ module.exports = {};