/**
* Now, that options hash for each property is repetitive and gross,
* so let's assign it to a variable and pass that variable in to each property.
*/
var propertyOptions = {
type: Sequelize.STRING,
allowNull: false
};
// Then, as you'd expect, pass that variable in as the value of each property key.
var Theme = sequelize.define('theme', {
name: JSON.parse(JSON.stringify(propertyOptions)),
author: JSON.parse(JSON.stringify(propertyOptions))
});
// Now, for some reason the next line doesn't work. It syncs a table, yes, but only
// with the "name" property. No author property.
Theme.sync({ force: true });
/**
* SQL that's ran:
* CREATE TABLE IF NOT EXISTS "themes" (
* "id" SERIAL ,
* "name" VARCHAR(255) NOT NULL,
* "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL,
* "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL,
* PRIMARY KEY ("id")
* );
*
* Notice the lack of `author` column?
*/
// To make things weirder, creating and persisting a model with both the `name` and `author`
// defined sets the value of the `author` property as `name` property in the DB.
var theme = Theme.create({
name: 'Ember (Dark)',
author: '@cdl'
});
/**
* SQL that's ran:
* INSERT INTO "themes" (
* "id","name","updatedAt","createdAt"
* ) VALUES (
* DEFAULT,'@cdl','2015-08-16 17:41:29.352 +00:00','2015-08-16 17:41:29.352 +00:00'
* ) RETURNING *;
*
* See: uses the `author` value to fill the `name` column.
*
*
* sidebar_development=> SELECT * FROM themes;
* id | name | createdAt | updatedAt
* ----+------+----------------------------+----------------------------
* 1 | @cdl | 2015-08-16 11:41:29.352-06 | 2015-08-16 11:41:29.352-06
*/