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 🙏

© 2024 – Pkg Stats / Ryan Hefner

drizzle-kiss

v0.0.10

Published

A multi-purpose package for drizzle.

Downloads

5

Readme

drizzle-kiss

A multi-purpose package for drizzle.

Features:

  • generate prisma schema from your drizzle schema
  • generate an abstraction client for your drizzle schema.

Drizzle to Prisma generator

I like making my models using Prisma schema, however it's severely limited. It forces you to repeat yourself if you want a common structure for all models (i.e. createdAt, updatedAt, etc.), and you cannot split your models into different files.

This package allows you to use the flexibility of defining models using drizzle, and then converting that to a single-file prisma schema.

NOTE: This package currently only supports postgres.

Usage:

  1. Make your drizzle schemas, relations & enums
  2. Create a new file for generating prisma schema. In there, import your drizzle schema
  3. Have the following code in your file:
// file: prisma-generator.ts
import * as schema from "./my-drizzle-schema.ts"
import { parseDrizzle, generateToPrisma } from "drizzle-kiss";

const parsed = parseDrizzle(schema);
console.log(parsed);

const generated = await generateToPrisma({
    parsed
});

console.log(generated);
await Bun.write("./schema.prisma", generated.prismaSchemaString);
  1. Run bun run prisma-generator.ts

Note:

  • Due to an issue with prisma, ensure all your model names, field names, enum names, enum values are appropriate. You cannot start with _, and cannot use dots in your naming.

Drizzle KISS (keep it simple, stupid)

Note: Currently not implemented

Do you miss the abstraction layer Prisma provided you with? I do. That's why I made this package.

You're probably asking why, when drizzle is meant for a lower level. Well, simply because:

I want a basic crud abstraction layer. I don't want to mess with transactions and boilerplate for super simple use cases.

Installation

bun add drizzle-kiss

Usage

  1. Create a file called "drizzle.kiss.ts" (or whatever, really)
  2. Include the following in the "drizzle.kiss.ts" file
import * as schema from "./your/drizzle/schema.ts";
import { genTypes } from "drizzle-kiss";

console.log("Generating types using drizzle-kiss for my drizzle db ♥");

genTypes({
    schema,
    out: "./src/kiss.types.ts"
});
  1. Run drizzle.kiss.ts every time you make changes to your schema. The package will parse your drizzle schema and generate types in the kiss.types.ts file.
  2. Use drizzle-kiss for your basic CRUD needs:
import type * as kissTypes from "./kiss.types.ts";
import * as schema from "./your/drizzle/schema.ts";

const db = drizlle();
const kiss = kiss<kissTypes>();

//> Using drizzle-kiss
const user = await kiss.createOne(schema.User, {
    data: {
        name: "Username",
        email: "[email protected]",
        workspace: {
            create: {
                name: "Test Workspace",
                stripeCustomerId: "123"
            }
        }
    }
});
console.log(`Created user with id ${ user.id }, and workspace with id ${ user.workspace.id }`);

//> Using drizzle-orm directly
const result = await db.transaction(async tx => {
    const createdUser = await db.insert(schema.User)
        .values({
            name: "Username",
            email: "[email protected]"
        })
        .returning()
        .onConflictDoNothing()
        .execute()
        .then(r => r.at(0));

    if (!createdUser) {
        throw new Error(`Failed to create user!`);
    }

    const createdWorkspace = await db.insert(schema.Workspace)
        .values({
            userId: createdUser.id,
            name: "Test Workspace",
            stripeCustomerId: "123"
        })
        .returning()
        .onConflictDoNothing()
        .execute()
        .then(r => r.at(0));

    if (!createdWorkspace) {
        throw new Error(`Failed to create workspace!`);
    }

    return {
        user: createdUser,
        workspace: createdWorkspace
    };
});
console.log(user.id, workspace.id);

Roadmap

  • [ ] Avoid the generation step. Preferably, it should just use types to infer all the models.
  • [ ] Support other dialects databases than just Postgres