VibORM
Fields

Blob Field

Blob field type for binary data

Blob Field

The Blob field type stores binary data like files, images, or encrypted content.

Basic Usage

import { s } from "viborm";

s.blob()               // Required Uint8Array
s.blob().nullable()    // Uint8Array | null

Chainable Methods

s.blob()
  .nullable()           // Allow NULL
  .default(new Uint8Array())  // Static default
  .map("column_name")   // Custom column name

Native Database Types

import { TYPES } from "viborm";

s.blob(TYPES.PG.BLOB.BYTEA)  // BYTEA
import { TYPES } from "viborm";

s.blob(TYPES.MYSQL.BLOB.BLOB)        // BLOB (64KB)
s.blob(TYPES.MYSQL.BLOB.TINYBLOB)    // TINYBLOB (256B)
s.blob(TYPES.MYSQL.BLOB.MEDIUMBLOB)  // MEDIUMBLOB (16MB)
s.blob(TYPES.MYSQL.BLOB.LONGBLOB)    // LONGBLOB (4GB)
s.blob(TYPES.MYSQL.BLOB.BINARY(16))  // BINARY(16)
s.blob(TYPES.MYSQL.BLOB.VARBINARY(255))  // VARBINARY(255)
import { TYPES } from "viborm";

s.blob(TYPES.SQLITE.BLOB.BLOB)  // BLOB

Type Mapping

ModifierTypeScriptPostgreSQLMySQLSQLite
s.blob()Uint8ArrayBYTEABLOBBLOB
.nullable()Uint8Array | nullBYTEA NULLBLOB NULLBLOB NULL
.array()Uint8Array[]BYTEA[]JSONJSON

Size Limits by Type

MySQL TypeMax Size
TINYBLOB255 bytes
BLOB65,535 bytes (64KB)
MEDIUMBLOB16,777,215 bytes (16MB)
LONGBLOB4,294,967,295 bytes (4GB)

Examples

// File content
const fileContent = s.blob();

// Avatar image (nullable)
const avatar = s.blob().nullable();

// Encrypted data
const encryptedData = s.blob();

// Fixed-size hash
const passwordHash = s.blob(TYPES.MYSQL.BLOB.BINARY(64));

Working with Blobs

// Create with Uint8Array
const fileBuffer = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]);

await client.file.create({
  data: {
    name: "hello.txt",
    content: fileBuffer,
  }
});

// Read blob data
const file = await client.file.findUnique({
  where: { id: "file_123" },
});

// file.content is Uint8Array
const text = new TextDecoder().decode(file.content);

Converting to/from Other Types

// String to Uint8Array
const encoder = new TextEncoder();
const bytes = encoder.encode("Hello, World!");

// Uint8Array to String
const decoder = new TextDecoder();
const text = decoder.decode(bytes);

// Buffer to Uint8Array (Node.js)
const buffer = Buffer.from("Hello");
const uint8 = new Uint8Array(buffer);

// Uint8Array to Buffer (Node.js)
const backToBuffer = Buffer.from(uint8);

// Base64 to Uint8Array
const base64 = "SGVsbG8=";
const binaryString = atob(base64);
const bytes2 = Uint8Array.from(binaryString, c => c.charCodeAt(0));

When to Use Blob

Use CaseRecommendation
Small files (<1MB)Blob field
Large files (>1MB)Object storage (S3, etc.) + URL field
PasswordsHash + Blob
Encryption keysBlob
ThumbnailsBlob
DocumentsObject storage

On this page