@open-kingdom/shared-backend-data-access-database-setup 0.0.2-13 → 0.0.2-15

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/README.md CHANGED
@@ -1,77 +1,117 @@
1
- # data-access-database-setup
1
+ # `@open-kingdom/shared-backend-data-access-database-setup`
2
2
 
3
- This library provides a configurable database setup module for NestJS applications using Drizzle ORM with SQLite.
3
+ A NestJS global dynamic module that opens a `better-sqlite3` connection, wraps it with Drizzle ORM, applies SQLite PRAGMAs, and provides the `BetterSQLite3Database` instance to the NestJS DI container under the `DB_TAG` injection token.
4
4
 
5
- ## Features
5
+ ---
6
6
 
7
- - Dynamic module configuration for database setup
8
- - Configurable schema injection
9
- - SQLite database configuration with Drizzle ORM
10
- - Example schema implementation
7
+ ## Exports
11
8
 
12
- ## Usage
9
+ | Export | Kind | Description |
10
+ | ---------------------------- | ----------- | -------------------------------------------------------------------- |
11
+ | `DatabaseSetupModule` | `class` | Global NestJS dynamic module. Use `.register(options)` to configure. |
12
+ | `DatabaseSetupModuleOptions` | `interface` | Type-only export. Type of the argument accepted by `.register()`. |
13
13
 
14
- ### Basic Usage
14
+ ---
15
+
16
+ ## Type Definitions
17
+
18
+ `DatabaseSetupModuleOptions` is a generic interface with a type parameter `TSchema extends Record<string, unknown>` (defaulting to `Record<string, unknown>`). It accepts the following properties:
19
+
20
+ | Property | Type | Required | Default | Description |
21
+ | ---------- | ------------------------ | -------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------- |
22
+ | `schema` | `TSchema` | Yes | — | Drizzle table definitions object. Pass all tables the application needs so relational queries resolve correctly. |
23
+ | `filename` | `string` | No | `'demo.db'` | Path to the SQLite database file. Relative paths resolve from the process working directory. The file is created if it does not exist. |
24
+ | `pragmas` | `Record<string, string>` | No | `{}` | SQLite PRAGMAs applied immediately after the connection opens. Each key/value pair is executed as `sqlite.pragma('key = value')`. |
25
+
26
+ The injection token is **not** exported from this package. It is `DB_TAG` from `@open-kingdom/shared-poly-util-constants`.
27
+
28
+ ---
29
+
30
+ ## Module Registration
31
+
32
+ `DatabaseSetupModule` is declared `global: true`. Register it **once** in the root `AppModule`. All other modules throughout the application can inject the database without importing this module again.
15
33
 
16
34
  ```typescript
35
+ // app.module.ts
17
36
  import { Module } from '@nestjs/common';
18
37
  import { DatabaseSetupModule } from '@open-kingdom/shared-backend-data-access-database-setup';
19
- import { mySchema } from './my-schema';
38
+ import * as schema from '@open-kingdom/demo-scaffold-backend-feature-root-schema';
20
39
 
21
40
  @Module({
22
41
  imports: [
23
42
  DatabaseSetupModule.register({
24
- schema: mySchema,
25
- tag: 'MY_DB',
26
- filename: 'my-app.db',
43
+ schema,
44
+ filename: process.env['DB_FILENAME'] ?? 'app.db',
45
+ pragmas: {
46
+ journal_mode: 'WAL',
47
+ foreign_keys: 'ON',
48
+ },
27
49
  }),
28
50
  ],
29
51
  })
30
52
  export class AppModule {}
31
53
  ```
32
54
 
33
- ### Using the Example Schema
55
+ ---
34
56
 
35
- ```typescript
36
- import { Module } from '@nestjs/common';
37
- import { DatabaseSetupModule, exampleSchema } from '@open-kingdom/shared-backend-data-access-database-setup';
57
+ ## Configuration Options
38
58
 
39
- @Module({
40
- imports: [
41
- DatabaseSetupModule.register({
42
- schema: exampleSchema,
43
- }),
44
- ],
45
- })
46
- export class AppModule {}
47
- ```
59
+ | Option | Type | Default | Description |
60
+ | ---------- | ----------------------------------------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
61
+ | `schema` | `TSchema extends Record<string, unknown>` | — (required) | All Drizzle table definition objects the application needs. Pass the full merged schema so that relational queries (`db.query.*`) resolve correctly. |
62
+ | `filename` | `string` | `'demo.db'` | Path to the SQLite database file. Relative paths resolve from the process working directory. The file is created if it does not exist. |
63
+ | `pragmas` | `Record<string, string>` | `{}` | SQLite PRAGMAs applied immediately after the connection opens. Each entry is executed as `sqlite.pragma('key = value')`. Common values: `{ journal_mode: 'WAL', foreign_keys: 'ON' }`. |
64
+
65
+ ---
48
66
 
49
- ### Configuration Options
67
+ ## Module Behavior
50
68
 
51
- The `DatabaseSetupModule.register()` method accepts the following options:
69
+ 1. `DatabaseSetupModule.register(options)` returns a `DynamicModule` with `global: true`.
70
+ 2. The module factory opens (or creates) the SQLite file at `options.filename` using `better-sqlite3`.
71
+ 3. Each entry in `options.pragmas` is applied synchronously before any queries run.
72
+ 4. Drizzle ORM wraps the connection using the provided `options.schema`.
73
+ 5. The resulting `BetterSQLite3Database<TSchema>` is provided under the `DB_TAG` token and exported globally.
52
74
 
53
- - `schema`: **Required** - The database schema object containing table definitions
54
- - `tag`: **Optional** - Database connection tag (defaults to 'DB_DEV')
55
- - `filename`: **Optional** - SQLite database filename (defaults to 'demo.db')
75
+ Migration execution is **not** performed automatically. Migrations are managed via `drizzle-kit` scripts in the application's `package.json`. The typical scripts are `db:generate` (runs `drizzle-kit generate`) and `db:migrate` (runs `drizzle-kit migrate`), both pointing to a `drizzle.config.ts` configuration file.
56
76
 
57
- ## Schema Definition
77
+ ---
58
78
 
59
- Your schema should be defined using Drizzle ORM's table builders:
79
+ ## Injecting the Database
60
80
 
61
81
  ```typescript
62
- import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core';
82
+ import { Injectable, Inject } from '@nestjs/common';
83
+ import { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';
84
+ import { DB_TAG } from '@open-kingdom/shared-poly-util-constants';
85
+ import * as schema from '@open-kingdom/demo-scaffold-backend-feature-root-schema';
86
+
87
+ @Injectable()
88
+ export class MyService {
89
+ constructor(@Inject(DB_TAG) private db: BetterSQLite3Database<typeof schema>) {}
90
+
91
+ async findAll() {
92
+ return this.db.query.myTable.findMany();
93
+ }
94
+ }
95
+ ```
63
96
 
64
- export const users = sqliteTable('users', {
65
- id: integer('id').primaryKey({ autoIncrement: true }),
66
- username: text('username').notNull().unique(),
67
- email: text('email').notNull().unique(),
68
- });
97
+ ---
69
98
 
70
- export const mySchema = {
71
- users,
72
- };
73
- ```
99
+ ## API Reference
100
+
101
+ ### `DatabaseSetupModule.register<TSchema>(options: DatabaseSetupModuleOptions<TSchema>): DynamicModule`
74
102
 
75
- ## Running unit tests
103
+ Static factory method returning a NestJS `DynamicModule`. Must be called in the `imports` array of a module decorator.
76
104
 
77
- Run `nx test data-access-database-setup` to execute the unit tests via [Jest](https://jestjs.io).
105
+ **Provided token:** `DB_TAG` `BetterSQLite3Database<TSchema>`
106
+
107
+ **Global:** `true` — the provided token is visible to all modules in the application without further imports.
108
+
109
+ **Throws at startup** if the SQLite file cannot be opened (permission denied, invalid directory, etc.).
110
+
111
+ ---
112
+
113
+ ## Testing
114
+
115
+ ```bash
116
+ nx test @open-kingdom/shared-backend-data-access-database-setup
117
+ ```
@@ -2,6 +2,7 @@ import { DynamicModule } from '@nestjs/common';
2
2
  export interface DatabaseSetupModuleOptions<TSchema extends Record<string, unknown> = Record<string, unknown>> {
3
3
  filename?: string;
4
4
  schema: TSchema;
5
+ pragmas?: Record<string, string>;
5
6
  }
6
7
  export declare class DatabaseSetupModule {
7
8
  static register<TSchema extends Record<string, unknown>>(options: DatabaseSetupModuleOptions<TSchema>): DynamicModule;
@@ -1 +1 @@
1
- {"version":3,"file":"data-access-database-setup.module.d.ts","sourceRoot":"","sources":["../../src/lib/data-access-database-setup.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAKvD,MAAM,WAAW,0BAA0B,CACzC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAEjE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,OAAO,CAAC;CACjB;AAED,qBACa,mBAAmB;IAC9B,MAAM,CAAC,QAAQ,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACrD,OAAO,EAAE,0BAA0B,CAAC,OAAO,CAAC,GAC3C,aAAa;CAcjB"}
1
+ {"version":3,"file":"data-access-database-setup.module.d.ts","sourceRoot":"","sources":["../../src/lib/data-access-database-setup.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAMvD,MAAM,WAAW,0BAA0B,CACzC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAEjE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,OAAO,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC;AAED,qBACa,mBAAmB;IAC9B,MAAM,CAAC,QAAQ,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACrD,OAAO,EAAE,0BAA0B,CAAC,OAAO,CAAC,GAC3C,aAAa;CAqBjB"}
@@ -4,21 +4,27 @@ Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.DatabaseSetupModule = void 0;
5
5
  const tslib_1 = require("tslib");
6
6
  const common_1 = require("@nestjs/common");
7
- const nestjs_drizzle_better_sqlite3_1 = require("@knaadh/nestjs-drizzle-better-sqlite3");
7
+ const better_sqlite3_1 = tslib_1.__importDefault(require("better-sqlite3"));
8
+ const better_sqlite3_2 = require("drizzle-orm/better-sqlite3");
8
9
  const shared_poly_util_constants_1 = require("@open-kingdom/shared-poly-util-constants");
9
10
  let DatabaseSetupModule = DatabaseSetupModule_1 = class DatabaseSetupModule {
10
11
  static register(options) {
11
12
  return {
12
13
  module: DatabaseSetupModule_1,
13
- imports: [
14
- nestjs_drizzle_better_sqlite3_1.DrizzleBetterSQLiteModule.register({
15
- tag: shared_poly_util_constants_1.DB_TAG,
16
- sqlite3: {
17
- filename: options.filename || 'demo.db',
14
+ global: true,
15
+ providers: [
16
+ {
17
+ provide: shared_poly_util_constants_1.DB_TAG,
18
+ useFactory: () => {
19
+ const sqlite = new better_sqlite3_1.default(options.filename || 'demo.db');
20
+ for (const [key, value] of Object.entries(options.pragmas ?? {})) {
21
+ sqlite.pragma(`${key} = ${value}`);
22
+ }
23
+ return (0, better_sqlite3_2.drizzle)(sqlite, { schema: options.schema });
18
24
  },
19
- config: { schema: options.schema },
20
- }),
25
+ },
21
26
  ],
27
+ exports: [shared_poly_util_constants_1.DB_TAG],
22
28
  };
23
29
  }
24
30
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-kingdom/shared-backend-data-access-database-setup",
3
- "version": "0.0.2-13",
3
+ "version": "0.0.2-15",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -23,6 +23,7 @@
23
23
  }
24
24
  },
25
25
  "files": [
26
+ "README.md",
26
27
  "dist",
27
28
  "!**/*.tsbuildinfo"
28
29
  ],
@@ -35,8 +36,11 @@
35
36
  },
36
37
  "dependencies": {
37
38
  "tslib": "^2.3.0",
38
- "@knaadh/nestjs-drizzle-better-sqlite3": "^1.2.0",
39
+ "@open-kingdom/shared-poly-util-constants": "0.0.2-15"
40
+ },
41
+ "peerDependencies": {
39
42
  "@nestjs/common": "^11.0.0",
40
- "@open-kingdom/shared-poly-util-constants": "0.0.2-13"
43
+ "better-sqlite3": "^12.0.0",
44
+ "drizzle-orm": "^0.44.0"
41
45
  }
42
46
  }