PaulieScanlon / paulie-dev-2019-comments-repo

The repo that holds all the comments from paulie.dev
0 stars 0 forks source link

posts/2023/02/getting-started-with-cockroachdb-pg-promise-and-nextjs/ #15

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Paul Scanlon | Getting Started With CockroachDB, pg-promise and Next.js

In this guide you should find everything you need to get started with CockroachDB , pg-promise and Next.js (API Routes). But, before I…

https://paulie.dev/posts/2023/02/getting-started-with-cockroachdb-pg-promise-and-nextjs/

vitaly-t commented 1 year ago

You can replace this code:

const city = geo ? geo.city : 'Greenland';
const lat = geo ? geo.ll[0] : 65.95346100241352;
const lng = geo ? geo.ll[1] : -44.96798528799432;

const response = await db.one(
      'INSERT INTO locations (date, city, lat, lng, runtime) VALUES(${date}, ${city}, ${lat}, ${lng}, ${serverless}) RETURNING id',
      {
        date: date,
        city: city,
        lat: lat,
        lng: lng,
        serverless: 'serverless'
      }
    );

with this one:

const inputs = {
    date,
    city: geo ? geo.city : 'Greenland',
    lat: geo ? geo.ll[0] : 65.95346100241352,
    lng: geo ? geo.ll[1] : -44.96798528799432,
    runtime: 'serverless'
};

const {id} = await db.one('INSERT INTO locations(${this:name}) VALUES(${this:list}) RETURNING id', inputs);

which is much shorter and more elegant ;)