@rexezuge/d1 1.2.0 → 1.3.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.
- package/dist/BaseDAO.d.ts +2 -2
- package/dist/BaseDAO.d.ts.map +1 -1
- package/dist/BaseDAO.js +1 -1
- package/dist/BaseDAO.js.map +1 -1
- package/dist/D1Retry.d.ts +1 -1
- package/dist/D1Retry.d.ts.map +1 -1
- package/dist/D1Retry.js +1 -1
- package/dist/D1Retry.js.map +1 -1
- package/dist/index.d.ts +9 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -6
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/BaseDAO.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { D1_RETRY_DEFAULTS } from './D1Retry';
|
|
2
|
-
import type { D1PreparedStatement, D1Queryable, D1Result } from './D1Types';
|
|
1
|
+
import { D1_RETRY_DEFAULTS } from './D1Retry.ts';
|
|
2
|
+
import type { D1PreparedStatement, D1Queryable, D1Result } from './D1Types.ts';
|
|
3
3
|
/**
|
|
4
4
|
* Canonical DAO base: retry is a policy, methods are a promise.
|
|
5
5
|
*
|
package/dist/BaseDAO.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseDAO.d.ts","sourceRoot":"","sources":["../src/BaseDAO.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAoB,MAAM,
|
|
1
|
+
{"version":3,"file":"BaseDAO.d.ts","sourceRoot":"","sources":["../src/BaseDAO.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAoB,MAAM,cAAc,CAAC;AACnE,OAAO,KAAK,EAAE,mBAAmB,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAU/E;;;;;;;;;;;;;;;GAeG;AACH,uBAAe,OAAO;IACE,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,WA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ANGED
package/dist/BaseDAO.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseDAO.js","sourceRoot":"","sources":["../src/BaseDAO.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"BaseDAO.js","sourceRoot":"","sources":["../src/BaseDAO.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAWnE;;;;;;;;;;;;;;;GAeG;AACH,MAAe,OAAO;IACqB;IAAzC,YAAyC,QAAqB;QAArB,aAAQ,GAAR,QAAQ,CAAa;IAAG,CAAC;IAElE,2GAA2G;IACjG,SAAS,CAAC,GAAW,EAAE,WAAsB,EAAE;QACvD,OAAO,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACzG,CAAC;IAED;;;;;OAKG;IACO,KAAK,CAAC,GAAG,CAAC,GAAW,EAAE,WAAsB,EAAE;QACvD,OAAO,gBAAgB,CAAC,GAAsB,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,GAAG,EAAE,EAAE,eAAe,CAAC,CAAC;IACzG,CAAC;IAED;;;;;;;;OAQG;IACO,KAAK,CAAC,KAAK,CAAI,GAAW,EAAE,WAAsB,EAAE;QAC5D,MAAM,QAAQ,GAAG,MAAM,gBAAgB,CAAC,KAAK,IAA0C,EAAE;YACvF,MAAM,GAAG,GAAa,MAAM,I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,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IAClF,CAAC;AACH,CAAC;AAED,OAAO,EAAE,OAAO,EAAE,mBAAmB,EAAE,CAAC","sourcesContent":["import { DatabaseError } from '@rexezuge/errors';\nimport { D1_RETRY_DEFAULTS, executeWithRetry } from './D1Retry.ts';\nimport type { D1PreparedStatement, D1Queryable, D1Result } from './D1Types.ts';\n\n/**\n * A statement result plus the payload it produced, so the retry engine (which\n * inspects `success`) also handles wrapped results like batch arrays.\n */\ninterface StatementEnvelope<T> extends D1Result {\n value: T;\n}\n\n/**\n * Canonical DAO base: retry is a policy, methods are a promise.\n *\n * Converged from the nine consumer repos' BaseDAOs. The API is the **sql-first**\n * shape CalDAV-Bridge and ChordDHT-Tracker used, not the closure-first shape\n * Durable-DAV's family used: a DAO states its SQL and bindings directly, and\n * the retry/normalization wrapper is invisible at the call site. The closure\n * form made every read site pay for a wrapper 90% of them never needed, and\n * each of the two shapes drifted its own method names (`withRetry` vs\n * `run(sql, bindings)`); the sql-first form is what survived.\n *\n * Non-DAO concerns stay out: cursor pagination lives in `@rexezuge/shared`\n * (`CursorCodec`), subrequest accounting is an injectable port. Retry policy\n * lives in `D1Retry` — one schedule, one classifier, one place the\n * `Failed to <context>` prefix is added.\n */\nabstract class BaseDAO {\n protected constructor(protected readonly database: D1Queryable) {}\n\n /** A prepared statement with its bindings applied. Bindings are always parameters, never interpolation. */\n protected statement(sql: string, bindings: unknown[] = []): D1PreparedStatement {\n return bindings.length > 0 ? this.database.prepare(sql).bind(...bindings) : this.database.prepare(sql);\n }\n\n /**\n * Run a statement. D1 reports a constraint violation or a missing table by\n * returning `success: false` rather than by throwing, so both forms are\n * normalized to a `DatabaseError` here — ignoring the result form is what\n * let a `UNIQUE` failure reach a caller as an opaque 500.\n */\n protected async run(sql: string, bindings: unknown[] = []): Promise<D1Result> {\n return executeWithRetry((): Promise<D1Result> => this.statement(sql, bindings).run(), 'run statement');\n }\n\n /**\n * Read one row. A `null` result is \"no row\", a success, not an error; only a\n * rejected statement is retried and normalized.\n *\n * `null` rather than `undefined` because that is what the platform says\n * (`D1Result.first<T>()` returns `T | null`) and an adapter should hand back\n * what it was given. It also makes this layer agree with `KvCache`, which has\n * always returned `null` for a miss, so one convention covers both.\n */\n protected async first<T>(sql: string, bindings: unknown[] = []): Promise<T | null> {\n const envelope = await executeWithRetry(async (): Promise<StatementEnvelope<T | null>> => {\n const row: T | null = await this.statement(sql, bindings).first<T>();\n return { success: true, value: row };\n }, 'read a row');\n return envelope.value;\n }\n\n /** A list read. `.all()` returning `[]` is a success like `first()`'s null. */\n protected async all<T>(sql: string, bindings: unknown[] = []): Promise<T[]> {\n const envelope = await executeWithRetry(async (): Promise<StatementEnvelope<T[]>> => {\n const rows = await this.statement(sql, bindings).all<T>();\n return { success: true, value: rows.results ?? [] };\n }, 'read rows');\n return envelope.value;\n }\n\n /** A scalar count from a `SELECT count(*)` row; 0 when absent. */\n protected async count(sql: string, bindings: unknown[] = []): Promise<number> {\n const row = await this.first<{ count: number }>(sql, bindings);\n return row?.count ?? 0;\n }\n\n /**\n * A batch of prepared statements — one transaction and one subrequest,\n * however many statements it carries. Each result is checked, so a single\n * failure inside the batch raises rather than passing silently.\n */\n protected async batch(statements: D1PreparedStatement[]): Promise<D1Result[]> {\n if (statements.length === 0) return [];\n if (this.database.batch === undefined) {\n throw new DatabaseError('The D1 binding exposes no batch() — a multi-statement transaction is not available here');\n }\n const envelope = await executeWithRetry(async (): Promise<StatementEnvelope<D1Result[]>> => {\n const results: D1Result[] = await (this.database.batch as NonNullable<D1Queryable['batch']>)(statements);\n const failed: D1Result | undefined = results.find((result): boolean => !result.success);\n return { success: failed === undefined, error: failed?.error, value: results };\n }, 'run a batch');\n return envelope.value;\n }\n\n /**\n * Generic batched delete of rows older than a cutoff, in ID order, with\n * retry. Mirrors the per-DAO `DELETE ... WHERE id IN (SELECT ... LIMIT ?)`\n * pattern that every repo re-implemented per table. The cutoff is exclusive:\n * a delete never touches a row at exactly the boundary timestamp.\n */\n protected static async deleteOlderThan(\n db: D1Queryable,\n table: string,\n timeColumn: string,\n cutoff: number | string,\n limit: number,\n idColumn: string,\n ): Promise<number> {\n assertSqlIdentifier(table, 'table');\n assertSqlIdentifier(timeColumn, 'timeColumn');\n assertSqlIdentifier(idColumn, 'idColumn');\n const result: D1Result = await executeWithRetry(\n (): Promise<D1Result> =>\n db\n .prepare(\n `DELETE FROM ${table}\n WHERE ${idColumn} IN (\n SELECT ${idColumn} FROM ${table}\n WHERE ${timeColumn} < ?\n LIMIT ?\n )`,\n )\n .bind(cutoff, limit)\n .run(),\n `delete old rows from ${table}`,\n );\n return (result.meta as { changes?: number })?.changes ?? 0;\n }\n\n /** The retry schedule, for callers that surface it (health checks, tests). */\n protected static get retryDefaults(): typeof D1_RETRY_DEFAULTS {\n return D1_RETRY_DEFAULTS;\n }\n}\n\n/**\n * Allow-list one SQL identifier segment. Values never go through this — they\n * are always bound; this exists purely for the table/column names that\n * parameter binding cannot express.\n */\nfunction assertSqlIdentifier(value: string, label: string): void {\n if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(value)) {\n throw new Error(`Unsafe SQL identifier for ${label}: ${JSON.stringify(value)}`);\n }\n}\n\nexport { BaseDAO, assertSqlIdentifier };\n"]}
|
package/dist/D1Retry.d.ts
CHANGED
package/dist/D1Retry.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"D1Retry.d.ts","sourceRoot":"","sources":["../src/D1Retry.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"D1Retry.d.ts","sourceRoot":"","sources":["../src/D1Retry.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAgB7C;;;;GAIG;AACH,QAAA,MAAM,iBAAiB;;;CAAmF,CAAC;AAE3G,UAAU,cAAc;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,iBAAS,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAExC;AAED;;;;;GAKG;AACH,iBAAS,SAAS,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAE/D;AAMD;;;;;;;;;;;;;GAaG;AACH,iBAAe,gBAAgB,CAAC,CAAC,SAAS,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC,CA0CtI;AAED,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,KAAK,EAAE,CAAC;AACjE,YAAY,EAAE,cAAc,EAAE,CAAC"}
|
package/dist/D1Retry.js
CHANGED
package/dist/D1Retry.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"D1Retry.js","sourceRoot":"","sources":["../src/D1Retry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,kBAAkB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"D1Retry.js","sourceRoot":"","sources":["../src/D1Retry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAG5D;;;;;;;;;GASG;AAEH,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAC9B,MAAM,qBAAqB,GAAG,GAAG,CAAC;AAElC;;;;GAIG;AACH,MAAM,iBAAiB,GAAG,EAAE,UAAU,EAAE,mBAAmB,EAAE,WAAW,EAAE,qBAAqB,EAAW,CAAC;AAO3G,SAAS,KAAK,CAAC,EAAU;IACvB,OAA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E;YAC1E,+CAA+C;YAC/C,IAAI,KAAK,YAAY,aAAa,EAAE,CAAC;gBACnC,IAAI,CAAC,aAAa,IAAI,KAAK,CAAC,SAAS,EAAE,CAAC;oBACtC,MAAM,KAAK,CAAC,SAAS,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC,CAAC;oBAC7C,SAAS;gBACX,CAAC;gBACD,MAAM,KAAK,CAAC;YACd,CAAC;YACD,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;gBAC3B,MAAM,OAAO,GAAG,eAAe,CAAC,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;gBACxD,IAAI,CAAC,aAAa,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;oBACxC,MAAM,KAAK,CAAC,SAAS,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC,CAAC;oBAC7C,SAAS;gBACX,CAAC;gBACD,MAAM,OAAO,CAAC;YAChB,CAAC;YACD,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;IAED,uEAAuE;IACvE,4EAA4E;IAC5E,+BAA+B;IAC/B,MAAM,IAAI,aAAa,CAAC,aAAa,OAAO,UAAU,UAAU,GAAG,CAAC,WAAW,CAAC,CAAC;AACnF,CAAC;AAED,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,KAAK,EAAE,CAAC","sourcesContent":["import { DatabaseError } from '@rexezuge/errors';\nimport { isD1ErrorRetryable } from './D1ErrorClassifier.ts';\nimport type { D1Result } from './D1Types.ts';\n\n/**\n * The single retry engine for D1 statements.\n *\n * Converged from Durable-DAV's `D1Utils.ts` (the richest, most-documented\n * variant) with ChordDHT's byte-identical copy and the straight-line port in\n * Mail-Meow. The seven consumer repos each spelled this differently\n * (`executeD1WithRetry`, `runWithRetry`, `firstWithRetry` with its own schedule);\n * the kit ships one engine plus the schedule constants, and `BaseDAO` is the\n * only intended caller.\n */\n\nconst DEFAULT_MAX_RETRIES = 3;\nconst DEFAULT_BASE_DELAY_MS = 100;\n\n/**\n * The retry schedule, exported so `BaseDAO` and any other caller cannot\n * hard-code a second, divergent one — re-deriving `baseDelayMs * 2 ** n` at a\n * call site is the same drift one level up.\n */\nconst D1_RETRY_DEFAULTS = { maxRetries: DEFAULT_MAX_RETRIES, baseDelayMs: DEFAULT_BASE_DELAY_MS } as const;\n\ninterface D1RetryOptions {\n maxRetries?: number;\n baseDelayMs?: number;\n}\n\nfunction sleep(ms: number): Promise<void> {\n return new Promise((resolve: (value: void) => void): unknown => setTimeout(resolve, ms));\n}\n\n/**\n * Backoff before the next attempt. Exponential, and uncapped by design: the\n * default base (100 ms) tops out under a second by attempt 3, while an operator\n * who sets a larger base gets the longer wait they asked for. The point is to\n * ride out a busy/locked D1, not to implement a rate limiter.\n */\nfunction backoffMs(baseDelayMs: number, attempt: number): number {\n return baseDelayMs * 2 ** attempt;\n}\n\nfunction toDatabaseError(errorMessage: string, context: string): DatabaseError {\n return new DatabaseError(`Failed to ${context}: ${errorMessage}`, isD1ErrorRetryable(errorMessage));\n}\n\n/**\n * Run a D1 operation, retrying transient failures with exponential backoff.\n *\n * Two failure shapes are handled, because D1 reports both:\n *\n * 1. **A result with `success: false`.** The operation resolved; the database\n * said no. Retried when the error classifies as transient.\n * 2. **A thrown error.** A transport-level rejection, which never carries a\n * `D1Result` at all. Classified from the message the same way.\n *\n * A `DatabaseError` we raised ourselves in (1) arrives back through the `catch`\n * on the retry attempt, so it must be recognised and re-thrown rather than\n * re-wrapped into a second layer of `Failed to …`.\n */\nasync function executeWithRetry<T extends D1Result>(operation: () => Promise<T>, context: string, options?: D1RetryOptions): Promise<T> {\n const maxRetries: number = options?.maxRetries ?? DEFAULT_MAX_RETRIES;\n const baseDelayMs: number = options?.baseDelayMs ?? DEFAULT_BASE_DELAY_MS;\n\n for (let attempt = 0; attempt <= maxRetries; attempt++) {\n const isLastAttempt = attempt === maxRetries;\n try {\n const result: T = await operation();\n if (result.success) return result;\n const errorMessage: string = result.error ?? 'Unknown database error';\n const failure = toDatabaseError(errorMessage, context);\n if (!isLastAttempt && failure.retryable) {\n await sleep(backoffMs(baseDelayMs, attempt));\n continue;\n }\n throw failure;\n } catch (error: unknown) {\n // Already classified above, or raised by the operation itself. Either way\n // re-wrapping would bury the original context.\n if (error instanceof DatabaseError) {\n if (!isLastAttempt && error.retryable) {\n await sleep(backoffMs(baseDelayMs, attempt));\n continue;\n }\n throw error;\n }\n if (error instanceof Error) {\n const failure = toDatabaseError(error.message, context);\n if (!isLastAttempt && failure.retryable) {\n await sleep(backoffMs(baseDelayMs, attempt));\n continue;\n }\n throw failure;\n }\n throw error;\n }\n }\n\n // Unreachable: the loop either returns a result or throws on its final\n // iteration. Present only so the function is total for a reader who has not\n // traced the `continue` paths.\n throw new DatabaseError(`Failed to ${context} after ${maxRetries + 1} attempts`);\n}\n\nexport { backoffMs, D1_RETRY_DEFAULTS, executeWithRetry, sleep };\nexport type { D1RetryOptions };\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export { BaseDAO, assertSqlIdentifier } from './BaseDAO';
|
|
2
|
-
export { isD1ErrorRetryable, RETRYABLE_PATTERNS, NON_RETRYABLE_PATTERNS } from './D1ErrorClassifier';
|
|
3
|
-
export { isMissingSchemaError, MISSING_SCHEMA_PATTERN } from './isMissingSchemaError';
|
|
4
|
-
export { executableStatements, isExecutable, splitSql } from './splitSql';
|
|
5
|
-
export { backoffMs, D1_RETRY_DEFAULTS, executeWithRetry, sleep } from './D1Retry';
|
|
6
|
-
export type { D1RetryOptions } from './D1Retry';
|
|
7
|
-
export type { D1PreparedStatement, D1Queryable, D1Result } from './D1Types';
|
|
8
|
-
export { AesGcmKeyError, decryptSecret, encryptSecret, roundTripSecret, IV_BYTES, KEY_BYTES } from './aes-gcm';
|
|
9
|
-
export type { EncryptedEnvelope } from './aes-gcm';
|
|
1
|
+
export { BaseDAO, assertSqlIdentifier } from './BaseDAO.ts';
|
|
2
|
+
export { isD1ErrorRetryable, RETRYABLE_PATTERNS, NON_RETRYABLE_PATTERNS } from './D1ErrorClassifier.ts';
|
|
3
|
+
export { isMissingSchemaError, MISSING_SCHEMA_PATTERN } from './isMissingSchemaError.ts';
|
|
4
|
+
export { executableStatements, isExecutable, splitSql } from './splitSql.ts';
|
|
5
|
+
export { backoffMs, D1_RETRY_DEFAULTS, executeWithRetry, sleep } from './D1Retry.ts';
|
|
6
|
+
export type { D1RetryOptions } from './D1Retry.ts';
|
|
7
|
+
export type { D1PreparedStatement, D1Queryable, D1Result } from './D1Types.ts';
|
|
8
|
+
export { AesGcmKeyError, decryptSecret, encryptSecret, roundTripSecret, IV_BYTES, KEY_BYTES } from './aes-gcm.ts';
|
|
9
|
+
export type { EncryptedEnvelope } from './aes-gcm.ts';
|
|
10
10
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AACxG,OAAO,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AACzF,OAAO,EAAE,oBAAoB,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC7E,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrF,YAAY,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACnD,YAAY,EAAE,mBAAmB,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAC/E,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,aAAa,EAAE,eAAe,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAClH,YAAY,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export { BaseDAO, assertSqlIdentifier } from
|
|
2
|
-
export { isD1ErrorRetryable, RETRYABLE_PATTERNS, NON_RETRYABLE_PATTERNS } from
|
|
3
|
-
export { isMissingSchemaError, MISSING_SCHEMA_PATTERN } from
|
|
4
|
-
export { executableStatements, isExecutable, splitSql } from
|
|
5
|
-
export { backoffMs, D1_RETRY_DEFAULTS, executeWithRetry, sleep } from
|
|
6
|
-
export { AesGcmKeyError, decryptSecret, encryptSecret, roundTripSecret, IV_BYTES, KEY_BYTES } from
|
|
1
|
+
export { BaseDAO, assertSqlIdentifier } from "./BaseDAO.js";
|
|
2
|
+
export { isD1ErrorRetryable, RETRYABLE_PATTERNS, NON_RETRYABLE_PATTERNS } from "./D1ErrorClassifier.js";
|
|
3
|
+
export { isMissingSchemaError, MISSING_SCHEMA_PATTERN } from "./isMissingSchemaError.js";
|
|
4
|
+
export { executableStatements, isExecutable, splitSql } from "./splitSql.js";
|
|
5
|
+
export { backoffMs, D1_RETRY_DEFAULTS, executeWithRetry, sleep } from "./D1Retry.js";
|
|
6
|
+
export { AesGcmKeyError, decryptSecret, encryptSecret, roundTripSecret, IV_BYTES, KEY_BYTES } from "./aes-gcm.js";
|
|
7
7
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AACxG,OAAO,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AACzF,OAAO,EAAE,oBAAoB,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC7E,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAGrF,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,aAAa,EAAE,eAAe,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC","sourcesContent":["export { BaseDAO, assertSqlIdentifier } from './BaseDAO.ts';\nexport { isD1ErrorRetryable, RETRYABLE_PATTERNS, NON_RETRYABLE_PATTERNS } from './D1ErrorClassifier.ts';\nexport { isMissingSchemaError, MISSING_SCHEMA_PATTERN } from './isMissingSchemaError.ts';\nexport { executableStatements, isExecutable, splitSql } from './splitSql.ts';\nexport { backoffMs, D1_RETRY_DEFAULTS, executeWithRetry, sleep } from './D1Retry.ts';\nexport type { D1RetryOptions } from './D1Retry.ts';\nexport type { D1PreparedStatement, D1Queryable, D1Result } from './D1Types.ts';\nexport { AesGcmKeyError, decryptSecret, encryptSecret, roundTripSecret, IV_BYTES, KEY_BYTES } from './aes-gcm.ts';\nexport type { EncryptedEnvelope } from './aes-gcm.ts';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rexezuge/d1",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
"dist"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@rexezuge/errors": "1.
|
|
29
|
-
"@rexezuge/shared": "1.
|
|
28
|
+
"@rexezuge/errors": "1.3.0",
|
|
29
|
+
"@rexezuge/shared": "1.3.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/node": "26.6.4",
|