Create triggers for your Postgres database in a simple and fast way.
- Control your trigger creations, with restrict, to replace or not existing triggers
- Customize the function names and their identifiers
- Don't worry, if you try to add a script similar to an existing one, Postgre will warn you!
First you need to install pg
package:
npm i pg
Now you can install:
npm i pgsqltriggers-alternative
const TriggersPG = require('pgsqltriggers-alternative')
(async function() {
try {
const database = {
host: 'HOST',
user: 'USER',
database: 'DATA',
password: 'PASS'
}
const connect = TriggersPG.ConfigTriggerDB(database)
const create = await TriggersPG.CreateTriggers({
pool: connect,
scripts: [{
code: "INSERT INTO usersdetails (username) VALUES (NEW.name)",
action: "INSERT",
targetTable: "users"
}, {
code: "UPDATE usersdetails SET username = NEW.name WHERE username = OLD.name",
action: "UPDATE",
targetTable: "users",
functionName: "updateuserdetails_function",
},
{
code: "DELETE FROM usersdetails WHERE username = OLD.name",
action: "DELETE",
targetTable: "users",
triggerName: "deleteuserdetails_action"
}],
scriptsOpts: {
extensive: false
},
restrict: false
})
console.log(create) // Return rows effects
} catch (e) {
console.log(e) // thow Error
}
})()
The codes above are directed to actions in the "users" table
TriggersPG.CreateTriggers({
pool: any, // new Pool()...
scripts: [{
code: string, // -> Code query
action: string, // -> INSERT|UPDATE|DELETE,
targetTable: string, // -> Table name corresponding to actions,
functionName: string, // -> Optional @default: "trigger_action_targetTable"
triggerName: string // -> Optional @default: "targetTable_identifytg_action"
}],
scriptsOpts: { // -> Optional
extensive: false // -> @default : false | If you want for your own complete query, put it as true
,
restrict: true // -> Optional @default : true | If true, your code cannot replace existing functions or triggers.
})
True:
{ code: `CREATE FUNCTION trigger_function_name() RETURNS event_trigger AS $$
BEGIN
RAISE NOTICE 'funtion: % %', tg_event, tg_tag;
END;
$$ LANGUAGE plpgsql;` ...}
False:
{ code: "INSERT INTO usersdetails (username) VALUES (NEW.name)" ...}
When the value is false your code is embedded in a ready-to-run query
Now you can customize your trigger's function name and tag!
scripts: [{
code: "INSERT INTO usersdetails (username) VALUES (NEW.name)",
action: "INSERT",
targetTable: "users",
functionName: "mytiggerinsert_function",
triggerName: "mytiggerinsert_identifier"
}],