Simple abstraction for create composable sql queries base on mysql2
repository.
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
.
Recomended repositories for this porpuse
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:
- 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();
- 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',
});
const connection = await datasource.getConnection();
const tracks = await connection.select('*').from('tracks');
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]);
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]);
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]);
}
}