existing DB in mysql wan't to use persistance

12 views
Skip to first unread message

bob.l...@halton.ca

unread,
May 8, 2015, 10:53:12 AM5/8/15
to sequ...@googlegroups.com, bosko...@hotmail.com
I'd like to use sequelize to persist my objects in an existing DB.  Until now I've been using manual queries with node-mysql but it's getting to cumbersome.  I've read some of the documentation but I don't really understand how to use it for an existing DB.  For instance the DB has a widgets table with PK id and then I have a properties table and each property has an FK pointing to the id of widgets.  there are many properties that each point to the id of a particular widget.  This seems like it should be easy yet I don't even know where to start because the documentation asumes I'm creating the tables within sequelize and not starting with an existing DB that's already populated with values.  Does anyone have a link where I can see someone else doing a similar project?

Mick Hansen

unread,
May 8, 2015, 10:59:01 AM5/8/15
to bob.l...@halton.ca, sequ...@googlegroups.com, bosko...@hotmail.com
You can simply create your sequelize definitions to match those of your tables.
You dont have to use sync().

You can define custom primary keys by defining an attribute with primaryKey: true and autoIncrement: true.
For associations you can use the foreignKey options to define a custom/specific foreignKey attribute.

On Fri, May 8, 2015 at 4:53 PM, <bob.l...@halton.ca> wrote:
I'd like to use sequelize to persist my objects in an existing DB.  Until now I've been using manual queries with node-mysql but it's getting to cumbersome.  I've read some of the documentation but I don't really understand how to use it for an existing DB.  For instance the DB has a widgets table with PK id and then I have a properties table and each property has an FK pointing to the id of widgets.  there are many properties that each point to the id of a particular widget.  This seems like it should be easy yet I don't even know where to start because the documentation asumes I'm creating the tables within sequelize and not starting with an existing DB that's already populated with values.  Does anyone have a link where I can see someone else doing a similar project?



--
Mick Hansen
@mhansendev
mhansen.io

bob.l...@halton.ca

unread,
May 8, 2015, 12:06:05 PM5/8/15
to sequ...@googlegroups.com, bob.l...@halton.ca, bosko...@hotmail.com
Thank you for your reply.  This being my first time using sequelize, I'm still a little lost.
so let's assume I'm looking at the example at http://docs.sequelizejs.com/en/latest/docs/getting-started/

when I get to:

var User = sequelize.define('user', {
  firstName: {
    type: Sequelize.STRING,
    field: 'first_name' // Will result in an attribute that is firstName when user facing but first_name in the database
  },
  lastName: {
    type: Sequelize.STRING
  }
}, {
  freezeTableName: true // Model tableName will be the same as the model name
});

User.sync({force: true}).then(function () {
  // Table created
  return User.create({
    firstName: 'John',
    lastName: 'Hancock'
  });
});

how do I get the objects to create from DB instead?

Mick Hansen

unread,
May 8, 2015, 1:10:20 PM5/8/15
to bob.l...@halton.ca, sequ...@googlegroups.com, bosko...@hotmail.com
The first thing is to completely remove the User.sync() code block:
Secondly you simply define your model to match your database.

So if your have a USERS table with FIRST_NAME and LAST_NAME you could define it as


var User = sequelize.define('user', {
  firstName: {
    type: Sequelize.STRING,
    field: 'FIRST_NAME'
  },
  lastName: {
    type: Sequelize.STRING,
    field: 'LAST_NAME'
  }
}, {
  tableName: 'USERS'
});

User.findAll().then(function (users) {});

On Fri, May 8, 2015 at 6:06 PM, <bob.l...@halton.ca> wrote:
Thank you for your reply.  This being my first time using sequelize, I'm still a little lost.
so let's assume I'm looking at the example at http://docs.sequelizejs.com/en/latest/docs/getting-started/

when I get to:

var User = sequelize.define('user', {
  firstName: {
    type: Sequelize.STRING,
    field: 'first_name' // Will result in an attribute that is firstName when user facing but first_name in the database
  },
  lastName: {
    type: Sequelize.STRING
  }
}, {
  freezeTableName: true // Model tableName will be the same as the model name
});

User.sync({force: true}).then(function () {
  // Table created
  return User.create({
    firstName: 'John',
    lastName: 'Hancock'
  });
});You simply exclude the sync() part.

bob.l...@halton.ca

unread,
May 8, 2015, 1:47:45 PM5/8/15
to sequ...@googlegroups.com, bob.l...@halton.ca, bosko...@hotmail.com
Thank you so much Mick.
I think I'm getting somewhere.

in
User.findAll().then(function (users) {});

I tried
User.findAll().then(function (users) {console.log('the name is: ' + users.lastname});

it comes back undefined.  how do I access users.lastname?

bob.l...@halton.ca

unread,
May 8, 2015, 2:02:58 PM5/8/15
to sequ...@googlegroups.com, bob.l...@halton.ca
never mind.  I didn't realize it returned an array of users.  I got it now thanks!

going back to my widget example, I will need to figure out how to get all the properties that belong to a widget (users from the code examples)
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages