Skip to content

Commit

Permalink
Generate ON DELETE CASCADE for { many }
Browse files Browse the repository at this point in the history
Closes: #191

PR-URL: #196
  • Loading branch information
tshemsedinov committed Aug 6, 2021
1 parent 0263488 commit f90cc46
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Use `options.logger` and global `console` if no `options.console` for backward
compatibility, fallback will be better than crash
- Add `{ delete: 'cascade' }` to generate `ON DELETE CASCADE` for `{ many }`

## [1.2.0][] - 2021-08-04

Expand Down
10 changes: 5 additions & 5 deletions lib/pg.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ const createIndex = (entityName, indexName, def, entity) => {
};

const createMany = (entityName, { many }, model) => {
const fromEntity = toLowerCamel(entityName);
const toEntity = toLowerCamel(many);
const from = toLowerCamel(entityName);
const to = toLowerCamel(many);
const crossName = entityName + many;
const crossReference = {
[fromEntity]: { name: fromEntity, type: entityName, required: true },
[toEntity]: { name: toEntity, type: many, required: true },
[crossName]: { name: crossName, primary: [fromEntity, toEntity] },
[from]: { name: from, type: entityName, delete: 'cascade', required: true },
[to]: { name: to, type: many, delete: 'cascade', required: true },
[crossName]: { name: crossName, primary: [from, to] },
};
const entity = new Schema(crossName, crossReference);
model.entities.set(crossName, entity);
Expand Down

0 comments on commit f90cc46

Please sign in to comment.