@kozojs/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.
- package/lib/index.d.ts +24 -0
- package/lib/index.js +49 -0
- package/package.json +41 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { drizzle } from 'drizzle-orm/postgres-js';
|
|
2
|
+
import { drizzle as drizzle$1 } from 'drizzle-orm/mysql2';
|
|
3
|
+
import { drizzle as drizzle$2 } from 'drizzle-orm/better-sqlite3';
|
|
4
|
+
export { and, asc, desc, eq, like, not, or, sql } from 'drizzle-orm';
|
|
5
|
+
export { createInsertSchema, createSelectSchema } from 'drizzle-zod';
|
|
6
|
+
|
|
7
|
+
type DatabaseProvider = 'postgresql' | 'mysql' | 'sqlite';
|
|
8
|
+
interface DatabaseConfig {
|
|
9
|
+
provider: DatabaseProvider;
|
|
10
|
+
url?: string;
|
|
11
|
+
file?: string;
|
|
12
|
+
schema?: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
type DatabaseInstance = ReturnType<typeof drizzle> | ReturnType<typeof drizzle$1> | ReturnType<typeof drizzle$2>;
|
|
15
|
+
/**
|
|
16
|
+
* Create a database instance based on provider
|
|
17
|
+
*/
|
|
18
|
+
declare function createDatabase(config: DatabaseConfig): Promise<DatabaseInstance>;
|
|
19
|
+
/**
|
|
20
|
+
* Create an in-memory SQLite database (for testing)
|
|
21
|
+
*/
|
|
22
|
+
declare function createTestDatabase(schema?: Record<string, unknown>): ReturnType<typeof drizzle$2>;
|
|
23
|
+
|
|
24
|
+
export { type DatabaseConfig, type DatabaseInstance, type DatabaseProvider, createDatabase, createTestDatabase };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { drizzle as drizzlePg } from "drizzle-orm/postgres-js";
|
|
3
|
+
import { drizzle as drizzleMysql } from "drizzle-orm/mysql2";
|
|
4
|
+
import { drizzle as drizzleSqlite } from "drizzle-orm/better-sqlite3";
|
|
5
|
+
import postgres from "postgres";
|
|
6
|
+
import mysql from "mysql2/promise";
|
|
7
|
+
import Database from "better-sqlite3";
|
|
8
|
+
import { sql, eq, and, or, not, like, desc, asc } from "drizzle-orm";
|
|
9
|
+
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
|
|
10
|
+
async function createDatabase(config) {
|
|
11
|
+
const { provider, url, file, schema } = config;
|
|
12
|
+
switch (provider) {
|
|
13
|
+
case "postgresql": {
|
|
14
|
+
if (!url) throw new Error("PostgreSQL requires a connection URL");
|
|
15
|
+
const client = postgres(url);
|
|
16
|
+
return drizzlePg(client, { schema });
|
|
17
|
+
}
|
|
18
|
+
case "mysql": {
|
|
19
|
+
if (!url) throw new Error("MySQL requires a connection URL");
|
|
20
|
+
const connection = await mysql.createConnection(url);
|
|
21
|
+
return drizzleMysql(connection, { schema, mode: "default" });
|
|
22
|
+
}
|
|
23
|
+
case "sqlite": {
|
|
24
|
+
const dbFile = file || ":memory:";
|
|
25
|
+
const sqlite = new Database(dbFile);
|
|
26
|
+
return drizzleSqlite(sqlite, { schema });
|
|
27
|
+
}
|
|
28
|
+
default:
|
|
29
|
+
throw new Error(`Unknown database provider: ${provider}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function createTestDatabase(schema) {
|
|
33
|
+
const sqlite = new Database(":memory:");
|
|
34
|
+
return drizzleSqlite(sqlite, { schema });
|
|
35
|
+
}
|
|
36
|
+
export {
|
|
37
|
+
and,
|
|
38
|
+
asc,
|
|
39
|
+
createDatabase,
|
|
40
|
+
createInsertSchema,
|
|
41
|
+
createSelectSchema,
|
|
42
|
+
createTestDatabase,
|
|
43
|
+
desc,
|
|
44
|
+
eq,
|
|
45
|
+
like,
|
|
46
|
+
not,
|
|
47
|
+
or,
|
|
48
|
+
sql
|
|
49
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kozojs/db",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Database utilities for Kozo Framework - Drizzle ORM integration",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Kozo Team",
|
|
8
|
+
"files": [
|
|
9
|
+
"lib",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
12
|
+
"main": "./lib/index.js",
|
|
13
|
+
"types": "./lib/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": "./lib/index.js",
|
|
17
|
+
"types": "./lib/index.d.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup",
|
|
22
|
+
"dev": "tsup --watch"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"drizzle-orm": "^0.36.0",
|
|
26
|
+
"drizzle-zod": "^0.5.1",
|
|
27
|
+
"postgres": "^3.4.0",
|
|
28
|
+
"mysql2": "^3.11.0",
|
|
29
|
+
"better-sqlite3": "^11.0.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/better-sqlite3": "^7.6.0",
|
|
33
|
+
"@types/node": "^22.0.0",
|
|
34
|
+
"drizzle-kit": "^0.28.0",
|
|
35
|
+
"tsup": "^8.3.0",
|
|
36
|
+
"typescript": "^5.6.0"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"zod": "^3.23.0"
|
|
40
|
+
}
|
|
41
|
+
}
|