prisma-null-to-undefined-alvamind v1.0.0
⨠prisma-null-to-undefined-alvamind š ļø
Your Prisma DTO Transformation Wizard!
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 toundefined
in your DTOs. - Handles nested objects and arrays recursively.
- Effortlessly transforms all
- š”ļø Type Safety:
- Provides the
PrismaToDTO<T>
type that ensures your DTOs have the correct types (withundefined
instead ofnull
).
- Provides the
- ā
Assertion Function:
- Includes an
assertNullOrUndefined<T>
function that checks if a given object is of typePrismaToDTO<T>
, throws an error if the object isnull
orundefined
, ensuring your DTOs are in correct form and preventing null reference error.
- Includes an
- š¦ 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.
- Helps keep your codebase clean and avoids messy
- ā
Prevents Error:
- Prevents
null
object reference errors by converting them toundefined
- Prevents
āļø 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 withundefined
. - Usage: Apply this type to your Prisma model type to generate a DTO type with all
null
values replaced byundefined
. - How it Works:
- If a property is
null
, it becomesundefined
. - If a property is a
Date
, it remains aDate
- 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
.
- If a property is
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 isnull
orundefined
). - Usage: Call this function to validate the result of Prisma query before using it as a DTO.
- How it Works:
- If the input
prismaObject
isnull
orundefined
, 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 allnull
value toundefined
- If the input
š£ļø 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:
- Fork the repository.
- Create a new branch for your changes.
- Make your modifications and commit them with descriptive messages.
- 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:
- GitHub Sponsors: Link to GitHub Sponsors
- Buy us a coffee: Link to donation page
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! š
7 months ago