pretty-mongo
Advanced tools
Comparing version 0.0.5 to 0.1.6
32
API.md
@@ -1,2 +0,2 @@ | ||
### `initializeMongoDB({ uri, dbName, options })` | ||
### `initializeMongoDB({ uri, connectionName, options })` | ||
@@ -6,33 +6,33 @@ Will initialize the DB connection and allow you to use the rest of the functions. | ||
- `uri` - The MongoDB connection string | ||
- `dbName` - An arbitrary name to assign to the specific instance of MongoDB, that will allow you later on to get that very same instance by the name. This feature allows you to easily create multiple database connections. | ||
- `connectionName` - An arbitrary name to assign to the specific instance of MongoDB, that will allow you later on to get that very same instance by the name. This feature allows you to easily create multiple database connections. | ||
- `options` - Optional. The native MongoDbClientOptions. (see the types) | ||
```typescript | ||
await initializeMongoDB({ uri: 'mongodb://localhost/my-db', dbName: 'myDB' }) | ||
await initializeMongoDB({ uri: 'mongodb://localhost/my-db', connectionName: 'myDB' }) | ||
// or set your options | ||
await initializeMongoDB({ uri: 'mongodb://localhost/my-db', dbName: 'myDB', options: { useUnifiedTopology: false } }) | ||
await initializeMongoDB({ uri: 'mongodb://localhost/my-db', connectionName: 'myDB', options: { useUnifiedTopology: false } }) | ||
// getting it later on (it's a synchronous operation) | ||
const myDb = getDb({ dbName: 'myDb' }) | ||
const myDb = getDb({ connectionName: 'myDb' }) | ||
``` | ||
### `getDb({ dbName })` | ||
### `getDb({ connectionName })` | ||
Will return the native MongoDB `Db` object. Throws if no database by the name of `dbName` was initialized. | ||
Will return the native MongoDB `Db` object. Throws if no database by the name of `connectionName` was initialized. | ||
- `dbName` - The arbitrary name you've assigned to the database instance while initializing it. | ||
- `connectionName` - The arbitrary name you've assigned to the database instance while initializing it. | ||
```typescript | ||
const myDb = getDb({ dbName: 'myDb' }) | ||
const myDb = getDb({ connectionName: 'myDb' }) | ||
``` | ||
### `getClient({ dbName })` | ||
### `getClient({ connectionName })` | ||
Will return the native MongoDB `MongoClient` object. Throws if no database by the name of `dbName` was initialized. | ||
Will return the native MongoDB `MongoClient` object. Throws if no database by the name of `connectionName` was initialized. | ||
- `dbName` - The arbitrary name you've assigned to the database instance while initializing it. | ||
- `connectionName` - The arbitrary name you've assigned to the database instance while initializing it. | ||
```typescript | ||
const myDbClient = getClient({ dbName: 'myDb' }) | ||
const myDbClient = getClient({ connectionName: 'myDb' }) | ||
``` | ||
@@ -44,3 +44,3 @@ | ||
- `collection` - The MongoDB `Collection` object. The one you get by calling `getClient({ dbName }).collection('MyCollectionName')` | ||
- `collection` - The MongoDB `Collection` object. The one you get by calling `getClient({ connectionName }).collection('MyCollectionName')` | ||
@@ -57,4 +57,4 @@ ```typescript | ||
const dbName = 'myApp' | ||
const collection = getClient({ dbName }).collection('Users') | ||
const connectionName = 'myApp' | ||
const collection = getClient({ connectionName }).collection('Users') | ||
@@ -61,0 +61,0 @@ const usersCollectionMethods = new MongoCollectionMethods<UserDbModel>(collection) |
import { Db, MongoClient, MongoClientOptions } from 'mongodb'; | ||
export declare const getDb: ({ dbName }: { | ||
dbName: string; | ||
export declare const getDb: ({ connectionName }: { | ||
connectionName: string; | ||
}) => Db; | ||
export declare const getClient: ({ dbName }: { | ||
dbName: string; | ||
export declare const getClient: ({ connectionName }: { | ||
connectionName: string; | ||
}) => MongoClient; | ||
export declare const initializeMongoDB: ({ uri, dbName, options, }: { | ||
export declare const initializeMongoDB: ({ uri, connectionName, options, }: { | ||
uri: string; | ||
dbName: string; | ||
connectionName: string; | ||
options?: MongoClientOptions | undefined; | ||
}) => Promise<void>; |
@@ -6,14 +6,14 @@ "use strict"; | ||
const databases = {}; | ||
exports.getDb = ({ dbName }) => { | ||
if (!databases[dbName]) | ||
throw new Error(`Database ${dbName} not yet initialized!`); | ||
return databases[dbName].db; | ||
exports.getDb = ({ connectionName }) => { | ||
if (!databases[connectionName]) | ||
throw new Error(`Database ${connectionName} not yet initialized!`); | ||
return databases[connectionName].db; | ||
}; | ||
exports.getClient = ({ dbName }) => { | ||
if (!databases[dbName]) | ||
throw new Error(`Database ${dbName} not yet initialized!`); | ||
return databases[dbName].client; | ||
exports.getClient = ({ connectionName }) => { | ||
if (!databases[connectionName]) | ||
throw new Error(`Database ${connectionName} not yet initialized!`); | ||
return databases[connectionName].client; | ||
}; | ||
exports.initializeMongoDB = async ({ uri, dbName, options = {}, }) => { | ||
if (databases[dbName]) | ||
exports.initializeMongoDB = async ({ uri, connectionName, options = {}, }) => { | ||
if (databases[connectionName]) | ||
return; | ||
@@ -29,5 +29,5 @@ const { database } = mongoUri.parse(uri); | ||
const db = client.db(database); | ||
databases[dbName] = { client, db }; | ||
databases[connectionName] = { client, db }; | ||
const signals = [`SIGINT`, `SIGUSR1`, `SIGUSR2`, `SIGTERM`]; | ||
signals.forEach(s => process.once(s, async () => await client.close())); | ||
}; |
{ | ||
"name": "pretty-mongo", | ||
"version": "0.0.5", | ||
"version": "0.1.6", | ||
"description": "A set of helper tools to test mongo-based projects", | ||
@@ -5,0 +5,0 @@ "author": "Ehab Khaireldin", |
@@ -27,7 +27,7 @@ Get up and running with MongoDB in a few lines of code! | ||
*/ | ||
const dbName = 'myMainDbConnection' | ||
await initializeMongoDB({ uri, dbName }) | ||
const connectionName = 'myMainDbConnection' | ||
await initializeMongoDB({ uri, connectionName }) | ||
// Now we could easily access that connection from a different module synchronously. | ||
const myMainDb = getDb({ dbName }) | ||
const myMainDb = getDb({ connectionName }) | ||
@@ -93,7 +93,7 @@ // This will return the native MongoDB `Collection` object. | ||
uri: 'mongodb://localhost/myOlapDB', | ||
dbName: 'OLAP_DB', | ||
connectionName: 'OLAP_DB', | ||
}, | ||
{ | ||
uri: 'mongodb://localhost/myOltpDB', | ||
dbName: 'OLTP_DB', | ||
connectionName: 'OLTP_DB', | ||
}, | ||
@@ -104,6 +104,6 @@ ] | ||
// Now in your modules get a db by its name | ||
const myOlapDb = getDb({ dbName: 'OLAP_DB' }) | ||
const myOlapDb = getDb({ connectionName: 'OLAP_DB' }) | ||
// Will throw if you tried to get a db that you did not initialize | ||
const myNonExistingDb = getDb({ dbName: 'SANTA' }) // throws: Database SANTA not yet initialized! | ||
const myNonExistingDb = getDb({ connectionName: 'SANTA' }) // throws: Database SANTA not yet initialized! | ||
``` | ||
@@ -110,0 +110,0 @@ |
23770