Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Naming prefix option #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,35 @@ View the [postgraphile docs](https://www.graphile.org/postgraphile/extending/#lo

## Plugin Options

This plugin respects the default option to disable mutations all together via ```graphileBuildOptions```.
```enabled``` - Boolean. Specifiies if plugin is enabled or disabled for all tables. Enabled by default.
```ignore``` - Array of table names. Specifies which tables will be enabled if enabled prop is equal to false or which tables will be disabled if enabled prop is equal to true.
```prefix``` - String. Specifies whether to add a prefix to names of mutations and types generated by this plugin. By default no
prefix is added.

```js
postgraphile(pgConfig, schema, {
graphileBuildOptions: {
pgDisableDefaultMutations: true
}
multipleMutationsPluginOptions: {
// enabled by default
enabled: true,
ignore: ["tableName1", "tableName2"],
prefix: "mn",
},
},
});
```

## Smart Comments

You must use smart comments to enable the many create, update, and delete mutations for a table, since they are not enabled by default to prevent crowding with the other autogenerated postgraphile default mutations. The single tag ```@mncud``` is all that's needed.

```sql
comment on table public."Test" is
E'@mncud\n The test table is just for showing this example with comments.';
```

## Usage

The plugin creates new mutations that allow you to batch create, update, and delete items from a given table. It works with primary keys for updates and deletes using the input patch that postgraphile generates. All creates, updates, and deletes have scoped names with "mn" in front of the mutation name to prevent conflicts with other mutations.

### Creates
```mnCreateTest``` would be an example mutation name, and we'll say it has attributes of test1 (a boolean), and name (a varchar). You'll see the required input has the clientMutationId and also a field called ```mnTest```, where ```mnTest``` will take an array of items that use the table input type. Since it uses the table input type, the required items are all listed as expected. When creating records, any attributes left off will have their values set to ```default```.
```createTests``` would be an example mutation name, and we'll say it has attributes of test1 (a boolean), and name (a varchar). You'll see the required input has the clientMutationId and also a field called ```tests```, where ```tests``` will take an array of items that use the table input type. Since it uses the table input type, the required items are all listed as expected. When creating records, any attributes left off will have their values set to ```default```.

### Updates
```mnUpdateTestByName``` would be the update example name, assuming the name is the primary key. Updates have a required input with the clientMutatationId and a patch. The patch field accepts an array of table patch items. You ***MUST*** provide the primary key within the patch items b/c that is what's used in the where clause to update the correct row(s). Attributes that are not provided in the list of provided values, will not be updated. With that said, you can update different attributes in one record and leave them off in another and it will update both as expected.
```updateTestsByName``` would be the update example name, assuming the name is the primary key. Updates have a required input with the clientMutatationId and a patch. The patch field accepts an array of table patch items. You ***MUST*** provide the primary key within the patch items b/c that is what's used in the where clause to update the correct row(s). Attributes that are not provided in the list of provided values, will not be updated. With that said, you can update different attributes in one record and leave them off in another and it will update both as expected.

### Deletes
```mnDeleteTestByName``` would be the delete example name. Deletes have a required input with the clientMutationId and a patch. The patch field accepts an array of table patch items, but only the primary key items are used. You ***MUST*** provide the primary key(s) within the patch items b/c that is what's used in the where clause to delete the correct row(s).
```deleteTestsByName``` would be the delete example name. Deletes have a required input with the clientMutationId and a patch. The patch field accepts an array of table patch items, but only the primary key items are used. You ***MUST*** provide the primary key(s) within the patch items b/c that is what's used in the where clause to delete the correct row(s).

## Contribute

Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@
},
"homepage": "https://github.com/tjmoses/postgraphile-plugin-many-create-update-delete#readme",
"dependencies": {
"postgraphile": "^4.7.0",
"graphile-utils": "^4.5.6"
"graphile-utils": "^4.5.6",
"lodash": "^4.17.20",
"postgraphile": "^4.7.0"
},
"devDependencies": {
"@types/lodash": "^4.14.168",
"@types/nanographql": "^2.0.1",
"@types/pg": "^7.14.4",
"@typescript-eslint/parser": "^3.6.0",
"graphile-build": "^4.7.0",
"prettier-standard": "^16.4.1",
"rimraf": "^3.0.0",
"typescript": "^3.9.6",
"prettier-standard": "^16.4.1"
"typescript": "^3.9.6"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
Expand Down
58 changes: 58 additions & 0 deletions src/ManyInflectionPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as T from "./pluginTypes";

// TODO move all namings here
const PostgraphileManyInflectionPlugin: T.Plugin = (builder) => {
builder.hook("inflection", (inflection, build) =>
build.extend(inflection, {
_makeManyFieldNameWithConstraints({
detailedKeys,
action,
constraintType,
tableName,
}: {
detailedKeys: string[];
tableName: string;
constraintType: string;
action: string;
}) {
const pluralTableName = this.pluralize(tableName);
if (constraintType === "p") {
return this.camelCase(`${action}-${pluralTableName}`);
} else {
return this.camelCase(
`${action}-${pluralTableName}-by-${detailedKeys
.map((key) => this.column(key))
.join("-and-")}`
);
}
},

updateManyByKeys(detailedKeys, table, constraint) {
// respect pg-simplify-inflector plugin
if (constraint.tags.updateFieldName) {
return constraint.tags.updateFieldName;
}
return this._makeManyFieldNameWithConstraints({
detailedKeys,
action: "update",
constraintType: constraint.type,
tableName: this._singularizedTableName(table),
});
},
deleteManyByKeys(detailedKeys, table, constraint) {
// respect pg-simplify-inflector plugin
if (constraint.tags.deleteFieldName) {
return constraint.tags.deleteFieldName;
}
return this._makeManyFieldNameWithConstraints({
detailedKeys,
action: "delete",
constraintType: constraint.type,
tableName: this._singularizedTableName(table),
});
},
})
);
};

export default PostgraphileManyInflectionPlugin;
Loading