Skip to content

4.0 Model definition syntax #6524

Closed
Closed

Description

@felixfbecker

Because the dicussion is spread across many issues and PRs, I wanted to open a new issue to discuss the future syntax for model definition.

Currently, besides the old define(), this syntax is possible in v4.0.0-1:

export class User extends Model {
 
    get fullName() {
        return this.firstName + ' ' + this.lastName;
    }
 
    set fullName(fullName) {
        const names = fullName.split(' ');
        this.lastName = names.pop();
        this.firstName = names.join(' ');
    }
}
User.init(({
    username: {
        type: DataTypes.STRING,
        primaryKey: true
    },
    lastName: DataTypes.STRING,
    firstName: DataTypes.STRING,
}, { sequelize })

If you prefer to not do the initialization (which also attaches the connection to the model) inside the model file, but rather from a model index file, this is also possible:

export class User extends Model {
 
    get fullName() {
        return this.firstName + ' ' + this.lastName;
    }
 
    set fullName(fullName) {
        const names = fullName.split(' ');
        this.lastName = names.pop();
        this.firstName = names.join(' ');
    }

    static init(sequelize) {
        super.init({
            username: {
                type: DataTypes.STRING,
                primaryKey: true
            },
            lastName: DataTypes.STRING,
            firstName: DataTypes.STRING,
        }, { sequelize })
    }
}

With the help of a decorator library and Babel transpilation, this is also possible:

@Options({ sequelize })
@Attributes({
    username: {
        type: DataTypes.STRING,
        primaryKey: true
    },
    lastName: DataTypes.STRING,
    firstName: DataTypes.STRING,
})
export class User extends Model {
 
    get fullName() {
        return this.firstName + ' ' + this.lastName;
    }
 
    set fullName(fullName) {
        const names = fullName.split(' ');
        this.lastName = names.pop();
        this.firstName = names.join(' ');
    }
}

And with TypeScript you can go even further:

@Options({ sequelize })
export class User extends Model {
 
    @Attribute({
        type: DataTypes.STRING,
        primaryKey: true
    })
    public username: string;
 
    @Attribute(DataTypes.STRING)
    public firstName: string;
 
    @Attribute() // Type is inferred as DataTypes.STRING 
    public lastName: string;
 
    get fullName(): string {
        return this.firstName + ' ' + this.lastName;
    }
 
    set fullName(fullName: string) {
        const names = fullName.split(' ');
        this.lastName = names.pop();
        this.firstName = names.join(' ');
    }
}

BUT we definitely do not want to require the usage of any transpiler. The Model.init() API feels a bit weird, especially how the sequelize connection is passed. Some thoughts:

  • If we want to make models independent one day from connections, this is has the benefit of easily omitting the sequelize option
  • But we would probably still need to require that models are registered in a model manager, so we could inverse the syntax (something like sequelize.add(Model) instead of passing the connection. That is not implemented atm though, currently the model needs to know about the connection.

Please chime in and share your thoughts and ideas!

Issues/PRs for reference: #5898 #6439 #5877 #5924 #5205 #4728

Activity

  1. added
    status: in discussion
    on Aug 31, 2016
  2. added this to the 4.0 milestone on Aug 31, 2016
  3. felixfbecker commented on Aug 31, 2016

    @felixfbecker
    ContributorAuthor

    @leebenson

    The only small addition I'd love to see is a way to get the attributes defined in the class, similar to the syntax I referenced in #6439 (comment)

    @options({ tableName: 'user', sequelize }) class User extends Model {
    
     // Attributes can be specified by a decorator...
    @attribute name = {
       type: DataTypes.STRING,
       unique: true,
       // ...
     }
    
     // Class methods can be created using the `static` keyword
     static someClassMethod() {
       // `this` references the loaded sequelize instance... so we can get other models, etc.
     }
    
     // And instance methods are everything non-static
     someInstanceMethod() {
       // this.name, etc
     }
    }

    That's how Mobx do it, and adoption seems to be pretty good. Personally, I find the more that can be described succinctly in the body of the class, the easier it is to reason about what's a field, what's a class method and what's an instance, without having to define those in multiple places.

    So the attribute configuration is defined as an attribute name on the model prototype? Is that even supported syntax in Node 4/6? Is that the attribute initializer proposal? If yes, I don't think we should put the configuration directly on the prototype per-attribute. It should sit under attributes, as it does now. And we shouldn't try to make JavaScript look like TypeScript by abusing property initializers.

  4. leebenson commented on Aug 31, 2016

    @leebenson

    The decorator proposal is in flux, so I can't say whether that will be a future ECMAScript standard. That's basically what mobx do right now, and it works well.

    In the meantime, this is how I'm handling loading models in v4. I'd appreciate your advice if you think this abuses the current API in any way.

    Model loader

    (influenced by Sequelize CLI 2.3; updated to match my environment)

    // Checks whether we have the right env vars set; exits if not
    require('./env')('DB_NAME', 'DB_USER', 'DB_PASSWORD');
    
    import fs from 'fs';
    import path from 'path';
    import Sequelize from 'sequelize';
    
    // Delay returns a promise that resolves after the number of
    // milliseconds passed have elapsed.  This is useful for 'suspending'
    // an awaited statement inside of an async function (in other words -
    // to simulate a pause)
    import delay from 'delay';
    
    // Path to look for model files
    const modelPath = path.join(__dirname, 'db_models');
    
    // Open a new Sequelize client
    const sequelize = new Sequelize(
      process.env.DB_NAME, // database name
      process.env.DB_USER, // username
      process.env.DB_PASSWORD, // password
      {
        host: 'percona', // points to a linked Docker host
        dialect: 'mysql',
      },
    );
    
    // Load each model file
    export const models = Object.assign({}, ...fs.readdirSync(modelPath)
      .filter(function (file) {
        return (file.indexOf('.') !== 0) && (file.slice(-3) === '.js');
      })
      .map(function (file) {
        const model = require(path.join(modelPath, file)).default;
        return {
          [model.name]: model.init(model.fields, {
            sequelize,
          }),
        };
      })
    );
    
    // Load model associations
    for (const model of Object.keys(models)) {
      typeof models[model].associate === 'function' && models[model].associate(models);
    }
    
    // Ping checks that the database server can be connected to. It attempts
    // to connect every second for 60 seconds, before giving up and throwing
    // an error back up to the chain.  This function is especially useful when using
    // Docker Compose with a linked DB, which may not always be ready before
    // the API server starts
    export async function ping() {
      for (let i = 1; i <= 60; i++) {
        try {
          await sequelize.authenticate();
          return;
        } catch (e) {
          console.log(`DB connection attempt #${i} failed, retrying...`);
          await delay(1000);
        }
      }
    
      throw new Error("Couldn't connect to database!");
    }

    Sample Model definition (in this case, 'Session')

    export default class Session extends Model {
    
      static fields = {
        id: {
          type: Sequelize.UUID,
          defaultValue: Sequelize.UUIDV4,
          primaryKey: true,
        },
    
        browser: {
          type: Sequelize.TEXT,
        },
    
        expiresAt: {
          type: Sequelize.DATE,
          defaultValue() {
            return moment().add(30, 'days').toDate();
          },
        },
    
        whatever: {
          type: Sequelize.VIRTUAL,
          get() {
            return 'it worked!';
          },
        },
    
        idAgain: {
          type: Sequelize.VIRTUAL,
          get() {
            return this.get('id');
          },
        }
      };
    
      // Session middleware that Koa uses to create a `ctx.session` object
      // for all subsequent middleware. (method appears at class-level and
      // *not* on the instance
    
      static middleware() {
    
        // We'll return this as an async function, so that it can be invoked
        // in the Koa config and bound to the currently loaded model
        return async function (ctx, next) {
          // ... omitted for brevity
        }.bind(this);
      }
    
      // This function is instance-level... it'll bind to the active record
      instanceFunction() {
        return true;
      }
    }

    Invoking the above inside an async function seems to work perfectly...

    import * as db from './dbLoader';
    
    (async function () {
      // Wait for the DB to be ready
      await db.ping();
    
      // Start a new instance
      const s = new db.models.Session;
    
      // Make sure we've got what we expect...
      console.log('s.id ->', s.id);
      console.log('s.idAgain ->', s.idAgain);
      console.log('s.middleware -> ', s.middleware);
      console.log('s.fields ->', s.fields);
      console.log('s.whatever ->', s.whatever);
      console.log('s.instanceFunction() ->', s.instanceFunction());
    
      // Plus the static method means I can wire up things that belong at
      // the class-level, like middleware
      const app = new Koa;
      app.use(db.models.Session.middleware());
    })();

    ... which returns the expected output:

    server_1 | Executing (default): SELECT 1+1 AS result
    server_1 | s.id -> 5c3f792a-c66f-442d-96f8-ccbb630f7bae
    server_1 | s.idAgain -> 5c3f792a-c66f-442d-96f8-ccbb630f7bae
    server_1 | s.middleware -> undefined
    server_1 | s.fields -> undefined
    server_1 | s.whatever -> it worked!
    server_1 | s.instanceFunction() -> true

  5. leebenson commented on Aug 31, 2016

    @leebenson

    A few reasons why I chose the syntax in the above post.

    • IMO, decorators proved to be overkill; assigning a static fields object at the class level gives me the 'class encapsulation' I was looking for without relying on an ECMAScript proposal that's in flux.
    • Defining static/instance methods in one place and fields in another looked disjointed... this solves it by placing field defs inside the class, without bleeding out to the instance.
    • The model loader wires up the models, so all we need to care about is creating a class that extends Sequelize.Model and exporting it as the default. This is a good separation of concerns, I think - previously, I was doing the sequelize.define() separately inside of each model file and wound up with repeated boilerplate for every new table definition.
    • Table names are implicitly added from the class name, reducing repetition with an explicit tableName property.
  6. felixfbecker commented on Sep 1, 2016

    @felixfbecker
    ContributorAuthor

    @leebenson

    IMO, decorators proved to be overkill; assigning a static fields object at the class level gives me the 'class encapsulation' I was looking for without relying on an ECMAScript proposal that's in flux.

    See my previous comment. Yes, decorators are still a proposal, but the complete Angular 2 framework is already built on it and that is now at a release candidate, people already use this in production and it works very well.

    Please note that class properties can not be used in Node 4/6. It is a Stage 1 proposal, while decorators are already Stage 2. So the API you are proposing will not work without transpilation either, just like a decorator API.

    I think aestetically, a static property and a decorator are on the same level, but decorators fit the use case better semantically (you decorate the class with metadata) and they have the benefit of normalization, while the class property would result in unexpected behaviour and would still require a seperate .init() call.

    Defining static/instance methods in one place and fields in another looked disjointed

    I agree.

    Table names are implicitly added from the class name, reducing repetition with an explicit tableName property.

    This is already implemented in 4.0.0-1. The tableName was just an example of passing options, I removed it to clarify it.

    The model loader wires up the models, so all we need to care about is creating a class that extends Sequelize.Model and exporting it as the default. This is a good separation of concerns, I think - previously, I was doing the sequelize.define() separately inside of each model file and wound up with repeated boilerplate for every new table definition.

    I don't get what you are trying to say. Imo a model file should be as self-contained as possible. I actually even use late imports instead of the module.exports = function(sequelize, DataTypes) { pattern, so I don't even need a model index file that "wires up" the models, they wire up themselves, including associations. I tried to highlight a bit of this in my PR to update the docs, see https://github.com/felixfbecker/sequelize/blob/76590ab17ad8d160272a7e02bd7b381bc49ae919/docs/docs/models-definition.md#one-file-per-model-pattern

  7. leebenson commented on Sep 1, 2016

    @leebenson

    See my previous comment. Yes, decorators are still a proposal, but the complete Angular 2 framework is already built on it and that is now at a release candidate, people already use this in production and it works very well.

    No arguments there. I'm already using decorators with mobx too, and I love them! I just couldn't find a better fit for wiring up field definitions than a static property on the class. A decorator function seemed like overkill.

    I don't get what you are trying to say. Imo a model file should be as self-contained as possible. I actually even use late imports instead of the module.exports = function(sequelize, DataTypes) { pattern, so I don't even need a model index file that "wires up" the models, they wire up themselves, including associations. I tried to highlight a bit of this in my PR to update the docs, see https://github.com/felixfbecker/sequelize/blob/76590ab17ad8d160272a7e02bd7b381bc49ae919/docs/docs/models-definition.md#one-file-per-model-pattern

    What I'm saying is that defining field types within the body of the class seems like a better approach.

    In your example, you do this...

    class Pub extends Sequelize.Model { }
    Pub.init({
      name: Sequelize.STRING,
      address: Sequelize.STRING,
      latitude: {
        type: Sequelize.INTEGER,
        allowNull: true,
        defaultValue: null,
        validate: { min: -90, max: 90 }
      },
      longitude: {
        type: Sequelize.INTEGER,
        allowNull: true,
        defaultValue: null,
        validate: { min: -180, max: 180 }
      }
    }, {
      sequelize,
      validate: {
        bothCoordsOrNone() {
          if ((this.latitude === null) !== (this.longitude === null)) {
            throw new Error('Require either both latitude and longitude or neither')
          }
        }
      }
    })

    This part irks me...

    class Pub extends Sequelize.Model { }
    Pub.init({
    // ...
    })

    You've created a class to extend Model, but it's blank. Presumably the only benefit here is that you've removed the need to explicitly define tableName.

    Then in a separate call, you initialise it with .init() and feed in the field definitions.

    IMO, it looks better to keep them defined within the class. Whether it's a decorator or a static property is less relevant... more so is that syntax outside of the class makes it hard to reason about exactly what fields the model contains. I have 500+ lines of field definitions and boilerplate in some places... I'd rather keep that inside the class than stick it where my .init() code goes, which could well be somewhere outside of the model definition entirely.

    Just my personal preference.

  8. felixfbecker commented on Sep 1, 2016

    @felixfbecker
    ContributorAuthor

    Yes, init() after the class is definitely the ugly part about the API. But as I said, there is no way to do it without using a transpiler atm (and then decorators are a better solution). And we of course also still have define().

  9. leebenson commented on Sep 1, 2016

    @leebenson

    Another option is static method... they've been in Node since 4+ and don't require any transpilation at all:

    class Session extends Sequelize.Model {
      static fields() {
        return {
          id: {
            type: Sequelize.UUID,
            // ....
          }
        }
      }
    }
  10. mickhansen commented on Sep 1, 2016

    @mickhansen
    Contributor

    @felixfbecker Is the model name taken from the class name? In that case we need a way to override it because a lot of people generally have lower case model names but would probably have lint rules telling them to have uppercase class names

  11. felixfbecker commented on Sep 1, 2016

    @felixfbecker
    ContributorAuthor

    @leebenson Yes, but imo having a method that always returns the same configuration object just because the language doesn't have property declarations yet is syntax abuse and DSLish.

    @mickhansen yes, the model name is the name property of the class, which is the class name. It is a native property of all functions. It can be overridden with the modelName option or when using sequelize.define(). But to be honest I think we should use PascalCase naming in the docs because it is confusing when User.name !== 'User'.

  12. leebenson commented on Sep 1, 2016

    @leebenson

    @felixfbecker re: lower casing, I'm currently doing it explicitly like this (in my model loader)

    import utils from 'sequelize/lib/utils';
    
    // Load each model file
    export const models = Object.assign({}, ...fs.readdirSync(modelPath)
      .filter(function (file) {
        return (file.indexOf('.') !== 0) && (file.slice(-3) === '.js');
      })
      .map(function (file) {
        const model = require(path.join(modelPath, file)).default;
        return {
          [model.name]: model.init(
            (typeof model.fields === 'function' && model.fields()) || {},
            Object.assign({
                sequelize,
                tableName: utils.pluralize(model.name.toLowerCase()), // <--- this bit
              },
              (typeof model.attributes === 'function' && model.attributes()) || {},
            ),
          ),
        };
      })

    With my session definitions now looking closer to this:

    export default class Session extends Model {
    
      static fields() {
        return {
          id: {
            type: Sequelize.UUID,
            defaultValue: Sequelize.UUIDV4,
            primaryKey: true,
          },
         // ....
        };
      }
    
    //.....
    }

    So a Session class becomes a sessions table.

    I agree with you that a method returning the same static data isn't ideal. It's an alternative to requiring a transpilation step, though, and it's only run once. Plus, it's an opportunity to bind the current sequelize client as a parameter, in case definitions somehow rely on it.

    Hard to get that with a static class attribute or a decorator that's outside of the definition scope.

  13. felixfbecker commented on Sep 1, 2016

    @felixfbecker
    ContributorAuthor

    Please note that tableName and modelName are two different options.

    Plus, it's an opportunity to bind the current sequelize client as a parameter, in case definitions somehow rely on it.

    I am highly against this. In a static method, I expect this to be the class. Or are you not talking about binding?

    Hard to get that with a static class attribute or a decorator that's outside of the definition scope.

    It's super simple to share the sequelize instance is to simply have a module that exports it, and require it in the model files.

  14. mickhansen commented on Sep 1, 2016

    @mickhansen
    Contributor

    But to be honest I think we should use PascalCase naming in the docs because it is confusing when User.name !== 'User'

    Definitely, just need to make sure it's possible to reconfigure

  15. leebenson commented on Sep 1, 2016

    @leebenson

    Please note that tableName and modelName are two different options.

    I know, but in the absence of either being set explicitly, the pluralised version of class.name is used, right?

    I think this is what @mickhansen was referring to. It threw me off too. Hence the pluralize hack above. Model.init ought to lowercase the class name before it implicitly pluralises it for use as a model/table name.

    I am highly against this. In a static method, I expect this to be the class. Or are you not talking about binding? The best pattern to share the sequelize instance is to simply have a module that exports it, and require it in the model files.

    Binding as a parameter. Not to this.

    So you can do something like...

    class Session extends Sequelize.Model {
      static fields(sequelize, DataTypes) {
        return {
          id: {
            type: DataTypes.UUID,
            // ....
          }
        }
      }
    }

    I do use this elsewhere on my class, btw, for things like GraphQL...

    class Session extends Sequelize.Model {
    
      // GraphQL public mutations
      static mutations(db, types) {
    
        // models = access to `db`, which contains db.models.*, db.sequelize, etc
        // types = GraphQL types, defined outside of this class
    
        return {
          logout: {
            type: types.Session,
            async resolve(root, args, ctx) {
              // ctx = current Koa context for the web user
    
              await ctx.session.logout();
              return ctx.session;
            },
          },
        };
      }
    }

    It keeps model logic neatly in one place, without polluting any model instance.

  16. 56 remaining items

  17. epicbagel commented on Jul 10, 2017

    @epicbagel

    @leebenson one option is to spin this off into a separate library (e.g. sequelize-model-builder) that could hide some of the messiness with "init" rather than working to get this into Sequelize while annotations still require transpilation.

    It's something I've considered working on. We have a large number of model definitions and the approach you posted above is along the lines of the direction we'll like to take.

  18. dcworldwide commented on Jul 10, 2017

    @dcworldwide

    For typescript users I think typeorm completely nailed their orm api. https://github.com/typeorm/typeorm. Well worth reviewing for inspiration.

  19. felixfbecker commented on Jul 10, 2017

    @felixfbecker
    ContributorAuthor

    What exactly is the "messiness" with init? It's just a static constructor. It needs to normalise options, and initialises the model. This is not possible with static constructors, and decorators are simply a way to invoke that logic.


    Both typeorm and sequelize-typescript use terrible patterns for associations that will plain out break with native ES6 modules in Node:

    @HasMany(() => User)

    this is so incredibly hacky, a cyclic dependency that only works because of TS compiler implementation details when transpiling to CommonJS.

    Why does a "TypeScript library" for Sequelize have so many lines of code?!
    https://github.com/felixfbecker/sequelize-decorators is 39 lines and requires no init call either.

  20. snewell92 commented on Jul 10, 2017

    @snewell92

    re: "terrible patterns" Ough! Ouch. 🗡 💀

    re: messiness
    In regards to the messiness... yeah I stand with felix on this one. It's just not messy enough really messy at all to warrant wrapping the initialization process nor factoring that out. In my context, I prefer the single class with minimal decorator approach because that communicates more to my team.

    However, if I wasn't constrained by communicating to an older team, I would rather just use sequelize plainly ('idiomatically' if you will). If I wanted to cut down on lines of code, the decorator syntax OR static init would be leaner than what I have now.

    None of what @felixfbecker or @leebenson have suggested above is messy, it's an improvement over what existed.

    If you disagree, provide code samples to try to demonstrate some friction in the way you define your models and the way you wish you could define them, and then discussion can proceed, but I'm not seeing anything messy (even as someone who prefers the class approach).

    Implementation aside

    Also, if the friction is just a repeated init function for all your models, and you think it's violating SRP... you can factor that out in its own function, or perhaps have all your models inherit from the same prototype, or perhaps do a more oopy 'base class' dealio.

    The problem you'd run into is I'm willing to bet not all your init functions are created equal. There's some good reasons the sequelize team has it still.

    As it stands, Sequelize lets you choose how to do it. We are using JavaScript after all , anything is possible !

    You could even create the model builder yourself as a specialized model factory if you really wanted to. If you want it, just do it! Then share it here and the sequelize team will use what they think is valuable, and keep in userland what they believe to be best left in userland.

  21. leebenson commented on Jul 10, 2017

    @leebenson

    We are using JavaScript after all, anything is possible!

    Except operator overloads, first-class decorator support, list comprehensions, transactions that don't get lost inside of Promise chains... 😛

  22. keyiis commented on Aug 21, 2017

    @keyiis

    sequelize come on! ! ! i won't change to another orm(typeorm),i hope decorator become standard library in sequelize code and doc!

  23. RobinBuschmann commented on Aug 26, 2017

    @RobinBuschmann
    Member

    @HasMany(() => User)
    this is so incredibly hacky, a cyclic dependency that only works because of TS compiler implementation details when transpiling to CommonJS.

    @felixfbecker This is not true. ES6 modules are indeed able to handle cyclic dependencies. So this is not hacky at all and has nothing to do with 'implementation details' of the tsc.

    There are some browser(chrome canary, safari technology preview) out there, which implements ES6 modules natively. If you run the following code snippets (which includes a cyclic dependency) in one of these browsers, you will see that it works very well.

     // a.js
    import {b} from './b.js';
    
    export function a() {
        return b();
    }
    
    export function a1() {
        return 'a1';
    }
    // b.js
    import {a1} from './a.js';
    
    export function b() {
        return 'b' + a1();
    }
    // index.js
    import {a} from './a.js';
    
    console.log(a());
    <script type="module" src="index.js"></script>
  24. zhanwenchen commented on Sep 15, 2017

    @zhanwenchen
    Contributor

    After all night of tweaking, I finally have a working example, based on @leebenson and @felixfbecker's posts on this thread. @snewell92 take a look at this.

    @felixfbecker one needs to explicitly return super.init({...}, {sequelize}); in static init(...). The child init function should also take a sequelize instance.

    To use, put all 4 files in an empty directory and do node index.js.

    @mickhansen In my example, using Node 6.11.3 LTS, I did not need a transpiler for static.

    index.js
    // models/index.js
    /**
      index.js is an import utility that grabs all models in the same folder,
      and instantiate a Sequelize object once for all models (instead of for each model).
      This is done by passing the single Sequelize object to each
      model as a reference, which each model then piggy-backs (sequelize.define())
      for creating a single db class model.
    */
    
    "use strict"; // typical JS thing to enforce strict syntax
    
    const fs = require("fs"); // file system for grabbing files
    const path = require("path"); // better than '\/..\/' for portability
    const Sequelize = require("sequelize"); // Sequelize is a constructor
    const env = process.env.NODE_ENV || "development"; // use process environment
    const config = require(path.join(__dirname, '..', 'config.js'))[env] // Use the .config.json file in the parent folder
    const sequelize = new Sequelize(config.database, config.username, config.password, {
      dialect: config.dialect,
    });
    
    // Load each model file
    const models = Object.assign({}, ...fs.readdirSync(__dirname)
      .filter(file =>
        (file.indexOf(".") !== 0) && (file !== "index.js")
      )
      .map(function (file) {
        const model = require(path.join(__dirname, file));
        // console.log(model.init(sequelize).tableName)
        return {
          [model.name]: model.init(sequelize),
        };
      })
    );
    
    // Load model associations
    for (const model of Object.keys(models)) {
      typeof models[model].associate === 'function' && models[model].associate(models);
    }
    
    module.exports = models;
    Post.js
    // models/Post.js
    /**
      Post.js
      Class model for Post
    */
    
    'use strict';
    
    const Sequelize = require('sequelize');
    
    module.exports =
      class Post extends Sequelize.Model {
        static init(sequelize) {
          return super.init({
            title: {
              type: Sequelize.STRING,
              allowNull: false,
            },
            body: {
              type: Sequelize.TEXT,
              allowNull: false,
            },
            assets: {
              type: Sequelize.JSON,
              allowNull: true
            },
          }, { sequelize })
        };
    
        static associate(models) {
          // Using additional options like CASCADE etc for demonstration
          // Can also simply do Task.belongsTo(models.Post);
          this.hasMany(models.Comment, {
            onDelete: "CASCADE",
            foreignKey: {
              allowNull: false
            }
          });
    
          // Using additional options like CASCADE etc for demonstration
          // Can also simply do Task.belongsTo(models.Post);
          this.belongsTo(models.User, {
            onDelete: "CASCADE",
            foreignKey: {
              allowNull: false
            }
          });
        }
      }
    User.js
    // models/User.js
    /**
      User.js
      Class model for User
    */
    
    'use strict';
    
    const Sequelize = require('sequelize');
    
    module.exports =
      class User extends Sequelize.Model {
        static init(sequelize) {
          return super.init({
            firstName: {
              type: Sequelize.STRING,
              allowNull: false,
            },
            lastName: {
              type: Sequelize.STRING,
              allowNull: false,
            },
            username: {
              type: Sequelize.STRING,
              allowNull: false,
            },
            password_hash: {
              type: Sequelize.STRING,
              allowNull: false,
            },
            salt: {
              type: Sequelize.STRING,
              allowNull: false,
            },
            email: {
              type: Sequelize.STRING,
              allowNull: false,
              validate: {
                isEmail: true
              }
            },
            isActive: {
              type: Sequelize.BOOLEAN
            }
          }, { sequelize })
        };
    
        static associate(models) {
          // Using additional options like CASCADE etc for demonstration
          // Can also simply do Task.belongsTo(models.User);
          this.hasMany(models.Post, {
            onDelete: "CASCADE",
            foreignKey: {
              allowNull: false
            }
          });
    
          // Using additional options like CASCADE etc for demonstration
          // Can also simply do Task.belongsTo(models.User);
          this.hasMany(models.Comment, {
            onDelete: "CASCADE",
            foreignKey: {
              allowNull: false
            }
          });
        }
      }
    Comment.js
    // models/Comment.js
    /**
      Comment.js
      Class model for Comment
    */
    
    'use strict';
    
    const Sequelize = require('sequelize');
    
    module.exports =
      class Comment extends Sequelize.Model {
        static init(sequelize) {
          return super.init({
            title: {
              type: Sequelize.STRING,
              allowNull: false,
            },
            body: {
              type: Sequelize.TEXT,
              allowNull: false,
            }
          }, { sequelize })
        };
    
        static associate(models) {
          // Using additional options like CASCADE etc for demonstration
          // Can also simply do Task.belongsTo(models.Comment);
          this.belongsTo(models.User, {
            onDelete: "CASCADE",
            foreignKey: {
              allowNull: false
            }
          });
    
          // Using additional options like CASCADE etc for demonstration
          // Can also simply do Task.belongsTo(models.Comment);
          this.belongsTo(models.Post, {
            onDelete: "CASCADE",
            foreignKey: {
              allowNull: false
            }
          });
        }
      }
  25. snewell92 commented on Sep 15, 2017

    @snewell92

    Dang, neat!

    Related tidbit

    This is not strictly related, but I do something similar for initializing services in feathers by using fs to read a directory structure.

    One thing I commented on for a future item, is that if any of my services depend on one another, the way I'm initializing my services won't work.

    However, the way sequelize splits out associating models together after the models are all initialized could apply to that domain too. And thus, as long as my services aren't using each other in the init phase, I could compose them or use them in the associate phase. Exactly how your POC works.

  26. sylver commented on Nov 6, 2017

    @sylver
    Contributor

    Great post and thanks @zhanwenchen for this very detailed example which helped me a lot !

    However I'm still having issues figuring out how to define hooks, getters and setters in this case. I tried static methods, instance methods, direct definition like MyModel.beforeCreate = ..., etc. I'm unable to get it triggered whatever I try.

    Would you mind to give an example of the right way to do it please ?

  27. UnbrandedTech commented on Jan 17, 2018

    @UnbrandedTech

    @Sylv3r SUPER late to the game but I think you can add it to the init options...

    class User extends Model {
        static init(sequelize){
            return super.init({
               //definition
            }, {
                sequelize,
                hooks: {
                    beforeCreate: User.beforeCreate,
                    afterCreate: User.afterCreate,
                    beforeUpdate: User.beforeUpdate
                }
            })
        }
    }

    Haven't tested this, still researching if this is an ok pattern to follow. (The whole class extends thing)

  28. skrubinator88 commented on Feb 15, 2018

    @skrubinator88

    Wow this helped me a lot, thanks a ton!

  29. jnnkky commented on Mar 17, 2018

    @jnnkky

    Is there a reason why this approach is not documented anywhere in the documentation? Should I be using this or the normal .define way?

  30. UnbrandedTech commented on May 2, 2018

    @UnbrandedTech

    Just to confirm, my previous model does indeed work and is in production.

  31. tsirolnik commented on Oct 15, 2018

    @tsirolnik
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: in discussion

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions