TypeORM 是一个 ORM 框架,它可以运行在 NodeJS、Browser、Cordova、PhoneGap、Ionic、React Native、Expo 和 Electron 平台上,可以与 TypeScript 和 JavaScript (ES5,ES6,ES7,ES8)一起使用。 它的目标是始终支持最新的 JavaScript 特性并提供额外的特性以帮助你开发任何使用数据库的(不管是只有几张表的小型应用还是拥有多数据库的大型企业应用)应用程序。
TypeORM+ 是一个TypeORM的第三方增强库。TypeORM+添加了一些TypeORM暂不支持的特性。目前支持的特性如下:
- 支持软删除
- 支持根据页码和条数分页
- 支持唯一性冲突检测
- 支持设定自定义筛选条件
还有更多...
入门
安装
-
通过 npm
安装:
npm install typeorm-plus --save
-
你还需要安装 reflect-metadata
:
npm install reflect-metadata --save
并且需要在应用程序的全局位置导入(例如在app.ts
中)
import "reflect-metadata";
-
你可能还需要安装 node typings(以此来使用 Node 的智能提示):
npm install @types/node --save
-
安装数据库驱动:
-
MySQL 或者 MariaDB
npm install mysql --save
(也可以安装 mysql2
)
-
PostgreSQL
npm install pg --save
-
SQLite
npm install sqlite3 --save
-
Microsoft SQL Server
npm install mssql --save
-
sql.js
npm install sql.js --save
-
Oracle
npm install oracledb --save
根据你使用的数据库,仅安装其中一个即可。
要使 Oracle 驱动程序正常工作,需要按照其站点中的安装说明进行操作。
-
MongoDB (试验性)
npm install mongodb --save
-
NativeScript, react-native 和 Cordova
查看 支持的平台
TypeScript 配置
此外,请确保你使用的 TypeScript 编译器版本是2.3或更高版本,并且已经在 tsconfig.json
中启用了以下设置:
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
除此之外,你可能还需要在编译器选项的 lib
中启用 es6
,或者安装 es6-shim
的 @types
。
快速开始
如何快速开始上手 typeorm 请参考typeorm 文档 (https://github.com/typeorm/typeorm)