@arki/db 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.
- package/LICENSE +21 -0
- package/README.md +113 -0
- package/dist/builder.d.ts +119 -0
- package/dist/builder.d.ts.map +1 -0
- package/dist/builder.js +165 -0
- package/dist/builder.js.map +1 -0
- package/dist/bun.d.ts +22 -0
- package/dist/bun.d.ts.map +1 -0
- package/dist/bun.js +37 -0
- package/dist/bun.js.map +1 -0
- package/dist/client/ids.d.ts +12 -0
- package/dist/client/ids.d.ts.map +1 -0
- package/dist/client/ids.js +2 -0
- package/dist/client/ids.js.map +1 -0
- package/dist/client/index.d.ts +12 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +2 -0
- package/dist/client/index.js.map +1 -0
- package/dist/connection-options.d.ts +30 -0
- package/dist/connection-options.d.ts.map +1 -0
- package/dist/connection-options.js +41 -0
- package/dist/connection-options.js.map +1 -0
- package/dist/debug.d.ts +21 -0
- package/dist/debug.d.ts.map +1 -0
- package/dist/debug.js +21 -0
- package/dist/debug.js.map +1 -0
- package/dist/dot.d.ts +87 -0
- package/dist/dot.d.ts.map +1 -0
- package/dist/dot.js +104 -0
- package/dist/dot.js.map +1 -0
- package/dist/env.d.ts +23 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/env.js +77 -0
- package/dist/env.js.map +1 -0
- package/dist/factory.d.ts +18 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +31 -0
- package/dist/factory.js.map +1 -0
- package/dist/id-factory.d.ts +85 -0
- package/dist/id-factory.d.ts.map +1 -0
- package/dist/id-factory.js +119 -0
- package/dist/id-factory.js.map +1 -0
- package/dist/id.d.ts +4 -0
- package/dist/id.d.ts.map +1 -0
- package/dist/id.js +10 -0
- package/dist/id.js.map +1 -0
- package/dist/init.bun.d.ts +25 -0
- package/dist/init.bun.d.ts.map +1 -0
- package/dist/init.bun.js +29 -0
- package/dist/init.bun.js.map +1 -0
- package/dist/init.d.ts +16 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +24 -0
- package/dist/init.js.map +1 -0
- package/dist/orm.d.ts +3 -0
- package/dist/orm.d.ts.map +1 -0
- package/dist/orm.js +5 -0
- package/dist/orm.js.map +1 -0
- package/dist/pg.d.ts +2 -0
- package/dist/pg.d.ts.map +1 -0
- package/dist/pg.js +2 -0
- package/dist/pg.js.map +1 -0
- package/dist/runtime-local.d.ts +30 -0
- package/dist/runtime-local.d.ts.map +1 -0
- package/dist/runtime-local.js +31 -0
- package/dist/runtime-local.js.map +1 -0
- package/package.json +128 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ARKI Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# @arki/db
|
|
2
|
+
|
|
3
|
+
Drizzle ORM repositories and database helpers for ARKI. Provides:
|
|
4
|
+
|
|
5
|
+
- Two runtime entry points (`node-postgres` pool by default, Bun-native SQL via the `bun` export condition, embedded PGlite via `./runtime-local`).
|
|
6
|
+
- An env-aware connection pool reader (`getDbConnectionOptions`).
|
|
7
|
+
- A typed projection builder (`initDb.schema(...).create(...).projection`) for event-sourced read models.
|
|
8
|
+
- A branded-ID factory (`createPrefixedId`) for stable, type-safe IDs.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install @arki/db
|
|
14
|
+
# or
|
|
15
|
+
bun add @arki/db
|
|
16
|
+
# or
|
|
17
|
+
pnpm add @arki/db
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### Initialize a database from environment
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { createDb } from '@arki/db/init';
|
|
26
|
+
import { defineRelations } from '@arki/db/orm';
|
|
27
|
+
import { schema } from './schema';
|
|
28
|
+
|
|
29
|
+
export const db = createDb({
|
|
30
|
+
relations: defineRelations(schema, builder => ({
|
|
31
|
+
user: { posts: builder.many('post') },
|
|
32
|
+
post: { author: builder.one('user') },
|
|
33
|
+
})),
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`createDb` reads `DB_URL` and the pool tuning vars (`DB_POOL_MIN`, `DB_POOL_MAX`, `DB_POOL_IDLE_TIMEOUT`, `DB_POOL_CONNECTION_TIMEOUT`, `DB_STATEMENT_TIMEOUT`, …) from `process.env` and throws on missing `DB_URL`.
|
|
38
|
+
|
|
39
|
+
### Embedded local development (PGlite)
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { initDbRuntimeLocal } from '@arki/db/runtime-local';
|
|
43
|
+
|
|
44
|
+
const { db, pglite } = await initDbRuntimeLocal(
|
|
45
|
+
{ dataDir: './.local/db' },
|
|
46
|
+
{ relations },
|
|
47
|
+
);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Projections (event sourcing read models)
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { initDb } from '@arki/db/builder';
|
|
54
|
+
|
|
55
|
+
const d = initDb
|
|
56
|
+
.schema(schema)
|
|
57
|
+
.create({ connectionString: env.DB_URL, isDev: true });
|
|
58
|
+
|
|
59
|
+
const userProjection = d.projection
|
|
60
|
+
.named('user-projection')
|
|
61
|
+
.on(['UserRegistered'])
|
|
62
|
+
.handle(async (events, { db, repo: _repo }) => {
|
|
63
|
+
// ...
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Branded IDs
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { createPrefixedId, createIdFactory } from '@arki/db/id';
|
|
71
|
+
|
|
72
|
+
const userId = createPrefixedId('usr_'); // 'usr_…'
|
|
73
|
+
const idFactory = createIdFactory('post_');
|
|
74
|
+
const postId = idFactory.next();
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Subpath exports
|
|
78
|
+
|
|
79
|
+
- `@arki/db` / `@arki/db/init` — `createDb`, `initDb`, `initDbWithOptions`. Under Bun, `./init` resolves to a Bun-native SQL driver.
|
|
80
|
+
- `@arki/db/factory` — `createDb` + connection-option helpers.
|
|
81
|
+
- `@arki/db/builder` — typed projection builder.
|
|
82
|
+
- `@arki/db/runtime-local` — PGlite-backed embedded runtime.
|
|
83
|
+
- `@arki/db/env` — validated env object.
|
|
84
|
+
- `@arki/db/id`, `@arki/db/id-factory` — CUID-based prefixed-ID helpers.
|
|
85
|
+
- `@arki/db/orm` — re-exports of `drizzle-orm` + `drizzle-orm/zod` schema helpers.
|
|
86
|
+
- `@arki/db/pg` — re-export of `drizzle-orm/pg-core`.
|
|
87
|
+
- `@arki/db/bun` — Bun-SQL driver entry (also routed via the `bun` condition on `./init`).
|
|
88
|
+
- `@arki/db/client` — types-only client surface reserved for future Tier-3 sync work.
|
|
89
|
+
|
|
90
|
+
## Environment variables
|
|
91
|
+
|
|
92
|
+
| Variable | Purpose |
|
|
93
|
+
| --------------------------------------- | ------------------------------------------------------------- |
|
|
94
|
+
| `DB_URL` | Primary Postgres connection URL. |
|
|
95
|
+
| `PGHOST`, `PGPORT`, `PGUSER`, … | Individual params when `DB_URL` is not used. |
|
|
96
|
+
| `DB_POOL_MIN`, `DB_POOL_MAX` | Connection pool sizing (default `2` / `20`). |
|
|
97
|
+
| `DB_POOL_IDLE_TIMEOUT` | Idle pool timeout in ms (default `30000`). |
|
|
98
|
+
| `DB_POOL_CONNECTION_TIMEOUT` | Acquire timeout in ms (default `2000`). |
|
|
99
|
+
| `DB_LOGGING` | Enable Drizzle logger when truthy. |
|
|
100
|
+
| `DB_STATEMENT_TIMEOUT`, `DB_QUERY_TIMEOUT` | Per-statement / per-query timeouts in ms. |
|
|
101
|
+
|
|
102
|
+
## Documentation
|
|
103
|
+
|
|
104
|
+
`@arki/db` ships an optional `@arki/db/dot` adapter for the
|
|
105
|
+
[`@arki/dot`](https://www.npmjs.com/package/@arki/dot) framework.
|
|
106
|
+
|
|
107
|
+
- See `packages/dot/docs/` in the [`@arki/dot`](https://www.npmjs.com/package/@arki/dot)
|
|
108
|
+
package for plugin authoring, lifecycle, diagnostics, and the
|
|
109
|
+
[adapter authoring guide](https://github.com/arkijs/arki/blob/main/packages/dot/docs/adapter-authoring.md).
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
MIT — see [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { AnyRelations } from 'drizzle-orm';
|
|
2
|
+
import { defineRelations } from 'drizzle-orm';
|
|
3
|
+
import type { NodePgDatabase } from 'drizzle-orm/node-postgres';
|
|
4
|
+
import type { PoolConfig } from 'pg';
|
|
5
|
+
/**
|
|
6
|
+
* Projection middleware context
|
|
7
|
+
*/
|
|
8
|
+
export type ProjectionMiddlewareContext = {
|
|
9
|
+
next: () => Promise<void>;
|
|
10
|
+
meta: {
|
|
11
|
+
name: string;
|
|
12
|
+
eventCount: number;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Projection middleware function
|
|
17
|
+
*/
|
|
18
|
+
export type ProjectionMiddleware = (context: ProjectionMiddlewareContext) => Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Projection handler context
|
|
21
|
+
*/
|
|
22
|
+
export type ProjectionHandlerContext<TRepos, TRelations extends AnyRelations = AnyRelations> = {
|
|
23
|
+
repo: TRepos;
|
|
24
|
+
db: NodePgDatabase<TRelations>;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Projection handler function
|
|
28
|
+
*/
|
|
29
|
+
export type ProjectionHandler<TEvent, TRepos, TRelations extends AnyRelations = AnyRelations> = (events: TEvent[], context: ProjectionHandlerContext<TRepos, TRelations>) => Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Projection builder interface
|
|
32
|
+
*/
|
|
33
|
+
export type ProjectionBuilder<TEvent = never, TRepos = never, TRelations extends AnyRelations = AnyRelations> = {
|
|
34
|
+
named<TName extends string>(name: TName): ProjectionBuilderWithName<TEvent, TRepos, TRelations, TName>;
|
|
35
|
+
};
|
|
36
|
+
export type ProjectionBuilderWithName<_TEvent, TRepos, TRelations extends AnyRelations, TName extends string> = {
|
|
37
|
+
on<TEventType extends {
|
|
38
|
+
type: string;
|
|
39
|
+
}>(eventTypes: readonly TEventType['type'][] | TEventType['type'][]): ProjectionBuilderWithEvents<TEventType, TRepos, TRelations, TName>;
|
|
40
|
+
};
|
|
41
|
+
export type ProjectionBuilderWithEvents<TEvent, TRepos, TRelations extends AnyRelations, TName extends string> = {
|
|
42
|
+
handle(handler: ProjectionHandler<TEvent, TRepos, TRelations>): ProjectionDefinition<TName>;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Emmett-compatible projection definition.
|
|
46
|
+
*/
|
|
47
|
+
export type ProjectionDefinition<TName extends string = string> = {
|
|
48
|
+
name: TName;
|
|
49
|
+
canHandle: string[];
|
|
50
|
+
handle: (events: {
|
|
51
|
+
type: string;
|
|
52
|
+
}[]) => Promise<void>;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Database builder with projection support
|
|
56
|
+
*/
|
|
57
|
+
export type DbBuilder<TRelations extends AnyRelations, TRepos extends Record<string, unknown>> = {
|
|
58
|
+
readonly db: NodePgDatabase<TRelations>;
|
|
59
|
+
readonly isDev: boolean;
|
|
60
|
+
registerRepository<K extends string, R>(key: K, factory: (db: NodePgDatabase<TRelations>) => R): DbBuilder<TRelations, TRepos & Record<K, R>>;
|
|
61
|
+
projectionMiddleware(fn: ProjectionMiddleware): ProjectionMiddleware;
|
|
62
|
+
readonly projection: ProjectionBuilderRoot<TRepos, TRelations>;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Projection builder root that can have middleware applied
|
|
66
|
+
*/
|
|
67
|
+
export type ProjectionBuilderRoot<TRepos, TRelations extends AnyRelations = AnyRelations> = {
|
|
68
|
+
use(middleware: ProjectionMiddleware): ProjectionBuilderRoot<TRepos, TRelations>;
|
|
69
|
+
} & ProjectionBuilder<never, TRepos, TRelations>;
|
|
70
|
+
/**
|
|
71
|
+
* Schema configuration step
|
|
72
|
+
*/
|
|
73
|
+
export type SchemaBuilder<TRelations extends AnyRelations> = {
|
|
74
|
+
create(options: {
|
|
75
|
+
connectionString: string;
|
|
76
|
+
isDev?: boolean;
|
|
77
|
+
poolConfig?: Partial<PoolConfig>;
|
|
78
|
+
}): DbBuilder<TRelations, Record<string, never>>;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Initial builder step
|
|
82
|
+
*/
|
|
83
|
+
export type InitialBuilder = {
|
|
84
|
+
/**
|
|
85
|
+
* Set the database tables. Internally builds a relations config via
|
|
86
|
+
* `defineRelations()` so consumers keep passing a plain table record.
|
|
87
|
+
*
|
|
88
|
+
* Use this for backends that don't define explicit relations and rely on
|
|
89
|
+
* auto-derivation (no joins). For explicit RQBv2 relations, prefer
|
|
90
|
+
* `.relations(...)` with a `defineRelations(tables, cb)` result.
|
|
91
|
+
*/
|
|
92
|
+
schema<TSchema extends Record<string, unknown>>(schema: TSchema): SchemaBuilder<ReturnType<typeof defineRelations<TSchema>>>;
|
|
93
|
+
/**
|
|
94
|
+
* Pass a pre-built relations object (the result of
|
|
95
|
+
* `defineRelations(tables, cb)`) so the database knows about every table
|
|
96
|
+
* AND the explicit one/many relations declared by the application.
|
|
97
|
+
*/
|
|
98
|
+
relations<TRelations extends AnyRelations>(relations: TRelations): SchemaBuilder<TRelations>;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Initialize the database builder.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* const d = initDb
|
|
106
|
+
* .schema(schema)
|
|
107
|
+
* .create({
|
|
108
|
+
* connectionString: env.DB_URL,
|
|
109
|
+
* isDev: process.env.NODE_ENV === 'development',
|
|
110
|
+
* });
|
|
111
|
+
*
|
|
112
|
+
* const db = d.db;
|
|
113
|
+
* const projection = d.projection.named('my-proj').on(['EventA']).handle(async (events, ctx) => {
|
|
114
|
+
* // handler
|
|
115
|
+
* });
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
export declare const initDb: InitialBuilder;
|
|
119
|
+
//# sourceMappingURL=builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../src/builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C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{ initDbWithOptions } from './init.js';
|
|
4
|
+
/**
|
|
5
|
+
* Applies a middleware chain around a core handler function.
|
|
6
|
+
*/
|
|
7
|
+
function applyMiddleware(middleware, name, eventCount, coreHandler) {
|
|
8
|
+
const meta = { name, eventCount };
|
|
9
|
+
let next = coreHandler;
|
|
10
|
+
for (let i = middleware.length - 1; i >= 0; i--) {
|
|
11
|
+
const mw = middleware[i];
|
|
12
|
+
const currentNext = next;
|
|
13
|
+
next = () => mw({ next: currentNext, meta });
|
|
14
|
+
}
|
|
15
|
+
return next();
|
|
16
|
+
}
|
|
17
|
+
class ProjectionBuilderImpl {
|
|
18
|
+
repos;
|
|
19
|
+
_db;
|
|
20
|
+
_name;
|
|
21
|
+
_eventTypes;
|
|
22
|
+
_middleware = [];
|
|
23
|
+
constructor(repos, _db, middleware) {
|
|
24
|
+
this.repos = repos;
|
|
25
|
+
this._db = _db;
|
|
26
|
+
if (middleware) {
|
|
27
|
+
this._middleware = [...middleware];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
named(name) {
|
|
31
|
+
debugProjection('[builder] Setting projection name: %s', name);
|
|
32
|
+
this._name = name;
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
on(eventTypes) {
|
|
36
|
+
debugProjection('[builder] Registering event types: %o', eventTypes);
|
|
37
|
+
this._eventTypes = [...eventTypes];
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
handle(handler) {
|
|
41
|
+
if (!this._name) {
|
|
42
|
+
debugProjection('[builder] Error: Projection name is required');
|
|
43
|
+
throw new Error('Projection name is required. Call .named() first.');
|
|
44
|
+
}
|
|
45
|
+
if (!this._eventTypes) {
|
|
46
|
+
debugProjection('[builder] Error: Event types are required');
|
|
47
|
+
throw new Error('Event types are required. Call .on() first.');
|
|
48
|
+
}
|
|
49
|
+
const name = this._name;
|
|
50
|
+
const eventTypes = this._eventTypes;
|
|
51
|
+
const middleware = this._middleware;
|
|
52
|
+
const repos = this.repos;
|
|
53
|
+
const db = this._db;
|
|
54
|
+
debugProjection('[builder] Creating projection definition (name: %s, eventTypes: %o, middlewareCount: %d)', name, eventTypes, middleware.length);
|
|
55
|
+
return {
|
|
56
|
+
name,
|
|
57
|
+
canHandle: eventTypes,
|
|
58
|
+
handle: async (events) => {
|
|
59
|
+
const context = { repo: repos, db };
|
|
60
|
+
const coreHandler = () => handler(events, context);
|
|
61
|
+
if (middleware.length > 0) {
|
|
62
|
+
await applyMiddleware(middleware, name, events.length, coreHandler);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
await coreHandler();
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
use(middleware) {
|
|
71
|
+
debugProjection('[builder] Adding projection middleware (currentCount: %d)', this._middleware.length);
|
|
72
|
+
const newBuilder = new ProjectionBuilderImpl(this.repos, this._db, [
|
|
73
|
+
...this._middleware,
|
|
74
|
+
middleware,
|
|
75
|
+
]);
|
|
76
|
+
debugProjection('[builder] Middleware added (newCount: %d)', this._middleware.length + 1);
|
|
77
|
+
return newBuilder;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
class DbBuilderImpl {
|
|
81
|
+
db;
|
|
82
|
+
isDev;
|
|
83
|
+
_repos;
|
|
84
|
+
_repoFactories;
|
|
85
|
+
constructor(db, isDev, repoFactories) {
|
|
86
|
+
this.db = db;
|
|
87
|
+
this.isDev = isDev;
|
|
88
|
+
this._repoFactories = repoFactories ?? new Map();
|
|
89
|
+
}
|
|
90
|
+
registerRepository(key, factory) {
|
|
91
|
+
debugProjection('[builder] Registering repository (key: %s, currentCount: %d)', key, this._repoFactories.size);
|
|
92
|
+
const newFactories = new Map(this._repoFactories);
|
|
93
|
+
newFactories.set(key, factory);
|
|
94
|
+
debugProjection('[builder] Repository registered (key: %s, newCount: %d)', key, newFactories.size);
|
|
95
|
+
return new DbBuilderImpl(this.db, this.isDev, newFactories);
|
|
96
|
+
}
|
|
97
|
+
projectionMiddleware(fn) {
|
|
98
|
+
return fn;
|
|
99
|
+
}
|
|
100
|
+
get projection() {
|
|
101
|
+
if (!this._repos) {
|
|
102
|
+
debugProjection('[builder] Initializing repositories from factories (count: %d)', this._repoFactories.size);
|
|
103
|
+
const repos = {};
|
|
104
|
+
for (const [key, factory] of this._repoFactories) {
|
|
105
|
+
debugProjection('[builder] Creating repository: %s', key);
|
|
106
|
+
repos[key] = factory(this.db);
|
|
107
|
+
}
|
|
108
|
+
this._repos = repos;
|
|
109
|
+
debugProjection('[builder] All repositories initialized');
|
|
110
|
+
}
|
|
111
|
+
debugProjection('[builder] Creating projection builder');
|
|
112
|
+
return new ProjectionBuilderImpl(this._repos, this.db);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
class SchemaBuilderImpl {
|
|
116
|
+
relations;
|
|
117
|
+
constructor(relations) {
|
|
118
|
+
this.relations = relations;
|
|
119
|
+
}
|
|
120
|
+
create(options) {
|
|
121
|
+
debugProjection('[builder] Creating database builder (isDev: %s, hasPoolConfig: %s)', options.isDev ?? false, !!options.poolConfig);
|
|
122
|
+
const poolConfig = {
|
|
123
|
+
connectionString: options.connectionString,
|
|
124
|
+
...options.poolConfig,
|
|
125
|
+
};
|
|
126
|
+
debugProjection('[builder] Initializing database with pool config');
|
|
127
|
+
const db = initDbWithOptions(poolConfig, {
|
|
128
|
+
relations: this.relations,
|
|
129
|
+
logger: options.isDev,
|
|
130
|
+
});
|
|
131
|
+
debugProjection('[builder] Database builder created successfully');
|
|
132
|
+
return new DbBuilderImpl(db, options.isDev ?? false);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
class InitialBuilderImpl {
|
|
136
|
+
schema(schema) {
|
|
137
|
+
debugProjection('[builder] Setting database schema (deriving relations via defineRelations)');
|
|
138
|
+
const relations = defineRelations(schema);
|
|
139
|
+
return new SchemaBuilderImpl(relations);
|
|
140
|
+
}
|
|
141
|
+
relations(relations) {
|
|
142
|
+
debugProjection('[builder] Setting database relations (explicit RQBv2)');
|
|
143
|
+
return new SchemaBuilderImpl(relations);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Initialize the database builder.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* const d = initDb
|
|
152
|
+
* .schema(schema)
|
|
153
|
+
* .create({
|
|
154
|
+
* connectionString: env.DB_URL,
|
|
155
|
+
* isDev: process.env.NODE_ENV === 'development',
|
|
156
|
+
* });
|
|
157
|
+
*
|
|
158
|
+
* const db = d.db;
|
|
159
|
+
* const projection = d.projection.named('my-proj').on(['EventA']).handle(async (events, ctx) => {
|
|
160
|
+
* // handler
|
|
161
|
+
* });
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
export const initDb = new InitialBuilderImpl();
|
|
165
|
+
//# sourceMappingURL=builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.js","sourceRoot":"","sources":["../src/builder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAI9C,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAqI9C;;GAEG;AACH,SAAS,eAAe,CACtB,UAAkC,EAClC,IAAY,EACZ,UAAkB,EAClB,WAAgC;IAEhC,MAAM,IAAI,GAAG,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC;IAElC,IAAI,IAAI,GAAG,WAAW,CAAC;IACvB,KAAK,IAAI,CAAC,GAAG,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAChD,MAAM,EAAE,GAAG,UAAU,CAAC,CAAC,CAAE,CAAC;QAC1B,MAAM,WAAW,GAAG,IAAI,CAAC;QACzB,IAAI,GAAG,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,IAA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SQLDatabase } from 'drizzle-orm/bun-sql/postgres';
|
|
3
|
+
import type { DrizzlePgConfig } from 'drizzle-orm/pg-core/utils';
|
|
4
|
+
/**
|
|
5
|
+
* Initialize database with Bun's native SQL driver.
|
|
6
|
+
* @param connectionString - PostgreSQL connection string.
|
|
7
|
+
* @param config - Drizzle pg configuration.
|
|
8
|
+
*/
|
|
9
|
+
export declare const initDb: <TRelations extends AnyRelations>(connectionString: string, config: Omit<DrizzlePgConfig<TRelations>, "relations"> & {
|
|
10
|
+
relations: TRelations;
|
|
11
|
+
}) => BunSQLDatabase<TRelations>;
|
|
12
|
+
/**
|
|
13
|
+
* Create a database connection using environment configuration with Bun SQL.
|
|
14
|
+
* @param config - Drizzle pg configuration.
|
|
15
|
+
* @throws Error if DB_URL is not configured.
|
|
16
|
+
*/
|
|
17
|
+
export declare function createDb<TRelations extends AnyRelations>(config: Omit<DrizzlePgConfig<TRelations>, 'relations'> & {
|
|
18
|
+
relations: TRelations;
|
|
19
|
+
}): BunSQLDatabase<TRelations>;
|
|
20
|
+
export { getDbConnectionOptions, getDbConnectionParams } from './connection-options.js';
|
|
21
|
+
export type { BunSQLDatabase } from 'drizzle-orm/bun-sql/postgres';
|
|
22
|
+
//# sourceMappingURL=bun.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bun.d.ts","sourceRoot":"","sources":["../src/bun.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAEnE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAKjE;;;;GAIG;AACH,eAAO,MAAM,MAAM,GAAI,UAAU,SAAS,YAAY,EACpD,kBAAkB,MAAM,EACxB,QAAQ,IAAI,CAAC,eAAe,CAAC,UAAU,CAAC,EAAE,WAAW,CAAC,GAAG;IAAE,SAAS,EAAE,UAAU,CAAA;CAAE,KACjF,cAAc,CAAC,UAAU,CAU3B,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,UAAU,SAAS,YAAY,EACtD,MAAM,EAAE,IAAI,CAAC,eAAe,CAAC,UAAU,CAAC,EAAE,WAAW,CAAC,GAAG;IAAE,SAAS,EAAE,UAAU,CAAA;CAAE,GACjF,cAAc,CAAC,UAAU,CAAC,CAiB5B;AAED,OAAO,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAExF,YAAY,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC"}
|
package/dist/bun.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { SQL } from 'bun';
|
|
2
|
+
import { drizzle } from 'drizzle-orm/bun-sql/postgres';
|
|
3
|
+
import { debugInit } from './debug.js';
|
|
4
|
+
import { env } from './env.js';
|
|
5
|
+
/**
|
|
6
|
+
* Initialize database with Bun's native SQL driver.
|
|
7
|
+
* @param connectionString - PostgreSQL connection string.
|
|
8
|
+
* @param config - Drizzle pg configuration.
|
|
9
|
+
*/
|
|
10
|
+
export const initDb = (connectionString, config) => {
|
|
11
|
+
debugInit('[bun] Initializing database with connection string (logger: %s)', !!config.logger);
|
|
12
|
+
const client = new SQL(connectionString);
|
|
13
|
+
const db = drizzle({ ...config, client });
|
|
14
|
+
debugInit('[bun] Database initialized successfully with Bun SQL driver');
|
|
15
|
+
return db;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Create a database connection using environment configuration with Bun SQL.
|
|
19
|
+
* @param config - Drizzle pg configuration.
|
|
20
|
+
* @throws Error if DB_URL is not configured.
|
|
21
|
+
*/
|
|
22
|
+
export function createDb(config) {
|
|
23
|
+
debugInit('[bun] Creating database from environment configuration');
|
|
24
|
+
const connectionUrl = env.DB_URL;
|
|
25
|
+
if (!connectionUrl) {
|
|
26
|
+
debugInit('[bun] Database creation failed: DB_URL not configured');
|
|
27
|
+
throw new Error('Database URL is not configured. Please set DB_URL environment variable.');
|
|
28
|
+
}
|
|
29
|
+
const db = initDb(connectionUrl, {
|
|
30
|
+
...config,
|
|
31
|
+
logger: config.logger ?? (env.DB_LOGGING || env.NODE_ENV === 'development'),
|
|
32
|
+
});
|
|
33
|
+
debugInit('[bun] Database created successfully');
|
|
34
|
+
return db;
|
|
35
|
+
}
|
|
36
|
+
export { getDbConnectionOptions, getDbConnectionParams } from './connection-options.js';
|
|
37
|
+
//# sourceMappingURL=bun.js.map
|
package/dist/bun.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bun.js","sourceRoot":"","sources":["../src/bun.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,KAAK,CAAC;AAG1B,OAAO,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAC;AAGvD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B;;;;GAIG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CACpB,gBAAwB,EACxB,MAAkF,EACtD,EAAE;IAC9B,SAAS,CAAC,iEAAiE,EAAE,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAE9F,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,gBAAgB,CAAC,CAAC;IAEzC,MAAM,EAAE,GAAG,OAAO,CAAa,EAAE,GAAG,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IAEtD,SAAS,CAAC,6DAA6D,CAAC,CAAC;IAEzE,OAAO,EAAE,CAAC;AACZ,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CACtB,MAAkF;IAElF,SAAS,CAAC,wDAAwD,CAAC,CAAC;IAEpE,MAAM,aAAa,GAAG,GAAG,CAAC,MAAM,CAAC;IACjC,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,SAAS,CAAC,uDAAuD,CAAC,CAAC;QACnE,MAAM,IAAI,KAAK,CAAC,yEAAyE,CAAC,CAAC;IAC7F,CAAC;IAED,MAAM,EAAE,GAAG,MAAM,CAAC,aAAa,EAAE;QAC/B,GAAG,MAAM;QACT,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,UAAU,IAAI,GAAG,CAAC,QAAQ,KAAK,aAAa,CAAC;KAC5E,CAAC,CAAC;IAEH,SAAS,CAAC,qCAAqC,CAAC,CAAC;IAEjD,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,OAAO,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Branded ID type re-exports for the client surface.
|
|
3
|
+
*
|
|
4
|
+
* V1: @arki/contracts does not yet export named branded ID types.
|
|
5
|
+
* Future versions will re-export them here once they are defined, e.g.:
|
|
6
|
+
*
|
|
7
|
+
* export type { UserId, OrgId, AssetId } from '@arki/contracts';
|
|
8
|
+
*
|
|
9
|
+
* See docs/superpowers/specs/2026-05-16-arki-offline-design.md §3.3.
|
|
10
|
+
*/
|
|
11
|
+
export type {};
|
|
12
|
+
//# sourceMappingURL=ids.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ids.d.ts","sourceRoot":"","sources":["../../src/client/ids.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,YAAY,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ids.js","sourceRoot":"","sources":["../../src/client/ids.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @arki/db/client — types-only client surface.
|
|
3
|
+
*
|
|
4
|
+
* V1 is intentionally empty: this subpath claims the namespace for future
|
|
5
|
+
* Drizzle-on-client / Tier-3 sync work (Electric / PowerSync / Zero adapters).
|
|
6
|
+
* No runtime imports are allowed here — this file must remain free of any
|
|
7
|
+
* server-only dependencies (postgres, drizzle-orm connection utilities, etc.).
|
|
8
|
+
*
|
|
9
|
+
* See docs/superpowers/specs/2026-05-16-arki-offline-design.md §3.3.
|
|
10
|
+
*/
|
|
11
|
+
export type * from './ids.js';
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,mBAAmB,UAAU,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get database connection options from environment variables
|
|
3
|
+
* @returns Connection options object for pg Pool
|
|
4
|
+
*/
|
|
5
|
+
export declare function getDbConnectionOptions(): {
|
|
6
|
+
connectionString: string;
|
|
7
|
+
min: number;
|
|
8
|
+
max: number;
|
|
9
|
+
idleTimeoutMillis: number;
|
|
10
|
+
connectionTimeoutMillis: number;
|
|
11
|
+
statement_timeout: number;
|
|
12
|
+
query_timeout: number;
|
|
13
|
+
idle_in_transaction_session_timeout: number;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Get individual database connection parameters from environment variables
|
|
17
|
+
* Useful when DB_URL is not available but individual parameters are set
|
|
18
|
+
* @returns Connection parameters object
|
|
19
|
+
*/
|
|
20
|
+
export declare function getDbConnectionParams(): {
|
|
21
|
+
user: string | undefined;
|
|
22
|
+
password: string | undefined;
|
|
23
|
+
host: string;
|
|
24
|
+
port: number;
|
|
25
|
+
database: string | undefined;
|
|
26
|
+
ssl: boolean | {
|
|
27
|
+
rejectUnauthorized: boolean;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=connection-options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection-options.d.ts","sourceRoot":"","sources":["../src/connection-options.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,wBAAgB,sBAAsB;;;;;;;;;EAkBrC;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB;;;;;;;;;EAgBpC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { debugFactory } from './debug.js';
|
|
2
|
+
import { env } from './env.js';
|
|
3
|
+
/**
|
|
4
|
+
* Get database connection options from environment variables
|
|
5
|
+
* @returns Connection options object for pg Pool
|
|
6
|
+
*/
|
|
7
|
+
export function getDbConnectionOptions() {
|
|
8
|
+
debugFactory('[factory] Retrieving database connection options from environment');
|
|
9
|
+
const connectionUrl = env.DB_URL;
|
|
10
|
+
const options = {
|
|
11
|
+
connectionString: connectionUrl,
|
|
12
|
+
min: env.DB_POOL_MIN,
|
|
13
|
+
max: env.DB_POOL_MAX,
|
|
14
|
+
idleTimeoutMillis: env.DB_POOL_IDLE_TIMEOUT,
|
|
15
|
+
connectionTimeoutMillis: env.DB_POOL_CONNECTION_TIMEOUT,
|
|
16
|
+
statement_timeout: env.DB_STATEMENT_TIMEOUT,
|
|
17
|
+
query_timeout: env.DB_QUERY_TIMEOUT,
|
|
18
|
+
idle_in_transaction_session_timeout: env.DB_IDLE_IN_TRANSACTION_SESSION_TIMEOUT,
|
|
19
|
+
};
|
|
20
|
+
debugFactory('[factory] Connection options retrieved (hasUrl: %s)', !!connectionUrl);
|
|
21
|
+
return options;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Get individual database connection parameters from environment variables
|
|
25
|
+
* Useful when DB_URL is not available but individual parameters are set
|
|
26
|
+
* @returns Connection parameters object
|
|
27
|
+
*/
|
|
28
|
+
export function getDbConnectionParams() {
|
|
29
|
+
debugFactory('[factory] Retrieving database connection parameters from environment');
|
|
30
|
+
const params = {
|
|
31
|
+
user: env.PGUSER,
|
|
32
|
+
password: env.PGPASSWORD,
|
|
33
|
+
host: env.PGHOST,
|
|
34
|
+
port: env.PGPORT,
|
|
35
|
+
database: env.PGDATABASE,
|
|
36
|
+
ssl: env.PGSSLMODE === 'disable' ? false : { rejectUnauthorized: env.PGSSLMODE === 'verify-full' },
|
|
37
|
+
};
|
|
38
|
+
debugFactory('[factory] Connection parameters retrieved (host: %s, port: %d, database: %s, ssl: %s)', params.host, params.port, params.database, env.PGSSLMODE);
|
|
39
|
+
return params;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=connection-options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection-options.js","sourceRoot":"","sources":["../src/connection-options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC1C,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B;;;GAGG;AACH,MAAM,UAAU,sBAAsB;IACpC,YAAY,CAAC,mEAAmE,CAAC,CAAC;IAElF,MAAM,aAAa,GAAG,GAAG,CAAC,MAAM,CAAC;IACjC,MAAM,OAAO,GAAG;QACd,gBAAgB,EAAE,aAAa;QAC/B,GAAG,EAAE,GAAG,CAAC,WAAW;QACpB,GAAG,EAAE,GAAG,CAAC,WAAW;QACpB,iBAAiB,EAAE,GAAG,CAAC,oBAAoB;QAC3C,uBAAuB,EAAE,GAAG,CAAC,0BAA0B;QACvD,iBAAiB,EAAE,GAAG,CAAC,oBAAoB;QAC3C,aAAa,EAAE,GAAG,CAAC,gBAAgB;QACnC,mCAAmC,EAAE,GAAG,CAAC,sCAAsC;KAChF,CAAC;IAEF,YAAY,CAAC,qDAAqD,EAAE,CAAC,CAAC,aAAa,CAAC,CAAC;IAErF,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,qBAAqB;IACnC,YAAY,CAAC,sEAAsE,CAAC,CAAC;IAErF,MAAM,MAAM,GAAG;QACb,IAAI,EAAE,GAAG,CAAC,MAAM;QAChB,QAAQ,EAAE,GAAG,CAAC,UAAU;QACxB,IAAI,EAAE,GAAG,CAAC,MAAM;QAChB,IAAI,EAAE,GAAG,CAAC,MAAM;QAChB,QAAQ,EAAE,GAAG,CAAC,UAAU;QACxB,GAAG,EAAE,GAAG,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,kBAAkB,EAAE,GAAG,CAAC,SAAS,KAAK,aAAa,EAAE;KACnG,CAAC;IAEF,YAAY,CAAC,uFAAuF,EAClG,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,QAAQ,EAAE,GAAG,CAAC,SAAS,CAAC,CAAC;IAE5D,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/dist/debug.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createDebugLogger } from '@arki/log/debug';
|
|
2
|
+
/**
|
|
3
|
+
* Debug logger for database initialization and connection operations
|
|
4
|
+
* Enable with: DEBUG=db:init
|
|
5
|
+
*/
|
|
6
|
+
export declare const debugInit: createDebugLogger.Debugger;
|
|
7
|
+
/**
|
|
8
|
+
* Debug logger for database factory operations
|
|
9
|
+
* Enable with: DEBUG=db:factory
|
|
10
|
+
*/
|
|
11
|
+
export declare const debugFactory: createDebugLogger.Debugger;
|
|
12
|
+
/**
|
|
13
|
+
* Debug logger for projection builder operations
|
|
14
|
+
* Enable with: DEBUG=db:projection
|
|
15
|
+
*/
|
|
16
|
+
export declare const debugProjection: createDebugLogger.Debugger;
|
|
17
|
+
/**
|
|
18
|
+
* Enable all database debug logs
|
|
19
|
+
* Enable with: DEBUG=db:*
|
|
20
|
+
*/
|
|
21
|
+
//# sourceMappingURL=debug.d.ts.map
|