الـ objects والـ arrays: destructuring و spread و map و filter و reduce
الـ object شنطة بأسماء والـ array list بترتيب. القراءة بالنقطة والأقواس، والإضافة، و undefined للحاجة المش موجودة. والـ destructuring والـ rest علشان تشيل الـ password من الـ response، والـ spread للنسخ. و filter و map و reduce و find و some و every، وفخ sort مع الأرقام.
نص شرح المعلم والكود الذي كتبه ونتيجة تشغيله، بنفس ترتيب الدرس.
أهلًا بيك. أي حاجة في الـ backend، مستخدم أو order أو كورس، بنمسكها كـ object. والـ object شنطة فيها قيم، كل قيمة ليها اسم، والاسم ده اسمه key. user فيه id و name و email. و roles جُوّاه array، يعني list بترتيب. وبنقرا بالـ dot أو بالـ square brackets، وبنضيف بنفس الطريقة. نشغّل.
const user = {
id: 7,
name: "مُنى",
email: "[email protected]",
roles: ["student"],
};
console.log(user.name, user.roles[0]);
console.log(user["email"]);
console.log(user.phone);
user.roles.push("teacher");
user.verified = true;
console.log(user);المخرجات
$ node main.js مُنى student [email protected] undefined { id: 7, name: 'مُنى', email: '[email protected]', roles: [ 'student', 'teacher' ], verified: true }
الاسم وأول role. الـ email بالـ brackets، ودي مفيدة لما اسم الـ key جُوّه متغير. و phone مش موجود، فرجع undefined من غير أي error. وفي الآخر الـ object بعد ما زودنا teacher و verified. خد بالك: user كان const، ومع كده عدلنا جُوّاه. const بتمنع إنك تحط object تاني مكانه، مش إنك تعدّل جُوّاه. الـ destructuring: بدل ما تكتب user dot name و user dot city، بتطلّعهم في سطر واحد. والأهم: الـ rest، بالـ three dots. password لوحده، و publicUser فيه كل حاجة غيره. ودي بالزبط الطريقة اللي هنشيل بيها الـ password قبل ما نبعت المستخدم في response. وبعدين الـ spread، نفس الـ three dots، بس بتعمل نسخة جديدة فيها تعديل. وآخر سطرين destructuring لـ array. نشغّل.
const user = { id: 7, name: "مُنى", password: "secret", city: "Cairo" };
// destructuring: طلّع القيم في متغيرات
const { name, city } = user;
console.log(name, city);
// rest: كل حاجة ما عدا password
const { password, ...publicUser } = user;
console.log(publicUser);
// spread: نسخة جديدة فيها تعديل
const updated = { ...user, city: "Alex" };
console.log(user.city, updated.city);
const [first, second] = ["GET", "POST", "PUT"];
console.log(first, second);المخرجات
$ node main.js
مُنى Cairo
{ id: 7, name: 'مُنى', city: 'Cairo' }
Cairo Alex
GET POSTالاسم والمدينة. publicUser من غير password، وده اللي يتبعت. والنسخة الجديدة فيها Alex، والأصلية لسه Cairo، ما اتغيرتش. و first و second: GET و POST. الـ arrays وأهم أدواتها. عندنا أربع orders. filter بترجّع اللي الشرط بتاعه صح: المدفوع بس. map بتحوّل كل item لحاجة تانية: الإجمالي بس. reduce بتلم كل الـ items في قيمة واحدة: المجموع، وبتبدأ من zero. find بترجّع أول item يطابق. some: فيه واحد على الأقل؟ و every: كلهم؟ و includes: القيمة دي موجودة؟ نشغّل.
const orders = [
{ id: 1, user: "mona", total: 250, paid: true },
{ id: 2, user: "omar", total: 90, paid: false },
{ id: 3, user: "mona", total: 400, paid: true },
{ id: 4, user: "sara", total: 60, paid: true },
];
const paid = orders.filter((o) => o.paid);
const totals = paid.map((o) => o.total);
const revenue = totals.reduce((sum, t) => sum + t, 0);
console.log(paid.length, totals, revenue);
console.log(orders.find((o) => o.id === 2));
console.log(orders.some((o) => o.total > 300), orders.every((o) => o.paid));
console.log(orders.map((o) => o.user).includes("sara"));المخرجات
$ node main.js
3 [ 250, 400, 60 ] 710
{ id: 2, user: 'omar', total: 90, paid: false }
true false
trueتلات orders مدفوعة، وأرقامها، والمجموع سبعمية وعشرة. find جابت الـ order بتاع عمر. some بـ true لأن فيه order فوق التلتمية، و every بـ false لأن order عمر مش مدفوع. و sara موجودة. الأدوات دي مش بتغيّر الـ array الأصلية، بترجّع واحدة جديدة، ودي حاجة بنحبها. فخ أخير. sort من غير حاجة بترتب الأرقام على إنها كلام! وكمان sort بتغيّر الـ array الأصلية، علشان كده بنعمل نسخة الأول بالـ spread. نشغّل.
const nums = [10, 9, 100, 1];
console.log([...nums].sort());
console.log([...nums].sort((a, b) => a - b));
console.log(nums);المخرجات
$ node main.js [ 1, 10, 100, 9 ] [ 1, 9, 10, 100 ] [ 10, 9, 100, 1 ]
من غير function: one، ten، one hundred، nine. ترتيب حروف، مش أرقام. بـ a minus b: الترتيب الصح. والأصلية زي ما هي، بفضل النسخة. افتكر: sort للأرقام لازم معاها function. التحدي: إيراد كل عميل. المدفوع بس. reduce تبني object: اسم المستخدم ومجموعه. Object dot entries تحوّله لـ array. ترتيب تنازلي. وطباعة. الكومنت فيه الخطوات والتلميحات، حتى شكل الـ reduce لو اتزنقت.
filter المدفوع ← reduce لـ { user: total } ← Object.entries ← sort تنازلي ← اطبع mona: 650
الحل. filter وبعده reduce على طول، وده اسمه chaining. جُوّه الـ reduce: لو المستخدم لسه مالوش قيمة، الـ double question mark بتخليها zero، ونزود عليها. وبعدين entries و sort بـ b minus a، تنازلي. وللطباعة destructuring جُوّه الـ for of. نشغّل.
const orders = [
{ id: 1, user: "mona", total: 250, paid: true },
{ id: 2, user: "omar", total: 90, paid: false },
{ id: 3, user: "mona", total: 400, paid: true },
{ id: 4, user: "sara", total: 60, paid: true },
{ id: 5, user: "omar", total: 300, paid: true },
];
const byUser = orders
.filter((o) => o.paid)
.reduce((acc, o) => {
acc[o.user] = (acc[o.user] ?? 0) + o.total;
return acc;
}, {});
const ranked = Object.entries(byUser).sort((a, b) => b[1] - a[1]);
for (const [user, total] of ranked) {
console.log(`${user}: ${total}`);
}المخرجات
$ node main.js mona: 650 omar: 300 sara: 60
مُنى ستمية وخمسين، وعمر تلتمية، لأن أول order ليه مش مدفوع، وسارة ستين. الدرس الجاي: JSON، اللغة اللي أي backend بيتكلم بيها.
03 / دروس المسار (122)
- 01أهلًا بيك: يعني إيه backend، والكورس ده ماشي إزاي
- 02الملفات والفولدرات والـ terminal: تتحرك على جهازك زي المبرمجين
- 03Node وأول برنامج: تشغّل وتقرا رسالة الخطأ
- 04القيم والمتغيرات والأنواع: const و let و typeof
- 05القرارات والتكرار: if و === والـ truthy والـ loops
- 06الـ functions: parameters و return والـ arrow functions والـ scope
- 07الـ objects والـ arrays: destructuring و spread و map و filter و reduceانت هنا
- 08JSON والنصوص والتواريخ: اللغة اللي الـ backend بيتكلم بيها
- 09الـ callbacks والـ closures وأول لمسة async: setTimeout
- 10الـ classes والـ errors: throw و try/catch و stack trace
- 11git: تحفَظ كل خطوة وترجعلها، وما تسرّبش أسرارك
- 12ESM و CommonJS و package exports: Node بيلاقي الـ modules إزاي
- 13الـ Promises و async/await: sequential و parallel والأخطاء والإلغاء
- 14الـ event loop والـ microtasks: Node بيعمل كل حاجة بـ thread واحد إزاي
- 15الـ Streams والـ Backpressure: ملفات أكبر من الـ memory
- 16الـ File System والـ paths: قراية وكتابة صح، ومن غير path traversal
- 17الـ Buffers والبيانات الـ binary: الـ bytes من جُوّه
- 18Crypto APIs والأسرار: random و hash و HMAC و AES-GCM
- 19الـ Worker Threads: شغل CPU تقيل من غير ما توقف الـ server
- 20الـ Errors على مستوى الـ process: operational و programmer و crash-only
- 21الـ Node test runner والـ debugging: tests من غير مكتبات
- 22HTTP من جُوّه: الـ methods والـ status codes والـ headers
- 23Express والـ Routing: الـ params والـ query والترتيب والـ Router
- 24Express 5: إيه اللي اتغيّر، وإزاي تنقل مشروع قديم
- 25الـ Middleware ودورة حياة الـ request جُوّه Express
- 26Validation للمدخلات بـ Zod: ما تصدّقش أي حاجة جاية من برة
- 27تصنيف الأخطاء و Problem Details: شكل واحد لكل الـ errors
- 28الـ CORS والـ Cookies: المتصفح بيحمي المستخدم إزاي
- 29رفع الملفات وحدود الحجم: multer من غير ثغرات
- 30الـ Realtime: Server-Sent Events و WebSockets
- 31OpenAPI والـ API Versioning: عقد مكتوب، وتغيير من غير ما تكسر حد
- 32PostgreSQL: الـ DDL وأنواع البيانات الصح لكل عمود
- 33الـ Constraints: PK و FK و UNIQUE و CHECK — الـ database بتحمي نفسها
- 34الـ Normalization والـ ERD: تقسّم البيانات صح من أول يوم
- 35الـ JOINs والتجميع: INNER و LEFT و GROUP BY و HAVING و window functions
- 36JSONB في PostgreSQL: إمتى تستخدمه، وإزاي تسأله وتعمله index
- 37الـ Indexes من جُوّه: B-tree و GIN و BRIN
- 38Partial و Covering indexes: indexes أصغر وأسرع
- 39الـ Pagination بالـ Keyset: ليه OFFSET بيبطّأ وبيكرر
- 40Full Text Search في Postgres: بحث بالكلمات، بالعربي والإنجليزي
- 41EXPLAIN ANALYZE والـ Planner: اقرا الـ plan وصلّح الـ query
- 42الـ Transactions بـ node-postgres: كله أو ولا حاجة
- 43الـ MVCC ومستويات الـ Isolation: transactions كتير في نفس الوقت
- 44الـ Locks والـ Deadlocks: مين مستني مين
- 45SKIP LOCKED و Job Leasing: Postgres كـ job queue
- 46Row Level Security والـ Multi-tenancy: كل عميل يشوف بياناته بس
- 47Migrations من غير downtime: expand و backfill و contract
- 48الـ Replication والـ Read Replicas: نسخ حية من الـ database
- 49الـ Connection Pooling و PgBouncer: connections أقل، requests أكتر
- 50الـ Partitioning والـ VACUUM والمراقبة: Postgres على المدى الطويل
- 51Redis وأنواع البيانات: strings و hashes و lists و sets و sorted sets
- 52Cache-aside والـ TTL: Redis قدام الـ database
- 53الـ Cache invalidation والـ Stampede: لما الـ key الشهير يموت
- 54Lua والـ Atomicity في Redis: كام أمر كأنهم أمر واحد
- 55الـ Rate Limiting في Redis: fixed window و sliding window و token bucket
- 56الـ Sessions في Redis: تسجيل دخول تقدر تلغيه في أي وقت
- 57Redis Streams والـ Consumer Groups: log بيتوزع على workers
- 58Pub/Sub و BullMQ: إشارات بين الـ instances، وطوابير شغل محترمة
- 59Redis لما يقع: الـ persistence (RDB و AOF) والـ replication
- 60Redis في الإنتاج: الـ ACLs والـ Cluster والمراقبة
- 61تخزين الـ Passwords بـ Argon2id: صح من أول مرة
- 62Session Fixation والـ Rotation: id جديد مع كل صلاحية جديدة
- 63الـ Cookie Flags والـ CSRF: المتصفح بيبعت الـ cookie، فلازم تتأكد مين اللي طلب
- 64الـ XSS والـ Content Security Policy: ما تخليش النص يتحوّل لـ code
- 65الـ OAuth و OpenID Connect و PKCE: login بـ Google من غير ما تشوف الـ password
- 66التأكد من الـ JWT صح: التوقيع والـ claims والـ scopes
- 67الـ BOLA و BFLA: الـ token السليم مش معناه إن الطلب مسموح
- 68الـ RBAC و ABAC والملكية: policy واحدة بدل ifs متفرقة
- 69الـ MFA والـ recovery: التاني factor اللي بجد
- 70الـ Passkeys و WebAuthn: login من غير password وضد الـ phishing
- 71الـ Threat Modeling بـ STRIDE: فكّر زي المهاجم قبل ما تكتب code
- 72الـ SQL Injection والـ Parameterization: الـ data ما تبقاش code
- 73الـ SSRF والـ Egress: ما تخليش الـ server بتاعك يزور الشبكة الداخلية
- 74الـ Security Headers والـ TLS: كل response يقول للـ browser يحمي المستخدم إزاي
- 75توقيع الـ Webhooks والـ Replay: الـ webhook ده جاي فعلًا من Stripe؟
- 76تغيير الأسرار والـ HMAC: إزاي تغيّر مفتاح من غير ما كل حاجة تقع
- 77سلسلة التوريد والـ SBOM: إنت واثق في ألف package ما تعرفهمش؟
- 78الـ Audit والـ Privacy Logs: سجّل مين عمل إيه، من غير ما تسجّل الأسرار
- 79اختبار الأمان و OWASP ASVS: الأمن جُوّه الـ CI، مش audit آخر السنة
- 80الـ Prototype Pollution والـ Resource DoS: request واحد يخلّي الكل admin أو يوقّع الـ server
- 81الـ SLI والـ SLO والـ Error Budget: إمتى تصحّي الـ on-call وإمتى تسيبه نايم
- 82الـ Event Loop Delay والـ ELU: لما Node نفسه يتخنق
- 83الـ Worker Threads والـ CPU Profiling: الشغل التقيل بره الـ event loop، والوقت راح فين
- 84الـ Memory والـ GC والـ Leaks: الـ process اللي بيكبر لحد ما يقع
- 85الـ N+1 Queries والـ DataLoader: صفحة واحدة بمِيّتين query
- 86الـ HTTP Caching والـ Compression: أسرع request هو اللي ما بيوصلش
- 87الـ Load Testing والـ Soak Testing: الـ server بيستحمل قد إيه بجد
- 88الـ Concurrency Limits والـ Bulkheads: dependency بطيئة ما توقعش الـ app كله
- 89الـ Timeouts والـ Retries والـ Circuit Breakers: إزاي تتعامل مع dependency بطيئة أو واقعة
- 90OpenTelemetry والـ Distributed Tracing: الـ request راح فين وضاع وقته فين
- 91Docker Layers والـ Build Context: ليه الـ build بياخد دقيقة وفيه الـ .env بتاعك
- 92Multi-stage Builds و Non-root: image صغيرة ومن غير root
- 93Docker Compose والـ Networks: الـ services بتلاقي بعض إزاي
- 94Docker Volumes: الـ data بتتحفَظ فين
- 95Healthcheck و Restart و Resource Limits: running مش معناها شغّال
- 96BuildKit والـ Build Secrets: الـ token ما يدخلش الـ image
- 97Reverse Proxy و TLS: اللي واقف قدام الـ app
- 98Multi-arch Images والـ Native Modules: اشتغلت عندي
- 99CI/CD و Rollback و Canary: الـ deploy يبقى ممل
- 100Kubernetes من الأول: Pods و Deployments و Services و Probes
- 101Modular Monolith و DDD: حدود واضحة من غير network
- 102Transactional Outbox و Inbox: الـ event ما يضيعش وما يتنفذش مرتين
- 103Sagas: عملية واحدة على تلات services من غير transaction واحدة
- 104CQRS و Event Sourcing: الـ events هي الحقيقة
- 105الـ Queues والـ Brokers: BullMQ و RabbitMQ و Kafka و SQS
- 106Realtime على أكتر من instance: Redis pub/sub و presence و resume
- 107الـ Object Storage والـ Media: presigned URLs و uploads مباشرة
- 108التكامل مع الـ Email والـ Search: retries و webhooks و indexes
- 109GraphQL و gRPC في الإنتاج: contracts و DataLoader و limits و deadlines
- 110Kafka على أكتر من zone و region: الـ replication والـ ISR والـ acks والـ MirrorMaker والـ failover
- 111TypeScript و runtime boundaries: الـ types بتختفي، والـ schemas لأ
- 112ORM ولا SQL: query builders و repositories و migrations و transactions
- 113الـ Serverless من جُوّه: cold starts و freeze و timeouts و connections
- 114الـ Testing Pyramid: unit و integration و e2e على Postgres حقيقية
- 115Contract Testing و Property-Based Testing بـ fast-check
- 116الـ Payments: idempotency و webhooks و refunds و reconciliation
- 117الـ Timezones والـ Internationalization: UTC و DST والـ locales والـ plurals والفلوس
- 118الـ Privacy والـ Data Retention: consent و export و erasure و crypto shredding
- 119الـ ADRs والـ Feature Flags: القرارات المكتوبة والـ rollout من غير deploy
- 120Multi-region active-active: حل الـ conflicts بـ HLC و CRDTs
- 121Readiness و Liveness و Startup Probes: الـ pod جاهز ولا عايش؟
- 122Incident Runbooks: من الـ alert للـ postmortem