Change 'integerLength' in schema to 'dataLength'

This commit is contained in:
2026-08-05 09:47:22 +01:00
parent e51f524443
commit c40d8596c7
6 changed files with 147 additions and 106 deletions
+110 -71
View File
@@ -85,13 +85,13 @@ bun add github:moduletrace/bun-mariadb
Connection settings are read from the environment (not the config file): Connection settings are read from the environment (not the config file):
| Variable | Required | Description | | Variable | Required | Description |
| --------------------------------- | -------- | ------------------------------------ | | --------------------------------- | -------- | ---------------------------------- |
| `BUN_MARIADB_SERVER_HOST` | Yes | MariaDB host | | `BUN_MARIADB_SERVER_HOST` | Yes | MariaDB host |
| `BUN_MARIADB_SERVER_USERNAME` | Yes | Database user | | `BUN_MARIADB_SERVER_USERNAME` | Yes | Database user |
| `BUN_MARIADB_SERVER_PASSWORD` | Yes | Database password | | `BUN_MARIADB_SERVER_PASSWORD` | Yes | Database password |
| `BUN_MARIADB_SERVER_PORT` | No | Port (server default if omitted) | | `BUN_MARIADB_SERVER_PORT` | No | Port (server default if omitted) |
| `BUN_MARIADB_SERVER_SSL_KEY_PATH` | No | Optional SSL key path (legacy/env) | | `BUN_MARIADB_SERVER_SSL_KEY_PATH` | No | Optional SSL key path (legacy/env) |
Example `.env`: Example `.env`:
@@ -135,9 +135,22 @@ const schema: BUN_MARIADB_DatabaseSchemaType = {
{ {
tableName: "users", tableName: "users",
fields: [ fields: [
{ fieldName: "first_name", dataType: "VARCHAR", integerLength: 255 }, {
{ fieldName: "last_name", dataType: "VARCHAR", integerLength: 255 }, fieldName: "first_name",
{ fieldName: "email", dataType: "VARCHAR", integerLength: 255, unique: true }, dataType: "VARCHAR",
dataLength: 255,
},
{
fieldName: "last_name",
dataType: "VARCHAR",
dataLength: 255,
},
{
fieldName: "email",
dataType: "VARCHAR",
dataLength: 255,
unique: true,
},
{ fieldName: "bio", dataType: "LONGTEXT", html: true }, { fieldName: "bio", dataType: "LONGTEXT", html: true },
], ],
}, },
@@ -187,19 +200,19 @@ await BunMariaDB.delete({ table: "users", targetId: 1 });
The config file must be named `bun-mariadb.config.ts` and placed at the project root. The config file must be named `bun-mariadb.config.ts` and placed at the project root.
| Field | Type | Required | Description | | Field | Type | Required | Description |
| ------------------- | -------- | -------- | --------------------------------------------------------------------------- | | -------------------- | -------- | -------- | ----------------------------------------------------------------------------- |
| `db_name` | `string` | Yes | MariaDB database name | | `db_name` | `string` | Yes | MariaDB database name |
| `db_dir` | `string` | Yes | Directory for schema, types, and local artifacts (relative to project root) | | `db_dir` | `string` | Yes | Directory for schema, types, and local artifacts (relative to project root) |
| `db_backup_dir` | `string` | No | Backup directory name, relative to `db_dir` (default: `.backups`) | | `db_backup_dir` | `string` | No | Backup directory name, relative to `db_dir` (default: `.backups`) |
| `max_backups` | `number` | No | Max backup files to keep (default: `10`) | | `max_backups` | `number` | No | Max backup files to keep (default: `10`) |
| `max_exports` | `number` | No | Max export archives to keep (default: `10`) | | `max_exports` | `number` | No | Max export archives to keep (default: `10`) |
| `typedef_file_path` | `string` | No | Output path for generated TypeScript types (relative to project root) | | `typedef_file_path` | `string` | No | Output path for generated TypeScript types (relative to project root) |
| `db_config` | `object` | No | Extra options passed to Bun's `SQL` MariaDB adapter | | `db_config` | `object` | No | Extra options passed to Bun's `SQL` MariaDB adapter |
| `charset` | `string` | No | Database charset (default: `utf8mb4`) | | `charset` | `string` | No | Database charset (default: `utf8mb4`) |
| `connection_timeout`| `number` | No | Connection timeout in ms (default: `10000`) | | `connection_timeout` | `number` | No | Connection timeout in ms (default: `10000`) |
| `ssl_ca` | `string` | No | Path to SSL CA certificate (relative to project root) | | `ssl_ca` | `string` | No | Path to SSL CA certificate (relative to project root) |
| `html_sanitize` | `object` | No | Extra HTML sanitizer allowlists (see [HTML Sanitization](#html-sanitization)) | | `html_sanitize` | `object` | No | Extra HTML sanitizer allowlists (see [HTML Sanitization](#html-sanitization)) |
Schema file path is always: `{db_dir}/schema.ts`. Schema file path is always: `{db_dir}/schema.ts`.
@@ -226,9 +239,9 @@ interface BUN_MARIADB_TableSchemaType {
indexes?: BUN_MARIADB_IndexSchemaType[]; indexes?: BUN_MARIADB_IndexSchemaType[];
uniqueConstraints?: BUN_MARIADB_UniqueConstraintSchemaType[]; uniqueConstraints?: BUN_MARIADB_UniqueConstraintSchemaType[];
parentTableName?: string; // inherit / merge fields from another table parentTableName?: string; // inherit / merge fields from another table
tableNameOld?: string; // rename: old name triggers ALTER TABLE RENAME tableNameOld?: string; // rename: old name triggers ALTER TABLE RENAME
collation?: "utf8mb4_bin" | "utf8mb4_unicode_520_ci"; collation?: "utf8mb4_bin" | "utf8mb4_unicode_520_ci";
isVector?: boolean; // mark as vector-oriented table isVector?: boolean; // mark as vector-oriented table
} }
``` ```
@@ -238,12 +251,38 @@ interface BUN_MARIADB_TableSchemaType {
type BUN_MARIADB_FieldSchemaType = { type BUN_MARIADB_FieldSchemaType = {
fieldName?: string; fieldName?: string;
dataType: dataType:
| "CHAR" | "VARCHAR" | "TEXT" | "TINYTEXT" | "MEDIUMTEXT" | "LONGTEXT" | "CHAR"
| "TINYINT" | "SMALLINT" | "MEDIUMINT" | "INT" | "BIGINT" | "VARCHAR"
| "FLOAT" | "DOUBLE" | "DECIMAL" | "TEXT"
| "BINARY" | "VARBINARY" | "BLOB" | "TINYBLOB" | "MEDIUMBLOB" | "LONGBLOB" | "TINYTEXT"
| "DATE" | "TIME" | "DATETIME" | "TIMESTAMP" | "YEAR" | "MEDIUMTEXT"
| "BOOLEAN" | "UUID" | "JSON" | "INET6" | "ENUM" | "SET" | "VECTOR"; | "LONGTEXT"
| "TINYINT"
| "SMALLINT"
| "MEDIUMINT"
| "INT"
| "BIGINT"
| "FLOAT"
| "DOUBLE"
| "DECIMAL"
| "BINARY"
| "VARBINARY"
| "BLOB"
| "TINYBLOB"
| "MEDIUMBLOB"
| "LONGBLOB"
| "DATE"
| "TIME"
| "DATETIME"
| "TIMESTAMP"
| "YEAR"
| "BOOLEAN"
| "UUID"
| "JSON"
| "INET6"
| "ENUM"
| "SET"
| "VECTOR";
primaryKey?: boolean; primaryKey?: boolean;
autoIncrement?: boolean; autoIncrement?: boolean;
notNullValue?: boolean; notNullValue?: boolean;
@@ -253,11 +292,11 @@ type BUN_MARIADB_FieldSchemaType = {
onUpdate?: string; onUpdate?: string;
onUpdateLiteral?: string; onUpdateLiteral?: string;
foreignKey?: BUN_MARIADB_ForeignKeyType; foreignKey?: BUN_MARIADB_ForeignKeyType;
integerLength?: string | number; // e.g. VARCHAR length dataLength?: string | number; // e.g. VARCHAR length
decimals?: string | number; // DECIMAL scale decimals?: string | number; // DECIMAL scale
options?: (string | number)[]; // ENUM / SET values options?: (string | number)[]; // ENUM / SET values
isVector?: boolean; // native VECTOR column isVector?: boolean; // native VECTOR column
vectorSize?: number; // dimensions (default: 1536) vectorSize?: number; // dimensions (default: 1536)
// Content mode flags (editor metadata); only `html: true` affects runtime: // Content mode flags (editor metadata); only `html: true` affects runtime:
html?: boolean; html?: boolean;
markdown?: boolean; markdown?: boolean;
@@ -368,10 +407,10 @@ Requires `mariadb` or `mysql` on `PATH`.
bunx bun-mariadb export [options] bunx bun-mariadb export [options]
``` ```
| Option | Description | | Option | Description |
| ------------------- | --------------------------------------------------------------------------- | | ---------------- | -------------------------------------------------------------------------- |
| `-o`, `--output` | Output archive path (`.tar.gz` or `.zip`). Defaults to `{db_dir}/.exports` | | `-o`, `--output` | Output archive path (`.tar.gz` or `.zip`). Defaults to `{db_dir}/.exports` |
| `-f`, `--format` | When `--output` is omitted: `tar.gz` (default) or `zip` | | `-f`, `--format` | When `--output` is omitted: `tar.gz` (default) or `zip` |
Creates a portable archive containing: Creates a portable archive containing:
@@ -400,11 +439,11 @@ Requires `mariadb-dump` or `mysqldump` on `PATH`. Zip output also requires `zip`
bunx bun-mariadb import [file] [options] bunx bun-mariadb import [file] [options]
``` ```
| Option | Description | | Option | Description |
| ---------------- | --------------------------------------------------------------------------- | | --------------- | --------------------------------------------------------------------- |
| `[file]` | Path to a `.sql` dump or export archive (`.tar.gz` / `.tar` / `.zip`) | | `[file]` | Path to a `.sql` dump or export archive (`.tar.gz` / `.tar` / `.zip`) |
| `--sql-only` | Archive only: restore SQL, do not overwrite `schema.ts` | | `--sql-only` | Archive only: restore SQL, do not overwrite `schema.ts` |
| `--schema-only` | Archive only: write `schema.ts`, do not restore SQL | | `--schema-only` | Archive only: write `schema.ts`, do not restore SQL |
Behavior: Behavior:
@@ -641,26 +680,26 @@ type ServerQueryParam<T> = {
### Equality Operators ### Equality Operators
| Equality | SQL Equivalent | | Equality | SQL Equivalent |
| ----------------------- | ------------------------------------------------------ | | ----------------------- | ----------------------------- |
| `EQUAL` (default) | `=` | | `EQUAL` (default) | `=` |
| `NOT EQUAL` | `!=` | | `NOT EQUAL` | `!=` |
| `LIKE` | `LIKE '%value%'` | | `LIKE` | `LIKE '%value%'` |
| `LIKE_RAW` | `LIKE 'value'` | | `LIKE_RAW` | `LIKE 'value'` |
| `LIKE_LOWER` | `LOWER(field) LIKE '%value%'` | | `LIKE_LOWER` | `LOWER(field) LIKE '%value%'` |
| `NOT LIKE` | `NOT LIKE '%value%'` | | `NOT LIKE` | `NOT LIKE '%value%'` |
| `GREATER THAN` | `>` | | `GREATER THAN` | `>` |
| `GREATER THAN OR EQUAL` | `>=` | | `GREATER THAN OR EQUAL` | `>=` |
| `LESS THAN` | `<` | | `LESS THAN` | `<` |
| `LESS THAN OR EQUAL` | `<=` | | `LESS THAN OR EQUAL` | `<=` |
| `IN` | `IN (...)` | | `IN` | `IN (...)` |
| `NOT IN` | `NOT IN (...)` | | `NOT IN` | `NOT IN (...)` |
| `BETWEEN` | `BETWEEN a AND b` | | `BETWEEN` | `BETWEEN a AND b` |
| `IS NULL` | `IS NULL` | | `IS NULL` | `IS NULL` |
| `IS NOT NULL` | `IS NOT NULL` | | `IS NOT NULL` | `IS NOT NULL` |
| `REGEXP` | `REGEXP` | | `REGEXP` | `REGEXP` |
| `FULLTEXT` | full-text match | | `FULLTEXT` | full-text match |
| `MATCH` | vector / specialized match | | `MATCH` | vector / specialized match |
```ts ```ts
const res = await BunMariaDB.select({ const res = await BunMariaDB.select({
@@ -715,7 +754,7 @@ MariaDB native `VECTOR(n)` columns and `VECTOR INDEX` are supported (MariaDB 11.
{ {
fieldName: "title", fieldName: "title",
dataType: "VARCHAR", dataType: "VARCHAR",
integerLength: 255, dataLength: 255,
}, },
], ],
indexes: [ indexes: [
@@ -825,11 +864,11 @@ const res = await BunMariaDB.select<BUN_MARIADB_MY_APP_USERS>({
Every table automatically receives: Every table automatically receives:
| Field | Type | Description | | Field | Type | Description |
| ------------ | ------------------------------- | -------------------------------------- | | ------------ | -------------------------------------------- | -------------------------------------- |
| `id` | `BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL` | Unique row identifier | | `id` | `BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL` | Unique row identifier |
| `created_at` | `BIGINT` | Unix timestamp set on insert | | `created_at` | `BIGINT` | Unix timestamp set on insert |
| `updated_at` | `BIGINT` | Unix timestamp updated on every update | | `updated_at` | `BIGINT` | Unix timestamp updated on every update |
You do not need to declare these in your schema. You do not need to declare these in your schema.
+14 -16
View File
@@ -7,9 +7,9 @@ export default function mapDataType(field) {
} }
switch (dataType) { switch (dataType) {
case "CHAR": case "CHAR":
return `CHAR(${field.integerLength || 255})`; return `CHAR(${field.dataLength || 255})`;
case "VARCHAR": case "VARCHAR":
return `VARCHAR(${field.integerLength || 255})`; return `VARCHAR(${field.dataLength || 255})`;
case "TEXT": case "TEXT":
return "TEXT"; return "TEXT";
case "TINYTEXT": case "TINYTEXT":
@@ -19,36 +19,34 @@ export default function mapDataType(field) {
case "LONGTEXT": case "LONGTEXT":
return "LONGTEXT"; return "LONGTEXT";
case "TINYINT": case "TINYINT":
return field.integerLength return field.dataLength
? `TINYINT(${field.integerLength})` ? `TINYINT(${field.dataLength})`
: "TINYINT"; : "TINYINT";
case "SMALLINT": case "SMALLINT":
return field.integerLength return field.dataLength
? `SMALLINT(${field.integerLength})` ? `SMALLINT(${field.dataLength})`
: "SMALLINT"; : "SMALLINT";
case "MEDIUMINT": case "MEDIUMINT":
return field.integerLength return field.dataLength
? `MEDIUMINT(${field.integerLength})` ? `MEDIUMINT(${field.dataLength})`
: "MEDIUMINT"; : "MEDIUMINT";
case "INT": case "INT":
return field.integerLength ? `INT(${field.integerLength})` : "INT"; return field.dataLength ? `INT(${field.dataLength})` : "INT";
case "BIGINT": case "BIGINT":
return field.integerLength return field.dataLength ? `BIGINT(${field.dataLength})` : "BIGINT";
? `BIGINT(${field.integerLength})`
: "BIGINT";
case "FLOAT": case "FLOAT":
return "FLOAT"; return "FLOAT";
case "DOUBLE": case "DOUBLE":
return "DOUBLE"; return "DOUBLE";
case "DECIMAL": case "DECIMAL":
if (field.integerLength && field.decimals) { if (field.dataLength && field.decimals) {
return `DECIMAL(${field.integerLength}, ${field.decimals})`; return `DECIMAL(${field.dataLength}, ${field.decimals})`;
} }
return "DECIMAL(10,2)"; return "DECIMAL(10,2)";
case "BINARY": case "BINARY":
return `BINARY(${field.integerLength || 1})`; return `BINARY(${field.dataLength || 1})`;
case "VARBINARY": case "VARBINARY":
return `VARBINARY(${field.integerLength || 255})`; return `VARBINARY(${field.dataLength || 255})`;
case "BLOB": case "BLOB":
return "BLOB"; return "BLOB";
case "TINYBLOB": case "TINYBLOB":
+4 -1
View File
@@ -213,7 +213,10 @@ export type BUN_MARIADB_FieldSchemaType = {
onDelete?: string; onDelete?: string;
onDeleteLiteral?: string; onDeleteLiteral?: string;
cssFiles?: string[]; cssFiles?: string[];
integerLength?: string | number; /**
* Datatype length. Eg 255 for VARCHAR
*/
dataLength?: string | number;
decimals?: string | number; decimals?: string | number;
code?: boolean; code?: boolean;
options?: (string | number)[]; options?: (string | number)[];
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@moduletrace/bun-mariadb", "name": "@moduletrace/bun-mariadb",
"version": "1.0.8", "version": "1.0.9",
"description": "Schema-driven MariaDB manager for Bun", "description": "Schema-driven MariaDB manager for Bun",
"author": "Benjamin Toby", "author": "Benjamin Toby",
"license": "MIT", "license": "MIT",
+14 -16
View File
@@ -13,9 +13,9 @@ export default function mapDataType(
switch (dataType) { switch (dataType) {
case "CHAR": case "CHAR":
return `CHAR(${field.integerLength || 255})`; return `CHAR(${field.dataLength || 255})`;
case "VARCHAR": case "VARCHAR":
return `VARCHAR(${field.integerLength || 255})`; return `VARCHAR(${field.dataLength || 255})`;
case "TEXT": case "TEXT":
return "TEXT"; return "TEXT";
case "TINYTEXT": case "TINYTEXT":
@@ -25,36 +25,34 @@ export default function mapDataType(
case "LONGTEXT": case "LONGTEXT":
return "LONGTEXT"; return "LONGTEXT";
case "TINYINT": case "TINYINT":
return field.integerLength return field.dataLength
? `TINYINT(${field.integerLength})` ? `TINYINT(${field.dataLength})`
: "TINYINT"; : "TINYINT";
case "SMALLINT": case "SMALLINT":
return field.integerLength return field.dataLength
? `SMALLINT(${field.integerLength})` ? `SMALLINT(${field.dataLength})`
: "SMALLINT"; : "SMALLINT";
case "MEDIUMINT": case "MEDIUMINT":
return field.integerLength return field.dataLength
? `MEDIUMINT(${field.integerLength})` ? `MEDIUMINT(${field.dataLength})`
: "MEDIUMINT"; : "MEDIUMINT";
case "INT": case "INT":
return field.integerLength ? `INT(${field.integerLength})` : "INT"; return field.dataLength ? `INT(${field.dataLength})` : "INT";
case "BIGINT": case "BIGINT":
return field.integerLength return field.dataLength ? `BIGINT(${field.dataLength})` : "BIGINT";
? `BIGINT(${field.integerLength})`
: "BIGINT";
case "FLOAT": case "FLOAT":
return "FLOAT"; return "FLOAT";
case "DOUBLE": case "DOUBLE":
return "DOUBLE"; return "DOUBLE";
case "DECIMAL": case "DECIMAL":
if (field.integerLength && field.decimals) { if (field.dataLength && field.decimals) {
return `DECIMAL(${field.integerLength}, ${field.decimals})`; return `DECIMAL(${field.dataLength}, ${field.decimals})`;
} }
return "DECIMAL(10,2)"; return "DECIMAL(10,2)";
case "BINARY": case "BINARY":
return `BINARY(${field.integerLength || 1})`; return `BINARY(${field.dataLength || 1})`;
case "VARBINARY": case "VARBINARY":
return `VARBINARY(${field.integerLength || 255})`; return `VARBINARY(${field.dataLength || 255})`;
case "BLOB": case "BLOB":
return "BLOB"; return "BLOB";
case "TINYBLOB": case "TINYBLOB":
+4 -1
View File
@@ -202,7 +202,10 @@ export type BUN_MARIADB_FieldSchemaType = {
onDelete?: string; onDelete?: string;
onDeleteLiteral?: string; onDeleteLiteral?: string;
cssFiles?: string[]; cssFiles?: string[];
integerLength?: string | number; /**
* Datatype length. Eg 255 for VARCHAR
*/
dataLength?: string | number;
decimals?: string | number; decimals?: string | number;
code?: boolean; code?: boolean;
options?: (string | number)[]; options?: (string | number)[];