SAP HANA
安装
TypeORM 依赖 @sap/hana-client 来建立数据库连接:
npm install @sap/hana-client如果你使用的是 TypeORM 0.3.25 或更早版本,还需要 hdb-pool 来管理连接池。
数据源选项
通用数据源选项请参阅数据源选项。
host- SAP HANA 服务器的主机名。例如"localhost"。port- SAP HANA 服务器的端口号。例如30015。username- 连接 SAP HANA 服务器的用户名。例如"SYSTEM"。password- 连接 SAP HANA 服务器的密码。例如"password"。database- 要连接的数据库名。例如"HXE"。encrypt- 是否加密连接。例如true。sslValidateCertificate- 是否校验 SSL 证书。例如true。key、cert和ca- 加密连接所用的私钥、公钥证书和证书颁发机构。driver- 可选的显式@sap/hana-client模块实例。如果省略,TypeORM 会自动加载@sap/hana-client。pool— 连接池配置对象:maxConnectedOrPooled(number)— 池中活跃或空闲连接的最大数量(默认:10)。maxPooledIdleTime(秒)— 空闲连接被关闭前的时间(默认:30)。maxWaitTimeoutIfPoolExhausted(毫秒)- 等待连接变为可用的时间(默认:0,不等待)。需要@sap/hana-client版本2.27或更高。pingCheck(boolean)— 使用前是否校验连接(默认:false)。poolCapacity(number)— 保持可用的最大连接数(默认:不限制)。
更多细节以及 extra 属性,请参阅 SAP HANA Client 的官方文档:Node.js Connection Properties。
列类型
SAP HANA 2.0 和 SAP HANA Cloud 支持的数据类型略有不同。更多信息请查看 SAP Help 页面:
TypeORM 的 SapDriver 支持 tinyint、smallint、integer、bigint、smalldecimal、decimal、real、double、date、time、seconddate、timestamp、boolean、char、nchar、varchar、nvarchar、text、alphanum、shorttext、array、varbinary、blob、clob、nclob、st_geometry、st_point、real_vector 和 half_vector。其中部分数据类型在 SAP HANA Cloud 中已被弃用或移除,连接到 Cloud 数据库时它们会被转换为最接近的可用替代类型。
向量类型
real_vector 和 half_vector 数据类型分别在 SAP HANA Cloud(2024Q1 和 2025Q2)中引入,同样需要受支持版本的 @sap/hana-client。
为了与 PostgreSQL 的向量支持保持一致,TypeORM 还提供了别名:
vector(real_vector的别名)- 以 4 字节浮点数存储向量halfvec(half_vector的别名)- 以 2 字节浮点数存储向量,更节省内存
@Entity()
export class Document {
@PrimaryGeneratedColumn()
id: number
// 使用 SAP HANA 原生类型名
@Column("real_vector", { length: 1536 })
embedding: Buffer | number[]
@Column("half_vector", { length: 768 })
reduced_embedding: Buffer | number[]
// 使用跨数据库别名(推荐)
@Column("vector", { length: 1536 })
universal_embedding: Buffer | number[]
@Column("halfvec", { length: 768 })
universal_reduced_embedding: Buffer | number[]
}默认情况下,客户端会以 fvecs/hvecs 格式返回 Buffer,这更高效。通过在连接选项中加入 { extra: { vectorOutputType: "Array" } },可以让驱动把值转换成 number[]。关于 REAL_VECTOR 或 HALF_VECTOR 的更多信息,请查看 SAP HANA Client 文档。
进行相似性搜索时,请使用相应的向量函数。
Installation
TypeORM relies on @sap/hana-client for establishing the database connection:
npm install @sap/hana-clientIf you are using TypeORM 0.3.25 or earlier, hdb-pool is also required for managing the pool.
Data Source Options
See Data Source Options for the common data source options.
host- The hostname of the SAP HANA server. For example,"localhost".port- The port number of the SAP HANA server. For example,30015.username- The username to connect to the SAP HANA server. For example,"SYSTEM".password- The password to connect to the SAP HANA server. For example,"password".database- The name of the database to connect to. For example,"HXE".encrypt- Whether to encrypt the connection. For example,true.sslValidateCertificate- Whether to validate the SSL certificate. For example,true.key,certandca- Private key, public certificate and certificate authority for the encrypted connection.driver- Optional explicit@sap/hana-clientmodule instance. If omitted, TypeORM loads@sap/hana-clientautomatically.pool— Connection pool configuration object:maxConnectedOrPooled(number) — Max active or idle connections in the pool (default: 10).maxPooledIdleTime(seconds) — Time before an idle connection is closed (default: 30).maxWaitTimeoutIfPoolExhausted(milliseconds) - Time to wait for a connection to become available (default: 0, no wait). Requires@sap/hana-clientversion2.27or later.pingCheck(boolean) — Whether to validate connections before use (default: false).poolCapacity(number) — Maximum number of connections to be kept available (default: no limit).
See the official documentation of SAP HANA Client for more details as well as the extra properties: Node.js Connection Properties.
Column Types
SAP HANA 2.0 and SAP HANA Cloud support slightly different data types. Check the SAP Help pages for more information:
TypeORM's SapDriver supports tinyint, smallint, integer, bigint, smalldecimal, decimal, real, double, date, time, seconddate, timestamp, boolean, char, nchar, varchar, nvarchar, text, alphanum, shorttext, array, varbinary, blob, clob, nclob, st_geometry, st_point, real_vector and half_vector. Some of these data types have been deprecated or removed in SAP HANA Cloud, and will be converted to the closest available alternative when connected to a Cloud database.
Vector Types
The real_vector and half_vector data types were introduced in SAP HANA Cloud (2024Q1 and 2025Q2 respectively), and require a supported version of @sap/hana-client as well.
For consistency with PostgreSQL's vector support, TypeORM also provides aliases:
vector(alias forreal_vector) - stores vectors as 4-byte floatshalfvec(alias forhalf_vector) - stores vectors as 2-byte floats for memory efficiency
@Entity()
export class Document {
@PrimaryGeneratedColumn()
id: number
// Using SAP HANA native type names
@Column("real_vector", { length: 1536 })
embedding: Buffer | number[]
@Column("half_vector", { length: 768 })
reduced_embedding: Buffer | number[]
// Using cross-database aliases (recommended)
@Column("vector", { length: 1536 })
universal_embedding: Buffer | number[]
@Column("halfvec", { length: 768 })
universal_reduced_embedding: Buffer | number[]
}By default, the client will return a Buffer in the fvecs/hvecs format, which is more efficient. It is possible to let the driver convert the values to a number[] by adding { extra: { vectorOutputType: "Array" } } to the connection options. Check the SAP HANA Client documentation for more information about REAL_VECTOR or HALF_VECTOR.
Use the appropriate vector functions for similarity searches.