@f0rbit/corpus 0.3.4 → 0.3.5
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.
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @module Backends
|
|
3
3
|
* @description Cloudflare Workers storage backend using D1 and R2.
|
|
4
4
|
*/
|
|
5
|
-
import type { Backend, EventHandler } from
|
|
5
|
+
import type { Backend, EventHandler } from "../types.js";
|
|
6
6
|
type D1Database = {
|
|
7
7
|
prepare: (sql: string) => unknown;
|
|
8
8
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cloudflare.d.ts","sourceRoot":"","sources":["../../backend/cloudflare.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"cloudflare.d.ts","sourceRoot":"","sources":["../../backend/cloudflare.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAOH,OAAO,KAAK,EAAE,OAAO,EAA2B,YAAY,EAAwC,MAAM,aAAa,CAAC;AAIxH,KAAK,UAAU,GAAG;IAAE,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAA;CAAE,CAAC;AACxD,KAAK,QAAQ,GAAG;IACf,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC;QAAE,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,CAAC;QAAC,WAAW,EAAE,MAAM,OAAO,CAAC,WAAW,CAAC,CAAA;KAAE,GAAG,IAAI,CAAC,CAAC;IACpH,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,GAAG,UAAU,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACnF,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACvC,IAAI,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC,CAAC;CACvD,CAAC;AAEF,MAAM,MAAM,uBAAuB,GAAG;IACrC,EAAE,EAAE,UAAU,CAAC;IACf,EAAE,EAAE,QAAQ,CAAC;IACb,QAAQ,CAAC,EAAE,YAAY,CAAC;CACxB,CAAC;AAgIF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,yBAAyB,CAAC,MAAM,EAAE,uBAAuB,GAAG,OAAO,CA2OlF"}
|
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
* @module Backends
|
|
3
3
|
* @description Cloudflare Workers storage backend using D1 and R2.
|
|
4
4
|
*/
|
|
5
|
-
import {
|
|
5
|
+
import { and, desc, eq, gt, inArray, like, lt, sql } from "drizzle-orm";
|
|
6
6
|
import { drizzle } from "drizzle-orm/d1";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
7
|
+
import { corpus_observations, create_observations_client } from "../observations/index.js";
|
|
8
|
+
import { first, to_fallback, to_nullable } from "../result.js";
|
|
9
|
+
import { corpus_snapshots } from "../schema.js";
|
|
10
|
+
import { err, ok } from "../types.js";
|
|
11
|
+
import { create_emitter, parse_snapshot_meta } from "../utils.js";
|
|
12
12
|
function create_cloudflare_storage(db) {
|
|
13
13
|
return {
|
|
14
14
|
async put_row(row) {
|
|
@@ -20,24 +20,20 @@ function create_cloudflare_storage(db) {
|
|
|
20
20
|
return err({
|
|
21
21
|
kind: "storage_error",
|
|
22
22
|
cause: cause,
|
|
23
|
-
operation: "observations.put"
|
|
23
|
+
operation: "observations.put",
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
},
|
|
27
27
|
async get_row(id) {
|
|
28
28
|
try {
|
|
29
|
-
const rows = await db
|
|
30
|
-
.select()
|
|
31
|
-
.from(corpus_observations)
|
|
32
|
-
.where(eq(corpus_observations.id, id))
|
|
33
|
-
.limit(1);
|
|
29
|
+
const rows = await db.select().from(corpus_observations).where(eq(corpus_observations.id, id)).limit(1);
|
|
34
30
|
return ok(to_nullable(first(rows)));
|
|
35
31
|
}
|
|
36
32
|
catch (cause) {
|
|
37
33
|
return err({
|
|
38
34
|
kind: "storage_error",
|
|
39
35
|
cause: cause,
|
|
40
|
-
operation: "observations.get"
|
|
36
|
+
operation: "observations.get",
|
|
41
37
|
});
|
|
42
38
|
}
|
|
43
39
|
},
|
|
@@ -87,11 +83,7 @@ function create_cloudflare_storage(db) {
|
|
|
87
83
|
},
|
|
88
84
|
async delete_row(id) {
|
|
89
85
|
try {
|
|
90
|
-
const existing = await db
|
|
91
|
-
.select()
|
|
92
|
-
.from(corpus_observations)
|
|
93
|
-
.where(eq(corpus_observations.id, id))
|
|
94
|
-
.limit(1);
|
|
86
|
+
const existing = await db.select().from(corpus_observations).where(eq(corpus_observations.id, id)).limit(1);
|
|
95
87
|
if (existing.length === 0) {
|
|
96
88
|
return ok(false);
|
|
97
89
|
}
|
|
@@ -102,16 +94,13 @@ function create_cloudflare_storage(db) {
|
|
|
102
94
|
return err({
|
|
103
95
|
kind: "storage_error",
|
|
104
96
|
cause: cause,
|
|
105
|
-
operation: "observations.delete"
|
|
97
|
+
operation: "observations.delete",
|
|
106
98
|
});
|
|
107
99
|
}
|
|
108
100
|
},
|
|
109
101
|
async delete_by_source(store_id, version, path) {
|
|
110
102
|
try {
|
|
111
|
-
const conditions = [
|
|
112
|
-
eq(corpus_observations.source_store_id, store_id),
|
|
113
|
-
eq(corpus_observations.source_version, version)
|
|
114
|
-
];
|
|
103
|
+
const conditions = [eq(corpus_observations.source_store_id, store_id), eq(corpus_observations.source_version, version)];
|
|
115
104
|
if (path !== undefined) {
|
|
116
105
|
conditions.push(eq(corpus_observations.source_path, path));
|
|
117
106
|
}
|
|
@@ -129,10 +118,10 @@ function create_cloudflare_storage(db) {
|
|
|
129
118
|
return err({
|
|
130
119
|
kind: "storage_error",
|
|
131
120
|
cause: cause,
|
|
132
|
-
operation: "observations.delete_by_source"
|
|
121
|
+
operation: "observations.delete_by_source",
|
|
133
122
|
});
|
|
134
123
|
}
|
|
135
|
-
}
|
|
124
|
+
},
|
|
136
125
|
};
|
|
137
126
|
}
|
|
138
127
|
/**
|
|
@@ -252,7 +241,7 @@ export function create_cloudflare_backend(config) {
|
|
|
252
241
|
}
|
|
253
242
|
},
|
|
254
243
|
async *list(store_id, opts) {
|
|
255
|
-
const conditions = [
|
|
244
|
+
const conditions = [eq(corpus_snapshots.store_id, store_id)];
|
|
256
245
|
if (opts?.before) {
|
|
257
246
|
conditions.push(lt(corpus_snapshots.created_at, opts.before.toISOString()));
|
|
258
247
|
}
|
|
@@ -267,7 +256,15 @@ export function create_cloudflare_backend(config) {
|
|
|
267
256
|
if (opts?.limit) {
|
|
268
257
|
query = query.limit(opts.limit);
|
|
269
258
|
}
|
|
270
|
-
|
|
259
|
+
let rows;
|
|
260
|
+
try {
|
|
261
|
+
rows = await query;
|
|
262
|
+
}
|
|
263
|
+
catch (cause) {
|
|
264
|
+
const error = { kind: "storage_error", cause: cause, operation: "metadata.list" };
|
|
265
|
+
emit({ type: "error", error });
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
271
268
|
let count = 0;
|
|
272
269
|
for (const row of rows) {
|
|
273
270
|
const meta = snapshot_row_to_meta(row);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@f0rbit/corpus",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.5",
|
|
4
4
|
"description": "A functional snapshotting library for TypeScript with versioned data storage, lineage tracking, and multiple backend support",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"main": "dist/index.js",
|