s-m-r-t

Getting Started

What does s-m-r-t stand for?

https://www.youtube.com/watch?v=ls5BFzuxGw4

What is s-m-r-t?

s-m-r-t is a full-stack TypeScript framework that abstracts away implementation details for databases, REST APIs, MCP tools, and CLI commands. Define your data models once with the @smrt decorator, and get auto-generated APIs, CLI commands, and Claude integration.

Which databases does s-m-r-t support?

s-m-r-t supports multiple databases through adapters:

  • PostgreSQL (recommended for production)
  • SQLite (great for development and testing)
  • MySQL/MariaDB
  • JSON files (for static sites)

Do I need to know SQL?

No! s-m-r-t abstracts database operations into TypeScript methods. However, understanding basic database concepts (tables, relationships, indexes) is helpful for building efficient applications.

Can I use s-m-r-t with an existing database?

Yes! s-m-r-t can work with existing databases. Use the scanner to generate SmrtObject classes from your existing schema, or manually define models that map to your tables.

Configuration & Setup

How do I configure database connections?

Create a smrt.config.ts file:

typescript
export default {
  database: {
    type: 'postgresql',
    host: process.env.DB_HOST || 'localhost',
    port: 5432,
    database: 'myapp',
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD
  }
};

How do I enable API generation?

Add api: true to your @smrt decorator:

typescript
@smrt({
  api: true,  // Generates all CRUD endpoints
  // OR specify operations:
  api: {
    include: ['list', 'get', 'create']
  }
})
class Product extends SmrtObject {
  // ...
}

How do I set up multi-tenancy?

Use the smrt-tenancy module with the @TenantScoped decorator. All queries will automatically filter by the current tenant context.

Development

How do I run migrations?

s-m-r-t can auto-generate migrations from your models. Use the CLI:

bash
smrt migrations generate
smrt migrations run

How do I test s-m-r-t applications?

Use smrt-vitest for testing:

typescript
import { createTestDb, createFixture } from '@happyvertical/smrt-vitest';

describe('Task Tests', () => {
  it('should create task', async () => {
    const db = await createTestDb();
    const tasks = await TaskCollection.create({ db });

    const task = await createFixture(tasks, 'Task', {
      title: 'Test Task'
    });

    expect(task.title).toBe('Test Task');
  });
});

Can I use s-m-r-t with SvelteKit?

Yes! s-m-r-t works great with SvelteKit. Use the template-sveltekit template to get started with a pre-configured setup.

How do I integrate Claude AI?

s-m-r-t includes built-in MCP (Model Context Protocol) support. Enable it with mcp: true in your @smrt decorator, then start the MCP server with smrt-dev-mcp start. Claude Code can then interact with your objects using natural language.

Data Modeling

How do I define relationships between models?

Use @foreignKey and @manyToMany decorators:

typescript
@smrt()
class Order extends SmrtObject {
  @foreignKey(() => User)
  userId: string = '';

  @manyToMany(() => Product, { through: 'order_products' })
  products: Product[] = [];
}

What's the difference between CTI and STI?

CTI (Class Table Inheritance): Each class gets its own table. More normalized but requires joins.
STI (Single Table Inheritance): All subclasses share one table with a discriminator field. Faster queries but denormalized.

Use CTI when subclasses have many unique fields. Use STI when they're similar.

How do I add computed properties?

Use TypeScript getters:

typescript
@smrt()
class Order extends SmrtObject {
  @field()
  subtotal: number = 0;

  @field()
  taxRate: number = 0.08;

  get total(): number {
    return this.subtotal * (1 + this.taxRate);
  }
}

Performance

How do I optimize queries?

  • Use select to fetch only needed fields
  • Add indexes with @field({index: true})
  • Use pagination with limit and offset
  • Eager load relationships to avoid N+1 queries
  • Use connection pooling in production

Should I use caching?

Yes! For frequently accessed data, implement caching at the collection level or use a dedicated caching layer (Redis, Memcached). s-m-r-t collections support custom caching strategies.

How do I handle large datasets?

  • Always use pagination for lists
  • Implement cursor-based pagination for large tables
  • Use background jobs for batch operations
  • Consider data archiving strategies
  • Optimize indexes for common queries

Troubleshooting

Build fails with "Cannot find module"

Ensure all s-m-r-t packages are installed and have matching versions. Run npm install and check for peer dependency warnings.

Database connection fails

Check:

  • Database server is running
  • Connection credentials are correct
  • Firewall allows connections
  • Database exists and user has permissions

API endpoints return 404

Verify:

  • api: true is set in @smrt decorator
  • API server is running
  • Manifest was generated (run build)
  • Correct port and prefix in config

TypeScript errors with decorators

Enable experimental decorators in tsconfig.json:

json
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Deployment

How do I deploy s-m-r-t applications?

s-m-r-t applications can be deployed anywhere Node.js runs:

  • Docker containers (recommended)
  • Kubernetes
  • Traditional VPS (DigitalOcean, Linode)
  • Serverless platforms (with limitations)
  • Static hosts for adapter-static builds

What environment variables do I need?

At minimum:

  • DB_HOST, DB_USER, DB_PASSWORD
  • NODE_ENV (production, development)
  • PORT for the API server
  • Any API keys for third-party services

Should I run migrations in production?

Yes, but with caution:

  • Test migrations in staging first
  • Run during maintenance windows for large changes
  • Keep backups before schema changes
  • Use migration tools for rollback capability

Still Need Help?