TypeORM - Tutorialspoint

TypeORM i

TypeORM

About the Tutorial

TypeORM is an object-relational mapper library for TypeScript and JavaScript. TypeORM is a tool in the Micro-frameworks category of the tech stack. This tutorial walks through the basics of TypeORM framework, how to set up entity objects, how to configure relationship between objects, how to store/retrieve data from/to the database, how to customize the repository instance to manipulate the given database and finally conclude with different database operations.

Audience

This tutorial is prepared for professionals who are aspiring to make a career in the field of back-end database development using object relation mapping. This tutorial is intended to make you comfortable in getting started with the TypeORM concepts.

Prerequisites

Before proceeding with the various types of concepts given in this tutorial, we assume that the readers have the basic understanding of database and objects in programming languages. In addition to this, it will be very helpful, if the readers have a sound knowledge on TypeScript and JavaScript.

Copyright & Disclaimer

Copyright 2020 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at contact@

ii

TypeORM

Table of Contents

About the Tutorial ........................................................................................................................................... ii Audience.......................................................................................................................................................... ii Prerequisites.................................................................................................................................................... ii Copyright & Disclaimer .................................................................................................................................... ii Table of Contents ........................................................................................................................................... iii 1. TypeORM -- Introduction .........................................................................................................................1 Overview.......................................................................................................................................................... 1 Features of TypeORM...................................................................................................................................... 1 Benefits of TypeORM....................................................................................................................................... 1 2. TypeORM -- Installation ...........................................................................................................................3 Install TypeORM .............................................................................................................................................. 3 Install database driver ..................................................................................................................................... 4 3. TypeORM -- Creating a Simple Project .....................................................................................................6 Project structure.............................................................................................................................................. 6 ormconfig.json file........................................................................................................................................... 7 4. TypeORM -- Connection API.....................................................................................................................9 Creating a new connection .............................................................................................................................. 9 5. TypeORM -- Entity .................................................................................................................................12 Introduction................................................................................................................................................... 12 Columns ......................................................................................................................................................... 14 6. TypeORM -- Relations ............................................................................................................................21 One-to-One.................................................................................................................................................... 21 One-to-Many and Many-to-One.................................................................................................................... 23 Many-to-Many............................................................................................................................................... 24 7. TypeORM -- Working with Repository ...................................................................................................26 Repository types ............................................................................................................................................ 26

iii

TypeORM

Repository API ............................................................................................................................................... 27 8. TypeORM -- Working with Entity Manager ............................................................................................30

Entity Manager API........................................................................................................................................ 30 9. TypeORM -- Query Builder .....................................................................................................................33

Connection .................................................................................................................................................... 33 Entity manager .............................................................................................................................................. 33 Repository ..................................................................................................................................................... 33 Aliases............................................................................................................................................................ 33 Parameters .................................................................................................................................................... 34 Adding expression ......................................................................................................................................... 34 10. TypeORM -- Query Operations...............................................................................................................39 Build insert query .......................................................................................................................................... 39 Build update query ........................................................................................................................................ 40 Build select query .......................................................................................................................................... 41 Build delete query ......................................................................................................................................... 42 11. TypeORM -- Transactions.......................................................................................................................44 Creating transactions..................................................................................................................................... 44 Transaction in QueryRunner .......................................................................................................................... 45 12. TypeORM -- Indices................................................................................................................................46 Column indices .............................................................................................................................................. 46 Unique indices ............................................................................................................................................... 46 Spatial indices ................................................................................................................................................ 47 13. TypeORM -- Entity Listener and Logging ................................................................................................48 Subscriber ...................................................................................................................................................... 48 Logging .......................................................................................................................................................... 50 Custom logger ............................................................................................................................................... 51 14. TypeORM with JavaScript .......................................................................................................................52 15. TypeORM -- Working with MongoDB.....................................................................................................56

iv

TypeORM Creating a project .......................................................................................................................................... 56 Define entities and columns .......................................................................................................................... 56 MongoDB EntityManager .............................................................................................................................. 58 16. TypeORM with Express ...........................................................................................................................59 Creating a simple application ........................................................................................................................ 59 17. TypeORM -- Migrations..........................................................................................................................65 Creating new migration ................................................................................................................................. 65 Revert migration............................................................................................................................................ 68 18. TypeORM -- Working with CLI ................................................................................................................70 Create TypeORM project ............................................................................................................................... 70 Conclusion ..................................................................................................................................................... 71

v

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download