Search code examples
postgresqlbookshelf.jsknex.js

What is the numeric type equivalent in KnexJS (Postgres)?


I am creating a table using Knex.JS, and the table has a column for a currency value.

For example, here is the column amount:

knex.schema.createTable('payment', function(table) {
  table.increments();
  table.float('amount');
})

Currently, I am using the float type, but I'd like to use the numeric type. What is the equivalent of the numeric type in Knex.JS?

Thanks.


Solution

  • For currency decimal is best match, so your code may look like:

    knex.schema.createTable('payment', function(table) {
      table.increments();
      table.decimal('amount',14,2); // e.g. 14 positions, 2 for the cents
    });
    

    see http://knexjs.org/#Schema-decimal