moleculer-db-adapter-typeorm-mongo
MongoDB adapter for Moleculer DB service with typeorm. Still a work in progress but stable.
Essentially a clone and modification of the great work on adaptor for Sequelize by the author of the project and dkuida.
it covers only the basics - but when you need more than basics just use the exposed
serviceadapterrepository;
Features
- Standard and multi-tenancy database connecitons
- Create additional named typeorm connections in a service
- List databases from a connection
Install
$ npm install moleculer-db-adapter-typeorm-mongo --save
Usage
/** * TypeORM db connection */adapter: database: serviceDB name: serviceName // eslint-disable-next-line @typescript-eslint/ban-ts-ignore // @ts-ignore type: 'mongodb' host: 'localhost' port: 27017 entities: serviceEntity synchronize: true useNewUrlParser: true useUnifiedTopology: true /** * TypeORM db model */model: Entity /** * Service db connection mode * Either 'mt' for multitenant or 'stanard' for standard connection * string is case insensative. */mode: 'standard'
Todo
- gridfs integration for file / image storage in MongoDB
Test
$ npm test
Settings
Property | Type | Default | Description |
---|
No settings.
Note:
idField
does not work with Sequelize adapter as you can freely set your own ID while creating the model.
Actions
Methods
find
Entity find
Parameters
Property | Type | Default | Description |
---|---|---|---|
filters |
Object |
required | Filters object to apply |
Examples
todo example
findOne
Entity fineone
Parameters
Property | Type | Default | Description |
---|---|---|---|
query |
Object |
required | Query object |
Examples
todo example
findById
Entity find by id
Parameters
Property | Type | Default | Description |
---|---|---|---|
id |
String |
required | id of document |
Examples
todo example
findByIds
Entity find by array of ids
Parameters
Property | Type | Default | Description |
---|---|---|---|
idList |
Array.<(Object|String)> |
required | list of ids |
Examples
todo example
count
Entity count
Parameters
Property | Type | Default | Description |
---|---|---|---|
filters |
Object |
required | Object of filters to apply |
Examples
todo example
insert
Entity insert
Parameters
Property | Type | Default | Description |
---|---|---|---|
entity |
Object |
required | docuemnt to save |
Examples
todo example
create
Entity create
Parameters
Property | Type | Default | Description |
---|---|---|---|
entity |
Object |
required | document to create |
Examples
todo example
insertMany
Entity insert array of objects
Parameters
Property | Type | Default | Description |
---|---|---|---|
entities |
Array.<Object> |
required | Array of entities |
Examples
todo example
updateMany
Entity update many
Parameters
Property | Type | Default | Description |
---|---|---|---|
where |
Promise |
required | Promise Object filter |
update |
Promise |
required | Promise Object update deep partial |
Examples
todo example
updateById
Entity update by id
Parameters
Property | Type | Default | Description |
---|---|---|---|
id |
String |
required | id of document |
update |
Object |
required | update object using deep partial |
Examples
todo example
removeMany
Entity remove many
Parameters
Property | Type | Default | Description |
---|---|---|---|
where |
Promise |
required | promise object filter |
Examples
todo example
removeById
Entity remove by id
Parameters
Property | Type | Default | Description |
---|---|---|---|
id |
String |
required | id of document |
Examples
todo example
connect
Conect to database
Parameters
Property | Type | Default | Description |
---|---|---|---|
mode |
String |
required | mode of adapter, either mt or standard |
options |
Object |
required | connection options |
cb |
Object |
required | callback with connection in mt mode |
Results
Type: Connection
, Repository
, ConnectionManager
returns Connection, repository and connection manager
Examples
this; // connects to default db connection oof service// new connection objectconst newConnection = database: 'products' type: StringprocessenvDBENGINE username: processenvMONGOUSERNAME password: processenvMONGOPASSWORD host: processenvDBHOST port: NumberprocessenvDBPORT authSource: processenvAUTHSOURCE appname: 'Cameo:service:testapiMT:Products' entities: Products synchronize: processenvSYNCHRONIZE useNewUrlParser: processenvUSENEWURLPARSER useUnifiedTopology: processenvUSENEWURLPARSER;const productsConnection = await async { // pass connection type, connection object and callback to retrieve the new connection object already connected await this; };// use new connection to query databaseawait productsConnectionconnection;console;// close connection when doneawait productsConnectionconnection;// use manager (Connection manager) to get all the available connectionsconsole;// get connection you want to useconst connection = productsConnectionmanagerconsole;// opens connectionconnection;...// closes connectionconnection;
disconnect
Disconnect from database
Parameters
Property | Type | Default | Description |
---|
No input parameters.
Examples
this; // disconnects default service connection
createCursor
Create cursor
Parameters
Property | Type | Default | Description |
---|---|---|---|
params |
Object |
required | |
isCounting |
Boolean |
false |
Examples
todo example
addDBUser
Add user to database
Parameters
Property | Type | Default | Description |
---|---|---|---|
obj |
any | required | Connection object |
obj.url |
String |
required | Database url |
obj.connectionOpts |
Object |
required | Database connection options |
obj.[key: |
any | required | string] - Additional Key pair paramaters |
userOpts |
any | required | User object |
userOpts.username |
String |
required | User name of user to be added |
userOpts.password |
String |
required | Password of user to be added |
userOpts.options |
Object |
required | Additional options to be passed to add user |
Examples
todo example
command
Run database command
Parameters
Property | Type | Default | Description |
---|---|---|---|
url |
String |
required | Mongodb url |
connectionOpts |
Object |
required | Mongodb connection options |
command |
Object |
required | Command object to send |
options |
Object |
required | Additional options to add |
Examples
todo example
createDB
Create Database
Parameters
Property | Type | Default | Description |
---|---|---|---|
obj |
any | required | Connection object |
obj.url |
String |
required | Mongo db url |
obj.connectionOpts |
Object |
required | Mongo connection options |
obj.databaseName |
String |
required | Mongo db name |
userOpts |
any | required | User object (optional) |
userOpts.username |
String |
required | User name |
userOpts.Password |
String |
required | User Password |
userOpts.options |
Object |
required | User options |
Examples
await thisadapter;
listDataBases
List Databases on server
Parameters
Property | Type | Default | Description |
---|---|---|---|
url |
any | required | Mongodb url wihtout database |
opts |
any | required | Mongodb connection options |
Examples
console;
removeUser
Remove user from db
Parameters
Property | Type | Default | Description |
---|---|---|---|
obj |
any | required | Database connection object |
userOpts |
any | required | User object if user to be removed |
Examples
todo example
updateDBUser
Update DB User
Parameters
Property | Type | Default | Description |
---|---|---|---|
obj |
any | required | Connection object |
obj.url |
String |
required | Mongo db url |
obj.connectionOpts |
Object |
required | Mongo connection options |
userOpts |
any | required | User object |
userOpts.username |
String |
required | User name |
userOpts.Password |
String |
required | User Password |
userOpts.options |
Object |
required | User options (roles) |
Examples
todo example
License
The project is available under the MIT license.
Contact
Copyright (c) 2020 Karnith