import { pool } from '../models/pool';
const createUsersTable = `
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
firstName VARCHAR NOT NULL CHECK (LENGTH(firstName) BETWEEN 3 AND 150),
lastName VARCHAR(86),
userEmail TEXT NOT NULL UNIQUE CHECK (LENGTH(userEmail) BETWEEN 5 AND 280),
mobileNumber TEXT NOT NULL UNIQUE CHECK (mobileNumber ~ '^([0|\+[0-9]{2})?([7-9][0-9]{9})$'),
password VARCHAR NOT NULL CHECK (password ~ '^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,}$'),
gender VARCHAR NOT NULL CHECK (gender IN ('MALE','FEMALE','OTHERS'))
)
`;
const addAdressColumnInUsersTable=`
ALTER TABLE IF EXISTS users ADD COLUMN address VARCHAR;
`;
export const executeQueryArray = async (arr) =>
new Promise((resolve) => {
const stop = arr.length;
arr.forEach(async (q, index) => {
await pool.query(q);
if (index + 1 === stop) resolve();
});
});
export const createTables = () => executeQueryArray([createTableQueries]);
export const updateTables= () => executeQueryArray([addAdressColumnInUsersTable]);
I called the createTables, updateTables functions in the app.js file and after restarting the application with nodemon getting error like ** address column is already added into users table.** So new I would to configure to run all sql scripts at the time or application start and it should run only once. can any one help me out to solve this issue with any predefined js library or even with manual implementation ?