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

slonik-plus

v0.0.2

Published

Extends slonik to add definable typescript models and additional features

Downloads

1

Readme

slonik-plus

Extends slonik to add definable typescript models and additional features like upsert.

It is not an ORM, but it adds the benefits of definable TypeScript models that can make querying / updates much easier.

Notes

This is a library I've written and have used extensively in large enterprise systems. I feel it would be useful to others, so I'm open sourcing it.

Unfortunately, I don't have time to write full, proper external documentation yet. However, its options are documented, and can be seen using intellisense or by examining the code in this repo.

Future Improvement

Now that it's open source, there is certainly room for improvement and expanded functionality. You are welcome to submit PRs and requests in the issues!

Also, if anyone finds it useful and would like to help by expanding it or the readme, it would be greatly appreciated!

Usage

Upgraded Pool

// Note: In addition to our extras, all slonik exports are included, so you can import them 
//   directly from the slonik-plus library

import { createPoolPlus } from "slonik-plus";

const pool = createPoolPlus(/* ... */);

Models

Unlike an ORM, models do not get created on PG. They're simply a typescript definition which provide binding to database objects & queries.

// Note: Check intellisense for more options in the `@record` param such as appending 
//   to the query or overriding entirely, which allows you to create a model with a specific 
//   query attached

@record({ table: 'listing' }) 
export class ListingRecord extends DbRecord<
  ListingRecord,

  // Note: This optional config object allows customizing the constructor's input parameter
  {
    exclude: 'listingId'  // Exclude the listingId, because it's auto-generated by PG
    partial: 'allowsHourly' // Make optional, because the PG table has a default value specifed
  }
> {
  // Note: primaryKey can also be numeric for multi-prop keys
  @column({ type: 'bigint', primaryKey: true, out: false }) 
  readonly declare listingId: number;

  @column({ type: 'listing.listing_site' })
  readonly declare listingSite: T;

  @column({ type: 'text' })
  declare title: string;

  @column({ type: 'timestamptz', out: false })
  readonly declare postDt: Date;

  // Note: A trigger sets this field, so we make it readonly and use out: false to 
  //   prevent it from being updatable
  @column({ type: 'timestamptz', out: false })
  readonly declare updatedDt?: Date;

  @column({ type: 'text' })
  declare summary?: string;

  @column({ type: 'bool' })
  declare allowsHourly: boolean;
}

Working with models

// Get listing records
const recs = await pool.maybeMany(
  ListingRecord, 
  { append: sql.fragment`WHERE allows_hourly = true` } // See intellisense for more options
); 

// Can still use regular queries
const manualRecs = await pool.maybeMany(sql.unsafe`SELECT * from listing`);

// Upsert
const rec1 = new ProjectListing({ /* ... */ });
const rec2 = new ProjectListing({ /* ... */ });
await pool.upsert(
  [ rec1, rec2 ], // Can also update a signle record
  { /* Note: See intellisense for optional extended options */ }
);

// Modify & Update
rec1.allowsHourly = false;
await rec1.update(pool);