@moriajs/db 0.1.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.
@@ -0,0 +1,4 @@
1
+
2
+ > @moriajs/db@0.1.1 build C:\Codes\node\2026\github\moriajs\packages\db
3
+ > tsc
4
+
@@ -0,0 +1,7 @@
1
+
2
+ > @moriajs/db@0.0.1 typecheck C:\Codes\node\2026\github\moriajs\packages\db
3
+ > tsc --noEmit
4
+
5
+ src/index.ts(63,37): error TS7016: Could not find a declaration file for module 'pg'. 'C:/Codes/node/2026/github/moriajs/node_modules/.pnpm/pg@8.18.0/node_modules/pg/esm/index.mjs' implicitly has an 'any' type.
6
+ Try `npm i --save-dev @types/pg` if it exists or add a new declaration (.d.ts) file containing `declare module 'pg';`
7
+  ELIFECYCLE  Command failed with exit code 2.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Guntur D
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,71 @@
1
+ /**
2
+ * @moriajs/db
3
+ *
4
+ * Database-agnostic adapter layer built on Kysely.
5
+ * Default: PostgreSQL (production), SQLite (development).
6
+ * Designed to work with kysely-schema for schema-first development.
7
+ */
8
+ import { Kysely } from 'kysely';
9
+ /**
10
+ * Supported database adapters.
11
+ */
12
+ export type DatabaseAdapter = 'pg' | 'sqlite' | 'mysql';
13
+ /**
14
+ * Configuration for creating a database connection.
15
+ */
16
+ export interface DatabaseConfig {
17
+ /** Database adapter to use */
18
+ adapter: DatabaseAdapter;
19
+ /** Connection URL (for pg/mysql) */
20
+ url?: string;
21
+ /** File path (for sqlite) */
22
+ filename?: string;
23
+ /** Connection pool size */
24
+ pool?: {
25
+ min?: number;
26
+ max?: number;
27
+ };
28
+ }
29
+ /**
30
+ * Create a Kysely database instance with the specified adapter.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * // Production (PostgreSQL)
35
+ * const db = await createDatabase({
36
+ * adapter: 'pg',
37
+ * url: process.env.DATABASE_URL,
38
+ * });
39
+ *
40
+ * // Development (SQLite)
41
+ * const db = await createDatabase({
42
+ * adapter: 'sqlite',
43
+ * filename: './dev.db',
44
+ * });
45
+ * ```
46
+ */
47
+ export declare function createDatabase<T>(config: DatabaseConfig): Promise<Kysely<T>>;
48
+ /**
49
+ * Create a MoriaJS database plugin for Fastify integration.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * import { createApp } from '@moriajs/core';
54
+ * import { createDatabasePlugin } from '@moriajs/db';
55
+ *
56
+ * const app = await createApp();
57
+ * await app.use(createDatabasePlugin({
58
+ * adapter: 'sqlite',
59
+ * filename: './dev.db',
60
+ * }));
61
+ * ```
62
+ */
63
+ export declare function createDatabasePlugin(config: DatabaseConfig): {
64
+ name: string;
65
+ register({ server }: {
66
+ server: {
67
+ decorate: (key: string, value: unknown) => void;
68
+ };
69
+ }): Promise<void>;
70
+ };
71
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,MAAM,EAAkC,MAAM,QAAQ,CAAC;AAEhE;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,IAAI,GAAG,QAAQ,GAAG,OAAO,CAAC;AAExD;;GAEG;AACH,MAAM,WAAW,cAAc;IAC3B,8BAA8B;IAC9B,OAAO,EAAE,eAAe,CAAC;IACzB,oCAAoC;IACpC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,6BAA6B;IAC7B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,2BAA2B;IAC3B,IAAI,CAAC,EAAE;QACH,GAAG,CAAC,EAAE,MAAM,CAAC;QACb,GAAG,CAAC,EAAE,MAAM,CAAC;KAChB,CAAC;CACL;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,cAAc,CAAC,CAAC,EAAE,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAGlF;AA0CD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,cAAc;;yBAGxB;QAAE,MAAM,EAAE;YAAE,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,IAAI,CAAA;SAAE,CAAA;KAAE;EAKjG"}
package/dist/index.js ADDED
@@ -0,0 +1,89 @@
1
+ /**
2
+ * @moriajs/db
3
+ *
4
+ * Database-agnostic adapter layer built on Kysely.
5
+ * Default: PostgreSQL (production), SQLite (development).
6
+ * Designed to work with kysely-schema for schema-first development.
7
+ */
8
+ import { Kysely, PostgresDialect, SqliteDialect } from 'kysely';
9
+ /**
10
+ * Create a Kysely database instance with the specified adapter.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * // Production (PostgreSQL)
15
+ * const db = await createDatabase({
16
+ * adapter: 'pg',
17
+ * url: process.env.DATABASE_URL,
18
+ * });
19
+ *
20
+ * // Development (SQLite)
21
+ * const db = await createDatabase({
22
+ * adapter: 'sqlite',
23
+ * filename: './dev.db',
24
+ * });
25
+ * ```
26
+ */
27
+ export async function createDatabase(config) {
28
+ const dialect = await createDialect(config);
29
+ return new Kysely({ dialect });
30
+ }
31
+ /**
32
+ * Create the appropriate Kysely dialect based on adapter config.
33
+ */
34
+ async function createDialect(config) {
35
+ switch (config.adapter) {
36
+ case 'pg': {
37
+ // Dynamic import — pg is an optional dependency
38
+ // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-explicit-any
39
+ const pg = globalThis.require('pg');
40
+ return new PostgresDialect({
41
+ pool: new pg.Pool({
42
+ connectionString: config.url,
43
+ min: config.pool?.min ?? 2,
44
+ max: config.pool?.max ?? 10,
45
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
+ }),
47
+ });
48
+ }
49
+ case 'sqlite': {
50
+ // Dynamic import — better-sqlite3 is an optional dependency
51
+ const BetterSqlite3Module = await import('better-sqlite3');
52
+ const BetterSqlite3 = BetterSqlite3Module.default;
53
+ return new SqliteDialect({
54
+ database: new BetterSqlite3(config.filename ?? ':memory:'),
55
+ });
56
+ }
57
+ case 'mysql': {
58
+ throw new Error('@moriajs/db: MySQL adapter is not yet implemented. ' +
59
+ 'Contributions welcome!');
60
+ }
61
+ default:
62
+ throw new Error(`@moriajs/db: Unknown adapter "${config.adapter}"`);
63
+ }
64
+ }
65
+ /**
66
+ * Create a MoriaJS database plugin for Fastify integration.
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * import { createApp } from '@moriajs/core';
71
+ * import { createDatabasePlugin } from '@moriajs/db';
72
+ *
73
+ * const app = await createApp();
74
+ * await app.use(createDatabasePlugin({
75
+ * adapter: 'sqlite',
76
+ * filename: './dev.db',
77
+ * }));
78
+ * ```
79
+ */
80
+ export function createDatabasePlugin(config) {
81
+ return {
82
+ name: '@moriajs/db',
83
+ async register({ server }) {
84
+ const db = await createDatabase(config);
85
+ server.decorate('db', db);
86
+ },
87
+ };
88
+ }
89
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAwBhE;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAI,MAAsB;IAC1D,MAAM,OAAO,GAAG,MAAM,aAAa,CAAC,MAAM,CAAC,CAAC;IAC5C,OAAO,IAAI,MAAM,CAAI,EAAE,OAAO,EAAE,CAAC,CAAC;AACtC,CAAC;AAED;;GAEG;AACH,KAAK,UAAU,aAAa,CAAC,MAAsB;IAC/C,QAAQ,MAAM,CAAC,OAAO,EAAE,CAAC;QACrB,KAAK,IAAI,CAAC,CAAC,CAAC;YACR,gDAAgD;YAChD,qGAAqG;YACrG,MAAM,EAAE,GAAI,UAAkB,CAAC,OAAO,CAAC,IAAI,CAA6D,CAAC;YACzG,OAAO,IAAI,eAAe,CAAC;gBACvB,IAAI,EAAE,IAAI,EAAE,CAAC,IAAI,CAAC;oBACd,gBAAgB,EAAE,MAAM,CAAC,GAAG;oBAC5B,GAAG,EAAE,MAAM,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC;oBAC1B,GAAG,EAAE,MAAM,CAAC,IAAI,EAAE,GAAG,IAAI,EAAE;oBAC3B,8DAA8D;iBACjE,CAAQ;aACZ,CAAC,CAAC;QACP,CAAC;QAED,KAAK,QAAQ,CAAC,CAAC,CAAC;YACZ,4DAA4D;YAC5D,MAAM,mBAAmB,GAAG,MAAM,MAAM,CAAC,gBAAgB,CAAC,CAAC;YAC3D,MAAM,aAAa,GAAG,mBAAmB,CAAC,OAAO,CAAC;YAClD,OAAO,IAAI,aAAa,CAAC;gBACrB,QAAQ,EAAE,IAAI,aAAa,CAAC,MAAM,CAAC,QAAQ,IAAI,UAAU,CAAC;aAC7D,CAAC,CAAC;QACP,CAAC;QAED,KAAK,OAAO,CAAC,CAAC,CAAC;YACX,MAAM,IAAI,KAAK,CACX,qDAAqD;gBACrD,wBAAwB,CAC3B,CAAC;QACN,CAAC;QAED;YACI,MAAM,IAAI,KAAK,CAAC,iCAAiC,MAAM,CAAC,OAAO,GAAG,CAAC,CAAC;IAC5E,CAAC;AACL,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,oBAAoB,CAAC,MAAsB;IACvD,OAAO;QACH,IAAI,EAAE,aAAa;QACnB,KAAK,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAmE;YACtF,MAAM,EAAE,GAAG,MAAM,cAAc,CAAC,MAAM,CAAC,CAAC;YACxC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QAC9B,CAAC;KACJ,CAAC;AACN,CAAC"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@moriajs/db",
3
+ "version": "0.1.1",
4
+ "type": "module",
5
+ "description": "MoriaJS database — Kysely adapters with kysely-schema integration",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "dependencies": {
15
+ "kysely": "^0.27.0"
16
+ },
17
+ "optionalDependencies": {
18
+ "pg": "^8.13.0",
19
+ "better-sqlite3": "^11.0.0"
20
+ },
21
+ "devDependencies": {
22
+ "typescript": "^5.7.0",
23
+ "rimraf": "^6.0.0",
24
+ "@types/node": "^22.0.0",
25
+ "@types/better-sqlite3": "^7.6.0"
26
+ },
27
+ "peerDependencies": {
28
+ "@moriajs/core": "0.1.1"
29
+ },
30
+ "license": "MIT",
31
+ "author": "Guntur-D <guntur.d.npm@gmail.com>",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/guntur-d/moriajs.git",
35
+ "directory": "packages/db"
36
+ },
37
+ "homepage": "https://github.com/guntur-d/moriajs/tree/main/packages/db#readme",
38
+ "bugs": {
39
+ "url": "https://github.com/guntur-d/moriajs/issues"
40
+ },
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "scripts": {
45
+ "build": "tsc",
46
+ "dev": "tsc --watch",
47
+ "typecheck": "tsc --noEmit",
48
+ "clean": "rimraf dist .turbo"
49
+ }
50
+ }
package/src/index.ts ADDED
@@ -0,0 +1,119 @@
1
+ /**
2
+ * @moriajs/db
3
+ *
4
+ * Database-agnostic adapter layer built on Kysely.
5
+ * Default: PostgreSQL (production), SQLite (development).
6
+ * Designed to work with kysely-schema for schema-first development.
7
+ */
8
+
9
+ import { Kysely, PostgresDialect, SqliteDialect } from 'kysely';
10
+
11
+ /**
12
+ * Supported database adapters.
13
+ */
14
+ export type DatabaseAdapter = 'pg' | 'sqlite' | 'mysql';
15
+
16
+ /**
17
+ * Configuration for creating a database connection.
18
+ */
19
+ export interface DatabaseConfig {
20
+ /** Database adapter to use */
21
+ adapter: DatabaseAdapter;
22
+ /** Connection URL (for pg/mysql) */
23
+ url?: string;
24
+ /** File path (for sqlite) */
25
+ filename?: string;
26
+ /** Connection pool size */
27
+ pool?: {
28
+ min?: number;
29
+ max?: number;
30
+ };
31
+ }
32
+
33
+ /**
34
+ * Create a Kysely database instance with the specified adapter.
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * // Production (PostgreSQL)
39
+ * const db = await createDatabase({
40
+ * adapter: 'pg',
41
+ * url: process.env.DATABASE_URL,
42
+ * });
43
+ *
44
+ * // Development (SQLite)
45
+ * const db = await createDatabase({
46
+ * adapter: 'sqlite',
47
+ * filename: './dev.db',
48
+ * });
49
+ * ```
50
+ */
51
+ export async function createDatabase<T>(config: DatabaseConfig): Promise<Kysely<T>> {
52
+ const dialect = await createDialect(config);
53
+ return new Kysely<T>({ dialect });
54
+ }
55
+
56
+ /**
57
+ * Create the appropriate Kysely dialect based on adapter config.
58
+ */
59
+ async function createDialect(config: DatabaseConfig) {
60
+ switch (config.adapter) {
61
+ case 'pg': {
62
+ // Dynamic import — pg is an optional dependency
63
+ // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-explicit-any
64
+ const pg = (globalThis as any).require('pg') as { Pool: new (opts: Record<string, unknown>) => unknown };
65
+ return new PostgresDialect({
66
+ pool: new pg.Pool({
67
+ connectionString: config.url,
68
+ min: config.pool?.min ?? 2,
69
+ max: config.pool?.max ?? 10,
70
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
71
+ }) as any,
72
+ });
73
+ }
74
+
75
+ case 'sqlite': {
76
+ // Dynamic import — better-sqlite3 is an optional dependency
77
+ const BetterSqlite3Module = await import('better-sqlite3');
78
+ const BetterSqlite3 = BetterSqlite3Module.default;
79
+ return new SqliteDialect({
80
+ database: new BetterSqlite3(config.filename ?? ':memory:'),
81
+ });
82
+ }
83
+
84
+ case 'mysql': {
85
+ throw new Error(
86
+ '@moriajs/db: MySQL adapter is not yet implemented. ' +
87
+ 'Contributions welcome!'
88
+ );
89
+ }
90
+
91
+ default:
92
+ throw new Error(`@moriajs/db: Unknown adapter "${config.adapter}"`);
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Create a MoriaJS database plugin for Fastify integration.
98
+ *
99
+ * @example
100
+ * ```ts
101
+ * import { createApp } from '@moriajs/core';
102
+ * import { createDatabasePlugin } from '@moriajs/db';
103
+ *
104
+ * const app = await createApp();
105
+ * await app.use(createDatabasePlugin({
106
+ * adapter: 'sqlite',
107
+ * filename: './dev.db',
108
+ * }));
109
+ * ```
110
+ */
111
+ export function createDatabasePlugin(config: DatabaseConfig) {
112
+ return {
113
+ name: '@moriajs/db',
114
+ async register({ server }: { server: { decorate: (key: string, value: unknown) => void } }) {
115
+ const db = await createDatabase(config);
116
+ server.decorate('db', db);
117
+ },
118
+ };
119
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "rootDir": "src"
6
+ },
7
+ "include": [
8
+ "src"
9
+ ]
10
+ }