npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-sequelize-auto-generator

v1.0.22

Published

Config based GraphQL schema generator.

Downloads

32

Readme

Graphql Sequelize Auto Generator (GSA)

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.

Prerequisite Knowledge

In order to use this tool, you must know how to use

Peer Dependencies

See peer dependencies

How To Use

npm package

Install package graphql-sequelize-auto-generator

$npm install graphql-sequelize-auto-generator

Config

Follow the config tutorial.

Scripts

{
  "gsa": "gsa -c ./gsa.config.ts"
}

Generate

$npm run gsa

Code Usage

Follow the code usage tutorial.

Config

Create a gsa.config.ts file in your project root.

gsa.config.ts

import { GsaConfig } from "graphql-sequelize-auto-generator";
import { FilterOperators } from "graphql-sequelize-auto-generator/build/cli/enums";

const config: GsaConfig = {
	// Generated sequelize models and codes dir
	codeFilesDir: "./src/generated/gsa",
	// Generated GraphQL schema and non code files
	nonCodeFilesDir: "./src/nonCode/generated/gsa",
	// Custom GraphQL schema
	additionalGqlSchemas: ["./src/nonCode/schema.gql"],
	// Database host
	host: process.env.DB_HOST!,
	// Database port
	port: parseInt(process.env.DB_PORT!),
	// Database name
	database: process.env.DB_NAME!,
	// Database username
	username: process.env.DB_USER!,
	// Database password
	password: process.env.DB_PASS!,
	// Database type
	dialect: "postgres",
	// Tables to skip generating sequelize models
	skipTables: ["public.sequelize_migrations"],
	// See Table Config section
	tableConfigs: []
}

Table Config

{
	// Name of the database table including the schema
	name: "public.users",
	// You can create multiple alias of one table such as User, PublicUser, PrivateUser
	aliasConfigs: [
		{
			// Name of the GraphQL type that will be generated
			name: "User",
			// Read
			get: {
				input: {
					// Exposed filter fields
					filterFields: [
						{	// Primary field filter
							// Name of the field from the generated sequelize model in <codeFilesDir>/sequelize/Users.ts
							name: "id",
							// Allowed operators
							operators: [FilterOperators.Equal],
						},
						{
							name: "username",
							operators: [FilterOperators.Like],
						},
						{	// Associated field filter
							// This is an associated table
							name: "userUserRoles",
							filterFields: [
								{
									// Associated table of the one above
									name: "role",
									filterFields: [
										{
											name: "rolePermissions",
											filterFields: [
												{
													name: "permission",
													filterFields: [
														{
															// The actual field that will be filtered for this nested filter
															name: "id",
															operators: [FilterOperators.Equal],
														},
														{
															name: "name",
															operators: [FilterOperators.Like],
														},
													],
												},
											],
										},
									],
								},
							],
						},
					],
					// Exposed sort fields
					sortByFields: [
						{	// Simple sort field
							name: "id",
						},
						{
							name: "username",
						},
						{	// Associated sort field
							name: "userUserRoles",
							sortByFields: [{
								name: "role",
								sortByFields: [{
									// The actual field that will be sorted for this nested sorting
									name: "code",
								}, {
									name: "name",
								}],
							}],
						},
					],
				},
				output: {
					// Exposed primary output fields
					primaryFields: {
						include: ["id", "username"],
					},
					// Exposed associated output fields
					associatedFields: [
						{
							name: "userUserRoles",
							// The name of the GraphQL type, this is defined in the aliasConfigs array element
							aliasName: "UserRole",
						},
					],
				},
			},
			// Create
			add: {
				input: {
					// Exposed primary input fields when adding
					primaryFields: {
						include: ["username", "password"],
					},
					// Exposed primary output fields when adding
					associatedFields: [
						{
							name: "userUserRoles",
							isOptional: false,
							input: {
								primaryFields: {
									isOptional: false,
									include: ["roleId"],
								},
							},
						},
					],
				},
				output: {
					primaryFields: {
						include: ["id"],
					},
				},
			},
			// Update
			edit: {
				input: {
					primaryFields: {
						include: ["firstName", "lastName"],
					},
					associatedFields: [
						{
							name: "userUserRoles",
							isOptional: false,
							input: {
								primaryFields: {
									include: ["roleId"],
								},
							},
						},
					],
				},
			},
			// Delete
			remove: {
				// Generates remove mutation
				isEnabled: true,
			},
		},
	],
},

Code Usage

Creating a gsa object.

import { GraphQLSequelizeAuto } from "graphql-sequelize-auto-generator";
import { initModels } from "../generated/gsa/sequelize/init-models";

initModels(sequelize);  // Your sequelize object

const gsa = new GraphQLSequelizeAuto(
	sequelize,
	path.resolve(__dirname, "./nonCode/generated/gsa/mappings.generated.json"), // This is generated by gsa in the nonCode dir
);

Using the gsa object in the resolver.

import {
	GetUsersOutput,
	QueryGetUsersArgs,
} from "../generated/gsa/graphql.generated";

// This is using Apollo GraphQL server resolver. But as long as the library you're using has GraphQLResolveInfo then it should work
getUsers: async (
	args: QueryGetUsersArgs,
	_context: unknown,
	graphqlResolveInfo: GraphQLResolveInfo,
): Promise<GetUsersOutput> => {
	return gsa.getAll(args, graphqlResolveInfo);
},

Scalar Resolvers

You can resolve the generated scalars yourself or use gsaResolvers like in the example.

import { gsaResolvers } from "graphql-sequelize-auto-generator";

Example Project

See example project at example.

See a full config example at gsa.config.ts.

See the generated GraphQL schema at schema.generated.gql.

Database Support

The goal is to support all databases that sequelize supports.

Currently only tested PostgreSQL and SQL Server.