npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dynamo-sequelize

v3.1.0

Published

A sequelize wrapper to support Sequelize 6+ and Dynamodb

Downloads

190

Readme

dynamo-sequelize

Build Status Coverage Status

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

Use

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

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:

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

Instance methods

const user = User.create({id : 'xx'})
await user.destroy()
user.name = 'yyy'
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:
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)

// 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

# compile
npm run build

# test
npm run jest

License

MIT