支持的平台
NodeJS
TypeORM 兼容 Node.js 20+,目前每次提交都会在 Node.js 20 和 24 上测试。
浏览器
你可以在浏览器中使用 sql.js。
Webpack 配置
在 browser 文件夹中,该包还包含一个编译为 ES2015 模块的版本。如果你想使用不同的加载器,可以从这里入手。在 TypeORM 0.1.7 之前,该包的设置方式会让 webpack 这类加载器自动使用 browser 文件夹。从 0.1.7 开始,为了支持在 Node.js 项目中使用 Webpack,这一做法被取消了。这意味着必须使用 NormalModuleReplacementPlugin 来确保浏览器项目加载到正确的版本。在你的 webpack 配置文件中,该插件的配置如下:
plugins: [
..., // 你已有的任何插件
new webpack.NormalModuleReplacementPlugin(/typeorm$/, function (result) {
result.request = result.request.replace(/typeorm/, "typeorm/browser");
}),
new webpack.ProvidePlugin({
'window.SQL': 'sql.js/dist/sql-wasm.js'
})
]并确保 sql-wasm.wasm 文件存在于你的公共路径中。
配置示例
new DataSource({
type: "sqljs",
entities: [Photo],
synchronize: true,
})别忘了引入 reflect-metadata
在你的主 html 页面中,你需要引入 reflect-metadata:
<script src="./node_modules/reflect-metadata/Reflect.js"></script>Capacitor
请参阅 Capacitor 官方文档中的使用 TypeORM 的 Capacitor 驱动类型。
Cordova / Ionic 应用
TypeORM 能够借助 cordova-sqlite-storage 插件运行在 Cordova/Ionic 应用上。你可以像在浏览器包中那样在模块加载器之间做选择。重要:与 Ionic 一起使用时,需要一个自定义的 webpack 配置文件。注意,使用 cordova-sqlite-storage 插件时目前不支持事务。更多信息参见 Cordova SQLite 的限制。
Expo
TypeORM 能够借助 Expo SQLite API 运行在 Expo 应用上。
NativeScript
tns install webpack(为什么需要 webpack 请看下面)tns plugin add nativescript-sqlite在应用的入口点创建一个 DataSource
import driver from "nativescript-sqlite"
const dataSource = new DataSource({
database: "test.db",
type: "nativescript",
driver,
entities: [
Todo, //... 你的任何实体
],
logging: true,
})注意:这仅适用于 NativeScript 4.x 及以上版本
与 NativeScript 一起使用时,必须使用 webpack。typeorm/browser 包是带 import/export 的原始 ES7 代码,它 无法 直接运行,必须被打包。请使用 tns run --bundle 方式
在这里查看示例!
React Native
TypeORM 能够借助 react-native-sqlite-storage 插件运行在 React Native 应用上。
NodeJS
TypeORM is compatible with Node.js 20+ and currently each commit is tested on Node.js 20 and 24.
Browser
You can use sql.js in the browser.
Webpack configuration
In the browser folder the package also includes a version compiled as a ES2015 module. If you want to use a different loader this is the point to start. Prior to TypeORM 0.1.7, the package is setup in a way that loaders like webpack will automatically use the browser folder. With 0.1.7 this was dropped to support Webpack usage in Node.js projects. This means, that the NormalModuleReplacementPlugin has to be used to insure that the correct version is loaded for browser projects. The configuration in your webpack config file, for this plugin looks like this:
plugins: [
..., // any existing plugins that you already have
new webpack.NormalModuleReplacementPlugin(/typeorm$/, function (result) {
result.request = result.request.replace(/typeorm/, "typeorm/browser");
}),
new webpack.ProvidePlugin({
'window.SQL': 'sql.js/dist/sql-wasm.js'
})
]and make sure sql-wasm.wasm file exists in your public path.
Example of configuration
new DataSource({
type: "sqljs",
entities: [Photo],
synchronize: true,
})Don't forget to include reflect-metadata
In your main html page, you need to include reflect-metadata:
<script src="./node_modules/reflect-metadata/Reflect.js"></script>Capacitor
See Using TypeORM with the Capacitor driver type in the official Capacitor docs.
Cordova / Ionic apps
TypeORM is able to run on Cordova/Ionic apps using the cordova-sqlite-storage plugin. You have the option to choose between module loaders just like in browser package. Important: For use with Ionic, a custom webpack config file is needed. Note that there is currently no support for transactions when using the cordova-sqlite-storage plugin. See Cordova SQLite limitations for more information.
Expo
TypeORM is able to run on Expo apps using the Expo SQLite API.
NativeScript
tns install webpack(read below why webpack is required)tns plugin add nativescript-sqliteCreate a DataSource in your app's entry point
import driver from "nativescript-sqlite"
const dataSource = new DataSource({
database: "test.db",
type: "nativescript",
driver,
entities: [
Todo, //... whatever entities you have
],
logging: true,
})Note: This works only with NativeScript 4.x and above
When using with NativeScript, using webpack is compulsory. The typeorm/browser package is raw ES7 code with import/export which will NOT run as it is. It has to be bundled. Please use the tns run --bundle method
Checkout example here!
React Native
TypeORM is able to run on React Native apps using the react-native-sqlite-storage plugin.