पाठ 11 / 35

Creating and Reading Records

यह पाठ अभी हिंदी में अनुवादित नहीं है, इसलिए अंग्रेज़ी में दिखाया जा रहा है।

Insert rows and read them back by primary key, by unique field and as lists, and handle the not-found case correctly in each ORM.

लेखक: · 15 साल से वेब डेवलपर

Single row, unique row, many rows

Reads come in three flavours, and each ORM names them differently. By primary key: Prisma findUnique({ where: { id } }), TypeORM findOneBy({ id }), Sequelize findByPk(id). First match of a condition: Prisma findFirst, TypeORM findOne({ where }), Sequelize findOne({ where }). All matches: Prisma findMany, TypeORM find, Sequelize findAll. The important behaviour to remember is what happens when nothing matches: the single-row methods return null, and the list methods return an empty array. They do not throw. If a missing row is genuinely an error in your flow, check for null yourself or use Prisma's findUniqueOrThrow or TypeORM's findOneByOrFail. For writes, Prisma create({ data }) and Sequelize Model.create(values) insert and return the new record with generated fields like id; in TypeORM, save performs the insert and fills those fields on the entity you passed in.

The four basic operations

Almost every feature you build is a combination of these four operations, each of which maps to one SQL statement.

Four connected stages: create inserts a row, read selects rows, update changes rows, delete removes rows.
Figure 4.1: create (INSERT) -> read (SELECT) -> update (UPDATE) -> delete (DELETE)

Read patterns side by side

Check for null before using the result. Passing u?.id (that is, undefined) into the next query is not safe: TypeORM ignores undefined in where and would return every post, and Sequelize throws on an undefined where value.

// Prisma
const user = await prisma.user.findUnique({ where: { email } });
if (!user) throw new Error('User not found');
const recent = await prisma.post.findMany({ where: { authorId: user.id } });

// TypeORM
const u2 = await userRepo.findOneBy({ email });
if (!u2) throw new Error('User not found');
const recent2 = await postRepo.find({ where: { author: { id: u2.id } } });

// Sequelize
const u3 = await User.findOne({ where: { email } });
if (!u3) throw new Error('User not found');
const recent3 = await Post.findAll({ where: { authorId: u3.id } });

Sequelize instances are not plain objects

A Sequelize result is a model instance that wraps the data in dataValues and carries methods. JSON.stringify and instance.get({ plain: true }) give you a plain object, and findAll({ raw: true }) skips instance creation entirely. Prisma always returns plain objects; TypeORM returns entity class instances.

त्वरित जाँच: prisma.user.findUnique({ where: { id: 999 } }) is called and no such user exists. What happens?

  • It throws a not-found error
  • It returns an empty array
  • It returns null
  • It creates the user
Answer

It returns null — findUnique returns null when nothing matches. Use findUniqueOrThrow if you want an error instead. List methods return an empty array.