Collections
Mission
Collections in Thinking Differently are dynamic, virtual namespaces. Learn how to allocate database boundaries on the fly without tables or schemas.
The Payoff
By completing this module, you will understand how to construct reference queries, isolate builders, and design clean collection naming patterns.
Estimated Time: 3 minutesModules: 3 Concepts
Virtual Creation
If you reference a collection key that does not exist yet, the database will allocate and create the collection dynamically the moment your first document is inserted.
Obtain a namespace hook
Invoke .collection() on your initialized SDK client. This is a local, synchronous operation.
Understand Virtual Creation
No table generation or schema setup is required. The namespace is dynamically created on first insertion.
Interactive Code Snippet
collections_demo.ts
import { sdk } from "./lib/td";// Create a collection reference (synchronous, local operation)const usersRef = sdk.collection("users");const logsRef = sdk.collection("system_logs");Important Design Aspect
`QueryBuilder` instances are **mutable**. Methods like `.where()`, `.limit()`, and `.sort()` modify the internal filter state of the builder and return the builder instance (`this`) to support fluent chaining.
Multiple Collection References
Calling `.collection("name")` returns a **new, independent** `QueryBuilder` instance on every call. The main SDK client itself does not store any query or collection-specific state. This means you can declare and use multiple collection references (e.g. `usersRef` and `logsRef`) simultaneously without them overwriting each other.
Observe filter mutation side-effects
Chaining filter modifiers like .where() directly changes the internal states of the builder.
Establish isolated queries
Ensure separate queries use distinct collection calls so filter parameters do not bleed together.
Interactive Code Snippet
typescript
const query = sdk.collection("products");// Calling .where() modifies the 'query' instance directly!query.where("category", "=", "electronics");// 'query' now has the electronics filter built-inconst items = await query.get();- Reusability: Store collection references in local variables or export them from a central controller config for easy access.
- Naming Conventions: We recommend using lowercase, alphanumeric strings for collection namespaces (e.g. `users`, `invoices`, `app_events`).