How to use TypeScript with Sequelize - node.js

I already have my server application written in Node, PostgreSQL, Sequelize using Fastify.
Now I would like to use TypeScript. Can anyone tell me how to begin rewriting my Server application using TypeScript.

Updated
/**
* Keep this file in sync with the code in the "Usage" section
* in /docs/manual/other-topics/typescript.md
*
* Don't include this comment in the md file.
*/
import {
Association, DataTypes, HasManyAddAssociationMixin, HasManyCountAssociationsMixin,
HasManyCreateAssociationMixin, HasManyGetAssociationsMixin, HasManyHasAssociationMixin,
HasManySetAssociationsMixin, HasManyAddAssociationsMixin, HasManyHasAssociationsMixin,
HasManyRemoveAssociationMixin, HasManyRemoveAssociationsMixin, Model, ModelDefined, Optional,
Sequelize, InferAttributes, InferCreationAttributes, CreationOptional, NonAttribute, ForeignKey,
} from 'sequelize';
const sequelize = new Sequelize('mysql://root:asd123#localhost:3306/mydb');
// 'projects' is excluded as it's not an attribute, it's an association.
class User extends Model<InferAttributes<User, { omit: 'projects' }>, InferCreationAttributes<User, { omit: 'projects' }>> {
// id can be undefined during creation when using `autoIncrement`
declare id: CreationOptional<number>;
declare name: string;
declare preferredName: string | null; // for nullable fields
// timestamps!
// createdAt can be undefined during creation
declare createdAt: CreationOptional<Date>;
// updatedAt can be undefined during creation
declare updatedAt: CreationOptional<Date>;
// Since TS cannot determine model association at compile time
// we have to declare them here purely virtually
// these will not exist until `Model.init` was called.
declare getProjects: HasManyGetAssociationsMixin<Project>; // Note the null assertions!
declare addProject: HasManyAddAssociationMixin<Project, number>;
declare addProjects: HasManyAddAssociationsMixin<Project, number>;
declare setProjects: HasManySetAssociationsMixin<Project, number>;
declare removeProject: HasManyRemoveAssociationMixin<Project, number>;
declare removeProjects: HasManyRemoveAssociationsMixin<Project, number>;
declare hasProject: HasManyHasAssociationMixin<Project, number>;
declare hasProjects: HasManyHasAssociationsMixin<Project, number>;
declare countProjects: HasManyCountAssociationsMixin;
declare createProject: HasManyCreateAssociationMixin<Project, 'ownerId'>;
// You can also pre-declare possible inclusions, these will only be populated if you
// actively include a relation.
declare projects?: NonAttribute<Project[]>; // Note this is optional since it's only populated when explicitly requested in code
// getters that are not attributes should be tagged using NonAttribute
// to remove them from the model's Attribute Typings.
get fullName(): NonAttribute<string> {
return this.name;
}
declare static associations: {
projects: Association<User, Project>;
};
}
class Project extends Model<
InferAttributes<Project>,
InferCreationAttributes<Project>
> {
// id can be undefined during creation when using `autoIncrement`
declare id: CreationOptional<number>;
// foreign keys are automatically added by associations methods (like Project.belongsTo)
// by branding them using the `ForeignKey` type, `Project.init` will know it does not need to
// display an error if ownerId is missing.
declare ownerId: ForeignKey<User['id']>;
declare name: string;
// `owner` is an eagerly-loaded association.
// We tag it as `NonAttribute`
declare owner?: NonAttribute<User>;
// createdAt can be undefined during creation
declare createdAt: CreationOptional<Date>;
// updatedAt can be undefined during creation
declare updatedAt: CreationOptional<Date>;
}
class Address extends Model<
InferAttributes<Address>,
InferCreationAttributes<Address>
> {
declare userId: ForeignKey<User['id']>;
declare address: string;
// createdAt can be undefined during creation
declare createdAt: CreationOptional<Date>;
// updatedAt can be undefined during creation
declare updatedAt: CreationOptional<Date>;
}
Project.init(
{
id: {
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true
},
name: {
type: new DataTypes.STRING(128),
allowNull: false
},
createdAt: DataTypes.DATE,
updatedAt: DataTypes.DATE,
},
{
sequelize,
tableName: 'projects'
}
);
User.init(
{
id: {
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true
},
name: {
type: new DataTypes.STRING(128),
allowNull: false
},
preferredName: {
type: new DataTypes.STRING(128),
allowNull: true
},
createdAt: DataTypes.DATE,
updatedAt: DataTypes.DATE,
},
{
tableName: 'users',
sequelize // passing the `sequelize` instance is required
}
);
Address.init(
{
address: {
type: new DataTypes.STRING(128),
allowNull: false
},
createdAt: DataTypes.DATE,
updatedAt: DataTypes.DATE,
},
{
tableName: 'address',
sequelize // passing the `sequelize` instance is required
}
);
// You can also define modules in a functional way
interface NoteAttributes {
id: number;
title: string;
content: string;
}
// You can also set multiple attributes optional at once
type NoteCreationAttributes = Optional<NoteAttributes, 'id' | 'title'>;
// And with a functional approach defining a module looks like this
const Note: ModelDefined<
NoteAttributes,
NoteCreationAttributes
> = sequelize.define(
'Note',
{
id: {
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true
},
title: {
type: new DataTypes.STRING(64),
defaultValue: 'Unnamed Note'
},
content: {
type: new DataTypes.STRING(4096),
allowNull: false
}
},
{
tableName: 'notes'
}
);
// Here we associate which actually populates out pre-declared `association` static and other methods.
User.hasMany(Project, {
sourceKey: 'id',
foreignKey: 'ownerId',
as: 'projects' // this determines the name in `associations`!
});
Address.belongsTo(User, { targetKey: 'id' });
User.hasOne(Address, { sourceKey: 'id' });
async function doStuffWithUser() {
const newUser = await User.create({
name: 'Johnny',
preferredName: 'John',
});
console.log(newUser.id, newUser.name, newUser.preferredName);
const project = await newUser.createProject({
name: 'first!'
});
const ourUser = await User.findByPk(1, {
include: [User.associations.projects],
rejectOnEmpty: true // Specifying true here removes `null` from the return type!
});
// Note the `!` null assertion since TS can't know if we included
// the model or not
console.log(ourUser.projects![0].name);
}
(async () => {
await sequelize.sync();
await doStuffWithUser();
})();
OLD
Using Decorators is something you should avoid as much as possible, they are not ECMAScript standard. They are even consider legacy. It is why I'm going to show you how to use sequelize with typescript.
we just need to follow the docs: https://sequelize.org/v5/manual/typescript.html but as it is not very clear, or at least to me. It took me a while understand it.
There it says that you need to install this tree things
* #types/node
* #types/validator // this one is not need it
* #types/bluebird
npm i -D #types/node #types/bluebird
then let's assume your project looks like so:
myProject
--src
----models
------index.ts
------user-model.ts
------other-model.ts
----controllers
----index.ts
--package.json
Let's create the user model first
`./src/models/user-model.ts`
import { BuildOptions, DataTypes, Model, Sequelize } from "sequelize";
export interface UserAttributes {
id: number;
name: string;
email: string;
createdAt?: Date;
updatedAt?: Date;
}
export interface UserModel extends Model<UserAttributes>, UserAttributes {}
export class User extends Model<UserModel, UserAttributes> {}
export type UserStatic = typeof Model & {
new (values?: object, options?: BuildOptions): UserModel;
};
export function UserFactory (sequelize: Sequelize): UserStatic {
return <UserStatic>sequelize.define("users", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
});
}
Now just to play arrow let's create another-model.ts
`./src/models/another-model.ts`
import { BuildOptions, DataTypes, Model, Sequelize } from "sequelize";
export interface SkillsAttributes {
id: number;
skill: string;
createdAt?: Date;
updatedAt?: Date;
}
export interface SkillsModel extends Model<SkillsAttributes>, SkillsAttributes {}
export class Skills extends Model<SkillsModel, SkillsAttributes> {}
export type SkillsStatic = typeof Model & {
new (values?: object, options?: BuildOptions): SkillsModel;
};
export function SkillsFactory (sequelize: Sequelize): SkillsStatic {
return <SkillsStatic>sequelize.define("skills", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
skill: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
});
}
Our Entities are done. now the db connection.
open ./src/models/index.ts there is where we gonna place the seqelize instance
`./src/models/index.ts`
import * as sequelize from "sequelize";
import {userFactory} from "./user-model";
import {skillsFactory} from "./other-model";
export const dbConfig = new sequelize.Sequelize(
(process.env.DB_NAME = "db-name"),
(process.env.DB_USER = "db-user"),
(process.env.DB_PASSWORD = "db-password"),
{
port: Number(process.env.DB_PORT) || 54320,
host: process.env.DB_HOST || "localhost",
dialect: "postgres",
pool: {
min: 0,
max: 5,
acquire: 30000,
idle: 10000,
},
}
);
// SOMETHING VERY IMPORTANT them Factory functions expect a
// sequelize instance as parameter give them `dbConfig`
export const User = userFactory(dbConfig);
export const Skills = skillsFactory(dbConfig);
// Users have skills then lets create that relationship
User.hasMay(Skills);
// or instead of that, maybe many users have many skills
Skills.belongsToMany(Users, { through: "users_have_skills" });
// the skill is the limit!
on our index.ts add, if you just want to open connection
db.sequelize
.authenticate()
.then(() => logger.info("connected to db"))
.catch(() => {
throw "error";
});
or if you want to create them tables
db.sequelize
.sync()
.then(() => logger.info("connected to db"))
.catch(() => {
throw "error";
});
some like this
import * as bodyParser from "body-parser";
import * as express from "express";
import { dbConfig } from "./models";
import { routes } from "./routes";
import { logger } from "./utils/logger";
import { timeMiddleware } from "./utils/middlewares";
export function expressApp () {
dbConfig
.authenticate()
.then(() => logger.info("connected to db"))
.catch(() => {
throw "error";
});
const app: Application = express();
if (process.env.NODE_ENV === "production") {
app.use(require("helmet")());
app.use(require("compression")());
} else {
app.use(require("cors")());
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true, limit: "5m" }));
app.use(timeMiddleware);
app.use("/", routes(db));
return app;
}
Once again the sky is the limit.
If you do this you'll have all the power of the autocomplete.
here an example: https://github.com/EnetoJara/resume-app

Use sequelize-typescript. Convert your tables and views into a class that extends Model object.
Use annotations in classes for defining your table.
import {Table, Column, Model, HasMany} from 'sequelize-typescript';
#Table
class Person extends Model<Person> {
#Column
name: string;
#Column
birthday: Date;
#HasMany(() => Hobby)
hobbies: Hobby[];
}
Create a connection to DB by creating the object:
const sequelize = new Sequelize(configuration...).
Then register your tables to this object.
sequelize.add([Person])
For further reference check this module.
Sequelize-Typescript

Related

What type should I assign to the parameter so that it does not give me this error?

import { Model, DataTypes } from 'sequelize';
interface IExtracciones {
id_extraccion: Number | null | undefined;
monto_extraido: Number;
fecha_de_extraccion: Date;
numero_de_cuenta: Number;
}
export default class Extracciones extends Model<IExtracciones> {}
Extracciones.init(
{
id_extraccion: {
type: DataTypes.INTEGER,
primaryKey: true,
},
monto_extraido: {
type: DataTypes.INTEGER,
allowNull: false,
},
fecha_de_extraccion: {
type: DataTypes.DATE,
allowNull: false,
},
numero_de_cuenta: {
type: DataTypes.INTEGER,
allowNull: false,
},
},
{
sequelize: bd,
tableName: 'extracciones',
}
);
function agregaRelacion(m1: typeof Model /*error here*/, m2: any, fk: String) {
m1.hasOne(m2, { foreignKey: fk });
m2.hasOne(m1, { foreignKey: fk });
}
The 'this' context of type 'typeof Model' is not assignable to method's 'this' of type 'ModelStatic<Model<{}, {}>>'.
Type 'typeof Model' is not assignable to type 'new () => Model<{}, {}>'.
Cannot assign an abstract constructor type to a non-abstract constructor type.ts(2684)
import { Model, DataTypes, Sequelize, ModelStatic } from 'sequelize';
interface IExtracciones {
id_extraccion: Number | null | undefined;
monto_extraido: Number;
fecha_de_extraccion: Date;
numero_de_cuenta: Number;
}
export default class Extracciones extends Model<IExtracciones> {}
Extracciones.init(
{
id_extraccion: {
type: DataTypes.INTEGER,
primaryKey: true,
},
monto_extraido: {
type: DataTypes.INTEGER,
allowNull: false,
},
fecha_de_extraccion: {
type: DataTypes.DATE,
allowNull: false,
},
numero_de_cuenta: {
type: DataTypes.INTEGER,
allowNull: false,
},
},
{
// change new Sequeilze()
sequelize: new Sequelize(),
tableName: 'extracciones',
}
);
// m2 replace any with appropriate modelname if needed
/* corrected fk type (String compiles to javascript, shouldn't used as type) */
function agregaRelacion(m1: ModelStatic<Extracciones>, m2: ModelStatic<any>, fk: string) {
m1.hasOne(m2, { foreignKey: fk });
m2.hasOne(m1, { foreignKey: fk });
}
Solution
imagine we have a users table and our users have skills so it is a one to many
right?
UserModel
import { BuildOptions, DataTypes, Model, Sequelize } from "sequelize";
export interface UserAttributes {
id: number;
name: string;
email: string;
password: string;
middleName: string;
lastName: string;
secondLastName: string;
active: boolean;
createdAt?: Date;
updatedAt?: Date;
}
export interface UserModel extends Model<UserAttributes>, UserAttributes {}
export class User extends Model<UserModel, UserAttributes> {}
export type UserStatic = typeof Model & {
new (values?: object, options?: BuildOptions): UserModel;
};
export function UserFactory (sequelize: Sequelize) {
return <UserStatic>sequelize.define("users", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
middleName: {
type: DataTypes.STRING,
allowNull: true,
},
lastName: {
type: DataTypes.STRING,
allowNull: false,
},
secondLastName: {
type: DataTypes.STRING,
allowNull: true,
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
});
}
SkillsModel
import { BuildOptions, DataTypes, Model, Sequelize } from "sequelize";
export interface SkillsAttributes {
skill: string;
}
export interface SkillsModel
extends Model<SkillsAttributes>,
SkillsAttributes {}
export type SkillsStatic = typeof Model & {
new (values?: object, options?: BuildOptions): SkillsModel;
};
export function SkillsFactory (sequelize: Sequelize) {
return <SkillsStatic>sequelize.define("skills", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
skill: {
type: DataTypes.STRING,
unique: true,
},
});
}
now imagine we have a dir call anyName and in there we have
we are going to export users and skills from our index file
index
import { Sequelize } from "sequelize";
import { CourseFactory, CourseStatic } from "./courses";
import { EducationFactory, EducationStatic } from "./education";
import { ExperienceFactory, ExperienceStatic } from "./experience";
import { FieldsFactory, FieldsStatic } from "./fields-of-interets";
import { GeneralFactory, GeneralStatic } from "./general";
import { SkillsFactory, SkillsStatic } from "./skills";
import { SkillsTypeFactory, SkillsTypeStatic } from "./skills-type";
import { SocialMediaFactory, SocialMediaStatic } from "./social-media";
import { UserFactory, UserStatic } from "./users";
export interface DB {
sequelize: Sequelize;
User: UserStatic;
Skills: SkillsStatic;
SkillsType: SkillsTypeStatic;
Experience: ExperienceStatic;
Education: EducationStatic;
Course: CourseStatic;
General: GeneralStatic;
SocialMedia: SocialMediaStatic;
FieldsOfInterest: FieldsStatic;
}
const sequelize = new Sequelize(
(process.env.DB_NAME = "rest_resume_api"),
(process.env.DB_USER = "john"),
(process.env.DB_PASSWORD = "password"),
{
port: Number(process.env.DB_PORT) || 54320,
host: process.env.DB_HOST || "localhost",
dialect: "postgres",
pool: {
min: 0,
max: 5,
acquire: 30000,
idle: 10000,
},
}
);
const User = UserFactory(sequelize);
const Skills = SkillsFactory(sequelize);
const SkillsType = SkillsTypeFactory(sequelize);
const Experience = ExperienceFactory(sequelize);
const Education = EducationFactory(sequelize);
const Course = CourseFactory(sequelize);
const General = GeneralFactory(sequelize);
const SocialMedia = SocialMediaFactory(sequelize);
const FieldsOfInterest = FieldsFactory(sequelize);
SkillsType.belongsTo(Skills);
SkillsType.belongsToMany(User, { through: "users_has_skills" });
User.belongsToMany(SkillsType, { through: "users_has_skills" });
Experience.belongsToMany(User, { through: "user_has_experience" });
User.belongsToMany(Experience, { through: "user_has_experience" });
Education.belongsToMany(User, { through: "user_has_education" });
User.belongsToMany(Education, { through: "user_has_education" });
Course.belongsTo(User);
General.belongsTo(User);
SocialMedia.belongsToMany(User, { through: "user_has_social" });
User.belongsToMany(SocialMedia, { through: "user_has_social" });
FieldsOfInterest.belongsToMany(User, { through: "user_has_fields" });
User.belongsToMany(FieldsOfInterest, { through: "user_has_fields" });
export const db: DB = {
sequelize,
User,
Skills,
};

pass a model instance to a function

This is the first time I am using sequelize (v6) and generally JS. and a little Confused. I have this function:
function userStandard(user: User, currentlyActiveRate: number){
}
//User is now
import { DataTypes } from "sequelize";
import sequelize from '../../../sequelize';
export const User = sequelize.define('User', {
email: {
type: DataTypes.STRING },
password_hash: {
type: DataTypes.STRING,
}
}, {
timestamps: true,
freezeTableName: false
});
I can't run/compile the app now with the following error:
'User' refers to a value, but is being used as a type here. Did you mean 'typeof User'?
if i do user: typeof User
it compiles but what is exactly the issue? Why doesn't it work without typeof?
You should declare an interface for your model as well as a type to describe a constructor and call define as a generic method:
import { Model, DataTypes, BuildOptions } from 'sequelize';
export interface UserModel extends Model {
email: string;
password_hash: string;
}
export type UserModelStatic = typeof Model
& { new(values?: Record<string, unknown>, options?: BuildOptions): UserModel }
const User = <UserModelStatic>sequelize.define('User', {
email: {
type: DataTypes.STRING },
password_hash: {
type: DataTypes.STRING,
}
}, {
timestamps: true,
freezeTableName: false
});
export { User }

How to create type safety for sequelize many to many associations methods, like (setModels, etc.)

I have model
model/Caregiver
import { BuildOptions, DataTypes, Model, Sequelize } from 'sequelize';
export interface CaregiverAttributes {
caregiver_id: number | string;
caregiver_notification_preferences?: string;
}
export interface CaregiverModel
extends Model<CaregiverAttributes>,
CaregiverAttributes {}
export type CaregiverStatic = typeof Model & {
new (values?: object, options?: BuildOptions): CaregiverModel;
};
export function CaregiverFactory(sequelize: Sequelize): CaregiverStatic {
return <CaregiverStatic>sequelize.define(
'caregiver',
{
caregiver_id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true,
},
caregiver_notification_preferences: {
type: DataTypes.STRING,
allowNull: true,
},
},
{
timestamps: false,
underscored: true,
tableName: 'caregiver',
}
);
}
and associations
Caregiver.belongsToMany(Patient, {
foreignKey: 'caregiver_id',
targetKey: 'caregiver_id',
as: 'Patient',
});
But when I try to get or set
const patients = await caregiver.getPatients();
I have an error: property getPatients does not exist on type CaregiverModel
Maybe someone have the same problem?
Notice: With // #ts-ignore works fine.
Solved
Find this paper https://vivacitylabs.com/setup-typescript-sequelize/ , where it is describe.

Sequelize bulk insert with associations

I'm trying to bulk insert with associations,
I have this 'Song' model which have one to many relationships with 'Genre' and 'Language' defined with the migrations CLI.
Song:
module.exports = (sequelize, DataTypes) => {
class Song extends Model {
static associate(models) {
// define association here
Song.hasMany(models["Language"])
Song.hasMany(models["Genre"])
}
};
Song.init({
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: DataTypes.STRING,
energy: {type: DataTypes.FLOAT, allowNull: false},
valence: {type: DataTypes.FLOAT, allowNull: false}
}, {
sequelize,
modelName: 'Song',
timestamps: true
});
return Song;
};
Language:
module.exports = (sequelize, DataTypes) => {
class Language extends Model {
static associate(models) {
// define association here
models["Language"].belongsTo(models["Song"])
}
};
Language.init({
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: DataTypes.STRING
}, {
sequelize,
modelName: 'Language',
indexes: [{unique: true, fields: ['name']}]
});
return Language;
};
Genre:
module.exports = (sequelize, DataTypes) => {
class Genre extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
models["Genre"].belongsTo(models["Song"])
}
};
Genre.init({
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: DataTypes.STRING
}, {
sequelize,
modelName: 'Genre',
indexes: [{unique: true, fields: ['name']}]
});
return Genre;
};
I'm trying to bulk insert songs with languages and genres like this:
Song.bulkCreate(songs, {
include: [Genre,Language]
}).then(() => {
const result = {
status: "ok",
message: "Upload Successfully!",
}
res.json(result);
});
each song in the songs array is structured like this:
{
name: "abc",
genres: [{name: "abc"}],
languages: [{name: "English"}],
energy: 1,
valence: 1
}
I'm ending up with a full songs table but genres and languages are empty
What am I doing wrong?
Thanks.
Just in case anyone else got here from a search, starting from version 5.14
Sequelize added the option to use include option in bulkCreate as follows:
await Song.bulkCreate(songsRecordsToCreate, {
include: [Genre,Language]
})
Edit 2nd Feb 2023
As none answered above, as of v5.14.0 the include option is now available on bulkInsert.
Unfortunately bulkCreate does not support include option like create do.
You should use create in a cycle inside a transaction.
const transaction = ...
for (const song of songs) {
await Song.create(song, {
include: [Genre,Language]
}, { transaction })
}
await transaction.commit()
or you can use Promise.all to avoid using for.

SEQUELIZE: "Cannot read property 'length' of undefined"

Does anyone know how to solve this? There is something I am not seeing. To see if the controller was working, I returned the response receiving the request in json, it worked, there was no error.
The problem seems to be with the controller, but the controller...
Edit
Controller
import Post from '../models/Post';
import * as Yup from 'yup';
class PostController {
async store(req, res) {
try {
//Checks if the fields have been filled correctly
const schema = Yup.object().shape({
title: Yup.string()
.required(),
content: Yup.string()
.required()
});
if(!(await schema.isValid(req.body))) {
return res.status(400).json({ error: 'Error 1' });
}
//Abstraction of fields
const { title, content } = req.body;
const createPost = await Post.create(title, content);
if(!createPost) {
return res.json({error: 'Error 2'})
}
//If everything is correct, the information will be registered and returned.
return res.json({
title,
content,
});
}
catch (err) {
console.log("Error: " + err);
return res.status(400).json({error: 'Error 3'});
}
}
}
export default new PostController();
Model:
import Sequelize, { Model } from 'sequelize';
class Post extends Model {
static init(sequelize) {
//Fields registered by the user
super.init({
id: {
type: Sequelize.INTEGER,
primaryKey: true
},
title: Sequelize.STRING,
content: Sequelize.TEXT,
created_at: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
},
updated_at: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
},
},
{
sequelize,
tableName: 'posts'
});
return this;
}
}
export default Post;
Migration:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('posts', {
id: {
type: Sequelize.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true
},
title: {
type: Sequelize.STRING,
allowNull: false,
},
content: {
type: Sequelize.TEXT,
allowNull: false,
},
created_at: {
type: Sequelize.DATE,
allowNull: false,
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
}
});
},
down: (queryInterface) => {
return queryInterface.dropTable('posts');
}
};
The terminal error:
TypeError: Cannot read property 'length' of undefined
You need to define all columns that you put on migration inside your model. As you put allowNull: false, the database needs the column info.
I believe that you can solve adding the columns that you declared on your migration inside your Model and, if you don't wanna to declare those columns on your Controller, add the defaultValue property on those columns.
This will allow to sequelize to insert the appropriate data on those columns. As example: defaultValue: Sequelize.NOW on created_at column.
Another thing that you need to put is the table name, like this (this is one Model that I use inside one of my projects:
static init(sequelize) {
super.init(
{
categoryId: {
type: Sequelize.INTEGER,
primaryKey: true,
field: 'id',
},
dateUpdated: {
type: Sequelize.DATE,
field: 'updated_at',
defaultValue: Sequelize.NOW,
},
// ... other fields here,
},
{
sequelize,
tableName: 'categories',
}
);
}
// ...
}
export default Category;
And, therefore, try to import as Instance, not as a class.
EDIT 1:
Other thing. The error that are in your answer is on line 140 of file lib/model.js (see here on github on the master repo of sequelize).
Looking at this, try to declare your primary key on model if if you didn't yet.
EDIT 2:
In your controller, try this (according to docs):
await Post.create({ title, content });
Try to pass an json object with info that you want to store and not as parameters.
EDIT 3:
You need to import database.js before call the controllers, I had problems in this point (follow a database.js that is working):
// Imports here
import Category from '../app/models/Category';
//...
const models = [
// Models that I want to use
Category,
//...
];
class Database {
constructor() {
this.init();
}
// Load on database init
init() {
this.connection = new Sequelize(databaseConfig);
models.forEach(model => model.init(this.connection));
}
}
export default new Database();
I was having the same problem
The problem with my code was that I was forgetting to add the module name to my database's index.js file inside my models array
eg: const **models** = [User,**Task**]
I found the answer on this post:
https://github.com/sequelize/sequelize/issues/11111#issuecomment-697078832
I had the same problem that Naruto Uzumaki. I forgot to put the connection with the model on the file that initialize the models.

Resources