@rudderjs/database 1.1.0 → 1.2.1

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 (126) hide show
  1. package/README.md +70 -0
  2. package/dist/db.d.ts +21 -3
  3. package/dist/db.d.ts.map +1 -1
  4. package/dist/db.js +27 -5
  5. package/dist/db.js.map +1 -1
  6. package/dist/index.d.ts +14 -2
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +23 -4
  9. package/dist/index.js.map +1 -1
  10. package/dist/native/adapter.d.ts +202 -0
  11. package/dist/native/adapter.d.ts.map +1 -0
  12. package/dist/native/adapter.js +440 -0
  13. package/dist/native/adapter.js.map +1 -0
  14. package/dist/native/compiler.d.ts +371 -0
  15. package/dist/native/compiler.d.ts.map +1 -0
  16. package/dist/native/compiler.js +978 -0
  17. package/dist/native/compiler.js.map +1 -0
  18. package/dist/native/dialect-mysql.d.ts +26 -0
  19. package/dist/native/dialect-mysql.d.ts.map +1 -0
  20. package/dist/native/dialect-mysql.js +188 -0
  21. package/dist/native/dialect-mysql.js.map +1 -0
  22. package/dist/native/dialect-pg.d.ts +26 -0
  23. package/dist/native/dialect-pg.d.ts.map +1 -0
  24. package/dist/native/dialect-pg.js +192 -0
  25. package/dist/native/dialect-pg.js.map +1 -0
  26. package/dist/native/dialect.d.ts +255 -0
  27. package/dist/native/dialect.d.ts.map +1 -0
  28. package/dist/native/dialect.js +237 -0
  29. package/dist/native/dialect.js.map +1 -0
  30. package/dist/native/driver.d.ts +37 -0
  31. package/dist/native/driver.d.ts.map +1 -0
  32. package/dist/native/driver.js +19 -0
  33. package/dist/native/driver.js.map +1 -0
  34. package/dist/native/drivers/better-sqlite3.d.ts +56 -0
  35. package/dist/native/drivers/better-sqlite3.d.ts.map +1 -0
  36. package/dist/native/drivers/better-sqlite3.js +171 -0
  37. package/dist/native/drivers/better-sqlite3.js.map +1 -0
  38. package/dist/native/drivers/mysql.d.ts +30 -0
  39. package/dist/native/drivers/mysql.d.ts.map +1 -0
  40. package/dist/native/drivers/mysql.js +176 -0
  41. package/dist/native/drivers/mysql.js.map +1 -0
  42. package/dist/native/drivers/postgres.d.ts +57 -0
  43. package/dist/native/drivers/postgres.d.ts.map +1 -0
  44. package/dist/native/drivers/postgres.js +155 -0
  45. package/dist/native/drivers/postgres.js.map +1 -0
  46. package/dist/native/errors.d.ts +43 -0
  47. package/dist/native/errors.d.ts.map +1 -0
  48. package/dist/native/errors.js +64 -0
  49. package/dist/native/errors.js.map +1 -0
  50. package/dist/native/index.d.ts +27 -0
  51. package/dist/native/index.d.ts.map +1 -0
  52. package/dist/native/index.js +55 -0
  53. package/dist/native/index.js.map +1 -0
  54. package/dist/native/isolation.d.ts +14 -0
  55. package/dist/native/isolation.d.ts.map +1 -0
  56. package/dist/native/isolation.js +37 -0
  57. package/dist/native/isolation.js.map +1 -0
  58. package/dist/native/query-builder.d.ts +303 -0
  59. package/dist/native/query-builder.d.ts.map +1 -0
  60. package/dist/native/query-builder.js +984 -0
  61. package/dist/native/query-builder.js.map +1 -0
  62. package/dist/native/replica-picker.d.ts +22 -0
  63. package/dist/native/replica-picker.d.ts.map +1 -0
  64. package/dist/native/replica-picker.js +65 -0
  65. package/dist/native/replica-picker.js.map +1 -0
  66. package/dist/native/schema/alter-blueprint.d.ts +37 -0
  67. package/dist/native/schema/alter-blueprint.d.ts.map +1 -0
  68. package/dist/native/schema/alter-blueprint.js +56 -0
  69. package/dist/native/schema/alter-blueprint.js.map +1 -0
  70. package/dist/native/schema/blueprint.d.ts +151 -0
  71. package/dist/native/schema/blueprint.d.ts.map +1 -0
  72. package/dist/native/schema/blueprint.js +286 -0
  73. package/dist/native/schema/blueprint.js.map +1 -0
  74. package/dist/native/schema/column.d.ts +168 -0
  75. package/dist/native/schema/column.d.ts.map +1 -0
  76. package/dist/native/schema/column.js +190 -0
  77. package/dist/native/schema/column.js.map +1 -0
  78. package/dist/native/schema/ddl-compiler.d.ts +34 -0
  79. package/dist/native/schema/ddl-compiler.d.ts.map +1 -0
  80. package/dist/native/schema/ddl-compiler.js +352 -0
  81. package/dist/native/schema/ddl-compiler.js.map +1 -0
  82. package/dist/native/schema/inspect.d.ts +67 -0
  83. package/dist/native/schema/inspect.d.ts.map +1 -0
  84. package/dist/native/schema/inspect.js +312 -0
  85. package/dist/native/schema/inspect.js.map +1 -0
  86. package/dist/native/schema/introspect.d.ts +34 -0
  87. package/dist/native/schema/introspect.d.ts.map +1 -0
  88. package/dist/native/schema/introspect.js +101 -0
  89. package/dist/native/schema/introspect.js.map +1 -0
  90. package/dist/native/schema/migration.d.ts +8 -0
  91. package/dist/native/schema/migration.d.ts.map +1 -0
  92. package/dist/native/schema/migration.js +19 -0
  93. package/dist/native/schema/migration.js.map +1 -0
  94. package/dist/native/schema/migrator.d.ts +144 -0
  95. package/dist/native/schema/migrator.d.ts.map +1 -0
  96. package/dist/native/schema/migrator.js +240 -0
  97. package/dist/native/schema/migrator.js.map +1 -0
  98. package/dist/native/schema/rebuild.d.ts +11 -0
  99. package/dist/native/schema/rebuild.d.ts.map +1 -0
  100. package/dist/native/schema/rebuild.js +92 -0
  101. package/dist/native/schema/rebuild.js.map +1 -0
  102. package/dist/native/schema/schema-builder.d.ts +46 -0
  103. package/dist/native/schema/schema-builder.d.ts.map +1 -0
  104. package/dist/native/schema/schema-builder.js +153 -0
  105. package/dist/native/schema/schema-builder.js.map +1 -0
  106. package/dist/native/schema/schema-facade.d.ts +63 -0
  107. package/dist/native/schema/schema-facade.d.ts.map +1 -0
  108. package/dist/native/schema/schema-facade.js +124 -0
  109. package/dist/native/schema/schema-facade.js.map +1 -0
  110. package/dist/native/schema/schema-types.d.ts +27 -0
  111. package/dist/native/schema/schema-types.d.ts.map +1 -0
  112. package/dist/native/schema/schema-types.js +52 -0
  113. package/dist/native/schema/schema-types.js.map +1 -0
  114. package/dist/native/schema/types-generator.d.ts +73 -0
  115. package/dist/native/schema/types-generator.d.ts.map +1 -0
  116. package/dist/native/schema/types-generator.js +181 -0
  117. package/dist/native/schema/types-generator.js.map +1 -0
  118. package/dist/registry-bridge.d.ts +24 -4
  119. package/dist/registry-bridge.d.ts.map +1 -1
  120. package/dist/registry-bridge.js +20 -0
  121. package/dist/registry-bridge.js.map +1 -1
  122. package/dist/sticky.d.ts +22 -0
  123. package/dist/sticky.d.ts.map +1 -0
  124. package/dist/sticky.js +61 -0
  125. package/dist/sticky.js.map +1 -0
  126. package/package.json +32 -2
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # @rudderjs/database
2
+
3
+ The SQL data-layer foundation for RudderJS — the `DB` facade, raw expressions, and the **built-in native SQL engine** (query compiler, schema builder, migrations, and drivers for SQLite / PostgreSQL / MySQL). `@rudderjs/orm` (the Eloquent-style `Model` layer) is built on top of this package and installs it automatically — most apps never add it by hand.
4
+
5
+ ```bash
6
+ pnpm add @rudderjs/orm # pulls in @rudderjs/database
7
+ ```
8
+
9
+ Node-only: never import this package from client-bundle-reachable code.
10
+
11
+ ## `DB` facade
12
+
13
+ Laravel-style raw-SQL entry point. Resolves the **same** active adapter your Models use (native, Prisma, or Drizzle) — one connection, never a second:
14
+
15
+ ```ts
16
+ import { DB, raw } from '@rudderjs/database'
17
+
18
+ const rows = await DB.select('SELECT * FROM users WHERE active = ?', [true])
19
+ const count = await DB.update('UPDATE posts SET draft = ? WHERE author_id = ?', [false, 7])
20
+
21
+ await DB.transaction(async () => {
22
+ // Model.* AND DB.* calls in here join the same transaction
23
+ })
24
+
25
+ DB.listen((e) => console.log(e.sql, e.duration)) // query events
26
+ const reporting = DB.connection('reporting') // named connections
27
+ ```
28
+
29
+ ## Native engine
30
+
31
+ The first-party SQL engine behind `engine: 'native'` connections — no external ORM. Headline API on the main entry:
32
+
33
+ ```ts
34
+ // database/migrations/2026_06_02_120000_create_posts_table.ts
35
+ import { Migration, Schema } from '@rudderjs/database'
36
+
37
+ export default class extends Migration {
38
+ async up() { await Schema.create('posts', (t) => { t.id(); t.string('title'); t.timestamps() }) }
39
+ async down() { await Schema.dropIfExists('posts') }
40
+ }
41
+ ```
42
+
43
+ Standalone (no framework — any Node app):
44
+
45
+ ```ts
46
+ import { NativeAdapter, BetterSqlite3Driver } from '@rudderjs/database'
47
+ ```
48
+
49
+ The full engine surface (compiler, dialects, `NativeQueryBuilder`, introspection, the schema→TS type generator) lives on the `./native` subpath. Drivers are optional peers — install the one you use: `better-sqlite3`, `postgres`, or `mysql2`.
50
+
51
+ > Historical note: the engine originally shipped inside `@rudderjs/orm` at `@rudderjs/orm/native`. That subpath remains as a permanent re-export of this package, so existing migration files and imports keep working unchanged.
52
+
53
+ ## Sticky-read scope (`./sticky`)
54
+
55
+ For read/write-split connections with `sticky: true`, reads after a write inside a request route to the writer. Outside a request scope (queue jobs, commands) wrap the work yourself:
56
+
57
+ ```ts
58
+ import { runWithDatabaseContext } from '@rudderjs/database/sticky'
59
+
60
+ await runWithDatabaseContext(async () => {
61
+ await Order.create({ ... })
62
+ await Order.query().latest().first() // routed to the writer
63
+ })
64
+ ```
65
+
66
+ ## Docs
67
+
68
+ - Database guide: https://rudderjs.com/guide/database
69
+ - Native engine: https://rudderjs.com/guide/database/native
70
+ - Connections, read/write split: https://rudderjs.com/guide/database/connections
package/dist/db.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { QueryListener, Row } from '@rudderjs/contracts';
1
+ import type { QueryListener, Row, TransactionOptions } from '@rudderjs/contracts';
2
2
  import { Expression } from './expression.js';
3
3
  /**
4
4
  * A scoped facade over one NAMED connection — what `DB.connection('reporting')`
@@ -13,7 +13,8 @@ export interface DBConnection {
13
13
  update(sql: string, bindings?: readonly unknown[]): Promise<number>;
14
14
  delete(sql: string, bindings?: readonly unknown[]): Promise<number>;
15
15
  statement(sql: string, bindings?: readonly unknown[]): Promise<number>;
16
- transaction<T>(fn: () => Promise<T>): Promise<T>;
16
+ transaction<T>(fn: () => Promise<T>, opts?: TransactionOptions): Promise<T>;
17
+ afterCommit(fn: () => void | Promise<void>): Promise<void>;
17
18
  listen(listener: QueryListener): Promise<void>;
18
19
  }
19
20
  /**
@@ -41,10 +42,27 @@ export declare const DB: {
41
42
  * rejects. Nested `DB.transaction()` / `Model.transaction()` calls map to
42
43
  * savepoints where the driver supports them.
43
44
  *
45
+ * `opts.isolationLevel` sets the transaction's isolation level (`'read
46
+ * uncommitted' | 'read committed' | 'repeatable read' | 'serializable'`) —
47
+ * outermost call only; SQLite-backed adapters throw (no isolation levels).
48
+ *
44
49
  * @throws if no transaction runner is registered (no database provider loaded),
45
50
  * or if the active adapter doesn't implement `transaction()`.
46
51
  */
47
- readonly transaction: <T>(fn: () => Promise<T>) => Promise<T>;
52
+ readonly transaction: <T>(fn: () => Promise<T>, opts?: TransactionOptions) => Promise<T>;
53
+ /**
54
+ * Queue `fn` to run after the transaction open in the current async context
55
+ * commits, mirroring Laravel's `DB::afterCommit` — the standard pattern for
56
+ * side effects that must not fire on a rollback (emails, webhooks, queue
57
+ * dispatches). Runs only when the OUTERMOST transaction commits; a rollback
58
+ * (or an enclosing savepoint's rollback) drops the callback. With no open
59
+ * transaction, `fn` runs immediately. Shares the queue with `transaction()` /
60
+ * `Model.transaction()` — the runner is `@rudderjs/orm`'s `afterCommit()`.
61
+ *
62
+ * @throws if no after-commit runner is registered (no database provider
63
+ * loaded, or `@rudderjs/orm` predates after-commit support).
64
+ */
65
+ readonly afterCommit: (fn: () => void | Promise<void>) => Promise<void>;
48
66
  /**
49
67
  * Register a query listener, mirroring Laravel's `DB::listen`. The listener
50
68
  * fires once per executed query with `{ sql, bindings, duration }` (duration
package/dist/db.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"db.d.ts","sourceRoot":"","sources":["../src/db.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAc,aAAa,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAA;AAOzE,OAAO,EAAE,UAAU,EAAO,MAAM,iBAAiB,CAAA;AAoCjD;;;;;;GAMG;AACH,MAAM,WAAW,YAAY;IAC3B,MAA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AAA;IAC3E,WAAW,CAAC,EAAE,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC1D,MAAM,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CAC/C;AAED;;;;GAIG;AAIH,eAAO,MAAM,EAAE;IACb,0DAA0D;2BACxC,MAAM,aAAY,SAAS,OAAO,EAAE,KAAQ,OAAO,CAAC,GAAG,EAAE,CAAC;IAI5E,mEAAmE;2BACjD,MAAM,aAAY,SAAS,OAAO,EAAE,KAAQ,OAAO,CAAC,MAAM,CAAC;IAI7E,mEAAmE;2BACjD,MAAM,aAAY,SAAS,OAAO,EAAE,KAAQ,OAAO,CAAC,MAAM,CAAC;IAI7E,mEAAmE;2BACjD,MAAM,aAAY,SAAS,OAAO,EAAE,KAAQ,OAAO,CAAC,MAAM,CAAC;IAI7E,6EAA6E;8BACxD,MAAM,aAAY,SAAS,OAAO,EAAE,KAAQ,OAAO,CAAC,MAAM,CAAC;IAIhF;;;;;;;;;;;;;;;OAeG;2BACe,CAAC,MAAM,MAAM,OAAO,CAAC,CAAC,CAAC,SAAS,kBAAkB,KAAG,OAAO,CAAC,CAAC,CAAC;IAIjF;;;;;;;;;;;OAWG;+BACmB,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAG,OAAO,CAAC,IAAI,CAAC;IAIhE;;;;;;;;;;;;OAYG;gCACc,aAAa,KAAG,IAAI;IAIrC,0EAA0E;0BAC/D,MAAM,GAAG,MAAM,KAAG,UAAU;IAIvC;;;;;;;;;;;OAWG;gCACc,MAAM,KAAG,YAAY;CA6B9B,CAAA"}
package/dist/db.js CHANGED
@@ -10,7 +10,7 @@
10
10
  // One adapter instance is shared with the Models — no second connection. If the
11
11
  // active adapter doesn't implement the seam (older/partial adapters), each method
12
12
  // throws a clear error naming that adapter.
13
- import { resolveAdapter, resolveTransactionRunner, resolveConnectionResolver, resolveNamedTransactionRunner, } from './registry-bridge.js';
13
+ import { resolveAdapter, resolveTransactionRunner, resolveConnectionResolver, resolveNamedTransactionRunner, resolveAfterCommitRunner, } from './registry-bridge.js';
14
14
  import { raw } from './expression.js';
15
15
  function adapterName(adapter) {
16
16
  return adapter.constructor?.name ?? 'the active adapter';
@@ -74,11 +74,30 @@ export const DB = {
74
74
  * rejects. Nested `DB.transaction()` / `Model.transaction()` calls map to
75
75
  * savepoints where the driver supports them.
76
76
  *
77
+ * `opts.isolationLevel` sets the transaction's isolation level (`'read
78
+ * uncommitted' | 'read committed' | 'repeatable read' | 'serializable'`) —
79
+ * outermost call only; SQLite-backed adapters throw (no isolation levels).
80
+ *
77
81
  * @throws if no transaction runner is registered (no database provider loaded),
78
82
  * or if the active adapter doesn't implement `transaction()`.
79
83
  */
80
- async transaction(fn) {
81
- return resolveTransactionRunner()(fn);
84
+ async transaction(fn, opts) {
85
+ return resolveTransactionRunner()(fn, opts);
86
+ },
87
+ /**
88
+ * Queue `fn` to run after the transaction open in the current async context
89
+ * commits, mirroring Laravel's `DB::afterCommit` — the standard pattern for
90
+ * side effects that must not fire on a rollback (emails, webhooks, queue
91
+ * dispatches). Runs only when the OUTERMOST transaction commits; a rollback
92
+ * (or an enclosing savepoint's rollback) drops the callback. With no open
93
+ * transaction, `fn` runs immediately. Shares the queue with `transaction()` /
94
+ * `Model.transaction()` — the runner is `@rudderjs/orm`'s `afterCommit()`.
95
+ *
96
+ * @throws if no after-commit runner is registered (no database provider
97
+ * loaded, or `@rudderjs/orm` predates after-commit support).
98
+ */
99
+ async afterCommit(fn) {
100
+ return resolveAfterCommitRunner()(fn);
82
101
  },
83
102
  /**
84
103
  * Register a query listener, mirroring Laravel's `DB::listen`. The listener
@@ -130,8 +149,11 @@ export const DB = {
130
149
  async statement(sql, bindings = []) {
131
150
  return requireAffecting(await adapterFor())(sql, bindings);
132
151
  },
133
- async transaction(fn) {
134
- return resolveNamedTransactionRunner()(name, fn);
152
+ async transaction(fn, opts) {
153
+ return resolveNamedTransactionRunner()(name, fn, opts);
154
+ },
155
+ async afterCommit(fn) {
156
+ return resolveAfterCommitRunner()(fn, { connection: name });
135
157
  },
136
158
  async listen(listener) {
137
159
  requireOnQuery(await adapterFor())(listener);
package/dist/db.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"db.js","sourceRoot":"","sources":["../src/db.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAC9D,EAAE;AACF,6EAA6E;AAC7E,yEAAyE;AACzE,8DAA8D;AAC9D,yEAAyE;AACzE,oFAAoF;AACpF,8DAA8D;AAC9D,EAAE;AACF,gFAAgF;AAChF,kFAAkF;AAClF,4CAA4C;AAG5C,OAAO,EACL,cAAc,EACd,wBAAwB,EACxB,yBAAyB,EACzB,6BAA6B,GAC9B,MAAM,sBAAsB,CAAA;AAC7B,OAAO,EAAc,GAAG,EAAE,MAAM,iBAAiB,CAAA;AAEjD,SAAS,WAAW,CAAC,OAAmB;IACtC,OAAO,OAAO,CAAC,WAAW,EAAE,IAAI,IAAI,oBAAoB,CAAA;AAC1D,CAAC;AAED,SAAS,gBAAgB,CAAC,OAAmB;IAC3C,IAAI,OAAO,OAAO,CAAC,SAAS,KAAK,UAAU,EAAE,CAAC;QAC5C,MAAM,IAAI,KAAK,CACb,iBAAiB,WAAW,CAAC,OAAO,CAAC,oCAAoC;YACvE,kDAAkD,CACrD,CAAA;IACH,CAAC;IACD,OAAO,OAAO,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;AACxC,CAAC;AAED,SAAS,gBAAgB,CAAC,OAAmB;IAC3C,IAAI,OAAO,OAAO,CAAC,kBAAkB,KAAK,UAAU,EAAE,CAAC;QACrD,MAAM,IAAI,KAAK,CACb,iBAAiB,WAAW,CAAC,OAAO,CAAC,6CAA6C;YAChF,8EAA8E,CACjF,CAAA;IACH,CAAC;IACD,OAAO,OAAO,CAAC,kBAAkB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;AACj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D,KAAK,CAAC,MAAM,CAAC,GAAG,EAAE,QAAQ,GAAG,EAAE;gBAC7B,OAAO,gBAAgB,CAAC,MAAM,UAAU,EAAE,CAAC,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAA;YAC5D,CAAC;YACD,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE,QAAQ,GAAG,EAAE;gBAChC,OAAO,gBAAgB,CAAC,MAAM,UAAU,EAAE,CAAC,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAA;YAC5D,CAAC;YACD,KAAK,CAAC,WAAW,CAAC,EAAE,EAAE,IAAI;gBACxB,OAAO,6BAA6B,EAAE,CAAC,IAAI,EAAE,EAAE,EAAE,IAAI,CAAC,CAAA;YACxD,CAAC;YACD,KAAK,CAAC,WAAW,CAAC,EAAE;gBAClB,OAAO,wBAAwB,EAAE,CAAC,EAAE,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAA;YAC7D,CAAC;YACD,KAAK,CAAC,MAAM,CAAC,QAAQ;gBACnB,cAAc,CAAC,MAAM,UAAU,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAA;YAC9C,CAAC;SACF,CAAA;IACH,CAAC;CACO,CAAA"}
package/dist/index.d.ts CHANGED
@@ -1,8 +1,20 @@
1
1
  export { DB } from './db.js';
2
2
  export type { DBConnection } from './db.js';
3
3
  export { Expression, raw } from './expression.js';
4
- export { registerAdapterResolver, resolveAdapter, registerTransactionRunner, resolveTransactionRunner, registerConnectionResolver, resolveConnectionResolver, registerNamedTransactionRunner, resolveNamedTransactionRunner, __resetAdapterResolver, } from './registry-bridge.js';
5
- export type { TransactionRunner, ConnectionResolver, NamedTransactionRunner } from './registry-bridge.js';
4
+ export { registerAdapterResolver, resolveAdapter, registerTransactionRunner, resolveTransactionRunner, registerConnectionResolver, resolveConnectionResolver, registerNamedTransactionRunner, resolveNamedTransactionRunner, registerAfterCommitRunner, resolveAfterCommitRunner, __resetAdapterResolver, } from './registry-bridge.js';
5
+ export type { TransactionRunner, ConnectionResolver, NamedTransactionRunner, AfterCommitRunner } from './registry-bridge.js';
6
6
  export type { Row, Executor, Transaction, Connection } from './execution.js';
7
7
  export type { QueryEvent, QueryListener } from '@rudderjs/contracts';
8
+ export { Migration } from './native/schema/migration.js';
9
+ export { Schema, withSchema } from './native/schema/schema-facade.js';
10
+ export { Migrator, discoverMigrations } from './native/schema/migrator.js';
11
+ export { SchemaBuilder } from './native/schema/schema-builder.js';
12
+ export { Blueprint } from './native/schema/blueprint.js';
13
+ export { NativeAdapter, native } from './native/adapter.js';
14
+ export type { NativeConfig, NativeDriverName } from './native/adapter.js';
15
+ export { BetterSqlite3Driver } from './native/drivers/better-sqlite3.js';
16
+ export { PostgresDriver } from './native/drivers/postgres.js';
17
+ export { MysqlDriver } from './native/drivers/mysql.js';
18
+ export type { Dialect } from './native/dialect.js';
19
+ export type { Driver } from './native/driver.js';
8
20
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAC5B,YAAY,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAC3C,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EACL,uBAAuB,EACvB,cAAc,EACd,yBAAyB,EACzB,wBAAwB,EACxB,0BAA0B,EAC1B,yBAAyB,EACzB,8BAA8B,EAC9B,6BAA6B,EAC7B,sBAAsB,GACvB,MAAM,sBAAsB,CAAA;AAC7B,YAAY,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAA;AACzG,YAAY,EAAE,GAAG,EAAE,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAC5E,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA"}
1
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AAA;AACvD,YAAY,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAClD,YAAY,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA"}
package/dist/index.js CHANGED
@@ -1,9 +1,28 @@
1
1
  // @rudderjs/database — the SQL data-layer foundation.
2
2
  //
3
- // PR1 establishes the package boundary + the public `DB` facade contract. The
4
- // query-builder breadth, connection manager, and native-engine internals land in
5
- // later PRs (see docs/plans/2026-06-01-database-package-extraction-PR1.md).
3
+ // The `DB` facade + registry bridge (Phase 1, #823) and since the Phase-2
4
+ // relocation (docs/plans/2026-06-04-database-extraction-phase-2.md) the
5
+ // native SQL engine's home. The full engine surface lives on the node-only
6
+ // `./native` subpath; the headline API apps touch (`Migration`, `Schema`,
7
+ // `NativeAdapter`, the drivers) is re-exported below so
8
+ // `import { Migration, Schema } from '@rudderjs/database'` is the canonical
9
+ // migration-file form going forward (`@rudderjs/orm/native` keeps working as a
10
+ // back-compat shim).
6
11
  export { DB } from './db.js';
7
12
  export { Expression, raw } from './expression.js';
8
- export { registerAdapterResolver, resolveAdapter, registerTransactionRunner, resolveTransactionRunner, registerConnectionResolver, resolveConnectionResolver, registerNamedTransactionRunner, resolveNamedTransactionRunner, __resetAdapterResolver, } from './registry-bridge.js';
13
+ export { registerAdapterResolver, resolveAdapter, registerTransactionRunner, resolveTransactionRunner, registerConnectionResolver, resolveConnectionResolver, registerNamedTransactionRunner, resolveNamedTransactionRunner, registerAfterCommitRunner, resolveAfterCommitRunner, __resetAdapterResolver, } from './registry-bridge.js';
14
+ // ─── Native engine — headline API ──────────────────────────
15
+ // The full engine surface (compiler, dialects, schema internals, …) is on the
16
+ // `./native` subpath; these are the names apps write daily. Node-only — this
17
+ // main entry is not client-bundle-reachable (the orm-side `db-bridge` is only
18
+ // imported from adapter providers).
19
+ export { Migration } from './native/schema/migration.js';
20
+ export { Schema, withSchema } from './native/schema/schema-facade.js';
21
+ export { Migrator, discoverMigrations } from './native/schema/migrator.js';
22
+ export { SchemaBuilder } from './native/schema/schema-builder.js';
23
+ export { Blueprint } from './native/schema/blueprint.js';
24
+ export { NativeAdapter, native } from './native/adapter.js';
25
+ export { BetterSqlite3Driver } from './native/drivers/better-sqlite3.js';
26
+ export { PostgresDriver } from './native/drivers/postgres.js';
27
+ export { MysqlDriver } from './native/drivers/mysql.js';
9
28
  //# 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,sDAAsD;AACtD,EAAE;AACF,8EAA8E;AAC9E,iFAAiF;AACjF,4EAA4E;AAE5E,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EACL,uBAAuB,EACvB,cAAc,EACd,yBAAyB,EACzB,wBAAwB,EACxB,0BAA0B,EAC1B,yBAAyB,EACzB,8BAA8B,EAC9B,6BAA6B,EAC7B,sBAAsB,GACvB,MAAM,sBAAsB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sDAAsD;AACtD,EAAE;AACF,4EAA4E;AAC5E,0EAA0E;AAC1E,2EAA2E;AAC3E,0EAA0E;AAC1E,wDAAwD;AACxD,4EAA4E;AAC5E,+EAA+E;AAC/E,qBAAqB;AAErB,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAE5B,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EACL,uBAAuB,EACvB,cAAc,EACd,yBAAyB,EACzB,wBAAwB,EACxB,0BAA0B,EAC1B,yBAAyB,EACzB,8BAA8B,EAC9B,6BAA6B,EAC7B,yBAAyB,EACzB,wBAAwB,EACxB,sBAAsB,GACvB,MAAM,sBAAsB,CAAA;AAK7B,8DAA8D;AAC9D,8EAA8E;AAC9E,6EAA6E;AAC7E,8EAA8E;AAC9E,oCAAoC;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AACxD,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAA;AACrE,OAAO,EAAE,QAAQ,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAA;AAC1E,OAAO,EAAE,aAAa,EAAE,MAAM,mCAAmC,CAAA;AACjE,OAAO,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAE3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,oCAAoC,CAAA;AACxE,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAA;AAC7D,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAA"}
@@ -0,0 +1,202 @@
1
+ import type { OrmAdapter, OrmAdapterProvider, QueryBuilder, OrmAdapterQueryOpts, QueryListener, TransactionOptions } from '@rudderjs/contracts';
2
+ import type { Dialect } from './dialect.js';
3
+ import type { Driver, Row } from './driver.js';
4
+ import type { ReadReplicaPicker } from './replica-picker.js';
5
+ import { SchemaBuilder } from './schema/schema-builder.js';
6
+ import type { ModelCastInfo } from './schema/schema-types.js';
7
+ /** Supported native drivers. SQLite (better-sqlite3), Postgres (porsager
8
+ * `postgres`), and MySQL (`mysql2`). */
9
+ export type NativeDriverName = 'sqlite' | 'pg' | 'mysql';
10
+ /** Config for {@link native} / {@link NativeAdapter.make}. */
11
+ export interface NativeConfig {
12
+ /**
13
+ * Pre-built {@link Driver} — skips driver setup and HMR caching. Use for
14
+ * tests or hand-wired setups (you own the driver's lifecycle).
15
+ */
16
+ driverInstance?: Driver;
17
+ /** Dialect to pair with a {@link driverInstance}. Defaults to SQLite — pass
18
+ * `new PgDialect()` when hand-wiring a Postgres driver. Ignored when a
19
+ * built-in `driver` is named (that path picks the matching dialect). */
20
+ dialect?: Dialect;
21
+ /** Which built-in driver to open. Defaults to `'sqlite'`. */
22
+ driver?: NativeDriverName;
23
+ /**
24
+ * Connection URL / path. For SQLite: a file path, `file:` URL, or `':memory:'`.
25
+ * For Postgres: a `postgres://…` connection string. Falls back to
26
+ * `DATABASE_URL`, then `:memory:` (SQLite only — a Postgres driver with no URL
27
+ * fails fast with a clear connection error).
28
+ */
29
+ url?: string;
30
+ /** Default primary-key column for models that don't override it. */
31
+ primaryKey?: string;
32
+ /**
33
+ * The `config/database.ts` connection name this adapter serves (passed by
34
+ * the provider / ConnectionManager factory). Keys the dev-HMR driver cache
35
+ * so each named connection holds its own driver and a config edit disposes
36
+ * only that connection's superseded client. Omitted for standalone use.
37
+ */
38
+ connectionName?: string;
39
+ /**
40
+ * Read-replica URLs (read/write split). When set, un-locked SELECTs (and
41
+ * `selectRaw`) round-robin across these; writes, DDL, locked selects, and
42
+ * every transaction run on the write connection (`url`). Same driver as the
43
+ * write connection.
44
+ */
45
+ readUrls?: string[];
46
+ /**
47
+ * How reads pick a replica: `'round-robin'` (default), `'random'`, a
48
+ * weights array (one non-negative weight per replica, in `readUrls` order),
49
+ * or a custom `(count) => index` function. See {@link ReadReplicaPicker}.
50
+ * Only meaningful with `readUrls`.
51
+ */
52
+ readPicker?: ReadReplicaPicker;
53
+ /**
54
+ * Sticky reads: after a write on this connection within the current request
55
+ * scope (see `@rudderjs/orm/sticky`), subsequent reads in that scope route
56
+ * to the write connection — read-your-writes under replication lag. Only
57
+ * meaningful with `readUrls`.
58
+ */
59
+ sticky?: boolean;
60
+ }
61
+ export declare class NativeAdapter implements OrmAdapter {
62
+ /** Where a (nested) transaction is opened. Top-level: the {@link Driver};
63
+ * scoped: the active {@link Transaction} (so nesting → SAVEPOINT). */
64
+ private readonly scope;
65
+ private readonly dialect;
66
+ private readonly primaryKey;
67
+ /** The owned connection — present only on the top-level adapter, `null` on
68
+ * a transaction-scoped one (it must not close the shared connection). */
69
+ private readonly driver;
70
+ /** Query listeners (`onQuery` / `DB.listen`). Shared BY REFERENCE with every
71
+ * transaction-scoped adapter spawned from this one, so a listener registered
72
+ * on the top-level adapter also sees queries run inside `transaction()`. */
73
+ private readonly listeners;
74
+ /** Connection name reported on query events — the `config/database.ts`
75
+ * connection name when the provider passed one, else the driver name
76
+ * (`'sqlite'` / `'pg'` / `'mysql'`); `undefined` for a caller-supplied
77
+ * {@link NativeConfig.driverInstance}. */
78
+ private readonly connection;
79
+ /** Read-replica drivers (read/write split). Owned by the top-level adapter
80
+ * (closed by `disconnect()`); always empty on a transaction-scoped one. */
81
+ private readonly readDrivers;
82
+ /** Sticky reads enabled for this connection (only meaningful with a split). */
83
+ private readonly sticky;
84
+ /** Where queries run — the top-level connection, or a transaction scope,
85
+ * wrapped with query-listener instrumentation (see {@link instrumentExecutor}). */
86
+ private readonly executor;
87
+ /** Read-executor picker — set only on a split connection (`readUrls`).
88
+ * Strategy from `readPicker` (round-robin default). Returns the write
89
+ * executor on a sticky hit. The QB calls it per read terminal; locked
90
+ * selects bypass it (see `_readExecutor`). */
91
+ private readonly readPick;
92
+ private constructor();
93
+ /** `'write'` when this adapter participates in a read/write split (directly
94
+ * or as a transaction scope spawned from one) — the query-event tag. */
95
+ private readonly splitTag;
96
+ /** Direct relations (`hasOne`/`hasMany`/`belongsTo`/`belongsToMany`) resolve
97
+ * in the ORM's Model layer — one batched `WHERE … IN` per relation, stitched
98
+ * onto the parents (same machinery as Drizzle and the polymorphic loader).
99
+ * The native QB's own `with()` stays a warn-only no-op: the adapter contract
100
+ * passes relation NAMES only, with no join shape to compile from. */
101
+ readonly eagerLoadStrategy: "model-layer";
102
+ /** Build a `NativeAdapter`, opening the configured driver (lazy import). */
103
+ static make(config?: NativeConfig): Promise<NativeAdapter>;
104
+ /** The adapter that owns the connection — queries and transactions both run
105
+ * on `driver`; `disconnect()` may close it. */
106
+ private static _topLevel;
107
+ query<T>(table: string, opts?: OrmAdapterQueryOpts): QueryBuilder<T>;
108
+ /**
109
+ * Raw `SELECT` for the `DB` facade (`DB.select`) — on a split connection
110
+ * routes to the read pool (sticky-aware, Laravel parity for `DB::select`);
111
+ * otherwise runs on this adapter's executor (the top-level connection, or
112
+ * the transaction scope when inside `transaction()`).
113
+ */
114
+ selectRaw(sql: string, bindings: readonly unknown[]): Promise<Row[]>;
115
+ /**
116
+ * Raw writing statement for the `DB` facade (`DB.insert`/`update`/`delete`/
117
+ * `statement`). On SQLite/Postgres the caller's `RETURNING *` makes
118
+ * `Executor.execute` resolve to the affected rows, so the count is
119
+ * `rows.length`. On MySQL (no RETURNING) the affected count comes from the
120
+ * driver's result metadata via {@link AffectingExecutor} instead.
121
+ */
122
+ affectingStatement(sql: string, bindings: readonly unknown[]): Promise<number>;
123
+ /**
124
+ * A {@link SchemaBuilder} bound to this adapter's connection — the DDL surface
125
+ * the migration runner drives. Runs on the same executor as queries, so a
126
+ * transaction-scoped adapter's schema builder participates in the transaction.
127
+ */
128
+ schemaBuilder(): SchemaBuilder;
129
+ /**
130
+ * A {@link SchemaBuilder} bound to a *recording* executor: every statement it
131
+ * would run is handed to `record` instead of touching the database. Powers
132
+ * `migrate --pretend` — the migrator binds this to a migration's `up()` and
133
+ * collects the DDL it would emit. Catalog reads (`hasTable`/`hasColumn`)
134
+ * resolve to "absent" (empty rows), which is the correct dry-run default.
135
+ */
136
+ pretendSchemaBuilder(record: (sql: string, bindings: readonly unknown[]) => void): SchemaBuilder;
137
+ /**
138
+ * Generate `app/Models/__schema/registry.d.ts` from THIS connection's live
139
+ * schema (GATE 7-types) — introspect every table, fold in each model's
140
+ * declared `casts`, and (re)write the registry. Node-only; the fs-writing
141
+ * orchestrator is lazily imported so the adapter's static eval graph stays
142
+ * import-light. Returns the written path + table count for the CLI to report.
143
+ */
144
+ generateSchemaTypes(cwd: string, models?: ModelCastInfo[]): Promise<{
145
+ path: string;
146
+ tableCount: number;
147
+ }>;
148
+ /**
149
+ * Database overview for `rudder db:show` — dialect, version, database name,
150
+ * and every user table (with sizes where the dialect can report them, row
151
+ * counts/views on request). Lazily imported like {@link generateSchemaTypes}
152
+ * to keep the adapter's static eval graph import-light.
153
+ */
154
+ inspectDatabase(opts?: {
155
+ counts?: boolean;
156
+ views?: boolean;
157
+ }): Promise<import('./schema/inspect.js').DatabaseInfo>;
158
+ /**
159
+ * Single-table detail for `rudder db:table <name>` — columns, indexes,
160
+ * foreign keys, row count. Null when the table doesn't exist.
161
+ */
162
+ inspectTable(table: string): Promise<import('./schema/inspect.js').TableInfo | null>;
163
+ /**
164
+ * Run `fn` inside a transaction (or a SAVEPOINT when already inside one),
165
+ * passing it a transaction-scoped `NativeAdapter` whose queries execute on the
166
+ * transaction's connection. The Model layer threads that scoped adapter
167
+ * through `AsyncLocalStorage` so existing `Model.query()` calls inside `fn`
168
+ * transparently join the transaction. Commits on resolve; rolls back and
169
+ * re-throws on reject.
170
+ *
171
+ * `opts.isolationLevel` is forwarded to the driver: SET TRANSACTION ISOLATION
172
+ * LEVEL at transaction start on Postgres/MySQL; SQLite throws (no isolation
173
+ * levels); any driver throws when this scope is already a SAVEPOINT.
174
+ */
175
+ transaction<T>(fn: (tx: OrmAdapter) => Promise<T>, opts?: TransactionOptions): Promise<T>;
176
+ /**
177
+ * Register a query listener ({@link OrmAdapter.onQuery}) — fired once per
178
+ * successfully executed query with the SQL, bindings, and wall-clock duration
179
+ * in ms. The app-facing entry point is `DB.listen()` (`@rudderjs/database`);
180
+ * Telescope's QueryCollector and Pulse's slow-query recorder hook in here too.
181
+ * Listener errors are swallowed — they never break the query. Registering on
182
+ * a transaction-scoped adapter registers on the shared (top-level) listener
183
+ * list. Transaction control statements (BEGIN/COMMIT/SAVEPOINT) run inside
184
+ * the driver and are not reported.
185
+ */
186
+ onQuery(listener: QueryListener): void;
187
+ connect(): Promise<void>;
188
+ disconnect(): Promise<void>;
189
+ }
190
+ /**
191
+ * Build the native ORM adapter provider — the `OrmAdapterProvider` shape the
192
+ * database provider consumes (mirrors `drizzle(...)` / the prisma factory).
193
+ *
194
+ * @example
195
+ * import { native } from '@rudderjs/database'
196
+ * database({
197
+ * default: 'main',
198
+ * connections: { main: native({ driver: 'sqlite', url: 'file:./dev.db' }) },
199
+ * })
200
+ */
201
+ export declare function native(config?: NativeConfig): OrmAdapterProvider;
202
+ //# sourceMappingURL=adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../src/native/adapter.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,UAAU,EAAE,kBAAkB,EAAE,YAAY,EAAE,mBAAmB,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAA;AAC/I,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAI3C,OAAO,KAAK,EAAE,MAAM,EAAyB,GAAG,EAAqB,MAAM,aAAa,CAAA;AAExF,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AAM5D,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AAE7D;yCACyC;AACzC,MAAM,MAAM,gBAAgB,GAAG,QAAQ,GAAG,IAAI,GAAG,OAAO,CAAA;AAExD,8DAA8D;AAC9D,MAAM,WAAW,YAAY;IAC3B;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB;;6EAEyE;IACzE,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,6DAA6D;IAC7D,MAAM,CAAC,EAAE,gBAAgB,CAAA;IACzB;;;;;OAKG;IACH,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,oEAAoE;IACpE,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAA;IACnB;;;;;OAKG;IACH,UAAU,CAAC,EAAE,iBAAiB,CA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