pgsqltriggers-alternative
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

pgsqltriggers-alternative

Create triggers for your Postgres database in a simple and fast way.

NPM

Observe

  • 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!

Installation

First you need to install pg package: npm i pg

Now you can install: npm i pgsqltriggers-alternative

In practice:

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

Payload:

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.
  })

Extensive examples

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

triggerName and FunctionName examples

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"
      }],

And the result is:

Package Sidebar

Install

npm i pgsqltriggers-alternative

Weekly Downloads

1

Version

0.2.1

License

MIT

Unpacked Size

60.8 kB

Total Files

20

Last publish

Collaborators

  • eduhcastro19