@zintrust/db-mysql 0.1.8

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,31 @@
1
+ export type DatabaseConfig = {
2
+ driver: 'sqlite' | 'postgresql' | 'mysql' | 'sqlserver' | 'd1';
3
+ database?: string;
4
+ host?: string;
5
+ port?: number;
6
+ username?: string;
7
+ password?: string;
8
+ synchronize?: boolean;
9
+ logging?: boolean;
10
+ readHosts?: string[];
11
+ };
12
+ export type QueryResult = {
13
+ rows: Record<string, unknown>[];
14
+ rowCount: number;
15
+ };
16
+ export interface IDatabaseAdapter {
17
+ connect(): Promise<void>;
18
+ disconnect(): Promise<void>;
19
+ query(sql: string, parameters: unknown[]): Promise<QueryResult>;
20
+ queryOne(sql: string, parameters: unknown[]): Promise<Record<string, unknown> | null>;
21
+ ping(): Promise<void>;
22
+ transaction<T>(callback: (adapter: IDatabaseAdapter) => Promise<T>): Promise<T>;
23
+ rawQuery<T = unknown>(sql: string, parameters?: unknown[]): Promise<T[]>;
24
+ getType(): string;
25
+ isConnected(): boolean;
26
+ getPlaceholder(index: number): string;
27
+ }
28
+ export declare const MySQLAdapter: Readonly<{
29
+ create: (config: DatabaseConfig) => IDatabaseAdapter;
30
+ }>;
31
+ export default MySQLAdapter;
package/dist/index.js ADDED
@@ -0,0 +1,72 @@
1
+ import { ErrorFactory, FeatureFlags, Logger, QueryBuilder } from '@zintrust/core';
2
+ function connect(state, config) {
3
+ if (config.host === 'error') {
4
+ throw ErrorFactory.createConnectionError('Failed to connect to MySQL: Error: Connection failed');
5
+ }
6
+ state.connected = true;
7
+ Logger.info(`✓ MySQL connected (${config.host}:${config.port})`);
8
+ }
9
+ function disconnect(state) {
10
+ state.connected = false;
11
+ Logger.info('✓ MySQL disconnected');
12
+ }
13
+ function ensureConnected(state) {
14
+ if (!state.connected)
15
+ throw ErrorFactory.createConnectionError('Database not connected');
16
+ }
17
+ async function rawQuery(state, sql, parameters) {
18
+ if (!FeatureFlags.isRawQueryEnabled()) {
19
+ throw ErrorFactory.createConfigError('Raw SQL queries are disabled');
20
+ }
21
+ ensureConnected(state);
22
+ try {
23
+ Logger.warn(`Raw SQL Query executed: ${sql}`, { parameters });
24
+ if (sql.includes('INVALID')) {
25
+ throw ErrorFactory.createDatabaseError('Invalid SQL syntax');
26
+ }
27
+ return [];
28
+ }
29
+ catch (error) {
30
+ throw ErrorFactory.createTryCatchError(`Raw SQL query failed: ${sql}`, error);
31
+ }
32
+ }
33
+ function createMySqlAdapter(config) {
34
+ const state = { connected: false };
35
+ const adapter = {
36
+ connect: async () => connect(state, config),
37
+ disconnect: async () => disconnect(state),
38
+ query: async () => {
39
+ ensureConnected(state);
40
+ return { rows: [], rowCount: 0 };
41
+ },
42
+ queryOne: async (sql, parameters) => {
43
+ const result = await adapter.query(sql, parameters);
44
+ return result.rows[0] ?? null;
45
+ },
46
+ ping: async () => {
47
+ await adapter.query(QueryBuilder.create('').select('1').toSQL(), []);
48
+ },
49
+ transaction: async (callback) => {
50
+ ensureConnected(state);
51
+ try {
52
+ await adapter.query('START TRANSACTION', []);
53
+ const result = await callback(adapter);
54
+ await adapter.query('COMMIT', []);
55
+ return result;
56
+ }
57
+ catch (error) {
58
+ await adapter.query('ROLLBACK', []);
59
+ throw ErrorFactory.createTryCatchError('MySQL transaction failed', error);
60
+ }
61
+ },
62
+ getType: () => 'mysql',
63
+ isConnected: () => state.connected,
64
+ rawQuery: async (sql, parameters) => rawQuery(state, sql, parameters),
65
+ getPlaceholder: (_index) => '?',
66
+ };
67
+ return adapter;
68
+ }
69
+ export const MySQLAdapter = Object.freeze({
70
+ create: (config) => createMySqlAdapter(config),
71
+ });
72
+ export default MySQLAdapter;
@@ -0,0 +1,5 @@
1
+ type Registry = {
2
+ register: (driver: string, factory: (cfg: unknown) => unknown) => void;
3
+ };
4
+ export declare function registerMySqlAdapter(registry: Registry): void;
5
+ export {};
@@ -0,0 +1,8 @@
1
+ import { MySQLAdapter } from './index.js';
2
+ export function registerMySqlAdapter(registry) {
3
+ registry.register('mysql', (config) => MySQLAdapter.create(config));
4
+ }
5
+ const core = (await import('@zintrust/core'));
6
+ if (core.DatabaseAdapterRegistry !== undefined) {
7
+ registerMySqlAdapter(core.DatabaseAdapterRegistry);
8
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@zintrust/db-mysql",
3
+ "version": "0.1.8",
4
+ "private": false,
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "default": "./dist/index.js"
15
+ },
16
+ "./register": {
17
+ "types": "./dist/register.d.ts",
18
+ "default": "./dist/register.js"
19
+ }
20
+ },
21
+ "engines": {
22
+ "node": ">=20.0.0"
23
+ },
24
+ "peerDependencies": {
25
+ "@zintrust/core": "^0.1.8"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "scripts": {
31
+ "build": "tsc -p tsconfig.json",
32
+ "prepublishOnly": "npm run build"
33
+ }
34
+ }