0.0.5 • Published 5 months ago

nuxt-sequelize v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

nuxt-sequelize

Sequelize module for Nuxt

Quick Start

  1. Add nuxt-sequelize, sequelize, and inflection dependencies to your project

    npm install nuxt-sequelize sequelize inflection
  2. Add nuxt-sequelize to the modules section of nuxt.config.ts

    export default defineNuxtConfig({
      modules: ["nuxt-sequelize"],
    });
  3. Start the development server:

    npm run dev

Usage

Configuration

  1. Configure Nuxt Sequelize with the sequelize property.

    export default defineNuxtConfig({
      sequelize: {
        dir: "server/models",
        options: {},
      },
    });
    KeyTypeRequiredDescription
    dirstringfalseUse the dir/ directory to automatically register models within your application.
    optionsUserOptionsfalseSequelize options
  2. 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();
});
0.0.5-beta.1

5 months ago

0.0.5-beta.2

5 months ago

0.0.4-beta.2

6 months ago

0.0.5-beta.3

5 months ago

0.0.4-beta.1

7 months ago

0.0.5-beta.4

5 months ago

0.0.4-beta.4

6 months ago

0.0.5-beta.5

5 months ago

0.0.4-beta.3

6 months ago

0.0.5

5 months ago

0.0.4-beta.6

6 months ago

0.0.4

6 months ago

0.0.4-beta.5

6 months ago

0.0.3

8 months ago

0.0.2

8 months ago

0.0.1-beta.3

8 months ago

0.0.1-beta.2

8 months ago

0.0.1-beta.1

8 months ago

0.0.1

8 months ago

0.0.0-beta.2

8 months ago

0.0.0-beta.1

8 months ago

1.0.2

8 months ago