@verdict-ai/db 0.0.0-stage → 0.1.0

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.
Files changed (47) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +21 -2
  3. package/dist/auth.d.ts +34 -0
  4. package/dist/auth.js +80 -0
  5. package/dist/auth.js.map +1 -0
  6. package/dist/d1.d.ts +1034 -0
  7. package/dist/d1.js +21 -0
  8. package/dist/d1.js.map +1 -0
  9. package/dist/dialects.d.ts +14 -0
  10. package/dist/dialects.js +66 -0
  11. package/dist/dialects.js.map +1 -0
  12. package/dist/documents.d.ts +87 -0
  13. package/dist/documents.js +45 -0
  14. package/dist/documents.js.map +1 -0
  15. package/dist/errors.d.ts +7 -0
  16. package/dist/errors.js +21 -0
  17. package/dist/errors.js.map +1 -0
  18. package/dist/index.d.ts +9 -0
  19. package/dist/index.js +12 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/postgres.d.ts +1925 -0
  22. package/dist/postgres.js +63 -0
  23. package/dist/postgres.js.map +1 -0
  24. package/dist/queries.d.ts +31 -0
  25. package/dist/queries.js +68 -0
  26. package/dist/queries.js.map +1 -0
  27. package/dist/schema/pg.d.ts +1892 -0
  28. package/dist/schema/pg.js +90 -0
  29. package/dist/schema/pg.js.map +1 -0
  30. package/dist/schema/sqlite.d.ts +2050 -0
  31. package/dist/schema/sqlite.js +90 -0
  32. package/dist/schema/sqlite.js.map +1 -0
  33. package/dist/sqlite.d.ts +25 -0
  34. package/dist/sqlite.js +64 -0
  35. package/dist/sqlite.js.map +1 -0
  36. package/dist/store.d.ts +47 -0
  37. package/dist/store.js +149 -0
  38. package/dist/store.js.map +1 -0
  39. package/drizzle/0000_init.sql +66 -0
  40. package/drizzle/0001_idempotency.sql +12 -0
  41. package/drizzle/meta/0000_snapshot.json +452 -0
  42. package/drizzle/meta/0001_snapshot.json +527 -0
  43. package/drizzle/meta/_journal.json +20 -0
  44. package/drizzle/pg/0000_init.sql +78 -0
  45. package/drizzle/pg/meta/0000_snapshot.json +605 -0
  46. package/drizzle/pg/meta/_journal.json +13 -0
  47. package/package.json +81 -3
package/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Verdict contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,22 @@
1
- # Temporary Holding Version
1
+ # @verdict-ai/db
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
+ Tenant-scoped Drizzle persistence for SQLite, Postgres and Cloudflare D1.
4
+
5
+ ```bash
6
+ npm install @verdict-ai/db @verdict-ai/core
7
+ ```
8
+
9
+ ```ts
10
+ import { fromSqliteFile } from "@verdict-ai/db/sqlite";
11
+ import { Engine } from "@verdict-ai/core";
12
+ const handle = await fromSqliteFile("./verdict.sqlite");
13
+ const engine = new Engine({ store: handle.store });
14
+ const rubric = await engine.createRubric({
15
+ name: "Review", criteria: [{ id: "quality", name: "Quality",
16
+ judge: { type: "human", raters: ["reviewer"], required: 1 } }],
17
+ });
18
+ console.log(rubric.id);
19
+ handle.sqlite.close();
20
+ ```
21
+
22
+ Requires Node 20 or newer. [Documentation](https://verdict-docs.developerolim.workers.dev/reference/rest). MIT licensed.
package/dist/auth.d.ts ADDED
@@ -0,0 +1,34 @@
1
+ import { apiKeys as sqliteApiKeys } from "./schema/sqlite.js";
2
+ import type { SqliteDb } from "./store.js";
3
+ import type { PgDb } from "./dialects.js";
4
+ import type { apiKeys as pgApiKeys } from "./schema/pg.js";
5
+ /** Structurally identical to @verdict-ai/api's `Auth` — kept local so db does not depend on api. */
6
+ export interface KeyAuth {
7
+ resolve(apiKey: string | undefined): Promise<string | undefined> | string | undefined;
8
+ }
9
+ /** Hashes UTF-8 input as hexadecimal SHA-256; rejects Web Crypto failures. */
10
+ export declare function sha256Hex(s: string): Promise<string>;
11
+ /** API keys stored hashed in the `api_keys` table. `fallback` lets env-configured keys keep working (bootstrap). */
12
+ export declare class DrizzleAuth implements KeyAuth {
13
+ private fallback?;
14
+ private onError;
15
+ private queries;
16
+ constructor(db: SqliteDb, fallback?: KeyAuth, apiKeys?: typeof sqliteApiKeys, onError?: (error: unknown) => void);
17
+ constructor(db: PgDb, fallback: KeyAuth | undefined, apiKeys: typeof pgApiKeys, onError?: (error: unknown) => void);
18
+ resolve(key: string | undefined): Promise<string | undefined>;
19
+ /** Create a key. Returns the plaintext exactly once. */
20
+ createKey(tenantId: string, name: string): Promise<{
21
+ id: string;
22
+ key: string;
23
+ prefix: string;
24
+ }>;
25
+ revokeKey(id: string, tenantId?: string): Promise<boolean>;
26
+ listKeys(tenantId: string): Promise<{
27
+ id: string;
28
+ name: string;
29
+ prefix: string;
30
+ createdAt: string;
31
+ lastUsedAt: string | null;
32
+ revokedAt: string | null;
33
+ }[]>;
34
+ }
package/dist/auth.js ADDED
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Plaintext keys are returned once; revocation takes precedence over bootstrap authentication.
3
+ */
4
+ import { ValidationError } from "@verdict-ai/core";
5
+ import { storeOperation } from "./errors.js";
6
+ import { sqliteQueries, postgresQueries } from "./queries.js";
7
+ import { schema as sqliteSchema } from "./schema/sqlite.js";
8
+ import { schema as pgSchema } from "./schema/pg.js";
9
+ import { eq } from "drizzle-orm";
10
+ import { apiKeys as sqliteApiKeys } from "./schema/sqlite.js";
11
+ /** Hashes UTF-8 input as hexadecimal SHA-256; rejects Web Crypto failures. */
12
+ export async function sha256Hex(s) {
13
+ const buf = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(s));
14
+ return [...new Uint8Array(buf)].map((b) => b.toString(16).padStart(2, "0")).join("");
15
+ }
16
+ const KEY_ENTROPY_BYTES = 24;
17
+ const DISPLAY_PREFIX_CHARS = 12;
18
+ function randomKey(prefix = "vk_live_") {
19
+ // why: 24 random bytes provide 192 bits of key entropy; the prefix is only an operator hint.
20
+ const bytes = crypto.getRandomValues(new Uint8Array(KEY_ENTROPY_BYTES));
21
+ return prefix + [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
22
+ }
23
+ /** API keys stored hashed in the `api_keys` table. `fallback` lets env-configured keys keep working (bootstrap). */
24
+ export class DrizzleAuth {
25
+ fallback;
26
+ onError;
27
+ queries;
28
+ constructor(db, fallback, apiKeys = sqliteApiKeys, onError = console.error) {
29
+ this.fallback = fallback;
30
+ this.onError = onError;
31
+ // why: constructor overloads pair the driver with its schema; raw rows use that dialect's own column codecs.
32
+ this.queries = apiKeys === sqliteApiKeys ? sqliteQueries(db, sqliteSchema) : postgresQueries(db, pgSchema);
33
+ }
34
+ async resolve(key) {
35
+ return storeOperation("auth.resolve", async () => {
36
+ if (!key)
37
+ return await this.fallback?.resolve(key);
38
+ const hash = await sha256Hex(key);
39
+ const rows = await this.queries.select("apiKeys", { where: eq(this.queries.tables.apiKeys.keyHash, hash), limit: 1 });
40
+ const row = rows[0];
41
+ if (!row)
42
+ return await this.fallback?.resolve(key);
43
+ // why: a revoked database key must not regain access through bootstrap fallback authentication.
44
+ if (row.revokedAt)
45
+ return undefined;
46
+ // why: last-used telemetry is advisory; a failed touch must not deny an otherwise valid key.
47
+ void Promise.resolve(this.queries.update("apiKeys", { lastUsedAt: new Date().toISOString() }, eq(this.queries.tables.apiKeys.id, row.id))).catch(error => this.onError(error));
48
+ return row.tenantId;
49
+ });
50
+ }
51
+ /** Create a key. Returns the plaintext exactly once. */
52
+ async createKey(tenantId, name) {
53
+ if (!tenantId.trim() || !name.trim())
54
+ throw new ValidationError("key tenant and name must not be empty");
55
+ return storeOperation(`auth.createKey:${tenantId}`, async () => {
56
+ const key = randomKey();
57
+ const id = `key_${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
58
+ const prefix = key.slice(0, DISPLAY_PREFIX_CHARS);
59
+ await this.queries.insert("apiKeys", [{ id, tenantId, name, keyHash: await sha256Hex(key), prefix, createdAt: new Date().toISOString() }]);
60
+ return { id, key, prefix };
61
+ });
62
+ }
63
+ async revokeKey(id, tenantId) {
64
+ return storeOperation("auth.revokeKey", async () => {
65
+ const rows = await this.queries.select("apiKeys", { where: eq(this.queries.tables.apiKeys.id, id), limit: 1 });
66
+ if (!rows[0] || (tenantId !== undefined && rows[0].tenantId !== tenantId))
67
+ return false;
68
+ await this.queries.update("apiKeys", { revokedAt: new Date().toISOString() }, eq(this.queries.tables.apiKeys.id, id));
69
+ return true;
70
+ });
71
+ }
72
+ async listKeys(tenantId) {
73
+ return storeOperation("auth.listKeys", async () => {
74
+ const rows = await this.queries.select("apiKeys", { where: eq(this.queries.tables.apiKeys.tenantId, tenantId) });
75
+ // why: key hashes and tenant internals are never part of the key-management response.
76
+ return rows.map(({ id, name, prefix, createdAt, lastUsedAt, revokedAt }) => ({ id, name, prefix, createdAt, lastUsedAt, revokedAt }));
77
+ });
78
+ }
79
+ }
80
+ //# sourceMappingURL=auth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE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