# dynamo-sequelize

> A sequelize wrapper to support Sequelize 6+ and Dynamodb

Latest version **3.1.0** (published 2022-07-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install dynamo-sequelize
pnpm add dynamo-sequelize
yarn add dynamo-sequelize
bun add dynamo-sequelize
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.0 |
| Published | 2022-07-19 |
| First published | 2019-07-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 31.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | ZHAO Xudong |
| Maintainers | zxdong262 |
| Keywords | Sequelize, Dynamodb |

## Links

- npm: https://www.npmjs.com/package/dynamo-sequelize
- Repository: https://github.com/zxdong262/dynamo-sequelize
- Homepage: https://github.com/zxdong262/dynamo-sequelize#readme
- Issues: https://github.com/zxdong262/dynamo-sequelize/issues
- npm.io page: https://npm.io/package/dynamo-sequelize

## Dependencies (3)

- [dynamoose](https://npm.io/package/dynamoose.md) ^2.8.5
- [sequelize](https://npm.io/package/sequelize.md) ^6.16.1
- [lodash.get](https://npm.io/package/lodash.get.md) ^4.4.2

## Recent versions

- 3.1.0 (latest) — 2022-07-19
- 3.0.0 — 2022-03-17
- 2.1.1 — 2022-03-17
- 2.1.0 — 2022-02-26
- 2.0.1 — 2022-02-22
- 2.0.0 — 2022-02-22
- 1.0.8 — 2022-02-17
- 1.0.7 — 2022-02-16
- 1.0.6 — 2022-02-15
- 1.0.5 — 2022-02-15
- 1.0.4 — 2022-02-15
- 1.0.3 — 2022-02-11
- 1.0.2 — 2022-02-11
- 1.0.1 — 2022-02-11
- 1.0.0 — 2022-02-10
- … 28 more at https://npm.io/package/dynamo-sequelize/versions

## README

# dynamo-sequelize

[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fatrox%2Fsync-dotenv%2Fbadge)](https://github.com/zxdong262/dynamo-sequelize/actions)
[![Coverage Status](https://coveralls.io/repos/github/zxdong262/dynamo-sequelize/badge.svg?branch=release)](https://coveralls.io/github/zxdong262/dynamo-sequelize?branch=release)

A sequelize wrapper to support Sequelize and Dynamodb(limited).

## Use

```js
import Sequelize from 'sequelize'
import SequelizeDynamo from 'dynamo-sequelize'
// or const SequelizeDynamo = require('dynamo-sequelize')

// Only when dialect === 'dynamo', will use dynamodb,
// otherwise all args passes to Sequelize
const sequelize = new SequelizeDynamo(
  'sqlite://./db.sqlite',
  {
    define: {
      saveUnknown: false,
      timestamps: true,
      jsonAsObject: true // set false only if you upgrade from old db to be compatible with old db
    },
    logging: false,
    dialect: 'dynamo'
  }
)
const User = sequelize.define('User', {
  id: {
    type: Sequelize.STRING,
    primaryKey: true,
    defaultValue: generate
  },
  name: {
    type: Sequelize.STRING
  },
  email: {
    type: Sequelize.STRING
  },
  data: {
    type: Sequelize.JSON
  },
  date: {
    type: Sequelize.DATE
  }
})
User.prototype.ac = function() {
  return 'ac'
}
let before = await User.findAll()
```

## Class methods

```js
User.find({
  limit: 1, // optional
  op: 'eq', //optional, could be 'contains'
  where: {
    name: 'x'
  }
})
User.findAll()
User.getOne({
  where: {
    name: 'xxx'
  }
})
User.findOne({
  where: {
    id: 'xxx'
  }
})
User.findByPk('xxx')
User.create({
  id: 'xxx'
  name: 'yyyy'
})
User.update({
  name: 'gggg'
}, {
  where: {
    id: 'xxx'
  }
})
User.destroy({
  where: {
    id: 'xxx'
  }
})
User.destroy({
  where: {
    id: ['xxx', 'yyy'],
  }
})
User.batchGet([
  {
    id: 'xxx'
  },
  {
    id: 'yyy'
  }
])
```

## JSON type with Object

By default, it will save JSON object as String in dynamodb. To save JSON object as Object in dynamodb:

```js
const sequelize = new SequelizeDynamo(
  '...',
  {
    define: {
      saveUnknown: true,
      timestamps: true,
      jsonAsObject: true,
    },
    logging: false,
    dialect: 'dynamo'
  }
)
```

## Instance methods

```js
const user = User.create({id : 'xx'})
await user.destroy()
user.name = 'yyy'
await user.save()
```

check more from [tests/dynamo.spec.js](tests/dynamo.spec.js)

## Supported features && limitations

- Enable dynamodb only when `dialect === 'dynamo'`
- Only support Model deinfe by `User.define`
- Only support Model methods: `find`, `findAll`, `findOne`, `create`, `findByPk`, `update`, `destroy`, `batchGet`, `getOne`.
- Only support instance/document methods: `destroy`, `save`.
- `find`, `findOne`, `getOne`, `findAll`, `update` and `destroy` only support `where` query.
- All `where` query keys must have non empty value.
- Set envs through .env file, check [.env.sample](.env.sample) for detail.
- Supported data types:

```js
function typeMapper(type) {
  switch (type) {
    case Sequelize.STRING:
    case Sequelize.TEXT:
      return String
    case Sequelize.JSON:
      return Object
    case Sequelize.BOOLEAN:
      return Boolean
    case Sequelize.INTEGER:
    case Sequelize.BIGINT:
    case Sequelize.FLOAT:
    case Sequelize.DECIMAL:
    case Sequelize.DOUBLE:
      return Number
    case Sequelize.DATE:
      return Date
    default:
      throw new Error(`do not support type: ${type}`)
  }
}
```

## User tip about performance

- Model methods: `find`, `findAll`, `getOne` use dynamodb scan, so be careful, in big dataset, this may cost unacceptable time.

## Use guide

- model created by sequelize.define can not be extended (since v2.x)

```js

// do this
const User = sequelize.define('User', {
  id: {
    type: Sequelize.STRING,
    primaryKey: true,
    defaultValue: generate
  },
  name: {
    type: Sequelize.STRING
  }
})

User.prototype.act = () => 'act'
export default User

// DO NOT do this
const User = sequelize.define('User', {
  id: {
    type: Sequelize.STRING,
    primaryKey: true,
    defaultValue: generate
  },
  name: {
    type: Sequelize.STRING
  }
})

class SubUser extends User

SubUser.prototype.act = () => 'act'

export default SubUser

```

## Why/when to use it

Sequelize is really easy to use, just lack dynamodb support, while for AWS Lambda user, Dynamodb ease the pain of VPS settings, ideal for lightweight services. With this module you may migrate to Dynamodb easily.

## Build/test

```bash
# compile
npm run build

# test
npm run jest
```

## License

MIT

---
_Source: https://npm.io/package/dynamo-sequelize · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
