ORM All In One
ORM All In One
ORM
Object Relational Mapping / 对象关系映射
ORM is a technique that allows you to communicate with a database using an object oriented pattern
Advantages of ORMs
ORMs map our database entries to objects that we define.
This allows us to write a lot of reusable logic for our database.
It also makes a lot of our more complex queries simpler and can cut down bloat in our code base.
Most ORMs also come with built in protection from SQL Injection.
Disadvantages of ORMs
ORMs aren’t perfect for every project.
Often times, you will not have direct control over the SQL the ORM is using under the hood.
This can sometimes make queries slower than they would be if you wrote them by hand.
ORMs also are more mistake prone than SQL, It’s easier to accidentally make a function that queries the database 200 times in an ORM vs SQL since you may not always realize when a function is triggering a query.
Although we’re not discussing NoSQL in this module, There are many NoSQL ORMs available as well!
https://en.wikipedia.org/wiki/Object-relational_mapping
ORMs
TypeORM
ORM for TypeScript
and JavaScript. Supports MySQL, PostgreSQL
, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL
databases. Works in NodeJS
, Browser, Ionic, Cordova and Electron platforms.
Supports MongoDB NoSQL
database.
https://www.npmjs.com/package/typeorm
https://github.com/typeorm/typeorm
Prisma
Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB
Prisma is a next-generation ORM that consists of these tools:
Prisma Client: Auto-generated and type-safe query builder for Node.js & TypeScript
Prisma Migrate: Declarative data modeling & migration system
Prisma Studio: GUI to view and edit data in your database
Prisma Client
can be used in any Node.js or TypeScript backend application
(including serverless applications
and microservices
).
This can be a REST API
, a GraphQL API
, a gRPC API
, or anything else that needs a database.
https://github.com/prisma/prisma
https://www.npmjs.com/package/prisma
Sequelize
Feature-rich ORM for modern Node.js
and TypeScript
, it supports PostgreSQL (with JSON and JSONB
support), MySQL, MariaDB, SQLite, MS SQL Server, Snowflake, Oracle DB (v6), DB2 and DB2 for IBM i.
https://github.com/sequelize/sequelize
https://www.npmjs.com/package/sequelize
https://www.npmjs.com/package/sequelize-cli
demos
TypeScript ORM demos
TypeORM
https://www.npmjs.com/package/typeorm
https://opencollective.com/typeorm
supports MySQL / MariaDB / Postgres / CockroachDB / SQLite / Microsoft SQL Server / Oracle / SAP Hana / sql.js
$ yarn add typeorm
$ typeorm init --name MyProject --database mysql
- DataMapper
// models
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
firstName: string;
@Column()
lastName: string;
@Column()
age: number;
}
// logic
const user = new User();
user.firstName = "Timber";
user.lastName = "Saw";
user.age = 25;
await repository.save(user);
const allUsers = await repository.find();
const firstUser = await repository.findOne(1); // find by id
const timber = await repository.findOne({ firstName: "Timber", lastName: "Saw" });
await repository.remove(timber);
- ActiveRecord
// models
import {Entity, PrimaryGeneratedColumn, Column, BaseEntity} from "typeorm";
@Entity()
export class User extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column()
firstName: string;
@Column()
lastName: string;
@Column()
age: number;
}
const user = new User();
user.firstName = "Timber";
user.lastName = "Saw";
user.age = 25;
await user.save();
const allUsers = await User.find();
const firstUser = await User.findOne(1);
const timber = await User.findOne({ firstName: "Timber", lastName: "Saw" });
await timber.remove();
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
RAW SQL / 原生 SQL
db.query("SELECT * FROM users");
SQL 注入
SQL Injection
// 设定$name 中插入了我们不需要的SQL语句
$name = "Qadir'; DELETE FROM users;";
mysqli_query($conn, "SELECT * FROM users WHERE name='{$name}'");
https://www.jianshu.com/p/078df7a35671
https://www.runoob.com/mysql/mysql-sql-injection.html
refs
Angular
+NestJS
https://www.sololearn.com/learning/1092
https://docs.nestjs.com/recipes/sql-typeorm
https://www.tutorialspoint.com/typeorm/typeorm_quick_guide.htm
Auto Open Gmail Link
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/13595464.html
未经授权禁止转载,违者必究!