项目作者: zxdong262

项目描述 :
A sequelize wrapper to support Sequelize 5+(partial) and dynamodb
高级语言: JavaScript
项目地址: git://github.com/zxdong262/dynamo-sequelize.git
创建时间: 2019-07-18T00:45:42Z
项目社区:https://github.com/zxdong262/dynamo-sequelize

开源协议:MIT License

下载


dynamo-sequelize

Build Status
Coverage Status

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

Use

  1. import Sequelize from 'sequelize'
  2. import SequelizeDynamo from 'dynamo-sequelize'
  3. // or const SequelizeDynamo = require('dynamo-sequelize')
  4. // Only when dialect === 'dynamo', will use dynamodb,
  5. // otherwise all args passes to Sequelize
  6. const sequelize = new SequelizeDynamo(
  7. 'sqlite://./db.sqlite',
  8. {
  9. define: {
  10. saveUnknown: false,
  11. timestamps: true,
  12. jsonAsObject: true // set false only if you upgrade from old db to be compatible with old db
  13. },
  14. logging: false,
  15. dialect: 'dynamo'
  16. }
  17. )
  18. const User = sequelize.define('User', {
  19. id: {
  20. type: Sequelize.STRING,
  21. primaryKey: true,
  22. defaultValue: generate
  23. },
  24. name: {
  25. type: Sequelize.STRING
  26. },
  27. email: {
  28. type: Sequelize.STRING
  29. },
  30. data: {
  31. type: Sequelize.JSON
  32. },
  33. date: {
  34. type: Sequelize.DATE
  35. }
  36. })
  37. User.prototype.ac = function() {
  38. return 'ac'
  39. }
  40. let before = await User.findAll()

Class methods

  1. User.find({
  2. limit: 1, // optional
  3. op: 'eq', //optional, could be 'contains'
  4. where: {
  5. name: 'x'
  6. }
  7. })
  8. User.findAll()
  9. User.getOne({
  10. where: {
  11. name: 'xxx'
  12. }
  13. })
  14. User.findOne({
  15. where: {
  16. id: 'xxx'
  17. }
  18. })
  19. User.findByPk('xxx')
  20. User.create({
  21. id: 'xxx'
  22. name: 'yyyy'
  23. })
  24. User.update({
  25. name: 'gggg'
  26. }, {
  27. where: {
  28. id: 'xxx'
  29. }
  30. })
  31. User.destroy({
  32. where: {
  33. id: 'xxx'
  34. }
  35. })
  36. User.destroy({
  37. where: {
  38. id: ['xxx', 'yyy'],
  39. }
  40. })
  41. User.batchGet([
  42. {
  43. id: 'xxx'
  44. },
  45. {
  46. id: 'yyy'
  47. }
  48. ])

JSON type with Object

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

  1. const sequelize = new SequelizeDynamo(
  2. '...',
  3. {
  4. define: {
  5. saveUnknown: true,
  6. timestamps: true,
  7. jsonAsObject: true,
  8. },
  9. logging: false,
  10. dialect: 'dynamo'
  11. }
  12. )

Instance methods

  1. const user = User.create({id : 'xx'})
  2. await user.destroy()
  3. user.name = 'yyy'
  4. await user.save()

check more from 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 for detail.
  • Supported data types:
  1. function typeMapper(type) {
  2. switch (type) {
  3. case Sequelize.STRING:
  4. case Sequelize.TEXT:
  5. return String
  6. case Sequelize.JSON:
  7. return Object
  8. case Sequelize.BOOLEAN:
  9. return Boolean
  10. case Sequelize.INTEGER:
  11. case Sequelize.BIGINT:
  12. case Sequelize.FLOAT:
  13. case Sequelize.DECIMAL:
  14. case Sequelize.DOUBLE:
  15. return Number
  16. case Sequelize.DATE:
  17. return Date
  18. default:
  19. throw new Error(`do not support type: ${type}`)
  20. }
  21. }

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)
  1. // do this
  2. const User = sequelize.define('User', {
  3. id: {
  4. type: Sequelize.STRING,
  5. primaryKey: true,
  6. defaultValue: generate
  7. },
  8. name: {
  9. type: Sequelize.STRING
  10. }
  11. })
  12. User.prototype.act = () => 'act'
  13. export default User
  14. // DO NOT do this
  15. const User = sequelize.define('User', {
  16. id: {
  17. type: Sequelize.STRING,
  18. primaryKey: true,
  19. defaultValue: generate
  20. },
  21. name: {
  22. type: Sequelize.STRING
  23. }
  24. })
  25. class SubUser extends User
  26. SubUser.prototype.act = () => 'act'
  27. 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

  1. # compile
  2. npm run build
  3. # test
  4. npm run jest

License

MIT