• Stars
    star
    1,173
  • Rank 39,852 (Top 0.8 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿš‚ Engine components of Prisma ORM

Prisma Engines

Query Engine Schema Engine + sql_schema_describer Cargo docs

This repository contains a collection of engines that power the core stack for Prisma, most prominently Prisma Client and Prisma Migrate.

If you're looking for how to install Prisma or any of the engines, the Getting Started guide might be useful.

This document describes some of the internals of the engines, and how to build and test them.

What's in this repository

This repository contains four engines:

  • Query engine, used by the client to run database queries from Prisma Client
  • Schema engine, used to create and run migrations and introspection
  • Prisma Format, used to format prisma files

Additionally, the psl (Prisma Schema Language) is the library that defines how the language looks like, how it's parsed, etc.

You'll also find:

  • libs, for various (small) libraries such as macros, user facing errors, various connector/database-specific libraries, etc.
  • a docker-compose.yml file that's helpful for running tests and bringing up containers for various databases
  • a flake.nix file for bringing up all dependencies and making it easy to build the code in this repository (the use of this file and nix is entirely optional, but can be a good and easy way to get started)
  • an .envrc file to make it easier to set everything up, including the nix shell

Documentation

The API docs (cargo doc) are published on our fabulous repo page.

Building Prisma Engines

Prerequisites:

  • Installed the latest stable version of the Rust toolchain. You can get the toolchain at rustup or the package manager of your choice.
  • Linux only: OpenSSL is required to be installed.
  • Installed direnv, then direnv allow on the repository root.
    • Make sure direnv is hooked into your shell
    • Alternatively: Load the defined environment in ./.envrc manually in your shell.
  • For m1 users: Install Protocol Buffers

Note for nix users: it should be enough to direnv allow. How to build:

To build all engines, simply execute cargo build on the repository root. This builds non-production debug binaries. If you want to build the optimized binaries in release mode, the command is cargo build --release.

Depending on how you invoked cargo in the previous step, you can find the compiled binaries inside the repository root in the target/debug (without --release) or target/release directories (with --release):

Prisma Component Path to Binary
Query Engine ./target/[debug|release]/query-engine
Schema Engine ./target/[debug|release]/schema-engine
Prisma Format ./target/[debug|release]/prisma-fmt

Prisma Schema Language

The Prisma Schema Language is a library which defines the data structures and parsing rules for prisma files, including the available database connectors. For more technical details, please check the library README.

The PSL is used throughout the schema engine, as well as prisma format. The DataModeL (DML), which is an annotated version of the PSL is also used as input for the query engine.

Query Engine

The Query Engine is how Prisma Client queries are executed. Here's a brief description of what it does:

  • takes as inputs an annotated version of the Prisma Schema file called the DataModeL (DML),
  • using the DML (specifically, the datasources and providers), it builds up a GraphQL model for queries and responses,
  • runs as a server listening for GraphQL queries,
  • it translates the queries to the respective native datasource(s) and returns GraphQL responses, and
  • handles all connections and communication with the native databases.

When used through Prisma Client, there are two ways for the Query Engine to be executed:

  • as a binary, downloaded during installation, launched at runtime; communication happens via HTTP (./query-engine/query-engine)
  • as a native, platform-specific Node.js addon; also downloaded during installation (./query-engine/query-engine-node-api)

Usage

You can also run the Query Engine as a stand-alone GraphQL server.

Warning: There is no guaranteed API stability. If using it on production please be aware the api and the query language can change any time.

Notable environment flags:

  • RUST_LOG_FORMAT=(devel|json) sets the log format. By default outputs json.
  • QE_LOG_LEVEL=(info|debug|trace) sets the log level for the Query Engine. If you need Query Graph debugging logs, set it to "trace"
  • FMT_SQL=1 enables logging formatted SQL queries
  • PRISMA_DML_PATH=[path_to_datamodel_file] should point to the datamodel file location. This or PRISMA_DML is required for the Query Engine to run.
  • PRISMA_DML=[base64_encoded_datamodel] an alternative way to provide a datamodel for the server.
  • RUST_BACKTRACE=(0|1) if set to 1, the error backtraces will be printed to the STDERR.
  • LOG_QUERIES=[anything] if set, the SQL queries will be written to the INFO log. Needs the right log level enabled to be seen from the terminal.
  • RUST_LOG=[filter] sets the filter for the logger. Can be either trace, debug, info, warning or error, that will output ALL logs from every crate from that level. The .envrc in this repo shows how to log different parts of the system in a more granular way.

Starting the Query Engine:

The engine can be started either with using the cargo build tool, or pre-building a binary and running it directly. If using cargo, replace whatever command that starts with ./query-engine with cargo run --bin query-engine --.

You can also pass --help to find out more options to run the engine.

Metrics

Running make show-metrics will start Prometheus and Grafana with a default metrics dashboard. Prometheus will scrape the /metrics endpoint to collect the engine's metrics

Navigate to http://localhost:3000 to view the Grafana dashboard.

Schema Engine

The Schema Engine does a couple of things:

  • creates new migrations by comparing the prisma file with the current state of the database, in order to bring the database in sync with the prisma file
  • run these migrations and keeps track of which migrations have been executed
  • (re-)generate a prisma schema file starting from a live database

The engine uses:

  • the prisma files, as the source of truth
  • the database it connects to, for diffing and running migrations, as well as keeping track of migrations in the _prisma_migrations table
  • the prisma/migrations directory which acts as a database of existing migrations

Prisma format

Prisma format can format prisma schema files. It also comes as a WASM module via a node package. You can read more here.

Debugging

When trying to debug code, here's a few things that might be useful:

  • use the language server; being able to go to definition and reason about code can make things a lot easier,
  • add dbg!() statements to validate code paths, inspect variables, etc.,
  • you can control the amount of logs you see, and where they come from using the RUST_LOG environment variable; see the documentation,
  • you can use the test-cli to test migration and introspection without having to go through the prisma npm package.

Testing

There are two test suites for the engines: Unit tests and integration tests.

  • Unit tests: They test internal functionality of individual crates and components.

    You can find them across the whole codebase, usually in ./tests folders at the root of modules. These tests can be executed via cargo test. Note that some of them will require the TEST_DATABASE_URL enviornment variable set up.

  • Integration tests: They run GraphQL queries against isolated instances of the Query Engine and asserts that the responses are correct.

    You can find them at ./query-engine/connector-test-kit-rs.

Set up & run tests:

Prerequisites:

  • Installed Rust toolchain.
  • Installed Docker.
  • Installed direnv, then direnv allow on the repository root.
    • Alternatively: Load the defined environment in ./.envrc manually in your shell.

Setup: There are helper make commands to set up a test environment for a specific database connector you want to test. The commands set up a container (if needed) and write the .test_config file, which is picked up by the integration tests:

  • make dev-mysql: MySQL 5.7
  • make dev-mysql8: MySQL 8
  • make dev-postgres: PostgreSQL 10
  • make dev-sqlite: SQLite
  • make dev-mongodb_5: MongoDB 5

*On windows: If not using WSL, make is not available and you should just see what your command does and do it manually. Basically this means editing the .test_config file and starting the needed Docker containers.

To actually get the tests working, read the contents of .envrc. Then Edit environment variables for your account from Windows settings, and add at least the correct values for the following variables:

  • WORKSPACE_ROOT should point to the root directory of prisma-engines project.
  • PRISMA_BINARY_PATH is usually %WORKSPACE_ROOT%\target\release\query-engine.exe.
  • SCHEMA_ENGINE_BINARY_PATH should be %WORKSPACE_ROOT%\target\release\schema-engine.exe.

Other variables may or may not be useful.

Run:

Run cargo test in the repository root.

Parallel rust-analyzer builds

When rust-analzyer runs cargo check it will lock the build directory and stop any cargo commands from running until it has completed. This makes the build process feel a lot longer. It is possible to avoid this by setting a different build path for rust-analyzer. To avoid this. Open VSCode settings and search for Check on Save: Extra Args. Look for the Rust-analyzer โ€บ Check On Save: Extra Args settings and add a new directory for rust-analyzer. Something like:

--target-dir:/tmp/rust-analyzer-check

Automated integration releases from this repository to npm

(Since July 2022). Any branch name starting with integration/ will, first, run the full test suite and, second, if passing, run the publish pipeline (build and upload engines to S3)

The journey through the pipeline is the same as a commit on the main branch.

  • It will trigger prisma/engines-wrapper and publish a new @prisma/engines-version npm package but on the integration tag.
  • Which triggers prisma/prisma to create a chore(Automated Integration PR): [...] PR with a branch name also starting with integration/
  • Since in prisma/prisma we also trigger the publish pipeline when a branch name starts with integration/, this will publish all prisma/prisma monorepo packages to npm on the integration tag.
  • Our ecosystem-tests tests will automatically pick up this new version and run tests, results will show in GitHub Actions

This end to end will take minimum ~1h20 to complete, but is completely automated ๐Ÿค–

Notes:

  • in prisma/prisma repository, we do not run tests for integration/ branches, it is much faster and also means that there is no risk of test failing (e.g. flaky tests, snapshots) that would stop the publishing process.
  • in prisma/prisma-engines tests must first pass, before publishing starts. So better keep an eye on them and restart them as needed.

Security

If you have a security issue to report, please contact us at [email protected]

More Repositories

1

prisma

Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB
TypeScript
38,335
star
2

prisma1

๐Ÿ’พ Database Tools incl. ORM, Migrations and Admin UI (Postgres, MySQL & MongoDB) [deprecated]
Scala
16,552
star
3

prisma-examples

๐Ÿš€ Ready-to-run Prisma example projects
TypeScript
6,000
star
4

studio

๐ŸŽ™๏ธ The easiest way to explore and manipulate your data in all of your Prisma projects.
1,830
star
5

prisma-client-js

Type-safe database client for TypeScript & Node.js (ORM replacement)
TypeScript
1,468
star
6

docs

๐Ÿ“š Prisma Documentation
MDX
968
star
7

migrate

Issues for Prisma Migrate are now tracked at prisma/prisma. This repo was used to track issues for Prisma Migrate Experimental and is now deprecated.
TypeScript
767
star
8

quaint

SQL Query AST and Visitor for Rust
Rust
582
star
9

tiberius

TDS 7.2+ (Microsoft SQL Server) driver for Rust
Rust
310
star
10

lens

๐Ÿ”ฌ Prisma design system
TypeScript
287
star
11

language-tools

๐ŸŒ Prisma Language Tools = Language Server and Prisma's VS Code extension.
TypeScript
250
star
12

awesome-links

Users browse through a list of curated links and bookmark their favorite ones. Code for prisma.io course.
TypeScript
209
star
13

database-schema-examples

Database Schema Examples we strive to support in Prisma
TSQL
205
star
14

ecosystem-tests

๐Ÿฅผ๐Ÿงฌ๐Ÿงช๐Ÿ”ฌ๐Ÿงซ๐Ÿฆ  - Continuously tests Prisma Client with various operating systems, frameworks, platforms, databases and more.
JavaScript
187
star
15

react-native-prisma

TypeScript
176
star
16

prisma-client-extensions

Examples of Prisma Client extensions.
TypeScript
165
star
17

blog-backend-rest-api-nestjs-prisma

A simple backend REST API for a blog built using NestJS, Prisma, PostgreSQL and Swagger.
TypeScript
157
star
18

dataguide

๐Ÿ—„๏ธ Prisma's Data Guide - A growing library of articles focused on making databases more approachable.
MDX
151
star
19

prisma-test-utils

A collection of data model agnostic test utils.
TypeScript
112
star
20

blogr-nextjs-prisma

TypeScript
110
star
21

extension-read-replicas

Prisma Client Extension to add read replica support to your Prisma Client
TypeScript
100
star
22

fullstack-prisma-nextjs-blog

Fullstack Blog with Next.js and Prisma
TypeScript
99
star
23

specs

โš  This repository is not actively used any more, please check out the Prisma Documentation for updated information on Prisma. โš 
Go
87
star
24

deployment-example-vercel

Prisma deployment to Vercel example
JavaScript
80
star
25

text-editors

TypeScript
79
star
26

templates

Ready-to-use Prisma schema examples
TypeScript
63
star
27

faunadb-rust

FaundaDB client for Rust
Rust
62
star
28

prisma-templates

Prisma templates for major cloud providers
52
star
29

prisma1-upgrade

Prisma 1 Upgrade is a CLI tool to help Prisma 1 users upgrade to Prisma 2.x or newer.
TypeScript
51
star
30

try-prisma

TypeScript
48
star
31

vim-prisma

Prisma support for Vim
Vim Script
46
star
32

nuxt-prisma

Prisma ORM integration for Nuxt
TypeScript
44
star
33

women-world-wide

JavaScript
40
star
34

codemods

A Collection of Codemods for Prisma 2
JavaScript
34
star
35

prisma-nuxt

Prisma example showing how to use Prisma in a Nuxt application.
Vue
28
star
36

nestjs-workshop-prisma-day-22

REST API for a rudimentary blog - from Prisma Day 22 NestJS workshop
TypeScript
24
star
37

engines-wrapper

๐ŸŒฏ @prisma/engines-version npm package
TypeScript
21
star
38

accelerate-speed-test

TypeScript
19
star
39

prisma-edge-functions

TypeScript
17
star
40

deployment-example-netlify

Prisma deployment to Netlify example
JavaScript
16
star
41

quickstart

๐Ÿ Starter templates for the 5min Quickstart in the Prisma docs.
JavaScript
16
star
42

studio-vercel-guide

A guide to deploying Studio alongside your app on Vercel
JavaScript
15
star
43

demo-sveltekit

Demo app for Sveltekit article
Svelte
13
star
44

prisma-client-extension-starter

Starter template repository for building publishable Prisma Client extensions
TypeScript
12
star
45

prisma-fmt-wasm

๐Ÿ’พ Build and release automation for @prisma/prisma-fmt-wasm
Shell
12
star
46

ent

An entity layer for Prisma 1 that uses the DataMapper pattern.
TypeScript
12
star
47

connection-string

connection-string + @pimeys/connection-string
Rust
12
star
48

reflector

๐Ÿชž Utilities for meta-level interactions with the Prisma toolkit in Node.js.
TypeScript
12
star
49

prisma1-json-schema

JSON schema of prisma.yml files
TypeScript
11
star
50

engine-images

๐Ÿ–ผ๏ธ Build & test images for Prisma engines.
Shell
11
star
51

eslint-config-prisma

๐Ÿงน Prisma's .eslintrc as an extensible shared config.
JavaScript
11
star
52

pris.ly

Prisma shortlink service (hosted on Vercel)
10
star
53

styles

Shared style resources between Graphcool projects
CSS
9
star
54

pulse-starter

A Pulse starter project. To be used inside of a railway.app template, locally, or with another Pulse-ready database.
TypeScript
7
star
55

prisma1-examples

TypeScript
7
star
56

sublimeText3

Sublime Text 3 package supporting syntax highlighting.
6
star
57

migrate-from-sequelize-to-prisma

A migration guide from Sequelize to Prisma example
JavaScript
6
star
58

opentls

TLS connections for Rust using OpenSSL
Rust
6
star
59

prisma-admin-custom-components

Examples of custom components for Prisma Admin
JavaScript
6
star
60

prisma-content-feedback

Feedback for documentation, articles, tutorials, examples, ...
6
star
61

prisma-admin-feedback

Feedback for Prisma Admin (currently in invite-only preview)
6
star
62

prisma-1-cloud-feedback

Feedback for Prisma Cloud
5
star
63

binding-argument-transform

A library to make Prisma 1 arguments compatible with Prisma 2.
TypeScript
5
star
64

migrate-from-typeorm-to-prisma

TypeScript
5
star
65

pulse-starter-old

A starter project to get you up and running with pulse.
TypeScript
4
star
66

nextjs-edge-functions

Demo of using Next.js with Prisma ORM deployed to Vercel Edge Functions
TypeScript
4
star
67

metrics-tutorial-prisma

Database Metrics with Prisma, Prometheus & Grafana
TypeScript
4
star
68

prisma-day

Prisma Day Code of Conduct
4
star
69

prisma-read-replica-middleware

TypeScript
4
star
70

prisma-platformatic

Prisma ๐Ÿ’š Platformatic exploration
JavaScript
4
star
71

p1-to-p2-upgrade-path-feedback

4
star
72

presskit

Presskit for Prisma
3
star
73

deployment-example-cloudflare-workers

Cloudflare workers deployment example
TypeScript
3
star
74

prisma-example-starter-template

3
star
75

pulse-railway-pg-config

A railway.app project template. Contains a Pulse ready postgres database and set up service. Read more about pulse
TypeScript
3
star
76

tech-writer-task-template

This repository is a template for Technical Writer candidates to use.
JavaScript
3
star
77

orm-benchmarks

Benchmark application to compare query latency of TypeScript ORMs.
JavaScript
3
star
78

fullstack-prisma-turso-embedded-db

TypeScript
2
star
79

db-push-non-idempotent-schemas

Collection of schemas that don't roundtrip if you db pull, then db push
Nix
2
star
80

prisma-fundamentals

An Instruqt lab to introduce students to the Prisma Schema
Shell
2
star
81

prisma-indexes

TypeScript
2
star
82

deployment-example-lambda-serverless-framework

TypeScript
2
star
83

.github

Org level configuration for installed GitHub apps
2
star
84

tracing-tutorial-prisma

Reference code for "Get Started With Tracing Using OpenTelemetry and Prisma Tracing".
TypeScript
2
star
85

benching_setup

TypeScript
2
star
86

client-planning

Placeholder repository for client planning tickets
1
star
87

fivetran-npm-downloads

Python
1
star
88

prisma-client-lib-go

Runtime of Prisma (v1) Go Client.
Go
1
star
89

read-replicas-demo

TypeScript
1
star
90

migrate-from-mongoose-to-prisma

JavaScript
1
star
91

fullstack-prisma-turso

TypeScript
1
star
92

accelerate-nextjs-starter

TypeScript
1
star
93

ci-reports

Repository for various reports from pull requests
HTML
1
star
94

pulse-resend-demo

TypeScript
1
star
95

language-tools-gitpod

Shell
1
star
96

test-vercel-access

HTML
1
star
97

action-node-cache-dependencies

Install and cache node modules.
1
star
98

docs-tools

Python
1
star
99

orm-cloudflare-worker-list-binaries

A cloudflare worker to list the contents of the `prisma-builds` bucket
TypeScript
1
star
100

pdp-spike-nextjs-opentelemetry

TypeScript
1
star