nuxt-sequelize v0.0.5
nuxt-sequelize
Quick Start
Add
nuxt-sequelize
,sequelize
, andinflection
dependencies to your projectnpm install nuxt-sequelize sequelize inflection
Add
nuxt-sequelize
to themodules
section ofnuxt.config.ts
export default defineNuxtConfig({ modules: ["nuxt-sequelize"], });
Start the development server:
npm run dev
Usage
Configuration
Configure Nuxt Sequelize with the
sequelize
property.export default defineNuxtConfig({ sequelize: { dir: "server/models", options: {}, }, });
Key Type Required Description dir string false Use the dir/ directory to automatically register models within your application. options UserOptions false Sequelize options Configure Sequelize secret via
.env
NUXT_SEQUELIZE_DIALECT='mysql' NUXT_SEQUELIZE_DATABASE='' NUXT_SEQUELIZE_HOST="" NUXT_SEQUELIZE_PORT=3306 NUXT_SEQUELIZE_USERNAME="admin" NUXT_SEQUELIZE_PASSWORD="admin"
Define Model
Files created under the model directory will be automatically registered under the sequelize object. The filename will be used as the model name.
// models/user.ts
import { DataTypes } from "sequelize";
export default defineSequelizeModel({
// ...ModelAttributes
});
Define Association
To define associations between models, create and configure the associations in the associations.ts
file located in the model
directory.
// model/associations.ts
export default defineSequelizeAssociation([
// user - profile
{
type: "1to1",
modelA: {
name: "user",
options: {
foreignKey: "user_id",
as: "profile",
},
},
modelB: {
name: "profile",
options: {
foreignKey: "user_id",
as: "user",
},
},
},
// user -> article
{
type: "1toN",
modelA: {
name: "user",
options: {
foreignKey: "user_id",
as: "articles",
},
},
modelB: {
name: "article",
options: {
foreignKey: "user_id",
as: "user",
},
},
},
// article <-> tag
{
type: "NtoM",
through: "articleTag",
modelA: {
name: "article",
options: {
foreignKey: "article_id",
as: "tags",
},
},
modelB: {
name: "tag",
options: {
foreignKey: "tag_id",
as: "articles",
},
},
},
]);
Use
Get the model collection through useSequelize
, and then use its attributes directly
// server/routes/hello.get.ts
export default defineEventHandler(async () => {
const sequelize = useSequelize();
const result = await sequelize.user.findAll();
return result;
});
Composables
defineSequelizeModel
This function helps to create a new Sequelize model. Example usage:
Using sequelize.define
:
// models/user.ts
import { DataTypes } from "sequelize";
export default defineSequelizeModel({
// ...ModelAttributes
});
Extending Model
// models/user.ts
import { DataTypes, Model } from "sequelize";
export default defineSequelizeModel((modelName, sequelize) => {
class User extends Model {}
User.init(
{
// ...ModelAttributes
},
{
sequelize,
modelName,
}
);
return User;
});
defineSequelizeAssociation
Define associations between models
// models/association.ts
export default defineSequelizeAssociation([
// ...Associations
])
useSequelize
Access the model collection of sequelize
export default defineEventHandler(async () => {
const sequelize = useSequelize();
});
useSequelizeClient
Access the instance of sequelize
export default defineEventHandler(async () => {
const client = useSequelizeClient();
});
9 months ago
9 months ago
11 months ago
9 months ago
12 months ago
9 months ago
10 months ago
9 months ago
11 months ago
9 months ago
10 months ago
10 months ago
10 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago