Skip to content
Draft
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
6 changes: 6 additions & 0 deletions .oxlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
"rules": {
"esm-imports/node-esm-imports": "off"
}
},
{
"files": ["examples/script/**/*"],
"rules": {
"esm-imports/node-esm-imports": "error"
}
}
],
"ignorePatterns": ["tests/test-projects/live-reloading/schemas/syntax-error.js"]
Expand Down
6 changes: 3 additions & 3 deletions docs/content/docs/context/get-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ Using the `getContext` function does not use the typical Keystone entry point -

```typescript
import { getContext } from '@keystone-6/core/context'
import config from './keystone'
import * as PrismaModule from './generated/prisma/client'
import config from './keystone.ts'
import * as PrismaModule from './generated/prisma/client.ts'

const context = getContext(config, PrismaModule)

// ... whatever you need
```

{% hint kind="tip" %}
For inspiration, see [the script example project](https://github.com/keystonejs/keystone/tree/main/examples/script), and how we use [`tsx`](https://github.com/esbuild-kit/tsx) to seed the database with some data.
For inspiration, see [the script example project](https://github.com/keystonejs/keystone/tree/main/examples/script), which uses Node.js's built-in TypeScript support to seed the database with some data. Running the example requires Node.js 22.18 or newer.
{% /hint %}

A context created in this way does not have an implicit `session`, nor is it a `sudo()` context.
Expand Down
10 changes: 10 additions & 0 deletions examples/script/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This project demonstrates how to write code to interact with Keystone without us

The `getContext` function does not adhere to the typical Keystone entry-point and requires that your Prisma schema has been previously built by Keystone.

This example requires Node.js 22.18 or newer because it uses Node.js's built-in TypeScript support.

## Instructions

To run this project, clone the Keystone repository locally, run `pnpm install` at the root of this repository, then navigate to this directory and run:
Expand All @@ -17,4 +19,12 @@ You can use the Admin UI to create items in your database.

You can also access a GraphQL Playground at [localhost:3000/api/graphql](http://localhost:3000/api/graphql), which allows you to directly run GraphQL queries and mutations.

Once the generated Prisma client and Keystone types exist, run the standalone script in another terminal:

```shell
pnpm go
```

You can generate those artifacts without starting the development server by running `pnpm keystone postinstall` first.

Congratulations, you're now up and running with Keystone! 🚀
2 changes: 1 addition & 1 deletion examples/script/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3'
import { config, list } from '@keystone-6/core'
import { allowAll } from '@keystone-6/core/access'
import { text, timestamp } from '@keystone-6/core/fields'
import type { TypeInfo } from './generated/keystone/types'
import type { TypeInfo } from './generated/keystone/types.ts'

export default config<TypeInfo>({
db: {
Expand Down
4 changes: 2 additions & 2 deletions examples/script/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
"name": "@keystone-6/example-script",
"version": null,
"private": true,
"type": "module",
"license": "MIT",
"scripts": {
"dev": "keystone dev",
"start": "keystone start",
"build": "keystone build",
"check": "keystone postinstall",
"go": "tsx script.ts"
"go": "node script.ts"
},
"dependencies": {
"@keystone-6/core": "workspace:^",
Expand All @@ -21,7 +22,6 @@
},
"devDependencies": {
"prisma": "catalog:",
"tsx": "^4.0.0",
"typescript": "catalog:"
}
}
4 changes: 2 additions & 2 deletions examples/script/script.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getContext } from '@keystone-6/core/context'
import config from './keystone'
import config from './keystone.ts'

import * as PrismaModule from './generated/prisma/client'
import * as PrismaModule from './generated/prisma/client.ts'

async function main() {
const context = getContext(config, PrismaModule)
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading