sanity-generator
Advanced tools
Comparing version 0.0.2 to 0.1.0
{ | ||
"name": "sanity-generator", | ||
"version": "0.0.2", | ||
"main": "./build/cli.js", | ||
"typings": "./build/cli.d.ts", | ||
"version": "0.1.0", | ||
"main": "build/index.js", | ||
"typings": "build/index.d.ts", | ||
"files": [ | ||
"build" | ||
], | ||
"bin": { | ||
"sanity-generator": "build/cli.js" | ||
"sanity-generator": "build/cli/main.js", | ||
"sg": "build/cli/main.js" | ||
}, | ||
"scripts": { | ||
"dev": "npx nodemon --exec ts-node -- src/cli --config ./playground/sanity-generator.config.ts", | ||
"dev": "npx nodemon --ignore playground/sanity-generator/ --exec ts-node -- src/cli/main generate --config ./playground/sanity-generator.config.ts", | ||
"dev:sanity": "cd ./playground/sanity && sanity dev", | ||
"build": "rimraf ./build && tsc", | ||
"preview": "node build/cli.js --config ./playground/sanity-generator.config.ts" | ||
"build": "rimraf ./build && tsc && cp -r src/static build/static", | ||
"preview": "node build/cli/main.js generate --config ./playground/sanity-generator.config.ts" | ||
}, | ||
"devDependencies": { | ||
"groqfmt-nodejs": "^0.0.4", | ||
"prettier": "^3.0.2", | ||
"prettier-plugin-groq": "^0.2.5", | ||
"rimraf": "^5.0.1", | ||
"@types/node": "^20.5.9", | ||
"tslib": "^2.6.2", | ||
@@ -24,3 +25,9 @@ "typescript": "^5.2.2" | ||
"dependencies": { | ||
"rimraf": "^5.0.1", | ||
"commander": "^11.0.0", | ||
"consola": "^3.2.3", | ||
"defu": "^6.1.2", | ||
"groqfmt-nodejs": "^0.0.5", | ||
"prettier": "^3.0.3", | ||
"serialize-javascript": "^6.0.1", | ||
"ts-node": "^10.9.1", | ||
@@ -27,0 +34,0 @@ "tsconfig-paths": "^4.2.0" |
@@ -1,43 +0,25 @@ | ||
# Sanity Generator | ||
Sanity Generator is a codegen tool for [Sanity](https://www.sanity.io) to automatically generate GROQ queries from a schema perspective. | ||
Sanity Generator aims to simplify the process of defining schemas and queries when working with [Sanity](https://www.sanity.io/) CMS. | ||
**Note: Currently in beta. Be cautious when using it in production.** | ||
It is based on the assumption that a given document schema shape, is not that different from it's corresponding query shape. Moreover it assumes that if you need to reshape a specific field type, you probably would like to do this on all occurences of that type through out all queries. | ||
## Minimal Configuration | ||
Certainly, this can be done with simple exporting, importing and composing of template literals. But this is quite repetitivie, and error prone. Sanity Generator is a CLI tool that aims to automate this while still providing all flexiblities of GROQ. | ||
To get started, define your document schemas and queries like this: | ||
```TypeScript | ||
// sanity-generator.config.ts | ||
export default createConfig({ | ||
schemas: { | ||
page: pageSchema, | ||
}, | ||
queries: { | ||
getPages: ({ schemas }) => /* groq */ ` | ||
*[_type == "${schemas.page.name}"] { | ||
${schemas.page.projection} | ||
} | ||
`, | ||
}, | ||
}); | ||
``` | ||
**Disclaimer: This is still Beta. Use with caution** | ||
When you run `$ npx sanity-generator`, it creates queries with a spread operator to return all fields. | ||
## Basic Usage | ||
```TypeScript | ||
// sanity-generator/queries/getPages.ts | ||
export const getPages = /* groq */ ` | ||
*[_type == "page"] { | ||
... | ||
} | ||
` | ||
``` | ||
Install from npm: | ||
## Resolver for Custom Types (aka. the actual use case) | ||
`$ npm install sanity-generator --save-dev` | ||
Define resolvers for field types from your Sanity schema. These resolvers are functions that return custom GROQ projections: | ||
`$ yarn add sanity-generator --dev` | ||
Create a config file: | ||
`$ npx sanity-generator init` | ||
Customize the config: | ||
```TypeScript | ||
@@ -47,6 +29,9 @@ // sanity-generator.config.ts | ||
schemas: { | ||
// All sanity document schemas you want to query go here. | ||
// The object key is for later referencing. | ||
page: pageSchema, | ||
}, | ||
resolvers: { | ||
// Resolve all field types of "localString" with this projection... | ||
// All field types you want to apply a custom GROQ query go here. | ||
// The object key must match a field type. | ||
localeString: (name: string) => /* groq */ ` | ||
@@ -57,2 +42,3 @@ "${name}": coalesce(${name}[$lang], ${name}.en) | ||
queries: { | ||
// Every property in here will be exported as a GROQ query. The function recieves a single argument that is an object with all processed schemas. Here you can get the projection with all resolvers applied, and for convinece also the schema name. | ||
getPages: ({ schemas }) => /* groq */ ` | ||
@@ -67,6 +53,19 @@ *[_type == "${schemas.page.name}"] { | ||
If you run `$ npx sanity-generator` now, it expands the projection to transform all types with their associated resolver: | ||
## How it works | ||
Sanity Generator simple traverses all branches of the document schema. If a branch holds no types that have a corresponding resolver, it uses the spread operator (`...`). If a branch holds a type that should be resolved differently, it writes the corresponding projections just as far as needed. | ||
Here is an example with the same query, without and with custom resolver. (Both with option `inlineResolver: true`) | ||
```TypeScript | ||
// sanity-generator/queries/getPages.ts | ||
// Generated query with no resolver | ||
export const getPages = /* groq */ ` | ||
*[_type == "page"] { | ||
... | ||
} | ||
` | ||
``` | ||
```TypeScript | ||
// Same query, with resolver | ||
export const getPages = /* groq */ ` | ||
@@ -92,1 +91,29 @@ *[_type == "page"] { | ||
``` | ||
## Options | ||
### CLI | ||
`$ npx sanity-generator` | ||
or | ||
`$ npx sg` | ||
| Command | Option | Description | | ||
| ---------- | ------------------ | ---------------------------------------------------------------------------------- | | ||
| `generate` | | Run the generator assuming the config is here: `~/sanity-generator.config.ts` | | ||
| `generate` | `--config` `-c` | Specify a path to the config file | | ||
| `init` | | Create a default config | | ||
### Config | ||
The module exports a `createConfig` function to provide better typesupport for the configuration object. | ||
| Property | Default | Description | | ||
| --------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| schemas | {} | See basic usage | | ||
| resolvers | {} | See basic usage | | ||
| queries | {} | See basic usage | | ||
| outPath | './sanity-generator` | Path to the destination folder for the queries | | ||
| inlineResolvers | false | By default, resolvers are imported as a function into the final query. Setting this to false, will inline the resolvers as a string into the query. | |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
3
116
30148
9
23
490
3
1
+ Addedconsola@^3.2.3
+ Addeddefu@^6.1.2
+ Addedgroqfmt-nodejs@^0.0.5
+ Addedprettier@^3.0.3
+ Addedrimraf@^5.0.1
+ Addedserialize-javascript@^6.0.1
+ Added@isaacs/cliui@8.0.2(transitive)
+ Added@pkgjs/parseargs@0.11.0(transitive)
+ Addedansi-regex@5.0.16.1.0(transitive)
+ Addedansi-styles@4.3.06.2.1(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@2.0.1(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedconsola@3.4.0(transitive)
+ Addedcross-spawn@7.0.6(transitive)
+ Addeddefu@6.1.4(transitive)
+ Addedeastasianwidth@0.2.0(transitive)
+ Addedemoji-regex@8.0.09.2.2(transitive)
+ Addedforeground-child@3.3.0(transitive)
+ Addedglob@10.4.5(transitive)
+ Addedgroqfmt-nodejs@0.0.5(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedjackspeak@3.4.3(transitive)
+ Addedlru-cache@10.4.3(transitive)
+ Addedminimatch@9.0.5(transitive)
+ Addedminipass@7.1.2(transitive)
+ Addedmkdirp@3.0.1(transitive)
+ Addedpackage-json-from-dist@1.0.1(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedpath-scurry@1.11.1(transitive)
+ Addedprettier@3.5.0(transitive)
+ Addedrandombytes@2.1.0(transitive)
+ Addedrimraf@5.0.10(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedserialize-javascript@6.0.2(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedsignal-exit@4.1.0(transitive)
+ Addedstring-width@4.2.35.1.2(transitive)
+ Addedstrip-ansi@6.0.17.1.0(transitive)
+ Addedwhich@2.0.2(transitive)
+ Addedwrap-ansi@7.0.08.1.0(transitive)