FileRefThe FileRef class represents a reference to a file stored in SurrealDB. File references are returned when querying records that contain file fields and provide access to file metadata such as the bucket, key, and media type.
Import:
import { FileRef } from 'surrealdb';
Source: value/file-ref.ts
bucketThe name of the storage bucket containing the file.
Type: string
Example:
console.log(fileRef.bucket); // "avatars"
keyThe unique key identifying the file within its bucket.
Type: string
Example:
console.log(fileRef.key); // "profile-photo.png"
.toString()Returns the string representation of the file reference.
Method SyntaxfileRef.toString()
string - The file reference as a string
.toJSON()Serializes the file reference for JSON output.
Method SyntaxfileRef.toJSON()
string - The JSON-safe representation
.equals(other)Compares this file reference with another for equality.
Method SyntaxfileRef.equals(other)
| Parameter | Type | Description |
|---|---|---|
other required | unknown | The value to compare against. |
boolean - True if both file references point to the same file
const ref1 = record1.avatar; const ref2 = record2.avatar; if (ref1.equals(ref2)) { console.log('Same file'); }
const user = await db.select(new RecordId('users', 'john')); if (user.avatar instanceof FileRef) { console.log('Bucket:', user.avatar.bucket); console.log('Key:', user.avatar.key); }
const [records] = await db.query<[{ avatar: FileRef }[]]>( 'SELECT avatar FROM users WHERE avatar IS NOT NONE' ); for (const record of records) { console.log(record.avatar.key); }