You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

sequelize-typescript

Package Overview
Dependencies
Maintainers
3
Versions
94
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sequelize-typescript

Decorators and some other features for sequelize

2.1.6
latest
Source
npm
Version published
Weekly downloads
267K
-25.69%
Maintainers
3
Weekly downloads
 
Created

What is sequelize-typescript?

sequelize-typescript is an ORM (Object-Relational Mapping) library for Node.js that integrates Sequelize with TypeScript. It allows developers to define models and interact with SQL databases using TypeScript decorators and classes, providing type safety and a more intuitive API.

What are sequelize-typescript's main functionalities?

Model Definition

Defines a model using TypeScript decorators. The `@Table` decorator marks the class as a table, and the `@Column` decorator defines the columns of the table.

```typescript
import { Table, Column, Model, DataType } from 'sequelize-typescript';

@Table
class User extends Model {
  @Column({
    type: DataType.STRING,
    allowNull: false
  })
  name!: string;

  @Column({
    type: DataType.STRING,
    allowNull: false
  })
  email!: string;
}
```

Associations

Defines associations between models using TypeScript decorators. The `@HasMany` decorator establishes a one-to-many relationship between `User` and `Post` models.

```typescript
import { Table, Column, Model, HasMany } from 'sequelize-typescript';

@Table
class User extends Model {
  @Column
  name!: string;

  @HasMany(() => Post)
  posts!: Post[];
}

@Table
class Post extends Model {
  @Column
  title!: string;

  @Column
  content!: string;
}
```

Querying

Performs queries on the database. The `findByPk` method fetches a user by primary key and includes associated posts.

```typescript
async function getUserWithPosts(userId: number) {
  const user = await User.findByPk(userId, {
    include: [Post]
  });
  return user;
}
```

Validation

Adds validation to model attributes. The `validate` option in the `@Column` decorator ensures that the `email` field contains a valid email address.

```typescript
import { Table, Column, Model, DataType } from 'sequelize-typescript';

@Table
class User extends Model {
  @Column({
    type: DataType.STRING,
    allowNull: false,
    validate: {
      isEmail: true
    }
  })
  email!: string;
}
```

Other packages similar to sequelize-typescript

Keywords

orm

FAQs

Package last updated on 24 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts