• Start

API Reference

/

Data types

FileRef

The FileRef class represents a reference to a file stored in SurrealDB.

The 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

new FileRef(bucket: string, key: string)

Parameter

Type

Description

bucketstring

The name of the storage bucket.

keystring

The unique key identifying the file within the bucket.

const fileRef = new FileRef('avatars', 'profile-photo.png');

await db.create(new RecordId('users', 'john')).content({
    name: 'John',
    avatar: fileRef
});

The name of the storage bucket containing the file.

Type: string

Example:

console.log(fileRef.bucket); // "avatars"

The unique key identifying the file within its bucket.

Type: string

Example:

console.log(fileRef.key); // "profile-photo.png"

Returns the string representation of the file reference.

Method Syntax
fileRef.toString()

string - The file reference as a string

Serializes the file reference for JSON output.

Method Syntax
fileRef.toJSON()

string - The JSON-safe representation

Compares this file reference with another for equality.

Method Syntax
fileRef.equals(other)

Parameter

Type

Description

otherunknown

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);
}

Was this page helpful?