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

@joseaburt/mysql2-query-builder

v0.1.5

Published

Simple abstraction for create composable sql queries base on `mysql2` repository.

Downloads

9

Readme

mysql2-query-builder

Simple abstraction for create composable sql queries base on mysql2 repository.

⚠️ This repo is part of a course that is in progress and maybe you can see that there is just a couple features at the moment, that is because while the course advance more features will be added. Check current one. Remember all extra queries born from a base function provided by mysql2 and I refer to #query function, from this we extends and get all those custom stuffs. 😉 Software is like that: we get base and plain features and we can create or extends for making the software evolution. I know you notice a lot of principles there and you will more with the help or software principles and software design patterns.

⚠️ This is a repo for teaching my junior students and I recommend to use more powerful solution like a ORM. However you can use this that is ready for production, more for those legacy projects that use direct string sql commands in the persistance layer.

Recomended repositories for this porpuse


Connection Configuration

createDataSource(options?: ConnectionOptions): DataSource

By the default the when we create the DataSource we can pass or not the connection options, so here we have two scenarios:

  1. If connection options are not passed to the createDataSource function, so that's why we want to use envariments variables for that connection.

| Variable | Description | | ----------------- | ----------------- | | DATABASE_HOST | Database host | | DATABASE_USER | Database username | | DATABASE_PASSWORD | Database password | | DATABASE_PORT | Database port | | DATABASE_NAME | Database name |

So we can get the datasource like:

const datasource = DataSource.createDataSource();
  1. We want to pass the connection options maybe because we want to connect to another database with different options:

So we can get the datasource like:

const datasource = DataSource.createDataSource({
  port: 3306,
  password: '',
  user: 'root',
  database: 'test',
  host: 'localhost',
});

Select Query

Select

const connection = await datasource.getConnection();

const tracks = await connection.select('*').from('tracks');

Where

const trackId = 7;
const connection = await datasource.getConnection();

// SELECT * FROM tracks WHERE id = 7;
const tracks = await connection
  .select('*')
  .from('tracks')
  .where('id = ?')
  .execute([trackId]);

// SELECT title as trackTitle, album_id as albumId FROM tracks WHERE id = 7;
const tracks = await connection
  .select('title as trackTitle', 'album_id as albumId')
  .from('tracks').where('id = ?')
  .execute([trackId]);

// SELECT t.title as trackTitle, t.album_id as albumId FROM t WHERE id = 7;
const tracks = await connection
  .select('t.title as trackTitle', 't.album_id as albumId')
  .from('tracks as t').where('id = ?')
  .execute([trackId]);

// SELECT * FROM tracks WHERE id = 7 AND deleted_at IS NULL;
const tracks = await connection
  .select('*')
  .from('tracks')
  .where('id = ?', 'deleted_at IS NULL')
  .execute([trackId]);

Join

You can use this join with all the posibilities that where and select (previously explained) provide you.

const fromAlbumId = 7;
const connection = await datasource.getConnection();

const artists = await connection
  .select('users.username', 'users.id', 'users.email')
  .from('album_artists')
  .join('users')
  .on('album_artists.artist_id = users.id')
  .where('album_artists.album_id = ?')
  .execute([fromAlbumId]);

Here some ideas

class Mysql2Repository {
  constructor(private datasource: DataSource) {}

  public async findAlbumById(id: number): Promise<Album> {
    const connection = await this.datasource.getConnection();
    const [album] = await connection.select('*').from('albums').where('id = ?').execute([id]);

    if (!album) throw new CustomError(404, 'RECORD_NOT_FOUND', `Album with id ${id} not found`);
    return album;
  }

  public async findAllAlbumArtists(albumId: number): Promise<Artist[]> {
    const connection = await this.datasource.getConnection();
    return await connection
      .select('users.username', 'users.id', 'users.email')
      .from('album_artists')
      .join('users')
      .on('album_artists.artist_id = users.id')
      .where('album_artists.album_id = ?')
      .execute([albumId]);
  }
}