Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bytetechnology/moleculer-context-db
A tool that wraps moleculer actions with transaction safeness and injects the database into the context
https://github.com/bytetechnology/moleculer-context-db
Last synced: 2 months ago
JSON representation
A tool that wraps moleculer actions with transaction safeness and injects the database into the context
- Host: GitHub
- URL: https://github.com/bytetechnology/moleculer-context-db
- Owner: bytetechnology
- Created: 2020-03-06T16:04:14.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-11T03:26:38.000Z (about 2 years ago)
- Last Synced: 2024-08-03T02:05:55.498Z (5 months ago)
- Language: TypeScript
- Size: 854 KB
- Stars: 2
- Watchers: 4
- Forks: 3
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-moleculer - moleculer-middleware-permissions - A database integrator for injecting a transaction safe database session into the context of the action. (Middlewares / Database)
README
# moleculer-context-db
A database integrator for injecting a transaction safe database session into the
context of the action. Currently, this only has built in support for [Mikro-ORM](https://mikro-orm.io/), and in that only SQL databases have been tested. Mongo support is experimental.## Setup
### Installation
To install with npm
```shell script
npm install moleculer-context-db
````moleculer` and `@mikro-orm/core` are peer dependecies and need to be installed separately.
### Importing
ES6 style
```js
import { MikroConnector, DatabaseContextManager } from 'moleculer-context-db';
```CommonJS
```js
const {
MikroConnector,
DatabaseContextManager
} = require('moleculer-context-db');
```### Configuration
You can create a new MikroConnector as such
```js
const connector = new MikroConnector();
```You will also need to install the appropriate database driver, e.g.:
```js
import {MongoDriver} from '@mikro-orm/mongodb';const connector = new MikroConnector();
```or
```js
npm i @mikro-orm/sqlite;const connector = new MikroConnector();
```You will then need to initialize the connector
```js
await connector.init({
type: 'sqlite', // or use 'mongo' for mongodb
dbName: ':memory',
entities: [YourEntity1, YourEntity2],
cache: {
enabled: false
}
});
```For mongo support, you will need to do:
```js
await connector.init({
type: 'mongo', // or use 'mongo' for mongodb
dbName: ,
clientUrl:
entities: [YourEntity1, YourEntity2],
cache: {
enabled: false
},
implicitTransactions: // needs to be true if you are running a replica set needing transaction support
});
```You can use all available options for MikroORM.init()
## Usage
To use, simply instantiate a DatabaseContextManager with the connector and then add
the result of the middleware method to your broker's middleware```javascript
const dbContextManager: DatabaseContextManager = new DatabaseContextManager(
connector
);yourMoleculerBroker.middlewares.add(DatabaseContextManager.middleware());
```The above statement will wrap all local actions with a Mikro-ORM transaction.