@aromix/core 0.4.9 → 0.5.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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { AxSchemaShape, AxObjectSchema } from '@aromix/validator';
2
+ import { MongoClientOptions, Db } from 'mongodb';
2
3
 
3
4
  type Loader<T> = () => Promise<{
4
5
  default: T;
@@ -21,6 +22,15 @@ declare function LoadEnv<Shape extends AxSchemaShape = AxSchemaShape>(options: P
21
22
  schema: AxObjectSchema<Shape>;
22
23
  }>): <Key extends keyof Shape["base"]>(key: Key) => Shape["base"][Key];
23
24
 
24
- declare function MongoCluster(): void;
25
+ interface MongoClusterOptions<Databases extends Record<string, string>> {
26
+ name?: string;
27
+ uri: string;
28
+ databases: Databases;
29
+ clientOptions?: MongoClientOptions;
30
+ }
31
+ type MongoClusterInstance<Databases extends Record<string, string>> = Unit & {
32
+ [Key in keyof Databases]: Db;
33
+ };
34
+ declare function MongoCluster<Databases extends Record<string, string>>(options: MongoClusterOptions<Databases>): MongoClusterInstance<Databases>;
25
35
 
26
36
  export { LoadEnv, MongoCluster, type Unit, system };
package/dist/index.js CHANGED
@@ -91,7 +91,52 @@ function LoadEnv(options) {
91
91
  }
92
92
 
93
93
  // src/common/MongoCluster.ts
94
- function MongoCluster() {
94
+ import { MongoClient } from "mongodb";
95
+ function MongoCluster(options) {
96
+ const label = options.name ?? "Mongo Cluster";
97
+ const client = new MongoClient(options.uri);
98
+ let connected = false;
99
+ const databases = {};
100
+ for (const key in options.databases) {
101
+ databases[key] = client.db(options.databases[key]);
102
+ }
103
+ return {
104
+ name: label,
105
+ async start() {
106
+ const dbNames = Object.entries(options.databases).map(([alias, dbName]) => `${alias}=${dbName}`).join(", ");
107
+ console.log(`[${label}] connecting\u2026 (${dbNames})`);
108
+ try {
109
+ await client.connect();
110
+ await client.db("admin").command({ ping: 1 });
111
+ connected = true;
112
+ console.log(`[${label}] connected`);
113
+ } catch (err) {
114
+ const reason = err instanceof Error ? err.message : String(err);
115
+ console.error(`[${label}] failed to connect: ${reason}`);
116
+ await client.close().catch(() => {
117
+ });
118
+ throw new Error(`[${label}] start failed: ${reason}`, { cause: err });
119
+ }
120
+ await client.connect();
121
+ },
122
+ async stop() {
123
+ if (!connected) {
124
+ console.log(`[${label}] stop called but never connected, skipping`);
125
+ return;
126
+ }
127
+ console.log(`[${label}] closing\u2026`);
128
+ try {
129
+ await client.close();
130
+ connected = false;
131
+ console.log(`[${label}] closed`);
132
+ } catch (err) {
133
+ const reason = err instanceof Error ? err.message : String(err);
134
+ console.error(`[${label}] error while closing: ${reason}`);
135
+ throw new Error(`[${label}] stop failed: ${reason}`, { cause: err });
136
+ }
137
+ },
138
+ ...databases
139
+ };
95
140
  }
96
141
  export {
97
142
  LoadEnv,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aromix/core",
3
- "version": "0.4.9",
3
+ "version": "0.5.0",
4
4
  "description": "The Core Package For Aromix",
5
5
  "type": "module",
6
6
  "license": "MIT",