Query & CRUD

Read Data

Reading Data with OptimaDB

OptimaDB provides a type-safe, ergonomic API for reading data from your tables. The OptimaTB class exposes methods like GetOne and Get for querying, and supports advanced features such as filtering, sorting, limiting, and joining related data via Extend.

Fetching a Single Record

Use the GetOne method to fetch a single record by its primary key or any unique field. This method is available on every OptimaTB instance.

typescript

typescript

1

2

Fetching Multiple Records

Use the Get method to fetch multiple records matching your criteria. You can filter, sort, and limit results. The return value is always an array of records.

typescript

typescript

1

2

Filtering, Sorting, and Limiting

Get supports advanced options for filtering, sorting, and limiting results. Filtering supports operators like $eq, $gt, $in, and more. Sorting and limiting are provided via the OrderBy and Limit options.

typescript

typescript

1

2

3

4

5

Joining Related Data with Extend

The Extend option allows you to automatically join related data from other tables. This leverages OptimaDB's relationship tracking and is fully type-safe. The joined data is returned under a $[TableName] property.

typescript

typescript

1

2

3

4

5

6

How It Works Internally

Under the hood, Get and GetOne build SQL queries using your filter objects, and Extend triggers additional queries to fetch related data based on relationships defined in your schema. All type checking and value formatting is handled automatically.

typescript

typescript

1

2

3

4

5

6

7

Summary

  • GetOne: Fetch a single record by unique fields, with optional Extend.
  • Get: Fetch multiple records with advanced filtering, sorting, and limiting.
  • Extend: Join related data automatically and type-safely.
  • All queries are type-checked and safe—no raw SQL required.

With OptimaDB, reading data is concise, robust, and type-safe. The OptimaTB API abstracts away SQL and lets you focus on your application logic.