Skip to content

Interface Definition Guidelines

When creating TypeScript interfaces for your Nobox record structures, follow these best practices to ensure clean, maintainable code.

❌ Don't Include These Fields

The following fields are automatically managed by Nobox and should never be included in your interface definitions:

  • id - Automatically generated by Nobox
  • createdAt - Automatically set by Nobox
  • updatedAt - Automatically updated by Nobox

✅ Correct Interface Definition

typescript
import { ReturnObject } from "nobox-client";

interface User {
  name: string;
  age: number;
  email: string;
  // Only include your business data fields
}

Using ReturnObject for Type Safety

When working with return types from Nobox operations, use the ReturnObject<T> type:

typescript
// For return types, use ReturnObject<T>
const user: ReturnObject<User> = await getUser(id);

// The ReturnObject type automatically includes:
// - id: string
// - createdAt: string
// - updatedAt: string
// Plus all your defined fields

Complete Example

typescript
import { Space, ReturnObject } from "nobox-client";
import { createRowSchema } from "../config";

// Define interface without id, createdAt, updatedAt
interface Tweet {
  content: string;
  authorId: string;
  media: string[];
  likesCount: number;
}

// Create structure
export const TweetStructure: Space<Tweet> = {
  space: "tweet",
  description: "Twitter clone tweet records",
  structure: {
    content: {
      description: "Tweet content text",
      type: String,
      required: true,
    },
    authorId: {
      description: "ID of the tweet author",
      type: String,
      required: true,
    },
    media: {
      description: "Array of media URLs",
      type: Array,
      required: false,
      defaultValue: [],
    },
    likesCount: {
      description: "Number of likes",
      type: Number,
      required: false,
      defaultValue: 0,
    },
  },
};

export const TweetModel = createRowSchema<Tweet>(TweetStructure);

// Usage with proper typing
const tweet: ReturnObject<Tweet> = await TweetModel.findOne({ id: "123" });

Why This Matters

  1. Clean Interfaces: Focus on business data, not system fields
  2. Type Safety: ReturnObject<T> ensures proper typing
  3. Consistency: Follows Nobox conventions
  4. Maintainability: Easier to understand and modify
  5. Prevents Confusion: Clear separation between user data and system data

Next Steps