Skip to content
On this page

stratadb / CollectionBuilder

Type Alias: CollectionBuilder<T>

ts
type CollectionBuilder<T> = object;

Defined in: src/collection-builder.ts:33

Fluent builder for creating collections with inline schema definition.

Remarks

CollectionBuilder provides a fluent API for defining collection schemas inline while creating the collection. This is an alternative to using createSchema() separately.

Example

typescript
// Fluent collection creation
const users = db.collection<User>('users')
  .field('name', { type: 'TEXT', indexed: true })
  .field('email', { type: 'TEXT', indexed: true, unique: true })
  .timestamps(true)
  .build();

Type Parameters

T

T extends Document

The document type extending Document

Methods

build()

ts
build(): Collection<T>;

Defined in: src/collection-builder.ts:94

Build and return the collection with the defined schema.

Returns

Collection<T>

The collection instance ready for operations


cache()

ts
cache(enabled): CollectionBuilder<T>;

Defined in: src/collection-builder.ts:87

Enable or disable query caching for this collection.

Parameters

enabled

boolean

Whether to enable query caching

Returns

CollectionBuilder<T>

The builder for method chaining

Remarks

Query caching stores SQL templates for repeated queries, improving performance at the cost of memory usage (up to 500 cached query templates).

Example

typescript
// Enable caching for this collection
const users = db.collection<User>('users')
  .field('name', { type: 'TEXT', indexed: true })
  .cache(true)
  .build();

compoundIndex()

ts
compoundIndex(
   name, 
   fields, 
options?): CollectionBuilder<T>;

Defined in: src/collection-builder.ts:52

Define a compound index spanning multiple fields.

Parameters

name

string

fields

readonly keyof T[]

options?
unique?

boolean

Returns

CollectionBuilder<T>


field()

ts
field<K>(name, options): CollectionBuilder<T>;

Defined in: src/collection-builder.ts:37

Define an indexed field with type checking.

Type Parameters

K

K extends string | number | symbol

Parameters

name

K

options
default?

T[K]

indexed?

boolean

nullable?

boolean

path?

JsonPath

type

TypeScriptToSQLite<T[K]>

unique?

boolean

Returns

CollectionBuilder<T>


timestamps()

ts
timestamps(enabled?): CollectionBuilder<T>;

Defined in: src/collection-builder.ts:61

Enable automatic timestamp management.

Parameters

enabled?

boolean

Returns

CollectionBuilder<T>


validate()

ts
validate(validator): CollectionBuilder<T>;

Defined in: src/collection-builder.ts:66

Add validation function using a type predicate.

Parameters

validator

(doc) => doc is T

Returns

CollectionBuilder<T>

Released under the MIT License.