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

@florajs/datasource-mysql

v5.0.0

Published

MySQL connection for Flora

Downloads

137

Readme

@florajs/datasource-mysql

NPM version NPM downloads

MySQL data source for Flora, based on the mysql module.

Usage

Add @florajs/datasource-mysql to Flora config

module.exports = {
    // …
    dataSources: {
        mysql: {
            constructor: require('@florajs/datasource-mysql'),
            options: {
                onConnect: ['SET SESSION max_execution_time = 30000'],
                servers: {
                    default: {
                        user: 'dbuser',
                        password: '…',
                        masters: [{ host: 'db01' }],
                        slaves: [{ host: 'db02' }, { host: 'db03' }],
                    },
                    specialServer: {
                        user: 'dbuser2',
                        password: '…',
                        masters: [{ host: 'specialdb01' }],
                    },
                },
            },
        },
        // …
    },
};

Use data source in resources

SELECT queries may be executed on one of the slaves servers (if present), unless useMaster is set to true.

// Get a Context instance
const db = api.dataSources.mysql.getContext({
    db: 'users', // database name
    // server: 'default', // default: 'default'
    // useMaster: false, // default: false
});

// Query
const allRows = await db.query(
    'SELECT firstname, lastname FROM profiles'
);

// Query with parameters
const someRows = await db.query(
    'SELECT firstname, lastname FROM profiles WHERE id = ?',
    [ 1000 ]
);

// Query with named parameters
const someRows = await db.query(
    'SELECT firstname, lastname FROM profiles WHERE id = :userId',
    { userId: 1000 }
);

// Single column
const ids = await db.queryCol(
    'SELECT id FROM profiles WHERE lastname = "Smith"'
);

// Single row (first result)
const { firstname, lastname } = await db.queryRow(
    'SELECT firstname, lastname FROM profiles WHERE id = 1000'
);

// Single value (first result)
const firstname = await db.queryOne(
    'SELECT firstname FROM profiles WHERE id = 1000'
);

Insert, update, delete

Write queries are executed on master servers automatically, even when useMaster was set to false in getContext.

// Insert a row
db.insert('profiles', {
    firstname: 'Max',
    lastname: 'Mustermann',
    updatedAt: db.raw('NOW()'), // pass through unescaped
});

// Upsert (insert or update)
db.upsert(
    'profiles',
    { id: 1000, firstname: 'Max' },
    [ 'firstname' ] // Update firstname to "Max" if profile with id 1000 already exists
);

// Update
db.update(
    'profiles',
    { updatedAt: db.raw('NOW()') }, // SET updatedAt=NOW()
    { id: 1000 } // WHERE id=1000
);

// Delete
db.delete('profiles', { id: 1000 });

Transactions

const trx = await db.transaction();
try {
    await trx.update('profiles', { firstname: 'John', lastname: 'Doe' }, { userId: 1337 });
    await trx.insert('log', { userId: 1337, msg: 'Changes applied' });
    await trx.commit();
} catch (err) {
    try {
        // Ignore rollback errors
        await trx.rollback();
    } catch (ignoreErr) {
        throw err;
    }
}

// Same as above, but shorter:
await db.transaction(async (trx) => {
    await trx.update('profiles', { firstname: 'John', lastname: 'Doe' }, { userId: 1337 });
    await trx.insert('log', { userId: 1337, msg: 'Changes applied' });
});

Features

  • When being used as data source for resource-processor, SQL queries are optimized and only the columns needed are selected.

License

MIT