[{"data":1,"prerenderedAt":569},["ShallowReactive",2],{"news-item-\u002Fnews\u002Ftypeorm":3},{"id":4,"title":5,"body":6,"category":556,"created by":557,"date":558,"description":559,"extension":560,"meta":561,"navigation":562,"path":563,"sections":564,"seo":565,"stem":566,"thumbnail":567,"__hash__":568},"content_en\u002Fnews\u002Ftypeorm.md","TypeORM and Query Builder in TypeORM",{"type":7,"value":8,"toc":539},"minimark",[9,17,21,24,29,32,35,39,42,45,48,51,55,58,61,64,67,70,73,79,82,88,91,102,108,111,117,123,126,132,138,141,147,153,159,161,167,173,179,181,187,193,199,201,207,213,219,221,227,233,236,239,244,250,252,258,263,269,271,277,282,288,293,299,304,310,315,318,321,327,333,336,342,346,352,354,359,363,369,371,376,380,386,388,393,397,403,405,410,415,421,423,428,433,439,445,449,455,457,463,467,473,475,481,485,491,495,501,505,511,515,518,521,524,527,531],[10,11,13],"h2",{"id":12},"introduction",[14,15,16],"strong",{},"Introduction",[18,19,20],"p",{},"TypeORM is an ORM that can run in NodeJS, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, Expo, and Electron platforms and can be used with TypeScript and JavaScript (ES5, ES6, ES7, ES8). Its goal is to always support the latest JavaScript features and provide additional features that help you to develop any kind of application that uses databases - from small applications with a few tables to large scale enterprise applications with multiple databases.",[18,22,23],{},"ORM is a type of tool that maps entities with database tables. ORM provides simplified development process by automating object-to-table and table-to-object conversion.",[25,26,28],"h3",{"id":27},"overview","Overview",[18,30,31],{},"TypeORM is an Object Relational Mapper library running in node.js and written in TypeScript. TypeScript is an improvement to JavaScript with optional typing. TypeScript is a compiled language. It is not interpreted at run-time. The TypeScript compiler takes TypeScript files (.ts) and compiles them in to JavaScript files (.js).",[18,33,34],{},"TypeORM supports multiple databases like MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana and WebSQL. TypeORM is an easy-to-use ORM to scaffold new apps that connect to databases. TypeORM functionality is RDBMS-specific concepts.",[25,36,38],{"id":37},"features-of-typeorm","Features of TypeORM",[18,40,41],{},"Automatically create database table schemes based on your models",[18,43,44],{},"Easily insert, update and delete object in the database",[18,46,47],{},"Create mapping (one-to-one, one-to-many and many-to-many) between tables",[18,49,50],{},"Provides simple CLI commands",[25,52,54],{"id":53},"benefits-of-typeorm","Benefits of TypeORM",[18,56,57],{},"TypeORM is easy to use ORM framework with simple coding. It has the following benefits:",[18,59,60],{},"1. High quality and loosely-coupled applications",[18,62,63],{},"2. Scalable applications",[18,65,66],{},"3. Easily integrate with other modules",[18,68,69],{},"4. Perfectly fits any architecture from small to enterprise apps",[18,71,72],{},"In typeorm there are many features but in this blog I will write about how to use query builder of typeorm with typescript.",[10,74,76],{"id":75},"query-builder",[14,77,78],{},"Query Builder",[18,80,81],{},"Query builder is used build complex SQL queries in an easy way. It is initialized from Connection method.",[25,83,85],{"id":84},"connection",[14,86,87],{},"Connection",[18,89,90],{},"Consider a simple example of how to use QueryBuilder using connection method:",[92,93,98],"pre",{"className":94,"code":96,"language":97},[95],"language-text","import {getConnection} from \"typeorm\";\n\nconst user = await getConnection()\n.createQueryBuilder()\n.from(\"user\", \"user\")\n.select(\"user\")\n.where(\"user.id = :id\", { id: 1 })\n.getOne();\n","text",[99,100,96],"code",{"__ignoreMap":101},"",[25,103,105],{"id":104},"repository",[14,106,107],{},"Repository",[18,109,110],{},"We can use repository to create query builder. It is described below,",[92,112,115],{"className":113,"code":114,"language":97},[95],"import {getRepository} from \"typeorm\"; \nconst user = await getRepository(User)\n.createQueryBuilder(\"user\")\n.where(\"user.id = :id\", { id: 1 }) \n.getOne();\n",[99,116,114],{"__ignoreMap":101},[25,118,120],{"id":119},"adding-expression-use-getconnection-no-model-needed",[14,121,122],{},"Adding expression use getConnection (no model needed)",[18,124,125],{},"table user: id, name, age",[18,127,128,131],{},[14,129,130],{},"where"," is used to filter the records if the condition is matched.",[92,133,136],{"className":134,"code":135,"language":97},[95],"getConnection().createQueryBuilder().from(\"user\", \"user\").select(\"user\") .where(\"user.id = :id\", { id: 1 }) .getRawOne();\n",[99,137,135],{"__ignoreMap":101},[18,139,140],{},"This query is equivalent to:",[92,142,145],{"className":143,"code":144,"language":97},[95],"select * from user where user.id=1;\n",[99,146,144],{"__ignoreMap":101},[18,148,149,152],{},[14,150,151],{},"orderBy"," is used to sort the records based on the field",[92,154,157],{"className":155,"code":156,"language":97},[95],"getConnection().createQueryBuilder().from(\"user\", \"user\").orderBy(\"user.name\");\n",[99,158,156],{"__ignoreMap":101},[18,160,140],{},[92,162,165],{"className":163,"code":164,"language":97},[95],"select * from user order by user.name;\n",[99,166,164],{"__ignoreMap":101},[18,168,169,172],{},[14,170,171],{},"groupBy",": It is used to group the records based on the specified column.",[92,174,177],{"className":175,"code":176,"language":97},[95],"getConnection().createQueryBuilder().from(\"user\", \"user\").groupBy(\"user.id\")\n",[99,178,176],{"__ignoreMap":101},[18,180,140],{},[92,182,185],{"className":183,"code":184,"language":97},[95],"select * from user group by user.id; \n",[99,186,184],{"__ignoreMap":101},[18,188,189,192],{},[14,190,191],{},"limit",": is used to limit the selection of rows.",[92,194,197],{"className":195,"code":196,"language":97},[95],"getConnection().createQueryBuilder().from(\"user\", \"user\").limit(5);\n",[99,198,196],{"__ignoreMap":101},[18,200,140],{},[92,202,205],{"className":203,"code":204,"language":97},[95],"select * from user limit 5; \n",[99,206,204],{"__ignoreMap":101},[18,208,209,212],{},[14,210,211],{},"offset"," is used to specify, how many rows to skip the result.",[92,214,217],{"className":215,"code":216,"language":97},[95],"getConnection().createQueryBuilder().from(\"user\", \"user\").offset(5);\n",[99,218,216],{"__ignoreMap":101},[18,220,140],{},[92,222,225],{"className":223,"code":224,"language":97},[95],"select * from user offset 5;\n",[99,226,224],{"__ignoreMap":101},[18,228,229,232],{},[14,230,231],{},"joins",": join clause is used to combine rows from two or more tables, based on a related column. The example below is two tables student with project  with 1-n relationship:",[18,234,235],{},"student:  id, name, email, phone",[18,237,238],{},"project : id, name, student_id",[18,240,241],{},[14,242,243],{},"leftJoinAndSelect",[92,245,248],{"className":246,"code":247,"language":97},[95],"const student = await getConnection().createQueryBuilder().from(\"student\", \"student\")\n.leftJoinAndSelect(\"project\", \"project\", \"project.student_id = student.id\")\n.where(\"student.name = :name\", { name: \"Student1\" })\n.getOne();\n",[99,249,247],{"__ignoreMap":101},[18,251,140],{},[92,253,256],{"className":254,"code":255,"language":97},[95],"SELECT student.*, project.* FROM student student LEFT JOIN project project \nON project.student_id = student.id WHERE student.name = 'Student1'\n",[99,257,255],{"__ignoreMap":101},[18,259,260],{},[14,261,262],{},"innerJoinAndSelect",[92,264,267],{"className":265,"code":266,"language":97},[95],"const student = await getConnection().createQueryBuilder().from(\"student\", \"student\") \n.innerJoinAndSelect(\"project\", \"project\", \"project.student_id = student.id\") .where(\"student.name = :name\", { name: \"student1\" }) .getOne();\n",[99,268,266],{"__ignoreMap":101},[18,270,140],{},[92,272,275],{"className":273,"code":274,"language":97},[95],"SELECT student.*, project.* FROM student student INNER JOIN project project \nON project.student_id = student.id WHERE student.name = 'student1';\n",[99,276,274],{"__ignoreMap":101},[18,278,279],{},[14,280,281],{},"Insert",[92,283,286],{"className":284,"code":285,"language":97},[95],"import {getConnection} from \"typeorm\"; \n\nawait getConnection()\n.createQueryBuilder()\n.insert() \n.into('student') \n.values([ { name: \"test\", email: \"test@gmail.com\", phone: \"09011112222\"},\n { name: \"test2\", email: \"test2@gmail.com\", phone: \"09011112222\"}\n]) \n.execute();\n",[99,287,285],{"__ignoreMap":101},[18,289,290],{},[14,291,292],{},"Update",[92,294,297],{"className":295,"code":296,"language":97},[95],"import {getConnection} from \"typeorm\"; \n\nawait getConnection().createQueryBuilder() \n.update('student') \n.set({ name: \"test3\", email: \"test3@gmail.com\"}) \n.where(\"id = :id\", { id: 1 }) \n.execute(); \n",[99,298,296],{"__ignoreMap":101},[18,300,301],{},[14,302,303],{},"Delete",[92,305,308],{"className":306,"code":307,"language":97},[95],"import {getConnection} from \"typeorm\"; \n\n await getConnection() \n.createQueryBuilder() \n.delete()\n.from('student')\n.where(\"id = :id\", { id: 1 }) .execute();\n",[99,309,307],{"__ignoreMap":101},[18,311,312],{},[14,313,314],{},"Subqueries",[18,316,317],{},"Example 1: Use subQuery to select field name in table student the record has email contains the string test",[18,319,320],{},"Example 2: Use subQuery to select max id in table project match the field name containing the string test and select studentId, studentName, projectId",[92,322,325],{"className":323,"code":324,"language":97},[95],"const student = await getConnection().createQueryBuilder().select(\"student.name\", \"name\") \n.from((subQuery) => { return subQuery.select(\"student.name\", \"name\") \n                     .from(\"student\", \"student\").where(\"student.email like :email\", { email: '%test%'}) },\n                   \"student\") .getRawMany();\n\nconst student = await getConnection().createQueryBuilder()\n.select(`student.id as studentId,student.name as studentName,project.id as projectId`)) \n.from(\"student\", \"student\").leftJoin(\"project\", \"project\", \"project.student_id = student.id\")\n.where (\"project.id = (select max(id) from project where name like '%test%' \")).getRawMany();\n",[99,326,324],{"__ignoreMap":101},[25,328,330],{"id":329},"adding-expression-use-getrepository-with-model",[14,331,332],{},"Adding expression use getRepository() with model",[18,334,335],{},"The example has the following User model:",[92,337,340],{"className":338,"code":339,"language":97},[95],"import {Entity, PrimaryGeneratedColumn, Column} from \"typeorm\";\n\n@Entity('user') \nexport class User { \n@PrimaryGeneratedColumn() \nid: number;\n \n@Column('varchar', {name: 'name', nullable: true, length: 50}) \nname: string | null; \n\n@Column('int', {name: 'age', nullable: true}) \nname: number | null; \n}\n",[99,341,339],{"__ignoreMap":101},[18,343,344,131],{},[14,345,130],{},[92,347,350],{"className":348,"code":349,"language":97},[95],"getRepository(User).createQueryBuilder(\"user\").where(\"user.id= :id\", { id: 1 });\n",[99,351,349],{"__ignoreMap":101},[18,353,140],{},[92,355,357],{"className":356,"code":144,"language":97},[95],[99,358,144],{"__ignoreMap":101},[18,360,361,152],{},[14,362,151],{},[92,364,367],{"className":365,"code":366,"language":97},[95],"getRepository(User).createQueryBuilder(\"user\").orderBy(\"user.name\");\n",[99,368,366],{"__ignoreMap":101},[18,370,140],{},[92,372,374],{"className":373,"code":164,"language":97},[95],[99,375,164],{"__ignoreMap":101},[18,377,378,172],{},[14,379,171],{},[92,381,384],{"className":382,"code":383,"language":97},[95],"getRepository(User).createQueryBuilder(\"user\") .groupBy(\"user.id\")\n",[99,385,383],{"__ignoreMap":101},[18,387,140],{},[92,389,391],{"className":390,"code":184,"language":97},[95],[99,392,184],{"__ignoreMap":101},[18,394,395,192],{},[14,396,191],{},[92,398,401],{"className":399,"code":400,"language":97},[95],"getRepository(User).createQueryBuilder(\"user\").limit(5);\n",[99,402,400],{"__ignoreMap":101},[18,404,140],{},[92,406,408],{"className":407,"code":204,"language":97},[95],[99,409,204],{"__ignoreMap":101},[18,411,412,414],{},[14,413,211],{}," is used to specify, how many rows to skip the result",[92,416,419],{"className":417,"code":418,"language":97},[95],"getRepository(User).createQueryBuilder(\"user\") .offset(5);\n",[99,420,418],{"__ignoreMap":101},[18,422,140],{},[92,424,426],{"className":425,"code":224,"language":97},[95],[99,427,224],{"__ignoreMap":101},[18,429,430,432],{},[14,431,231],{},": join clause is used to combine rows from two or more tables, based on a related column. Consider two entity:",[92,434,437],{"className":435,"code":436,"language":97},[95],"import {Entity, PrimaryGeneratedColumn, Column, OneToMany} from \"typeorm\";\nimport {Project} from \".\u002FProject\"; \n\n@Entity('student')\nexport class Student { \n@PrimaryGeneratedColumn()\nid: number; \n\n@Column('varchar', {name: 'name', nullable: true, length: 50})\nname: string | null;\n\n@Column('varchar', {name: 'email', nullable: true, length: 30})\nemail: string | null;\n\n@Column('varchar', {name: 'phone', nullable: true, length: 11})\nphone: string | null;\n\n@OneToMany(()=> Project, project => project.student) \nprojects: project[]; \n} \n",[99,438,436],{"__ignoreMap":101},[92,440,443],{"className":441,"code":442,"language":97},[95],"import {Entity, PrimaryGeneratedColumn, Column, ManyToOne}\n\nfrom \"typeorm\"; import {Student} from \".\u002FStudent\";\n\n@Entity('project') \nexport class Project {\n\n@PrimaryGeneratedColumn() \nid: number;\n\n@Column('varchar', {name: 'name', nullable: true, length: 50})\nname: string | null;\n\n@Column('bigint', {name: 'student_id', nullable: true, unsigned: true})\nstudentId: number | null;\n\n@ManyToOne(() => Student, student => student.projects)\n@JoinColumn([{name: 'student_id', referencedColumnName: 'id'}])\nstudent : Student;\n}\n",[99,444,442],{"__ignoreMap":101},[18,446,447],{},[14,448,243],{},[92,450,453],{"className":451,"code":452,"language":97},[95],"const student = await getRepository(Student).createQueryBuilder(\"student\")\n.leftJoinAndSelect(\"student.projects\", \"project\")\n.where(\"student.name = :name\", { name: \"Student1\" })\n.getOne();\n",[99,454,452],{"__ignoreMap":101},[18,456,140],{},[92,458,461],{"className":459,"code":460,"language":97},[95],"SELECT student.*, project.* FROM student student LEFT JOIN projects project \nON project.student = student.id WHERE student.name = 'Student1'\n",[99,462,460],{"__ignoreMap":101},[18,464,465],{},[14,466,262],{},[92,468,471],{"className":469,"code":470,"language":97},[95],"const student = await getRepository(Student).createQueryBuilder(\"student\")\n.innerJoinAndSelect(\"student.projects\", \"project\")\n.where(\"student.name = :name\", { name: \"student1\" })\n.getOne();\n",[99,472,470],{"__ignoreMap":101},[18,474,140],{},[92,476,479],{"className":477,"code":478,"language":97},[95],"SELECT student.*, project.* FROM students student INNER JOIN projects project \nON project.student = student.id WHERE student.name = 'Student1';\n",[99,480,478],{"__ignoreMap":101},[18,482,483],{},[14,484,281],{},[92,486,489],{"className":487,"code":488,"language":97},[95],"import {getConnection} from \"typeorm\"; \n\nawait getRepository(Student)\n.createQueryBuilder() \n.insert() \n.into(Student) \n.values([ { name: \"test\", email: \"test@gmail.com\", phone: \"09011112222\"}, \n{ name: \"test2\", email: \"test2@gmail.com\", phone: \"09011112222\"} ]) \n.execute();\n",[99,490,488],{"__ignoreMap":101},[18,492,493],{},[14,494,292],{},[92,496,499],{"className":497,"code":498,"language":97},[95],"import {getConnection} from \"typeorm\"; \n\nawait getRepository(Student)\n.createQueryBuilder() \n.update(Student) \n.set({ name: \"test3\", email: \"test3@gmail.com\"}) \n.where(\"id = :id\", { id: 1 }) .execute();\n",[99,500,498],{"__ignoreMap":101},[18,502,503],{},[14,504,303],{},[92,506,509],{"className":507,"code":508,"language":97},[95],"import {getConnection} from \"typeorm\"; \nawait getRepository(Student).createQueryBuilder() \n.delete() \n.from(Student) \n.where(\"id = :id\", { id: 1 }) \n.execute();\n",[99,510,508],{"__ignoreMap":101},[10,512,514],{"id":513},"conclusion","Conclusion",[18,516,517],{},"It is recommended to use query builder because it can build sql sentences from easy to complex",[18,519,520],{},"Syntax is similar to pure sql, so it is easy to code and read",[18,522,523],{},"It is also possible to use the query builder normally without the model declaration",[18,525,526],{},"In terms of performance, using query builder access time will be faster than using typeorm in api repository",[10,528,530],{"id":529},"references","References",[18,532,533],{},[534,535,536],"a",{"href":536,"rel":537},"https:\u002F\u002Ftypeorm.io\u002F",[538],"nofollow",{"title":101,"searchDepth":540,"depth":540,"links":541},2,[542,548,554,555],{"id":12,"depth":540,"text":16,"children":543},[544,546,547],{"id":27,"depth":545,"text":28},3,{"id":37,"depth":545,"text":38},{"id":53,"depth":545,"text":54},{"id":75,"depth":540,"text":78,"children":549},[550,551,552,553],{"id":84,"depth":545,"text":87},{"id":104,"depth":545,"text":107},{"id":119,"depth":545,"text":122},{"id":329,"depth":545,"text":332},{"id":513,"depth":540,"text":514},{"id":529,"depth":540,"text":530},"tech talk","Briswell Vietnam Co Ltd","2022-06-29","Introduction TypeORM is an ORM that can run in NodeJS, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, Expo, and Electron platforms and can be used with TypeScript and JavaScript (ES5, ES6, ES7, ES8). Its goal is to always support the latest JavaScript features and provide additional features that help you to develop any kind of application that uses databases - from small applications with a few tables to large scale enterprise applications with multiple databases.","md",{},true,"\u002Fnews\u002Ftypeorm",null,{"title":5,"description":559},"news\u002Ftypeorm","https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2022\u002F06\u002F08154314\u002Fquerybuilder-typeorm.jpg","qvXjLcOP5HQ4EsFIvd1p3aB39v8ptaw3yeTF2VUWcD8",1782263087051]