@lunora/d1 1.0.0-alpha.114 → 1.0.0-alpha.116

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/index.d.mts CHANGED
@@ -156,6 +156,7 @@ declare class D1Session {
156
156
  * left unretried.
157
157
  */
158
158
  all<T = unknown>(sql: string, ...binds: unknown[]): Promise<{
159
+ meta?: Record<string, unknown>;
159
160
  results: T[];
160
161
  success: boolean;
161
162
  }>;
@@ -379,6 +380,46 @@ declare class MigrationRunner {
379
380
  private assertUniqueVersions;
380
381
  private assertUniqueSql;
381
382
  }
383
+ /**
384
+ * Derive a low-cardinality tag for a rendered statement — `select:users`,
385
+ * `insert:orders`, `select:unknown` when no table token is recognisable.
386
+ *
387
+ * Cardinality is the whole design constraint. The rendered SQL is unique per
388
+ * shape and near-unique per call site, so grouping on it produces a tag space
389
+ * that grows without bound and answers nothing; verb + table is the coarsest
390
+ * label that still points at a specific culprit.
391
+ */
392
+ declare const d1QueryTag: (sql: string) => string;
393
+ /** One statement's cost, read off D1's `meta`. */
394
+ interface D1QueryCost {
395
+ /** D1's own server-side duration in ms, when reported. */
396
+ durationMs?: number;
397
+ /** Rows D1 SCANNED — the billed number, and the one a missing index inflates. */
398
+ rowsRead: number;
399
+ /** Rows written by a DML statement; `0` for reads. */
400
+ rowsWritten: number;
401
+ }
402
+ /**
403
+ * Project D1's `meta` onto {@link D1QueryCost}, or `undefined` when the result
404
+ * carried no usable accounting — a test double, or a `batch()` leg that reports
405
+ * none. Callers skip the emit entirely in that case rather than logging zeroes,
406
+ * which would read as "this query was free".
407
+ */
408
+ declare const readD1QueryCost: (meta: Record<string, unknown> | undefined) => D1QueryCost | undefined;
409
+ /**
410
+ * Emit one structured cost event per D1 statement into Workers Logs, so
411
+ * "D1 reads spiked" resolves to a tag by group-by.
412
+ *
413
+ * Best-effort by contract, exactly like the request-log emit: a serialization
414
+ * hiccup must never turn a served query into a failed one, so the caller wraps
415
+ * this and swallows. Emitting one line per D1 statement is proportionate —
416
+ * every one of them is already a network round trip, so the line is noise-free
417
+ * next to the work it describes.
418
+ *
419
+ * The SQL text itself is NEVER emitted: it carries inlined literals on some
420
+ * render paths, and the tag is the groupable thing anyway.
421
+ */
422
+ declare const emitD1QueryCost: (sql: string, meta: Record<string, unknown> | undefined) => void;
382
423
  /** Tuning for {@link withD1Retry}. */
383
424
  interface D1RetryOptions {
384
425
  /** Total attempts including the first. Default 3. Must be >= 1. */
@@ -465,4 +506,4 @@ declare const retryingExec: (exec: SqlCtxExec, options?: D1RetryOptions) => SqlC
465
506
  * the store core runs `sqliteEncode`/`sqliteDecode` on every engine.
466
507
  */
467
508
  declare const sqliteDialect: SqlDialect;
468
- export { D1Client, type D1ContextDatabaseOptions as D1CtxDbOptions, type D1RetryOptions, D1Session, D1TimeoutError, type ExportGlobalArgs, type FacetGlobalColumnOptions, type ExportRow as GlobalExportRow, type GlobalFacetResult, type GlobalFacetValue, type GlobalFilterClause, type ImportError as GlobalImportError, type ImportResult as GlobalImportResult, type GlobalTableInfo, type GlobalTablePage, type ImportGlobalArgs, type Migration, MigrationRunner, type MigrationRunnerResult, type ReadGlobalTablePageOptions, backfillD1SearchIndexes, createD1ContextDatabase as createD1CtxDb, exportGlobalRows, facetGlobalColumn, importGlobalRows, isTransientD1Error, listGlobalTables, readD1CdcChanges, readGlobalTablePage, retryingExec, runD1AggregateMigrations, runD1CdcMigration, runD1GlobalTableMigrations, runD1RankMigrations, runD1SearchMigrations, selectGlobalTables, sqliteDialect, sweepD1CdcRetention, withD1Retry };
509
+ export { D1Client, type D1ContextDatabaseOptions as D1CtxDbOptions, type D1QueryCost, type D1RetryOptions, D1Session, D1TimeoutError, type ExportGlobalArgs, type FacetGlobalColumnOptions, type ExportRow as GlobalExportRow, type GlobalFacetResult, type GlobalFacetValue, type GlobalFilterClause, type ImportError as GlobalImportError, type ImportResult as GlobalImportResult, type GlobalTableInfo, type GlobalTablePage, type ImportGlobalArgs, type Migration, MigrationRunner, type MigrationRunnerResult, type ReadGlobalTablePageOptions, backfillD1SearchIndexes, createD1ContextDatabase as createD1CtxDb, d1QueryTag, emitD1QueryCost, exportGlobalRows, facetGlobalColumn, importGlobalRows, isTransientD1Error, listGlobalTables, readD1CdcChanges, readD1QueryCost, readGlobalTablePage, retryingExec, runD1AggregateMigrations, runD1CdcMigration, runD1GlobalTableMigrations, runD1RankMigrations, runD1SearchMigrations, selectGlobalTables, sqliteDialect, sweepD1CdcRetention, withD1Retry };
package/dist/index.d.ts CHANGED
@@ -156,6 +156,7 @@ declare class D1Session {
156
156
  * left unretried.
157
157
  */
158
158
  all<T = unknown>(sql: string, ...binds: unknown[]): Promise<{
159
+ meta?: Record<string, unknown>;
159
160
  results: T[];
160
161
  success: boolean;
161
162
  }>;
@@ -379,6 +380,46 @@ declare class MigrationRunner {
379
380
  private assertUniqueVersions;
380
381
  private assertUniqueSql;
381
382
  }
383
+ /**
384
+ * Derive a low-cardinality tag for a rendered statement — `select:users`,
385
+ * `insert:orders`, `select:unknown` when no table token is recognisable.
386
+ *
387
+ * Cardinality is the whole design constraint. The rendered SQL is unique per
388
+ * shape and near-unique per call site, so grouping on it produces a tag space
389
+ * that grows without bound and answers nothing; verb + table is the coarsest
390
+ * label that still points at a specific culprit.
391
+ */
392
+ declare const d1QueryTag: (sql: string) => string;
393
+ /** One statement's cost, read off D1's `meta`. */
394
+ interface D1QueryCost {
395
+ /** D1's own server-side duration in ms, when reported. */
396
+ durationMs?: number;
397
+ /** Rows D1 SCANNED — the billed number, and the one a missing index inflates. */
398
+ rowsRead: number;
399
+ /** Rows written by a DML statement; `0` for reads. */
400
+ rowsWritten: number;
401
+ }
402
+ /**
403
+ * Project D1's `meta` onto {@link D1QueryCost}, or `undefined` when the result
404
+ * carried no usable accounting — a test double, or a `batch()` leg that reports
405
+ * none. Callers skip the emit entirely in that case rather than logging zeroes,
406
+ * which would read as "this query was free".
407
+ */
408
+ declare const readD1QueryCost: (meta: Record<string, unknown> | undefined) => D1QueryCost | undefined;
409
+ /**
410
+ * Emit one structured cost event per D1 statement into Workers Logs, so
411
+ * "D1 reads spiked" resolves to a tag by group-by.
412
+ *
413
+ * Best-effort by contract, exactly like the request-log emit: a serialization
414
+ * hiccup must never turn a served query into a failed one, so the caller wraps
415
+ * this and swallows. Emitting one line per D1 statement is proportionate —
416
+ * every one of them is already a network round trip, so the line is noise-free
417
+ * next to the work it describes.
418
+ *
419
+ * The SQL text itself is NEVER emitted: it carries inlined literals on some
420
+ * render paths, and the tag is the groupable thing anyway.
421
+ */
422
+ declare const emitD1QueryCost: (sql: string, meta: Record<string, unknown> | undefined) => void;
382
423
  /** Tuning for {@link withD1Retry}. */
383
424
  interface D1RetryOptions {
384
425
  /** Total attempts including the first. Default 3. Must be >= 1. */
@@ -465,4 +506,4 @@ declare const retryingExec: (exec: SqlCtxExec, options?: D1RetryOptions) => SqlC
465
506
  * the store core runs `sqliteEncode`/`sqliteDecode` on every engine.
466
507
  */
467
508
  declare const sqliteDialect: SqlDialect;
468
- export { D1Client, type D1ContextDatabaseOptions as D1CtxDbOptions, type D1RetryOptions, D1Session, D1TimeoutError, type ExportGlobalArgs, type FacetGlobalColumnOptions, type ExportRow as GlobalExportRow, type GlobalFacetResult, type GlobalFacetValue, type GlobalFilterClause, type ImportError as GlobalImportError, type ImportResult as GlobalImportResult, type GlobalTableInfo, type GlobalTablePage, type ImportGlobalArgs, type Migration, MigrationRunner, type MigrationRunnerResult, type ReadGlobalTablePageOptions, backfillD1SearchIndexes, createD1ContextDatabase as createD1CtxDb, exportGlobalRows, facetGlobalColumn, importGlobalRows, isTransientD1Error, listGlobalTables, readD1CdcChanges, readGlobalTablePage, retryingExec, runD1AggregateMigrations, runD1CdcMigration, runD1GlobalTableMigrations, runD1RankMigrations, runD1SearchMigrations, selectGlobalTables, sqliteDialect, sweepD1CdcRetention, withD1Retry };
509
+ export { D1Client, type D1ContextDatabaseOptions as D1CtxDbOptions, type D1QueryCost, type D1RetryOptions, D1Session, D1TimeoutError, type ExportGlobalArgs, type FacetGlobalColumnOptions, type ExportRow as GlobalExportRow, type GlobalFacetResult, type GlobalFacetValue, type GlobalFilterClause, type ImportError as GlobalImportError, type ImportResult as GlobalImportResult, type GlobalTableInfo, type GlobalTablePage, type ImportGlobalArgs, type Migration, MigrationRunner, type MigrationRunnerResult, type ReadGlobalTablePageOptions, backfillD1SearchIndexes, createD1ContextDatabase as createD1CtxDb, d1QueryTag, emitD1QueryCost, exportGlobalRows, facetGlobalColumn, importGlobalRows, isTransientD1Error, listGlobalTables, readD1CdcChanges, readD1QueryCost, readGlobalTablePage, retryingExec, runD1AggregateMigrations, runD1CdcMigration, runD1GlobalTableMigrations, runD1RankMigrations, runD1SearchMigrations, selectGlobalTables, sqliteDialect, sweepD1CdcRetention, withD1Retry };
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{exportGlobalRows as o,importGlobalRows as t,selectGlobalTables as a}from"./packem_shared/exportGlobalRows-wcVYIgvl.mjs";import{D1Client as i,D1Session as n}from"./packem_shared/D1Client-CzS0Qfa6.mjs";import{backfillD1SearchIndexes as D,createD1CtxDb as b,readD1CdcChanges as p,runD1AggregateMigrations as x,runD1CdcMigration as c,runD1GlobalTableMigrations as g,runD1RankMigrations as f,runD1SearchMigrations as m,sweepD1CdcRetention as C}from"./packem_shared/backfillD1SearchIndexes-V-i_kEvG.mjs";import{facetGlobalColumn as d,listGlobalTables as G,readGlobalTablePage as M}from"./packem_shared/facetGlobalColumn-DV6dTDEs.mjs";import{MigrationRunner as T}from"./packem_shared/MigrationRunner-BGcgKW27.mjs";import{D1TimeoutError as w,isTransientD1Error as S,retryingExec as y,withD1Retry as E}from"./packem_shared/D1TimeoutError-D4YUqZPk.mjs";import{default as q}from"./packem_shared/sqliteDialect-J6_WTDHg.mjs";import{applyCdcChanges as I}from"@lunora/shard-engine";import{createSqlCtxDb as j}from"@lunora/sql-store";export{i as D1Client,n as D1Session,w as D1TimeoutError,T as MigrationRunner,I as applyCdcChanges,D as backfillD1SearchIndexes,b as createD1CtxDb,j as createSqlCtxDb,o as exportGlobalRows,d as facetGlobalColumn,t as importGlobalRows,S as isTransientD1Error,G as listGlobalTables,p as readD1CdcChanges,M as readGlobalTablePage,y as retryingExec,x as runD1AggregateMigrations,c as runD1CdcMigration,g as runD1GlobalTableMigrations,f as runD1RankMigrations,m as runD1SearchMigrations,a as selectGlobalTables,q as sqliteDialect,C as sweepD1CdcRetention,E as withD1Retry};
1
+ import{exportGlobalRows as o,importGlobalRows as t,selectGlobalTables as a}from"./packem_shared/exportGlobalRows-wcVYIgvl.mjs";import{D1Client as i,D1Session as n}from"./packem_shared/D1Client-CzS0Qfa6.mjs";import{backfillD1SearchIndexes as D,createD1CtxDb as p,readD1CdcChanges as x,runD1AggregateMigrations as b,runD1CdcMigration as g,runD1GlobalTableMigrations as m,runD1RankMigrations as c,runD1SearchMigrations as f,sweepD1CdcRetention as u}from"./packem_shared/backfillD1SearchIndexes-V-i_kEvG.mjs";import{facetGlobalColumn as d,listGlobalTables as G,readGlobalTablePage as T}from"./packem_shared/facetGlobalColumn-DV6dTDEs.mjs";import{MigrationRunner as M}from"./packem_shared/MigrationRunner-BGcgKW27.mjs";import{d1QueryTag as h,emitD1QueryCost as w,readD1QueryCost as S}from"./packem_shared/d1QueryTag-BcEGW4q1.mjs";import{D1TimeoutError as Q,isTransientD1Error as k,retryingExec as q,withD1Retry as A}from"./packem_shared/D1TimeoutError-D4YUqZPk.mjs";import{default as P}from"./packem_shared/sqliteDialect-J6_WTDHg.mjs";import{applyCdcChanges as v}from"@lunora/shard-engine";import{createSqlCtxDb as B}from"@lunora/sql-store";export{i as D1Client,n as D1Session,Q as D1TimeoutError,M as MigrationRunner,v as applyCdcChanges,D as backfillD1SearchIndexes,p as createD1CtxDb,B as createSqlCtxDb,h as d1QueryTag,w as emitD1QueryCost,o as exportGlobalRows,d as facetGlobalColumn,t as importGlobalRows,k as isTransientD1Error,G as listGlobalTables,x as readD1CdcChanges,S as readD1QueryCost,T as readGlobalTablePage,q as retryingExec,b as runD1AggregateMigrations,g as runD1CdcMigration,m as runD1GlobalTableMigrations,c as runD1RankMigrations,f as runD1SearchMigrations,a as selectGlobalTables,P as sqliteDialect,u as sweepD1CdcRetention,A as withD1Retry};
@@ -0,0 +1 @@
1
+ const s="lunora",i="d1_query",c=new Set(["delete","insert","select","update"]),a=/\b(?:from|into|update)\s+["`[]?(?<table>[a-z_][\w$]*)/iu,u=/^\s*(?<verb>[a-z]+)/iu,d=o=>a.exec(o)?.groups?.table?.toLowerCase(),w=o=>{const t=u.exec(o)?.groups?.verb?.toLowerCase()??"",r=d(o);return c.has(t)?`${t}:${r??"unknown"}`:r===void 0?"other":`other:${r}`},n=(o,e)=>{const t=o?.[e];return typeof t=="number"&&Number.isFinite(t)?t:void 0},E=o=>{const e=n(o,"rows_read"),t=n(o,"rows_written");if(e===void 0&&t===void 0)return;const r=n(o,"duration");return{...r===void 0?{}:{durationMs:r},rowsRead:e??0,rowsWritten:t??0}},b=(o,e)=>{const t=E(e);if(t===void 0)return;const r=JSON.stringify({durationMs:t.durationMs,rowsRead:t.rowsRead,rowsWritten:t.rowsWritten,source:s,tag:w(o),type:i});console.log(r)};export{w as d1QueryTag,b as emitD1QueryCost,E as readD1QueryCost};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/d1",
3
- "version": "1.0.0-alpha.114",
3
+ "version": "1.0.0-alpha.116",
4
4
  "description": "D1 adapter for Lunora .global() tables, wrapping the Sessions API for read-your-writes",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -50,10 +50,10 @@
50
50
  "access": "public"
51
51
  },
52
52
  "dependencies": {
53
- "@lunora/errors": "1.0.0-alpha.35",
54
- "@lunora/platform": "1.0.0-alpha.28",
55
- "@lunora/shard-engine": "1.0.0-alpha.62",
56
- "@lunora/sql-store": "1.0.0-alpha.114",
53
+ "@lunora/errors": "1.0.0-alpha.36",
54
+ "@lunora/platform": "1.0.0-alpha.30",
55
+ "@lunora/shard-engine": "1.0.0-alpha.64",
56
+ "@lunora/sql-store": "1.0.0-alpha.116",
57
57
  "drizzle-orm": "^0.45.2"
58
58
  },
59
59
  "engines": {