Gathering detailed insights and metrics for graphql-sequelize-auto-generator
Gathering detailed insights and metrics for graphql-sequelize-auto-generator
Gathering detailed insights and metrics for graphql-sequelize-auto-generator
Gathering detailed insights and metrics for graphql-sequelize-auto-generator
npm install graphql-sequelize-auto-generator
Typescript
Module System
Node Version
NPM Version
TypeScript (93.23%)
JavaScript (6.77%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
19 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Jan 13, 2024
Latest Version
1.0.22
Package Id
graphql-sequelize-auto-generator@1.0.22
Unpacked Size
236.52 kB
Size
39.88 kB
File Count
44
NPM Version
10.2.4
Node Version
20.11.0
Published on
Feb 25, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
9
Config based GraphQL schema generator.
Generates GraphQL CRUD queries based on database schema and configuration. It also provides a service that will query the database based on the GraphQL query.
In order to use this tool, you must know how to use
Install package graphql-sequelize-auto-generator
$npm install graphql-sequelize-auto-generator
Follow the config tutorial.
1{ 2 "gsa": "gsa -c ./gsa.config.ts" 3}
$npm run gsa
Follow the code usage tutorial.
Create a gsa.config.ts
file in your project root.
gsa.config.ts
1import { GsaConfig } from "graphql-sequelize-auto-generator"; 2import { FilterOperators } from "graphql-sequelize-auto-generator/build/cli/enums"; 3 4const config: GsaConfig = { 5 // Generated sequelize models and codes dir 6 codeFilesDir: "./src/generated/gsa", 7 // Generated GraphQL schema and non code files 8 nonCodeFilesDir: "./src/nonCode/generated/gsa", 9 // Custom GraphQL schema 10 additionalGqlSchemas: ["./src/nonCode/schema.gql"], 11 // Database host 12 host: process.env.DB_HOST!, 13 // Database port 14 port: parseInt(process.env.DB_PORT!), 15 // Database name 16 database: process.env.DB_NAME!, 17 // Database username 18 username: process.env.DB_USER!, 19 // Database password 20 password: process.env.DB_PASS!, 21 // Database type 22 dialect: "postgres", 23 // Tables to skip generating sequelize models 24 skipTables: ["public.sequelize_migrations"], 25 // See Table Config section 26 tableConfigs: [] 27}
1{ 2 // Name of the database table including the schema 3 name: "public.users", 4 // You can create multiple alias of one table such as User, PublicUser, PrivateUser 5 aliasConfigs: [ 6 { 7 // Name of the GraphQL type that will be generated 8 name: "User", 9 // Read 10 get: { 11 input: { 12 // Exposed filter fields 13 filterFields: [ 14 { // Primary field filter 15 // Name of the field from the generated sequelize model in <codeFilesDir>/sequelize/Users.ts 16 name: "id", 17 // Allowed operators 18 operators: [FilterOperators.Equal], 19 }, 20 { 21 name: "username", 22 operators: [FilterOperators.Like], 23 }, 24 { // Associated field filter 25 // This is an associated table 26 name: "userUserRoles", 27 filterFields: [ 28 { 29 // Associated table of the one above 30 name: "role", 31 filterFields: [ 32 { 33 name: "rolePermissions", 34 filterFields: [ 35 { 36 name: "permission", 37 filterFields: [ 38 { 39 // The actual field that will be filtered for this nested filter 40 name: "id", 41 operators: [FilterOperators.Equal], 42 }, 43 { 44 name: "name", 45 operators: [FilterOperators.Like], 46 }, 47 ], 48 }, 49 ], 50 }, 51 ], 52 }, 53 ], 54 }, 55 ], 56 // Exposed sort fields 57 sortByFields: [ 58 { // Simple sort field 59 name: "id", 60 }, 61 { 62 name: "username", 63 }, 64 { // Associated sort field 65 name: "userUserRoles", 66 sortByFields: [{ 67 name: "role", 68 sortByFields: [{ 69 // The actual field that will be sorted for this nested sorting 70 name: "code", 71 }, { 72 name: "name", 73 }], 74 }], 75 }, 76 ], 77 }, 78 output: { 79 // Exposed primary output fields 80 primaryFields: { 81 include: ["id", "username"], 82 }, 83 // Exposed associated output fields 84 associatedFields: [ 85 { 86 name: "userUserRoles", 87 // The name of the GraphQL type, this is defined in the aliasConfigs array element 88 aliasName: "UserRole", 89 }, 90 ], 91 }, 92 }, 93 // Create 94 add: { 95 input: { 96 // Exposed primary input fields when adding 97 primaryFields: { 98 include: ["username", "password"], 99 }, 100 // Exposed primary output fields when adding 101 associatedFields: [ 102 { 103 name: "userUserRoles", 104 isOptional: false, 105 input: { 106 primaryFields: { 107 isOptional: false, 108 include: ["roleId"], 109 }, 110 }, 111 }, 112 ], 113 }, 114 output: { 115 primaryFields: { 116 include: ["id"], 117 }, 118 }, 119 }, 120 // Update 121 edit: { 122 input: { 123 primaryFields: { 124 include: ["firstName", "lastName"], 125 }, 126 associatedFields: [ 127 { 128 name: "userUserRoles", 129 isOptional: false, 130 input: { 131 primaryFields: { 132 include: ["roleId"], 133 }, 134 }, 135 }, 136 ], 137 }, 138 }, 139 // Delete 140 remove: { 141 // Generates remove mutation 142 isEnabled: true, 143 }, 144 }, 145 ], 146},
Creating a gsa
object.
1import { GraphQLSequelizeAuto } from "graphql-sequelize-auto-generator"; 2import { initModels } from "../generated/gsa/sequelize/init-models"; 3 4initModels(sequelize); // Your sequelize object 5 6const gsa = new GraphQLSequelizeAuto( 7 sequelize, 8 path.resolve(__dirname, "./nonCode/generated/gsa/mappings.generated.json"), // This is generated by gsa in the nonCode dir 9);
Using the gsa
object in the resolver.
1import { 2 GetUsersOutput, 3 QueryGetUsersArgs, 4} from "../generated/gsa/graphql.generated"; 5 6// This is using Apollo GraphQL server resolver. But as long as the library you're using has GraphQLResolveInfo then it should work 7getUsers: async ( 8 args: QueryGetUsersArgs, 9 _context: unknown, 10 graphqlResolveInfo: GraphQLResolveInfo, 11): Promise<GetUsersOutput> => { 12 return gsa.getAll(args, graphqlResolveInfo); 13},
You can resolve the generated scalars yourself or use gsaResolvers
like in the example
.
import { gsaResolvers } from "graphql-sequelize-auto-generator";
See example project at example
.
See a full config example at gsa.config.ts
.
See the generated GraphQL schema at schema.generated.gql
.
The goal is to support all databases that sequelize
supports.
Currently only tested PostgreSQL
and SQL Server
.
No vulnerabilities found.
No security vulnerabilities found.