# pgschema-deletecascade

> uses a pgschematojson object to build a cascade delete batch query for any row

Latest version **0.0.0** (published 2015-05-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install pgschema-deletecascade
pnpm add pgschema-deletecascade
yarn add pgschema-deletecascade
bun add pgschema-deletecascade
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.0 |
| Published | 2015-05-24 |
| First published | 2015-05-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ryan Day |
| Maintainers | soldair |
| Keywords | pg, postgres, database, delete, cascade |

## Links

- npm: https://www.npmjs.com/package/pgschema-deletecascade
- Repository: https://github.com/soldair/pgschema-deletecascade
- Issues: https://github.com/soldair/pgschema-deletecascade/issues
- npm.io page: https://npm.io/package/pgschema-deletecascade

## Recent versions

- 0.0.0 (latest) — 2015-05-24

## README

# pgschema-deletecascade

uses a pgschematojson object to build a cascade delete batch query for any row.

yes i know postgresql has [delete cascade](http://www.postgresql.org/docs/8.2/static/ddl-constraints.html). it might not be enabled for some reason. if you have this use case use this =)

the output sql string should be included in a transaction otherwise inconsistency problems will abound.

## example

```js
var del = require('pgschema-deletecascade')
var schema = require("./schema.json")

// now lets delete the row from the "main" table with primary key 1

var res = del(schema,"main",1)

// returns an array with the arguments that you would normally pass to pg query
// [query,arguments]

console.log(">query:")
process.stdout.write(res[0]+"\n")
console.log(">args")
console.log(res[1])

```
which outputs

```
>query:
delete from sub_sub where sub_id in (select id from sub where main_id in (?));
delete from sub where main_id in (?);
delete from main where id=?;
>args
[ 1, 1, 1 ]
```

in this case the schema.json looks like this

```js
var schema = {
  main:{
    id:{
      pkey:true,
      dependents:[["sub","main_id"]]
    }
  },
  sub:{
    id:{
      pkey:true,
      dependents:[["sub_sub","sub_id"]]
    },
    main_id:{
      fk:["main","id"]
    }
  },
  sub_sub:{
    sub_id:{
      fk:["sub","id"]
    }
  }
}
```

## api

### module.exports(schema,tableName,primaryKeyValue)
- schema, is an object as produced from your db schema from the module [pgschematojson](https://www.npmjs.com/package/pgschematojson)
- tableName, the table you would like to delete from
- primaryKeyvalue, the id of the row you want to delete

---
_Source: https://npm.io/package/pgschema-deletecascade · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
