1.0.0 • Published 7 months ago

prisma-null-to-undefined-alvamind v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

✨ prisma-null-to-undefined-alvamind šŸ› ļø

Your Prisma DTO Transformation Wizard!

npm version npm downloads npm license GitHub stars GitHub forks

Are you tired of dealing with nullable fields when mapping your Prisma models to DTOs? 😩 prisma-null-to-undefined-alvamind is here to save the day! šŸŽ‰ This library provides a TypeScript utility to automatically convert all null values to undefined in your Prisma DTOs. Say goodbye to those pesky null checks! šŸ‘‹

bun test test/test.test.ts
bun test v1.1.42 (50eec002)

test/test.test.ts:
āœ“ PrismaToDTO Type Conversion > should convert null to undefined
āœ“ PrismaToDTO Type Conversion > should handle nested objects
āœ“ PrismaToDTO Type Conversion > should handle arrays
āœ“ PrismaToDTO Type Conversion > should handle Date objects
āœ“ PrismaToDTO Type Conversion > should throw error for null input
āœ“ PrismaToDTO Type Conversion > should throw error for undefined input
āœ“ PrismaToDTO Type Conversion > should handle mixed nested arrays and objects
āœ“ PrismaToDTO Type Conversion > should handle optional properties [1.00ms]
āœ“ PrismaToDTO Type Conversion > should handle complex object with multiple nesting levels
āœ“ PrismaToDTO Type Conversion > should handle array of primitive values with null
āœ“ PrismaToDTO Type Conversion > should handle complex type unions

 11 pass
 0 fail
 27 expect() calls
Ran 11 tests across 1 files. [22.00ms]

✨ Features & Benefits

  • šŸ”„ Automatic Null to Undefined Conversion:
    • Effortlessly transforms all null values in your Prisma types to undefined in your DTOs.
    • Handles nested objects and arrays recursively.
  • šŸ›”ļø Type Safety:
    • Provides the PrismaToDTO<T> type that ensures your DTOs have the correct types (with undefined instead of null).
  • āœ… Assertion Function:
    • Includes an assertNullOrUndefined<T> function that checks if a given object is of type PrismaToDTO<T>, throws an error if the object is null or undefined, ensuring your DTOs are in correct form and preventing null reference error.
  • šŸ“¦ Lightweight:
    • Zero dependencies, keeping your project lean and mean.
  • āŒØļø TypeScript Support:
    • Written in TypeScript with full type definitions for a smooth development experience.
    • Works seamlessly with Prisma's generated types.
  • šŸš€ Easy to Use:
    • Simple and intuitive API that is very easy to integrate.
  • 🧘 Clean Code:
    • Helps keep your codebase clean and avoids messy null checks.
  • āœ… Prevents Error:
    • Prevents null object reference errors by converting them to undefined

āš™ļø Installation

Get started quickly! āš”ļø

npm install prisma-null-to-undefined-alvamind

šŸš€ Quick Start

Here's how you can use it in your project: šŸ˜Ž

import { PrismaToDTO, assertNullOrUndefined } from 'prisma-null-to-undefined-alvamind';

// Example Prisma type
type User = {
  id: number;
  name: string | null;
  email: string;
  address: {
    street: string | null;
    city: string;
  } | null;
   posts :  Array<{
        id: number;
        title: string | null;
    }> | null
  createdAt: Date | null;
};

// Example DTO type
type UserDTO = PrismaToDTO<User>;

const prismaUser: User = {
  id: 1,
  name: 'John Doe',
  email: 'john.doe@example.com',
    address: {
      street: null,
      city: 'New York',
  },
  posts: [{
     id : 1,
     title: null
   }],
  createdAt: new Date(),
};
// Assert null value become undefined
assertNullOrUndefined<User>(prismaUser)

// Now, userDTO will have `name` and `address.street` as `string | undefined`, `address` as ` { street : string | undefined, city:string } | undefined` and `posts` as `Array<{ id:number, title:string | undefined }> | undefined` and `createdAt` as `Date`
const userDTO: UserDTO = prismaUser;


console.log(userDTO);

šŸ“– API Reference

PrismaToDTO<T> Type

  • Purpose: This is a generic type that transforms all nullable properties in a Prisma model type (T) to optional properties with undefined.
  • Usage: Apply this type to your Prisma model type to generate a DTO type with all null values replaced by undefined.
  • How it Works:
    • If a property is null, it becomes undefined.
    • If a property is a Date, it remains a Date
    • If a property is an array, all elements of that array are also transformed using PrismaToDTO.
    • If a property is an object, the properties of that object are also transformed recursively using PrismaToDTO.

assertNullOrUndefined<T>(prismaObject: any): asserts prismaObject is PrismaToDTO<T> Function

  • Purpose: An assertion function that checks if a given object matches the PrismaToDTO<T> type and throws an error if it does not (if the object is null or undefined ).
  • Usage: Call this function to validate the result of Prisma query before using it as a DTO.
  • How it Works:
    • If the input prismaObject is null or undefined, it throws an error with the message "Invalid DTO conversion: Object is null or undefined".
    • If the input is valid then it will ensure type safety and prevent null object reference error by changing all null value to undefined

šŸ›£ļø Roadmap

Here's what we're planning for the future:

āœ… Done

  • Basic PrismaToDTO<T> type implementation.
  • Recursive handling of nested objects and arrays.
  • assertNullOrUndefined<T> assertion function.
  • Initial release as an npm package.
  • Comprehensive documentation and examples.

ā³ In Progress

  • Adding option to ignore some fields on transformation.

šŸš€ Future Goals

  • Explore options to handle other Prisma specific types.
  • Create a CLI tool for easy type generation.
  • Add more test cases to ensure it works perfectly on various Prisma schema.

šŸ“ Contribution

We welcome contributions! šŸŽ‰ If you have ideas for new features, improvements, or bug fixes, here's how you can contribute:

  1. Fork the repository.
  2. Create a new branch for your changes.
  3. Make your modifications and commit them with descriptive messages.
  4. Submit a pull request.

We appreciate your support! šŸ’Ŗ

šŸ’° Donation

If you find this library valuable and want to support its development, you can donate via:

Any amount is greatly appreciated! šŸ™ Your support helps us maintain and improve this library.

āš ļø Disclaimer

This library is provided as is, without any warranty. While we strive to ensure it works correctly and is helpful, we cannot guarantee that it will be suitable for all scenarios. Use it at your own risk. By using this library, you agree to our terms of use.

šŸ“œ License

prisma-null-to-undefined-alvamind is licensed under the MIT License. You're free to use, modify, and distribute it for your projects.

šŸ¤ Stay Connected

We'd love to hear your thoughts, suggestions, and experiences with prisma-null-to-undefined-alvamind. Let's make Prisma DTO handling a breeze! 🤘

Happy Coding! šŸš€

1.0.0

7 months ago