Aggregation এবং Grouping

Advanced Querying এবং Relations - টাইপওআরএম (TypeORM) - Web Development

346

TypeORM এবং Aggregation/Grouping

TypeORM একটি Object-Relational Mapping (ORM) লাইব্রেরি যা JavaScript এবং TypeScript-এ ডেটাবেস অপারেশন সহজ করে তোলে। ডেটাবেসের সাথে সম্পর্কিত কাজগুলো যেমন CRUD অপারেশন, Relations, Migrations ইত্যাদি TypeORM ব্যবহার করে সহজে করা যায়। এর মধ্যে একটি গুরুত্বপূর্ণ এবং শক্তিশালী ফিচার হল Aggregation এবং Grouping এর মাধ্যমে ডেটা সংগ্রহ এবং বিশ্লেষণ।

Aggregation এবং Grouping ডেটাবেসে ডেটার উপর উন্নত অপারেশন করে যেমন, ডেটার গড়, মোট, সর্বোচ্চ, ন্যূনতম মান বের করা, এবং ডেটাকে গ্রুপ করে উপস্থাপন করা।

Aggregation এবং Grouping TypeORM এ কীভাবে কাজ করে?

TypeORM-এ Aggregation এবং Grouping এর জন্য QueryBuilder ব্যবহার করা হয়, যা কাস্টম কুয়েরি তৈরি করতে সহায়তা করে। TypeORM এর QueryBuilder এর মাধ্যমে আমরা SQL এর GROUP BY, COUNT, SUM, AVG, MIN, MAX ইত্যাদি ফাংশন ব্যবহার করে ডেটার উপর গ্রুপিং এবং অ্যালগোরিদমিক হিসাব করতে পারি।


Aggregation অপারেশন

Aggregation হল ডেটা একত্রিত করার একটি প্রক্রিয়া, যেখানে একটি নির্দিষ্ট মান বের করা হয় যেমন গড় (average), মোট (sum), সর্বোচ্চ (maximum), ন্যূনতম (minimum), বা গণনা (count)।

১. COUNT (গণনা)

COUNT() ফাংশনটি ডেটাবেসের মধ্যে একটি নির্দিষ্ট শর্ত পূরণকারী রেকর্ডের সংখ্যা গণনা করে।

উদাহরণ:

ধরা যাক, আপনার একটি Post টেবিল আছে এবং আপনি জানাতে চান কতগুলো পোস্ট একটি নির্দিষ্ট User এর জন্য রয়েছে:

import { createConnection } from "typeorm";
import { Post } from "./entity/Post";

createConnection().then(async connection => {
  const postRepository = connection.getRepository(Post);

  const count = await postRepository
    .createQueryBuilder("post")
    .where("post.userId = :userId", { userId: 1 })
    .getCount();

  console.log("Total posts by User 1:", count);
}).catch(error => console.log(error));

এখানে, getCount() ফাংশনটি User 1 এর পোস্টের সংখ্যা রিটার্ন করবে।

২. SUM (মোট)

SUM() ফাংশনটি নির্দিষ্ট কলামের সমস্ত মানের মোট যোগফল বের করে।

উদাহরণ:

ধরা যাক, আপনার Order টেবিলের মধ্যে প্রতিটি অর্ডারের মূল্য রয়েছে এবং আপনি মোট অর্ডারের মূল্য বের করতে চান:

import { createConnection } from "typeorm";
import { Order } from "./entity/Order";

createConnection().then(async connection => {
  const orderRepository = connection.getRepository(Order);

  const totalAmount = await orderRepository
    .createQueryBuilder("order")
    .select("SUM(order.amount)", "totalAmount")
    .getRawOne();

  console.log("Total order amount:", totalAmount.totalAmount);
}).catch(error => console.log(error));

এখানে, SUM(order.amount) মোট অর্ডারের পরিমাণ গণনা করে এবং getRawOne() ফলাফল রিটার্ন করে।

৩. AVG (গড়)

AVG() ফাংশনটি একটি নির্দিষ্ট কলামের গড় মান বের করে।

উদাহরণ:

ধরা যাক, আপনি একটি Product টেবিলের মধ্যে দামগুলির গড় বের করতে চান:

import { createConnection } from "typeorm";
import { Product } from "./entity/Product";

createConnection().then(async connection => {
  const productRepository = connection.getRepository(Product);

  const averagePrice = await productRepository
    .createQueryBuilder("product")
    .select("AVG(product.price)", "averagePrice")
    .getRawOne();

  console.log("Average price of products:", averagePrice.averagePrice);
}).catch(error => console.log(error));

এখানে, AVG(product.price) প্রতিটি প্রোডাক্টের দামগুলোর গড় রিটার্ন করবে।

৪. MIN (ন্যূনতম) এবং MAX (সর্বোচ্চ)

MIN() এবং MAX() ফাংশনগুলি একটি কলামের ন্যূনতম এবং সর্বোচ্চ মান বের করে।

উদাহরণ:

import { createConnection } from "typeorm";
import { Product } from "./entity/Product";

createConnection().then(async connection => {
  const productRepository = connection.getRepository(Product);

  const minPrice = await productRepository
    .createQueryBuilder("product")
    .select("MIN(product.price)", "minPrice")
    .getRawOne();
  
  const maxPrice = await productRepository
    .createQueryBuilder("product")
    .select("MAX(product.price)", "maxPrice")
    .getRawOne();

  console.log("Minimum price:", minPrice.minPrice);
  console.log("Maximum price:", maxPrice.maxPrice);
}).catch(error => console.log(error));

এখানে, MIN(product.price) এবং MAX(product.price) ফাংশনগুলি প্রোডাক্টের সর্বনিম্ন এবং সর্বোচ্চ দাম রিটার্ন করবে।


Grouping অপারেশন

Grouping হল ডেটাকে বিভিন্ন গ্রুপে ভাগ করে তার উপর বিভিন্ন agregation অপারেশন করা। SQL-এ এটি GROUP BY ক্লজ দিয়ে করা হয়, এবং TypeORM-এ QueryBuilder এর মাধ্যমে এটি বাস্তবায়ন করা হয়।

১. GROUP BY

GROUP BY ডেটাকে এক বা একাধিক কলামের ভিত্তিতে গ্রুপ করে এবং প্রতিটি গ্রুপের উপর aggregation অপারেশন প্রয়োগ করা হয়।

উদাহরণ:

ধরা যাক, আপনি Order টেবিল থেকে প্রতিটি userId এর জন্য মোট অর্ডারের পরিমাণ বের করতে চান:

import { createConnection } from "typeorm";
import { Order } from "./entity/Order";

createConnection().then(async connection => {
  const orderRepository = connection.getRepository(Order);

  const ordersGroupedByUser = await orderRepository
    .createQueryBuilder("order")
    .select("order.userId")
    .addSelect("SUM(order.amount)", "totalAmount")
    .groupBy("order.userId")
    .getRawMany();

  console.log(ordersGroupedByUser);
}).catch(error => console.log(error));

এখানে, groupBy("order.userId") ব্যবহার করে আমরা প্রতিটি userId এর জন্য SUM(order.amount) গণনা করেছি।


সারাংশ

Aggregation এবং Grouping TypeORM-এ ডেটার উপর শক্তিশালী বিশ্লেষণ করার জন্য ব্যবহৃত হয়। Aggregation অপারেশন যেমন COUNT(), SUM(), AVG(), MIN(), এবং MAX() ডেটার উপর সারণী বা গণনা করতে সাহায্য করে, এবং Grouping অপারেশন ডেটাকে গ্রুপ করে সংশ্লিষ্ট আউটপুট প্রদান করে। TypeORM এর QueryBuilder ব্যবহার করে আপনি এসব অপারেশন সহজেই করতে পারেন, যা আপনার ডেটাবেস অপারেশনকে আরও সহজ, দ্রুত এবং কার্যকরী করে তোলে।

Content added By
Promotion

Are you sure to start over?

Loading...