https://github.com/devalexandre/moleculer-db-adapter-sequelize
https://github.com/devalexandre/moleculer-db-adapter-sequelize
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/devalexandre/moleculer-db-adapter-sequelize
- Owner: devalexandre
- License: mit
- Created: 2021-11-20T03:58:33.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-11-23T13:13:00.000Z (over 4 years ago)
- Last Synced: 2025-10-11T21:07:21.697Z (5 months ago)
- Language: JavaScript
- Size: 19.5 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README

# moleculer-db-adapter-sequelize [](https://www.npmjs.com/package/@indevweb/moleculer-db-adapter-sequelizee)




SQL adapter (Postgres, MySQL, SQLite, MSSQL & CockrachDB ) for Moleculer DB service with [sequelize-cockroachdb](https://github.com/cockroachdb/sequelize-cockroachdb).
# Features
- CockroachDb Support
- Sequelize 6 Support
# Install
```bash
$ npm install moleculer-db-adapter-sequelize sequelize-cockroachdb --save
```
You have to install additional packages for your database server:
```bash
# For SQLite
$ npm install sqlite3 --save
# For MySQL
$ npm install mysql2 --save
# For MariaDB
$ npm install mariadb --save
# For PostgreSQL
$ npm install pg pg-hstore --save
# For MSSQL
$ npm install tedious --save
```
## Usage
```js
"use strict";
const { ServiceBroker } = require("moleculer");
const DbService = require("moleculer-db");
const SqlAdapter = require("moleculer-db-adapter-sequelize");
const Sequelize = require("sequelize");
const broker = new ServiceBroker();
// Create a Sequelize service for `post` entities
broker.createService({
name: "posts",
mixins: [DbService],
adapter: new SqlAdapter("sqlite://:memory:"),
model: {
name: "post",
define: {
title: Sequelize.STRING,
content: Sequelize.TEXT,
votes: Sequelize.INTEGER,
author: Sequelize.INTEGER,
status: Sequelize.BOOLEAN
},
options: {
// Options from http://docs.sequelizejs.com/manual/tutorial/models-definition.html
}
},
});
broker.start()
// Create a new post
.then(() => broker.call("posts.create", {
title: "My first post",
content: "Lorem ipsum...",
votes: 0
}))
// Get all posts
.then(() => broker.call("posts.find").then(console.log));
```
### Raw queries
You can reach the `sequelize` instance via `this.adapter.db`. To call [Raw queries](http://docs.sequelizejs.com/manual/raw-queries.html):
```js
actions: {
findHello2() {
return this.adapter.db.query("SELECT * FROM posts WHERE title = 'Hello 2' LIMIT 1")
.then(([res, metadata]) => res);
}
}
```
## Options
Every constructor arguments are passed to the `Sequelize` constructor. Read more about [Sequelize connection](http://docs.sequelizejs.com/manual/installation/getting-started.html).
**Example with connection URI**
```js
new SqlAdapter("postgres://user:pass@example.com:5432/dbname");
```
**Example with connection options**
```js
new SqlAdapter('database', 'username', 'password', {
host: 'localhost',
dialect: /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */,
pool: {
max: 5,
min: 0,
idle: 10000
},
// SQLite only
storage: 'path/to/database.sqlite'
dialectOptions: {
// only cockroachdb
ssl: {
rejectUnauthorized: false,
// For secure connection:
/*ca: fs.readFileSync('certs/ca.crt')
.toString()*/
},
},
});
```
# Test
```
$ npm test
```
In development with watching
```
$ npm run ci
```
# License
The project is available under the [MIT license](https://tldrlegal.com/license/mit-license).