TypeScript中Uint8Array类型取值及TypeORM Sqljs数据库配置疑问
1. What values can a Uint8Array variable take in TypeScript?
A Uint8Array in TypeScript is a typed array designed to hold 8-bit unsigned integers—each element must be a number between 0 and 255, inclusive. Valid values for a variable typed as Uint8Array include:
- An empty initialized instance:
new Uint8Array() - An instance populated with valid 0-255 integers:
new Uint8Array([12, 45, 255]) - An instance created from another array or buffer source:
Uint8Array.from([7, 19])ornew Uint8Array(existingArrayBuffer) - If the type definition allows optional values (e.g.,
Uint8Array | null),nullorundefinedare also acceptable
2. What values are allowed for the Uint8Array-typed property in TypeORM's SqljsConnectionOptions?
For the SqljsConnectionOptions in TypeORM, the database field uses Uint8Array because Sql.js (the underlying SQLite driver) operates directly on the raw binary data of SQLite database files, not database names or file paths. Here’s what you can pass:
- A
Uint8Arraycontaining the full binary content of an existing SQLite.dbfile. For example:- In browsers: Read a user-uploaded database file using
FileReaderand convert the result to aUint8Array. - In Node.js: Read the file with
fs.readFileand cast the returned buffer to aUint8Array.
- In browsers: Read a user-uploaded database file using
- An empty
Uint8Array(new Uint8Array()) to create a fresh, in-memory SQLite database. - A serialized
Uint8Arrayexported from a previously used Sql.js database (viadb.export()), which lets you restore a saved database state.
Quick clarification on your string confusion
Unlike traditional SQL drivers that accept a database name or file path, Sql.js runs entirely in memory and requires the actual binary structure of the database. A string (like a database name) won’t work here—you need to provide the raw binary data packaged as a Uint8Array.
内容的提问来源于stack exchange,提问作者Michael W. Czechowski

