Giter Club home page Giter Club logo

moldymeat's Introduction

MoldyMeat

Effortlessly keep your database's tables in sync with your Sequelize models.

Rationale

Migrations should be a thing of the past. Here's why:

  1. They waste programmer-hours to debug when they break
  2. Depending on the framework, they have to be hand-written (violating DRY)
  3. They can make it difficult for new developers to be onboarded

Usage

MoldyMeat is a simple library that doesn't take any options and Just Works (tm). Here's how to use it:

const sequelize = /* create your Sequelize instance */;

const Model = sequelize.define(...);
const AnotherModel = sequelize.define(...);
// ...more models

const moldyMeat = new MoldyMeat({sequelize});
await moldyMeat.initialize();
await moldyMeat.updateSchema();

// Now, you do that voodoo that you do so well

After calling moldyMeat.updateSchema(), you are able to use your models as if you had written and ran migrations for them.

Hints

Unfortunately, there's not a NodeJS framework for reading minds (yet...), so there has to be a way to figure out if you, the developer, renamed a field, or just removed a field/column/attribute and created a semantically different (but structurally similar) one (or did something ambiguous like that). ORMs like the one in Django just ask the developer when they generate migrations, so MoldyMeat is forced to do something similar.

Enter hints.

Hints are the saved responses to questions moldymeat asks when it runs into an ambiguous data model change. They're stored locally, and are designed to be able to be commited to SCM. They're also designed to be deleteable - Once all databases that need updating have been updated (i.e. you updated production), they can be safely deleted (with those deletions being committed to SCM).

Using hints is as easy as calling updateSchema differently. In a development environment, you would call updateSchema with the generateHints argument set to true:

await moldyMeat.updateSchema({generateHints: true});

This tells MoldyMeat to generate hints by prompting you, the developer.

In production, you omit the argument:

await moldyMeat.updateSchema(); // defaults to generateHints=false, useHints=true

To turn off hints completely, set the useHints argument to false:

await moldyMeat.updateSchema({useHints: false}); // defaults to generateHints=false

How MoldyMeat works

Below is a diagram showing what happens when you use MoldyMeat's updateSchema method.

sequenceDiagram
    actor Developer
    participant Sequelize
    participant MoldyMeat
    participant Database
    Developer->>Sequelize: Define models
    activate Developer
    Developer->>MoldyMeat: Run updates
    deactivate Developer
    activate MoldyMeat
    MoldyMeat->>Sequelize: Load defined models
    Sequelize->>MoldyMeat: 
    MoldyMeat->>MoldyMeat: Build state tree from models
    MoldyMeat->>Database: Load state tree after last update
    Database->>MoldyMeat: 
    MoldyMeat->>MoldyMeat: Diff previous and newly built state trees
    MoldyMeat->>Developer: Ask about ambiguous changes
    Developer->>MoldyMeat: 
    MoldyMeat->>Sequelize: Generate SQL to perform update
    Sequelize->>MoldyMeat: 
    MoldyMeat-xDatabase: Run update SQL
    deactivate MoldyMeat
Loading

moldymeat's People

Contributors

natesymer avatar

Stargazers

Sander Mol avatar Rik Smale avatar Jared Wright avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.