npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

prisma-null-to-undefined-alvamind

v1.0.0

Published

A TypeScript utility to convert null values to undefined for Prisma DTOs.

Readme

✨ 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: '[email protected]',
    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

  • [x] Basic PrismaToDTO<T> type implementation.
  • [x] Recursive handling of nested objects and arrays.
  • [x] assertNullOrUndefined<T> assertion function.
  • [x] Initial release as an npm package.
  • [x] 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! 🚀