Functional programming is an increasingly popular style of programming in the JavaScript world, but what makes it so powerful? Functional programming relies heavily on the use of pure functions, high-order functions, immutable data structures, and more to create code that is easy to maintain, test, and modify.
Category: javascript
-
Learning Functional Programming with JavaScript – Anjana Vakil – JSUnconf -
Knex is a query builder for JavaScript that allows developers to build SQL queries using a fluent and intuitive interface. It supports multiple database systems such as Postgres, MSSQL, MySQL, MariaDB, SQLite3, Oracle, and Amazon Redshift. In addition to query building, Knex also provides features for database migrations, making it easy to create and modify database tables using JavaScript code instead of raw SQL scripts. Knex is a powerful tool for managing database operations and simplifying database interactions in JavaScript applications.
To add Knex to your Node.js project, you can follow these steps.
Install dependencies:
yarn add knexMake sure to install the driver that corresponds to your database of choice. Knex supports multiple database systems, such as Postgres, MSSQL, MySQL, MariaDB, SQLite3, Oracle, and Amazon Redshift. In my case I will use SQLite3.
yarn add sqlite3Init the configuration
Once you have Knex and the database driver installed, you can start using Knex in your project. You’ll typically need to configure Knex to connect to your database. Create a
knexfile.jsfile in your project’s root directory, and define the necessary configurations based on your environment settings.knex initIt will create the following
knexfile.jsmodule.exports = { development: { client: 'sqlite3', connection: { filename: './dev.sqlite3' }, debug: true }, staging: { client: 'postgresql', connection: { database: 'my_db', user: 'username', password: 'password' }, pool: { min: 2, max: 10 }, migrations: { tableName: 'knex_migrations' } }, production: { client: 'postgresql', connection: { database: 'my_db', user: 'username', password: 'password' }, pool: { min: 2, max: 10 }, migrations: { tableName: 'knex_migrations' } } };Let’s create now our first script with next to run a DB migration
npx knex migrate:make create_table_usersAs a good practice, create small tasks instead of one task with all the schema. This is useful to detect problems. For example, avoid creating all the init schema in one task, create one task per table and even create another separate task for constraints such as foreign keys
Use descriptive names such as “create_table_users”. Use also prefix for your actions such as “create_table_…” or “create_column_…”
Result:
migrations/20211005153317_create_table_users.jsOpen the file and paste this:
exports.up = function(knex) { return knex.schema.createTable("users", (tb) => { tb.increments("id"); tb.string("username", 100).notNullable(); tb.string("email", 100).notNullable(); tb.string("password", 100).notNullable(); }); }; exports.down = function(knex) { return knex.schema.dropTable("users"); };Save it and run:
knex migrate:latestOr select a different environment:
knex migrate:latest --env productionHandy commands:
npx knex migrate:latest # To run all pending migrations npx knex migrate:up # To run the next pending migration npx knex migrate:down # To roll back the most recent migration npx knex migrate:list # To show current state of migrations.Knex is a query builder among other things and it’s an awesome tool to run DB migrations to create tables and modify them as you usually do with SQL scripts.
That’s it! You have successfully added Knex to your Node.js project. Remember to refer to the Knex documentation for more details on how to use Knex for various database operations.
You can find more information about Knex on its official website at https://knexjs.org.
-
Regular expressions are those I don’t use so frequently so I need to wrap them all in a method with a short explanation about what they do.
So I created a simple JavaScript method that removes all newlines and multiple spaces (including tab spaces) by a single space/** * It replace all new lines and multiple spaces (including tab spaces) by a single space * @param {string } text * @return {string} a new cleaned string */ function cleanUpSpaces(text) { if (!text) { return text; } // s{2,} matches any white space (length >= 2) character (equal to [rntfv ]) return text.replace(/s{2,}/g, ' '); };Output example
// given `SELECT * FROM account WHERE id = 1234` // output SELECT * FROM account WHERE id = 1234 -
Regular expressions, commonly shortened to regex, are a powerful tool for working with text data in programming. One common task is determining whether a given string is a number. Fortunately, this is a task that can be accomplished with a relatively simple regex pattern.
To implement this regex pattern in code, simply use a regex matching function such as JavaScript’s `test()` method, which returns true or false depending on whether the pattern matches the input string. With this simple regex pattern, you can easily detect whether a given string is a number, making it a valuable addition to your programming toolkit.
/** * It's not strictly the same than isNumber() * * @param text * @return true if the given text is s number */ function isNumberText (text) { if (!text) return false; // the value must be a number, including float and not empty. const reg = new RegExp('^-?\d+\.?\d*$'); return reg.test(text); };You can test this regex at https://regex101.com/
Examples
// it will return true on these cases 12 "12" 32.3 "43.4" // false for these cases "22a" undefined null "" "1 2"