V
V
Vitaly2016-11-29 17:18:58
Node.js
Vitaly, 2016-11-29 17:18:58

How to get started with sequelize?

I can't figure out how to create a connection, where to start creating and using models.
An attempt to call User.findAll() crashes:
Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:8889

import * as DB from 'sequelize';

// import {connection} from "../db";
export const connection: DB.Sequelize = new DB('osbd', 'username', 'password', {
  host: 'localhost',
  port: 8889,
  dialect: 'mysql',
  
  pool: {
    max: 5,
    min: 0,
    idle: 10000
  },
});

export interface UserAttributes
{
  login: string;
  pass: string;
  name: string;
}

export interface UserInstance extends DB.Instance<UserInstance>, UserAttributes {}

export interface UserModel extends DB.Model<UserInstance, UserAttributes> {}

export const User: UserModel = connection.define<UserInstance, UserAttributes>('User', {
  login: {
    type: DB.STRING(64),
    unique: true,
  },
  pass: DB.STRING(32),
  name: DB.STRING(64),
});

User.findAll().then(console.log);

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly, 2016-11-29
@vitali1995

The solution was here:

export const connection: DB.Sequelize = new DB('bd', 'username', 'password', {
...
 dialectOptions: {
    socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock'
  },
...
});

P
pomeo, 2016-11-29
@pomeo

To start in the console
And then look at what structure he created and what lies inside. You can get away from this.

A
Alexander Wolf, 2016-11-29
@mannaro

mysql not running on 127.0.0.1:8889

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question