SQLite
安装
- 对于 Better SQLite:
npm install better-sqlite3- 对于 sql.js:
npm install sql.js- 对于 Capacitor、Cordova、Expo、NativeScript 和 React Native,请查看支持的平台。
数据源选项
通用数据源选项请参阅数据源选项。
better-sqlite3 数据源选项
database- 数据库路径。例如"mydb.sqlite"。enableWAL- 启用 WAL 模式(默认false)。参见 SQLite WAL 模式。fileMustExist- 如果数据库不存在,将抛出 Error 而不是创建新文件。对内存连接或只读连接无影响(默认false)。key- SQLCipher 的加密密钥。nativeBinding- 原生插件(better_sqlite3.node)的相对或绝对路径。prepareDatabase- 在 typeorm 中使用数据库之前要运行的函数。你可以在这里访问原始的 better-sqlite3 Database 对象。readonly- 以只读模式打开数据库连接(默认false)。statementCacheSize- SQLite 语句缓存大小,用于加速查询(默认100)。timeout- 在被锁定的数据库上执行查询时,在抛出 SQLITE_BUSY 错误之前等待的毫秒数(默认5000)。verbose- 一个函数,数据库连接执行的每条 SQL 字符串都会调用它。
sql.js 数据源选项
database:要导入的原始 UInt8Array 数据库。sqlJsConfig:sql.js 的可选初始化配置。autoSave:启用数据库变更的自动持久化,需要提供location或autoSaveCallback。设为true时,如果指定了location,每次变更都会保存到文件系统(Node.js)或localStorage/indexedDB(浏览器);否则会调用autoSaveCallback。autoSaveCallback:当数据库发生变更且autoSave已启用时调用的函数。该函数会收到一个表示数据库的UInt8Array。location:加载和保存数据库的文件位置。useLocalForage:启用 localforage 库,在浏览器环境中从 indexedDB 异步保存和加载数据库,而不是使用同步的 local storage 方法。你需要把 localforage node 模块加入项目,并在页面中引入 localforage.js。
capacitor 数据源选项
database- 数据库名(capacitor-sqlite 会加上后缀SQLite.db)driver- capacitor-sqlite 实例。例如new SQLiteConnection(CapacitorSQLite)。mode- 设置数据库加密模式:"no-encryption" | "encryption" | "secret" | "newsecret"version- 数据库版本journalMode- SQLite 日志模式(可选)
cordova 数据源选项
database- 数据库名location- 数据库保存位置。选项参见 cordova-sqlite-storage。
expo 数据源选项
database- 数据库名。例如 "mydb"。driver- Expo SQLite 模块。可选——TypeORM 会自动加载expo-sqlite。只有在你需要自定义实例(例如打过补丁的 fork)时才显式传入。
nativescript 数据源选项
database- 数据库名
react-native 数据源选项
database- 数据库名location- 数据库保存位置。选项参见 react-native-sqlite-storage。
列类型
int、int2、int8、integer、tinyint、smallint、mediumint、bigint、decimal、numeric、float、double、real、double precision、datetime、varying character、character、native character、varchar、nchar、nvarchar2、unsigned big int、boolean、blob、text、clob、date、json、jsonb
TypeORM 在 SQLite 中同时支持 json 和 jsonb 类型:
json存储为TEXT。jsonb存储为 SQLite 的二进制 JSON 格式。为了提供透明支持并获得更好的性能,TypeORM 会在持久化时自动用jsonb()函数包裹值,在读取时自动用json()函数包裹。
JSONB 支持需要 SQLite 3.45.0 或更高版本。使用 jsonb 列类型时,TypeORM 会在你的数据库结构中使用 jsonb 类型,而 SQLite 在内部把它当作二进制 BLOB 处理。
Installation
- for Better SQLite:
npm install better-sqlite3- for sql.js:
npm install sql.js- for Capacitor, Cordova, Expo, NativeScript and React Native, check the supported platforms.
Data Source Options
See Data Source Options for the common data source options.
better-sqlite3 data source options
database- Database path. For example,"mydb.sqlite".enableWAL- Enables WAL mode (defaultfalse). See SQLite WAL mode.fileMustExist- If the database does not exist, an Error will be thrown instead of creating a new file. Does not affect in-memory or readonly connections (defaultfalse).key- Encryption key for SQLCipher.nativeBinding- Relative or absolute path to the native addon (better_sqlite3.node).prepareDatabase- Function to run before a database is used in typeorm. You can access the original better-sqlite3 Database object here.readonly- Open the database connection in readonly mode (defaultfalse).statementCacheSize- Cache size of the SQLite statement to speed up queries (default100).timeout- The number of milliseconds to wait when executing queries on a locked database, before throwing a SQLITE_BUSY error (default5000).verbose- A function that gets called with every SQL string executed by the database connection.
sql.js data source options
database: The raw UInt8Array database that should be imported.sqlJsConfig: Optional initialize config for sql.js.autoSave: Enable automatic persistence of database changes, requires eitherlocationorautoSaveCallback. When set totrue, every change is saved to the file system (Node.js) or tolocalStorage/indexedDB(browser) iflocationis specified, or theautoSaveCallbackis invoked otherwise.autoSaveCallback: A function that gets called when changes to the database are made andautoSaveis enabled. The function gets aUInt8Arraythat represents the database.location: The file location to load and save the database to.useLocalForage: Enables the usage of the localforage library to save and load the database asynchronously from the indexedDB instead of using the synchrony local storage methods in a browser environment. The localforage node module needs to be added to your project, and the localforage.js should be imported in your page.
capacitor data source options
database- Database name (capacitor-sqlite will add the suffixSQLite.db)driver- The capacitor-sqlite instance. For example,new SQLiteConnection(CapacitorSQLite).mode- Set the mode for database encryption: "no-encryption" | "encryption" | "secret" | "newsecret"version- Database versionjournalMode- The SQLite journal mode (optional)
cordova data source options
database- Database namelocation- Where to save the database. See cordova-sqlite-storage for options.
expo data source options
database- Name of the database. For example, "mydb".driver- The Expo SQLite module. Optional — TypeORM loadsexpo-sqliteautomatically. Pass explicitly only when you need a custom instance (for example, a patched fork).
nativescript data source options
database- Database name
react-native data source options
database- Database namelocation- Where to save the database. See react-native-sqlite-storage for options.
Column Types
int, int2, int8, integer, tinyint, smallint, mediumint, bigint, decimal, numeric, float, double, real, double precision, datetime, varying character, character, native character, varchar, nchar, nvarchar2, unsigned big int, boolean, blob, text, clob, date, json, jsonb
TypeORM supports both json and jsonb types in SQLite:
jsonis stored asTEXT.jsonbis stored as SQLite's binary JSON format. TypeORM automatically wraps values with thejsonb()function during persistence and with thejson()function during retrieval for transparent support and better performance.
JSONB support requires SQLite 3.45.0 or newer. When using the jsonb column type, TypeORM will use the jsonb type in your database schema, which SQLite handles as a binary BLOB internally.