Implementation:TobikoData Sqlmesh Model File
| Knowledge Sources | |
|---|---|
| Domains | Web_UI, Data_Models, File_System |
| Last Updated | 2026-02-07 20:00 GMT |
Overview
File model class for the SQLMesh web UI representing individual files with content management and change tracking.
Description
ModelFile extends ModelArtifact to represent files in the file explorer with content editing capabilities. It tracks both original content (_content) and current content to detect unsaved changes, supports multiple file extensions (SQL, Python, CSV, YAML), and provides formatting state tracking. The class implements intelligent content updates that preserve unsaved local changes when synchronizing with remote file states.
Key features include change detection through fingerprinting, extension-based file type detection, basename extraction without extension, and special handling for restoring content from localStorage while preserving modifications.
Usage
Use this class to implement file editing functionality in the web UI, enabling users to edit SQL models, Python scripts, and other project files with proper change detection and state management.
Code Reference
Source Location
- Repository: TobikoData_Sqlmesh
- File: web/client/src/models/file.ts
Signature
export class ModelFile extends ModelArtifact<InitialFile> {
private _content: string = ''
content: string
extension: FileExtensions
isFormatted?: boolean
constructor(initial?: File | ModelFile, parent?: ModelDirectory)
get basename(): string
get isEmpty(): boolean
get isChanged(): boolean
get isSQL(): boolean
get fingerprint(): string
removeChanges(): void
copyName(): string
update(newFile?: File): void
static isModelFile(file: any): file is ModelFile
}
export const EnumFileExtensions = {
SQL: '.sql',
PY: '.py',
CSV: '.csv',
YAML: '.yaml',
YML: '.yml',
None: '',
} as const
Import
import { ModelFile, EnumFileExtensions } from '@models/file'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| initial | ModelFile | No | Initial file data with content and extension |
| parent | ModelDirectory | No | Parent directory reference |
Outputs
| Property | Type | Description |
|---|---|---|
| content | string | Current file content |
| extension | FileExtensions | File extension (.sql, .py, .csv, .yaml, .yml, or empty) |
| isFormatted | undefined | Format status (undefined means unknown) |
| basename | string | Filename without extension |
| isEmpty | boolean | True if content is empty |
| isChanged | boolean | True if content differs from original |
| isSQL | boolean | True if extension is .sql |
| fingerprint | string | Unique hash combining content, name, and path |
Usage Examples
// Create a new SQL model file
const modelFile = new ModelFile({
name: 'my_model.sql',
path: 'models/my_model.sql',
content: 'SELECT * FROM customers',
extension: EnumFileExtensions.SQL
}, parentDirectory)
// Check file type
if (modelFile.isSQL) {
console.log('This is a SQL model')
}
// Edit content
modelFile.content = 'SELECT id, name FROM customers'
// Check for unsaved changes
if (modelFile.isChanged) {
console.log('File has unsaved changes')
}
// Revert changes
modelFile.removeChanges()
// Get basename without extension
console.log(modelFile.basename) // "my_model"
// Update from remote file (preserves local changes if present)
modelFile.update({
name: 'my_model.sql',
path: 'models/my_model.sql',
content: 'SELECT * FROM new_source',
extension: '.sql'
})
// Generate copy name
const copyName = modelFile.copyName()
// Result: "Copy of my_model__unique123.sql"
// Type guard
if (ModelFile.isModelFile(someObject)) {
console.log(someObject.content)
}