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-flags

v0.0.3

Published

Use flags columns in drizzle. These columns can store multiple boolean values using a single integer column, which can save up a lot of space.

Downloads

14

Readme

drizzle-flags

This package can be used to use flags columns in drizzle. These columns can store multiple boolean values using a single integer column, which can save up a lot of space. For example, using this method 8 booleans can be stored in a single TINYINT.

[!IMPORTANT] Currently, this package only supports MySQL databases. Other databases will be added.

Installation

npm install drizzle-flags

Usage

Creating a flags column

Use the flags function to create a flags column. Provide the name of each flag. These names will not actually go in the database, so camelCase is fine.

It will create an integer column in the database, automatically sized depending on the number of flags. As each flag is a single bit, these are the maximum number of flags that can be stored in each integer type:

  • up to 8 flags -> TINYINT
  • up to 16 flags -> SMALLINT
  • up to 24 flags -> MEDIUMINT
  • up to 32 flags -> INT
  • up to 64 flags -> BIGINT
// schema.ts
import flags from 'drizzle-flags'

export const users = mysqlTable('users', {
	id: serial('id').primaryKey(),
	username: varchar('username', { length: 191 }).notNull().unique(),
	fullName: varchar('full_name', { length: 191 }),
	email: varchar('email', { length: 191 }).unique(),
	notifications: flags('notifications', [
		'app',
		'newFeatures',
		'tips',
		'marketing',
		'newsletter',
	]).default({
		app: true,
		newFeatures: true,
		tips: true,
		marketing: false,
		newsletter: false,
	}),
})

In this example, the notifications column will store 5 boolean values, indicating whether the user has enabled notifications for the app, new features, tips, marketing, and the newsletter channels. A TINYINT column will be created in the database to store these flags.

Reading and writing flags

Now that we have created the column, we can read and write the notifications column as an object with the flag names as keys.

const user = await db.query.users.findFirst({
	where: eq(schema.users.id, 1),
})

console.log(user?.notifications)

// Output:
// {
//     app: true,
//     newFeatures: true,
//     tips: true,
//     marketing: false,
//     newsletter: false,
// }

// enable `marketing` and `newsletter` notifications
await db
	.update(schema.users)
	.set({
		notifications: {
			app: true,
			newFeatures: true,
			tips: true,
			marketing: true,
			newsletter: true,
		},
	})
	.where(eq(schema.users.id, 1))

Filtering by flags

The f0 and f1 functions can be used to filter rows based on the value of a flag.

  • f0 stands for flag 0, where the selected flags are false.
  • f1 stands for flag 1, where the selected flags are true.

Here are some examples:

// Find users who have enabled the `newsletter` notifications
await db.query.users.findMany({
	where: f1(schema.users.notifications, 'newsletter'),
})

// Find users who have enabled the `app` and `newFeatures` notifications
await db.query.users.findMany({
	where: f1(schema.users.notifications, ['app', 'newFeatures']),
})

// Find users who have disabled the `tips` notifications
await db.query.users.findMany({
	where: f0(schema.users.notifications, 'tips'),
})

// Find users who have enabled the `app` and `tips` notifications
// and disabled the `newsletter` notifications
await db.query.users.findMany({
	where: and(
		f1(schema.users.notifications, ['app', 'tips']),
		f0(schema.users.notifications, 'newsletter'),
	),
})

Including flags in the extra fields

The flagsExtras function can be used to include the flags in the extra fields of the query.

const user = await db.query.users.findFirst({
	where: eq(schema.users.id, 1),
	columns: { id: true, username: true },
	extras: flagsExtras(schema.users.notifications),
})

console.log(user)

// Output:
// {
//     id: 1,
//     username: 'john_doe',
//     app: true,
//     newFeatures: true,
//     tips: true,
//     marketing: true,
//     newsletter: true,
// }