[Node.js] Sequelize Intro - 1

Intro to Object-Relational Maps (ORM)

We'll be using an ORM called Sequelize to manage the connection to our database. We'll cover the basics in this concept, but Sequelize is a powerful tool and is extremely well documented at http://docs.sequelizejs.com/

Models

A model is the data representation of some group of data. In object-oriented programing terms, a model is an object and is represented by a new class. It should usually represent a noun such as a user, a feed item, an order, etc. We use the @Table decorator and extend the base sequelize Model class to link our model to our database table.

Parameters

The model contains instance parameters. These can be other models or primitive fields. We use the @Column decorator to link our parameters to the table columns. The bang symbol ! specifies if the field in the table can be null. Sequelize handles the datatype mappings from TypeScript types to Postgres column datatypes.

Read more at the Sequelize docs entry on models.

 

Model:

复制代码
import {Table, Column, Model, HasMany, PrimaryKey, CreatedAt, UpdatedAt, ForeignKey} from 'sequelize-typescript';
import { User } from '../../users/models/User';

@Table
export class FeedItem extends Model<FeedItem> {
  @Column
  public caption!: string;

  @Column
  public url!: string;

  @Column
  @CreatedAt
  public createdAt: Date = new Date();

  @Column
  @UpdatedAt
  public updatedAt: Date = new Date();
}
复制代码

 

Usage:

复制代码
const items = await FeedItem.findAndCountAll({ order: [["id", "DESC"]] });

const feed = await FeedItem.findByPk(id);

const item = await new FeedItem({
    caption: caption,
    url: fileName,
  });

const saved_item = await item.save();
复制代码

 

Decorators

The Decorators (also known as Annotations) mentioned in this video are a feature of the sequelize-typescript package which allows us to link database features with our models. We exemplify this using the @CreatedAt and @UpdatedAt. This will set the option in the Postgres database to automatically set the date when any row is created, or updated and is useful when sorting and filtering our data.

Read more and view complete details on the model definition in the sequelize-typescript docs

posted @   Zhentiw  阅读(23)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-03-22 [Javascript] Create an Async Generator and Loop Through Generated Promises with "For Await Of" Loops
2020-03-22 [Javascript] Use an Array of Promises with a For Await Of Loop
2020-03-22 [macOS] macOS Catalina 10.15: Setting up a Brand New Mac for Development
2020-03-22 [Javascript] NaN is number type and NaN === NaN is false
2020-03-22 [Javascript] -0 & 0
2020-03-22 [Javascript] Object mental model
2019-03-22 [Angular] Use Angular components in AngularJS applications with Angular Elements
点击右上角即可分享
微信分享提示