@nestgate/core
Version:
Core utilities for NestJS - Mix classes, connect entities, and compose decorators with ease
277 lines (201 loc) • 4.47 kB
Markdown
# /core
Core utilities and composable decorators for NestJS applications.
## 📦 Installation
```bash
npm install /core
# or
pnpm add /core
# or
yarn add /core
```
## 🎯 Features
- **Mix** - Combine multiple classes with proper TypeScript types and metadata
- **ConnectOne** - Connect single nested entity with validation
- **ConnectMany** - Connect array of nested entities with validation
## 📚 API Reference
### Mix
Combine multiple classes into one, inheriting all properties and decorators.
```typescript
import { Mix } from '@nestgate/core';
class Timestamped {
createdAt: Date;
updatedAt: Date;
}
class SoftDelete {
deletedAt?: Date;
isDeleted: boolean;
}
// Combine multiple mixins
class User extends Mix(Timestamped, SoftDelete) {
id: string;
name: string;
}
```
**Features:**
- ✅ Full TypeScript type inference
- ✅ Swagger metadata preserved
- ✅ Validation decorators work correctly
- ✅ Multiple mixins supported
### ConnectOne
Connect a single nested entity with automatic validation and transformation.
```typescript
import { ConnectOne } from '@nestgate/core';
class Address {
street: string;
city: string;
country: string;
}
class User {
id: string;
name: string;
// Single nested object
address?: Address;
}
```
**Options:**
```typescript
interface ConnectOptions {
required?: boolean; // Default: true
description?: string; // Swagger description
example?: any; // Swagger example
deprecated?: boolean; // Mark as deprecated
exclude?: boolean; // Exclude from API response
}
```
**Features:**
- ✅ Automatic nested validation with ``
- ✅ JSON to class transformation with ``
- ✅ Swagger schema generation
- ✅ Optional/Required support
### ConnectMany
Connect an array of nested entities.
```typescript
import { ConnectMany } from '@nestgate/core';
class Post {
id: string;
title: string;
content: string;
}
class User {
id: string;
name: string;
// Array of nested objects
posts?: Post[];
}
```
**Features:**
- ✅ Validates each array item
- ✅ Transforms each item to class instance
- ✅ Swagger shows array type correctly
- ✅ Works with pagination
## 💡 Examples
### Complete CRUD Entity
```typescript
import { Mix, ConnectOne, ConnectMany } from '@nestgate/core';
// Base mixins
class Timestamped {
createdAt: Date;
updatedAt: Date;
}
class Authorable {
authorId: string;
}
// Related entities
class Tag {
name: string;
}
class Comment {
text: string;
createdAt: Date;
}
// Main entity with everything
class BlogPost extends Mix(Timestamped, Authorable) {
id: string;
title: string;
content: string;
tags: Tag[];
comments?: Comment[];
}
```
### Multi-level Nesting
```typescript
class Author {
id: string;
name: string;
}
class Comment {
text: string;
author: Author;
}
class Post {
title: string;
author: Author;
comments: Comment[];
}
```
### Excluding from Response
```typescript
class User {
id: string;
email: string;
// This won't appear in API response
internalSettings: UserSettings;
}
```
## 🔗 Related Packages
- [/rest](../rest) - REST API decorators
- [/entity](../entity) - Entity validation decorators
- [/setup](../setup) - Quick application setup
## 📝 License
MIT © [NestGate](https://nestgate.org)
## 🤝 Contributing
Contributions are welcome! Please see our [Contributing Guide](../../CONTRIBUTING.md).
## 💬 Support
- [Documentation](https://nestgate.org)
- [GitHub Issues](https://github.com/nestgate/nestgate/issues)
- [Discord](https://discord.gg/nestgate)