Skip to content

Commit f03f8ce

Browse files
committed
feat: add support for import/exporting NDJSON
1 parent 87d7d09 commit f03f8ce

7 files changed

+845
-12
lines changed

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,38 @@ Deserialize a JSON structure to a value which can be stored in a Deno KV store.
113113
Serialize a value which has been stored in a Deno KV store into a structure
114114
which can be safely converted to a JSON string.
115115

116+
## NDJSON
117+
118+
New line delimitated JSON ([NDJSON](https://github.com/ndjson/ndjson-spec)) is a
119+
standard for supporting JSON string encoding of data where each record of data
120+
is delimitated by a new line. This particular format is the most straight-
121+
forward way of supporting JSON encoding and streaming.
122+
123+
The toolbox includes the capabilities to export entries from a KV store to
124+
NDJSON, transform a byte stream of NDJSON into individual JSON KV entry
125+
representations, and be able to import KV entries from NDJSON encoded data.
126+
127+
### `exportEntries()`
128+
129+
Like `Deno.Kv.prototype.list()`, but entries are returned as a stream of bytes
130+
or strings encoded as NDJSON.
131+
132+
### `exportToResponse()`
133+
134+
Like `Deno.Kv.prototype.list()`, but a `Response` is returned with the selected
135+
entries encoded as NDJSON as the body of the response, suitable for sending to a
136+
client as a response to a query.
137+
138+
### `LineTransformStream()`
139+
140+
A transform stream which takes a byte stream, like from a `Request` body, of
141+
NDJSON encoded entry data and transforms it into individual chunks of JSON
142+
strings which can be used with `JSON.parse()`.
143+
144+
### `importEntries()`
145+
146+
Takes NDJSON encoded data and imports it into a Deno KV store.
147+
116148
## Keys
117149

118150
APIs for dealing with Deno KV keys.

_test_util.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { assert } from "https://deno.land/[email protected]/assert/assert.ts";
2-
export { delay } from "https://deno.land/[email protected]/async/delay.ts";
3-
export { assert } from "https://deno.land/[email protected]/assert/assert.ts";
4-
export { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts";
5-
export {
6-
assertNotEquals,
7-
} from "https://deno.land/[email protected]/assert/assert_not_equals.ts";
8-
export { assertStrictEquals } from "https://deno.land/[email protected]/assert/assert_strict_equals.ts";
9-
export { timingSafeEqual } from "https://deno.land/[email protected]/crypto/timing_safe_equal.ts";
1+
import { assert } from "jsr:@std/[email protected]/assert";
2+
export { concat } from "jsr:@std/[email protected]/concat";
3+
export { delay } from "jsr:@std/[email protected]/delay";
4+
export { assert } from "jsr:@std/[email protected]/assert";
5+
export { assertEquals } from "jsr:@std/[email protected]/assert_equals";
6+
export { assertNotEquals } from "jsr:@std/[email protected]/assert_not_equals";
7+
export { assertStrictEquals } from "jsr:@std/[email protected]/assert_strict_equals";
8+
export { timingSafeEqual } from "jsr:@std/[email protected]/timing_safe_equal";
109

1110
let kv: Deno.Kv | undefined;
1211
let path: string | undefined;

deno.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"./batched_atomic": "./batched_atomic.ts",
66
"./blob": "./blob.ts",
77
"./json": "./json.ts",
8-
"./keys": "./keys.ts"
8+
"./keys": "./keys.ts",
9+
"./ndjson": "./ndjson.ts"
910
},
1011
"tasks": {
1112
"coverage": "deno coverage --lcov --output=cov.lcov ./cov",

json.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import {
3838
decodeBase64Url,
3939
encodeBase64Url,
40-
} from "https://deno.land/[email protected].2/encoding/base64url.ts";
40+
} from "jsr:@std/encoding@0.218/base64url";
4141

4242
// Deno KV Key types
4343

keys.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
* @module
145145
*/
146146

147-
import { timingSafeEqual } from "https://deno.land/[email protected].2/crypto/timing_safe_equal.ts";
147+
import { timingSafeEqual } from "jsr:@std/crypto@0.218/timing_safe_equal";
148148

149149
function addIfUnique(set: Set<Deno.KvKeyPart>, item: Uint8Array) {
150150
for (const i of set) {

0 commit comments

Comments
 (0)