Languages
[Edit]
EN

Node.js - PostgreSQL Find rows created within the last week / weeks

0 points
Created by:
Kia-H
546

In this article, we would like to show you how to find rows created within the last week in the Postgres database from Node.js level.

Node.js - PostgreSQL Find rows created within the last week
Table used in the example - HeidiSQL

Note:Β at the end of this article you can find database preparation SQL queries.

const { Client } = require('pg');

const client = new Client({
    host: '127.0.0.1',
    user: 'my_username',
    database: 'my_database',
    password: 'my_password',
    port: 5432,
});

const fetchEventsFromLastWeeks = async (weekCount) => {
	const query = `    
            SELECT *
            FROM "events"
            WHERE "event_timestamp" > NOW() - $1 * interval '1 week';
    `;
    await client.connect();  // creates connection
    try {
        const { rows } = await client.query(query, [weekCount]);  // sends query
        return rows;
    } finally {
        await client.end();  // closes connection
    }
};

fetchEventsFromLastWeeks(1)
    .then(result => console.table(result))
    .catch(error => console.error(error.stack));

Result:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index) β”‚ id β”‚     event_timestamp      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚    0    β”‚ 8  β”‚ 2021-04-16T18:50:40.000Z β”‚
β”‚    1    β”‚ 9  β”‚ 2021-04-17T13:45:35.000Z β”‚
β”‚    2    β”‚ 10 β”‚ 2021-04-23T14:46:36.000Z β”‚
β”‚    3    β”‚ 11 β”‚ 2021-04-23T17:49:39.000Z β”‚
β”‚    4    β”‚ 12 β”‚ 2021-04-23T18:50:40.000Z β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Database preparation

create_tables.sqlΒ file:

CREATE TABLE "events" (
	"id" SERIAL PRIMARY KEY,
	"event_timestamp" TIMESTAMP NOT NULL
);

insert_data.sqlΒ file:

INSERT INTO "events" 
    ("event_timestamp")
VALUES
    ('2021-04-01 11:41:31'),
    ('2021-04-01 12:42:32'),
    ('2021-04-02 14:44:34'),
    ('2021-04-02 15:45:35'),
    ('2021-04-03 16:46:36'),
    ('2021-04-05 17:47:37'),
    ('2021-04-05 19:49:39'),
    ('2021-04-16 20:50:40'),
    ('2021-04-17 15:45:35'),
    ('2021-04-23 16:46:36'),
    ('2021-04-23 19:49:39'),
    ('2021-04-23 20:50:40');

Native SQL query (used in the above example):

SELECT *
FROM "events"
WHERE "event_timestamp" > NOW() - $1 * interval '1 week';
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Node.js - PostgreSQL - dates

Native Advertising
πŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

β€οΈπŸ’» πŸ™‚

Join