@friggframework/core 2.0.0--canary.482.bda352c.0 → 2.0.0--canary.487.5a8e56e.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.
@@ -1,27 +1,22 @@
1
1
  const { HealthCheckRepositoryMongoDB } = require('./health-check-repository-mongodb');
2
2
  const { HealthCheckRepositoryPostgreSQL } = require('./health-check-repository-postgres');
3
+ const { prisma } = require('../prisma');
3
4
  const config = require('../config');
4
5
 
5
6
  /**
6
- * Health Check Repository Factory
7
- * Creates the appropriate repository adapter based on database type
8
- *
9
- * Usage:
10
- * ```javascript
11
- * const repository = createHealthCheckRepository();
12
- * ```
13
- *
14
- * @returns {HealthCheckRepositoryInterface} Configured repository adapter
7
+ * @param {Object} [options]
8
+ * @param {Object} [options.prismaClient] - Prisma client (defaults to singleton)
9
+ * @returns {HealthCheckRepositoryInterface}
15
10
  */
16
- function createHealthCheckRepository() {
11
+ function createHealthCheckRepository({ prismaClient = prisma } = {}) {
17
12
  const dbType = config.DB_TYPE;
18
13
 
19
14
  switch (dbType) {
20
15
  case 'mongodb':
21
- return new HealthCheckRepositoryMongoDB();
16
+ return new HealthCheckRepositoryMongoDB({ prismaClient });
22
17
 
23
18
  case 'postgresql':
24
- return new HealthCheckRepositoryPostgreSQL();
19
+ return new HealthCheckRepositoryPostgreSQL({ prismaClient });
25
20
 
26
21
  default:
27
22
  throw new Error(
@@ -32,7 +27,6 @@ function createHealthCheckRepository() {
32
27
 
33
28
  module.exports = {
34
29
  createHealthCheckRepository,
35
- // Export adapters for direct testing
36
30
  HealthCheckRepositoryMongoDB,
37
31
  HealthCheckRepositoryPostgreSQL,
38
32
  };
@@ -76,9 +76,10 @@ class HealthCheckRepositoryInterface {
76
76
  /**
77
77
  * Get database connection state
78
78
  *
79
- * @returns {Object} Connection state info
79
+ * @returns {Promise<Object>} Connection state info
80
+ * @abstract
80
81
  */
81
- getDatabaseConnectionState() {
82
+ async getDatabaseConnectionState() {
82
83
  throw new Error('Method getDatabaseConnectionState must be implemented by subclass');
83
84
  }
84
85
  }
@@ -1,62 +1,67 @@
1
- const { prisma } = require('../prisma');
2
1
  const { mongoose } = require('../mongoose');
3
2
  const {
4
3
  HealthCheckRepositoryInterface,
5
4
  } = require('./health-check-repository-interface');
6
5
 
7
- /**
8
- * MongoDB-specific Health Check Repository
9
- *
10
- * Provides MongoDB-specific database operations for health testing.
11
- * Uses Mongoose for MongoDB-specific operations (raw access, ping).
12
- */
13
6
  class HealthCheckRepositoryMongoDB extends HealthCheckRepositoryInterface {
14
- constructor() {
7
+ /**
8
+ * @param {Object} params
9
+ * @param {Object} params.prismaClient - Prisma client instance
10
+ */
11
+ constructor({ prismaClient }) {
15
12
  super();
13
+ this.prisma = prismaClient;
16
14
  }
17
15
 
18
- getDatabaseConnectionState() {
19
- const stateMap = {
20
- 0: 'disconnected',
21
- 1: 'connected',
22
- 2: 'connecting',
23
- 3: 'disconnecting',
24
- };
25
- const readyState = mongoose.connection.readyState;
16
+ /**
17
+ * @returns {Promise<{readyState: number, stateName: string, isConnected: boolean}>}
18
+ */
19
+ async getDatabaseConnectionState() {
20
+ let isConnected = false;
21
+ let stateName = 'unknown';
22
+
23
+ try {
24
+ await this.prisma.$runCommandRaw({ ping: 1 });
25
+ isConnected = true;
26
+ stateName = 'connected';
27
+ } catch (error) {
28
+ stateName = 'disconnected';
29
+ }
26
30
 
27
31
  return {
28
- readyState,
29
- stateName: stateMap[readyState],
30
- isConnected: readyState === 1,
32
+ readyState: isConnected ? 1 : 0,
33
+ stateName,
34
+ isConnected,
31
35
  };
32
36
  }
33
37
 
38
+ /**
39
+ * @param {number} maxTimeMS
40
+ * @returns {Promise<number>} Response time in milliseconds
41
+ */
34
42
  async pingDatabase(maxTimeMS = 2000) {
35
43
  const pingStart = Date.now();
36
- await mongoose.connection.db.admin().ping({ maxTimeMS });
44
+ await this.prisma.$queryRaw`SELECT 1`.catch(() => {
45
+ return this.prisma.$runCommandRaw({ ping: 1 });
46
+ });
37
47
  return Date.now() - pingStart;
38
48
  }
39
49
 
40
50
  async createCredential(credentialData) {
41
- // Note: Collection existence is ensured at application startup via
42
- // initializeMongoDBSchema() in database/utils/mongodb-schema-init.js
43
- // This prevents "Cannot create namespace in multi-document transaction" errors
44
- return await prisma.credential.create({
51
+ return await this.prisma.credential.create({
45
52
  data: credentialData,
46
53
  });
47
54
  }
48
55
 
49
56
  async findCredentialById(id) {
50
- return await prisma.credential.findUnique({
57
+ return await this.prisma.credential.findUnique({
51
58
  where: { id },
52
59
  });
53
60
  }
54
61
 
55
62
  /**
56
- * Get raw credential from MongoDB bypassing Prisma encryption extension
57
- * Uses Mongoose to access raw MongoDB collection
58
- * @param {string} id - Credential ID
59
- * @returns {Promise<Object|null>} Raw credential from database
63
+ * @param {string} id
64
+ * @returns {Promise<Object|null>}
60
65
  */
61
66
  async getRawCredentialById(id) {
62
67
  const { ObjectId } = require('mongodb');
@@ -66,7 +71,7 @@ class HealthCheckRepositoryMongoDB extends HealthCheckRepositoryInterface {
66
71
  }
67
72
 
68
73
  async deleteCredential(id) {
69
- await prisma.credential.delete({
74
+ await this.prisma.credential.delete({
70
75
  where: { id },
71
76
  });
72
77
  }
@@ -1,59 +1,67 @@
1
- const { prisma } = require('../prisma');
2
1
  const {
3
2
  HealthCheckRepositoryInterface,
4
3
  } = require('./health-check-repository-interface');
5
4
 
6
- /**
7
- * PostgreSQL-specific Health Check Repository
8
- *
9
- * Provides PostgreSQL-specific database operations for health testing.
10
- * Uses Prisma raw queries for PostgreSQL-specific operations.
11
- */
12
5
  class HealthCheckRepositoryPostgreSQL extends HealthCheckRepositoryInterface {
13
- constructor() {
6
+ /**
7
+ * @param {Object} params
8
+ * @param {Object} params.prismaClient - Prisma client instance
9
+ */
10
+ constructor({ prismaClient }) {
14
11
  super();
12
+ this.prisma = prismaClient;
15
13
  }
16
14
 
17
- getDatabaseConnectionState() {
18
- // PostgreSQL connection state via Prisma
19
- // Note: Prisma doesn't expose connection state like Mongoose
20
- // We check if prisma is connected by attempting a query
15
+ /**
16
+ * @returns {Promise<{readyState: number, stateName: string, isConnected: boolean}>}
17
+ */
18
+ async getDatabaseConnectionState() {
19
+ let isConnected = false;
20
+ let stateName = 'unknown';
21
+
22
+ try {
23
+ await this.prisma.$queryRaw`SELECT 1`;
24
+ isConnected = true;
25
+ stateName = 'connected';
26
+ } catch (error) {
27
+ stateName = 'disconnected';
28
+ }
29
+
21
30
  return {
22
- readyState: 1, // Assume connected if Prisma instance exists
23
- stateName: 'connected',
24
- isConnected: true,
31
+ readyState: isConnected ? 1 : 0,
32
+ stateName,
33
+ isConnected,
25
34
  };
26
35
  }
27
36
 
37
+ /**
38
+ * @param {number} maxTimeMS
39
+ * @returns {Promise<number>} Response time in milliseconds
40
+ */
28
41
  async pingDatabase(maxTimeMS = 2000) {
29
42
  const pingStart = Date.now();
30
-
31
- // PostgreSQL ping using SELECT 1
32
- await prisma.$queryRaw`SELECT 1`;
33
-
43
+ await this.prisma.$queryRaw`SELECT 1`;
34
44
  return Date.now() - pingStart;
35
45
  }
36
46
 
37
47
  async createCredential(credentialData) {
38
- return await prisma.credential.create({
48
+ return await this.prisma.credential.create({
39
49
  data: credentialData,
40
50
  });
41
51
  }
42
52
 
43
53
  async findCredentialById(id) {
44
- return await prisma.credential.findUnique({
54
+ return await this.prisma.credential.findUnique({
45
55
  where: { id },
46
56
  });
47
57
  }
48
58
 
49
59
  /**
50
- * Get raw credential from PostgreSQL bypassing Prisma encryption extension
51
- * Uses $queryRaw to access raw PostgreSQL table
52
- * @param {string} id - Credential ID
53
- * @returns {Promise<Object|null>} Raw credential from database
60
+ * @param {string} id
61
+ * @returns {Promise<Object|null>}
54
62
  */
55
63
  async getRawCredentialById(id) {
56
- const results = await prisma.$queryRaw`
64
+ const results = await this.prisma.$queryRaw`
57
65
  SELECT * FROM "Credential" WHERE id = ${id}
58
66
  `;
59
67
 
@@ -61,12 +69,11 @@ class HealthCheckRepositoryPostgreSQL extends HealthCheckRepositoryInterface {
61
69
  return null;
62
70
  }
63
71
 
64
- // Return first result
65
72
  return results[0];
66
73
  }
67
74
 
68
75
  async deleteCredential(id) {
69
- await prisma.credential.delete({
76
+ await this.prisma.credential.delete({
70
77
  where: { id },
71
78
  });
72
79
  }
@@ -1,22 +1,17 @@
1
- /**
2
- * Use Case for checking database health.
3
- * Contains business logic for determining database connectivity and health status.
4
- */
5
1
  class CheckDatabaseHealthUseCase {
6
2
  /**
7
3
  * @param {Object} params
8
- * @param {import('../health-check-repository-interface').HealthCheckRepositoryInterface} params.healthCheckRepository
4
+ * @param {import('../repositories/health-check-repository-interface').HealthCheckRepositoryInterface} params.healthCheckRepository
9
5
  */
10
6
  constructor({ healthCheckRepository }) {
11
7
  this.repository = healthCheckRepository;
12
8
  }
13
9
 
14
10
  /**
15
- * Execute database health check
16
- * @returns {Promise<Object>} Health check result with status, state, and response time
11
+ * @returns {Promise<{status: string, state: string, responseTime?: number}>}
17
12
  */
18
13
  async execute() {
19
- const { stateName, isConnected } = this.repository.getDatabaseConnectionState();
14
+ const { stateName, isConnected } = await this.repository.getDatabaseConnectionState();
20
15
 
21
16
  const result = {
22
17
  status: isConnected ? 'healthy' : 'unhealthy',
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@friggframework/core",
3
3
  "prettier": "@friggframework/prettier-config",
4
- "version": "2.0.0--canary.482.bda352c.0",
4
+ "version": "2.0.0--canary.487.5a8e56e.0",
5
5
  "dependencies": {
6
6
  "@aws-sdk/client-apigatewaymanagementapi": "^3.588.0",
7
7
  "@aws-sdk/client-kms": "^3.588.0",
@@ -38,9 +38,9 @@
38
38
  }
39
39
  },
40
40
  "devDependencies": {
41
- "@friggframework/eslint-config": "2.0.0--canary.482.bda352c.0",
42
- "@friggframework/prettier-config": "2.0.0--canary.482.bda352c.0",
43
- "@friggframework/test": "2.0.0--canary.482.bda352c.0",
41
+ "@friggframework/eslint-config": "2.0.0--canary.487.5a8e56e.0",
42
+ "@friggframework/prettier-config": "2.0.0--canary.487.5a8e56e.0",
43
+ "@friggframework/test": "2.0.0--canary.487.5a8e56e.0",
44
44
  "@prisma/client": "^6.17.0",
45
45
  "@types/lodash": "4.17.15",
46
46
  "@typescript-eslint/eslint-plugin": "^8.0.0",
@@ -80,5 +80,5 @@
80
80
  "publishConfig": {
81
81
  "access": "public"
82
82
  },
83
- "gitHead": "bda352ca5630e5880fc4d75a1e84bb1157a6caf3"
83
+ "gitHead": "5a8e56e0a0507ac7b3ee9f9d2b88448569b44cd4"
84
84
  }