Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/eduhcastro/pgsqltriggers-alternative

A quick and simple way to create your triggers to PostgreSQL in Node.js
https://github.com/eduhcastro/pgsqltriggers-alternative

Last synced: 3 days ago
JSON representation

A quick and simple way to create your triggers to PostgreSQL in Node.js

Awesome Lists containing this project

README

        

# pgsqltriggers-alternative
Create triggers for your Postgres database in a simple and fast way.

[![NPM](https://nodei.co/npm/pgsqltriggers-alternative.png)](https://nodei.co/npm/pgsqltriggers-alternative/)

#

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:

```javascript
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:

```javascript
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:
```sql
{ code: `CREATE FUNCTION trigger_function_name() RETURNS event_trigger AS $$
BEGIN
RAISE NOTICE 'funtion: % %', tg_event, tg_tag;
END;
$$ LANGUAGE plpgsql;` ...}
```
False:
```javascript
{ 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!
```javascript
scripts: [{
code: "INSERT INTO usersdetails (username) VALUES (NEW.name)",
action: "INSERT",
targetTable: "users",
functionName: "mytiggerinsert_function",
triggerName: "mytiggerinsert_identifier"
}],
```
And the result is: