This time, I simply share a story about stepping on pits and climbing pits in the learning process.
The reason is that the author was reading a book about Egg, but the methods introduced in the book may have failed to function properly due to the passage of time. The egg-sequelize version is faulty, so use a custom sequelize-CLI. The rc, config files, and migration, Model, and Seeder directories are created manually.
Jumping pit
There was a problem with creating the model.
./node_modules/.bin/sequelize model:generate --name User
Copy the code
But when I typed it myself, I got an error indicating that the attributes parameter was missing
My first thought at this point is, is there an update to the CLI? Let me see what I should do with the current version of the CLI.
Grope for
Referring to the CLI documentation, I complete the Attributes parameter and successfully generate the Model and Migration. The generated Model would look something like this:
"use strict";
module.exports = (sequelize, DataTypes) = > {
const User = sequelize.define(
"User",
{
username: DataTypes.STRING
},
{}
);
return User;
};
Copy the code
So when you run it, you get an error
TypeError: sequelize.define is not a function
Copy the code
Because sequelize is actually the Application of the Egg, there’s never a define on the app
Climb the pit
Peruse egg-Sequelize’s readme for clues
Read the tutorials to see a full example.
A working Model should actually look like this:
// app/model/user.js
module.exports = app= > {
const { STRING, INTEGER, DATE } = app.Sequelize;
const User = app.model.define("user", {
login: STRING,
name: STRING(30),
password: STRING(32),
age: INTEGER,
last_sign_in_at: DATE,
created_at: DATE,
updated_at: DATE
});
User.findByLogin = async function(login) {
return await this.findOne({
where: {
login: login
}
});
};
// don't use arraw function
User.prototype.logSignin = async function() {
return await this.update({ last_sign_in_at: new Date()}); };return User;
};
Copy the code
Then follow the official tutorial to see that the Migration and Model are generated separately, and the Model is created manually. Parts of the Migration and Model overlap. Like this:
// user migraition
"use strict";
module.exports = {
up: async (queryInterface, Sequelize) => {
const { INTEGER, DATE, STRING } = Sequelize;
await queryInterface.createTable("users", {
// Look here look here <---------
id: { type: INTEGER, primaryKey: true.autoIncrement: true },
name: STRING(30),
age: INTEGER,
created_at: DATE,
updated_at: DATE
});
},
down: async queryInterface => {
await queryInterface.dropTable("users"); }};Copy the code
// user model
"use strict";
module.exports = app= > {
const { STRING, INTEGER, DATE } = app.Sequelize;
const User = app.model.define("user", {
// Look here look here <---------
id: { type: INTEGER, primaryKey: true.autoIncrement: true },
name: STRING(30),
age: INTEGER,
created_at: DATE,
updated_at: DATE
});
return User;
};
Copy the code
I’m going to write a small tool to automatically generate app.model. Define parameters using the contents of Migration
conclusion
Looking back at the experience, the initial focus was all on the CLI, but not on the egg-sequelize usage scenario, and therefore egg-sequelize. So the correct opening position is to find the answer in the official document first, read document, read document, read document.