Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/eduhcastro/pgsqltriggers-alternative
- Owner: eduhcastro
- License: mit
- Created: 2021-08-07T04:56:46.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-07-11T23:42:48.000Z (about 1 year ago)
- Last Synced: 2024-03-15T08:23:11.455Z (7 months ago)
- Language: TypeScript
- Size: 87.9 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
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: