Skip to content

Commit

Permalink
switch photon.js setup and imports to use npm install -g prisma/photon
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolasburk committed Nov 21, 2019
1 parent 607ec33 commit 5649cfd
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 129 deletions.
2 changes: 1 addition & 1 deletion docs/core/generators/photonjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The generated data access code of the `photonjs` generator targets [ES2016](http

Photon.js depends on a _query engine_ that's running as a _binary_ on the same host as your application. When deploying your Photon-based application to production, you need to ensure that the binary used by Photon can run in your production environment, i.e. it needs to be compatible with the runtime of your deployment provider.

The query engine binary is downloaded when you run `prisma2 generate`, it is then stored alongside the generated Photon code inside `node_modules/@generated` (or the [custom `output` path](../../photon/codegen-and-node-setup.md) you specified). This section explains how you can determine which binary should be downloaded when `prisma2 generate` is executed to ensure compatibility at runtime.
The query engine binary is downloaded when you run `prisma2 generate`, it is then stored alongside the generated Photon code inside `node_modules/@prisma` (or the [custom `output` path](../../photon/codegen-and-node-setup.md) you specified). This section explains how you can determine which binary should be downloaded when `prisma2 generate` is executed to ensure compatibility at runtime.

You can read more about this topic in the [specification](https://github.com/prisma/specs/blob/master/binaries/Readme.md).

Expand Down
11 changes: 7 additions & 4 deletions docs/photon/api.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generated Photon API (JavaScript/TypeScript)

Photon is a type-safe database client auto-generated based on your [data model definition](../data-modeling.md#data-model-definition) (which is a representation of your database schema). This page explains the generated API operations you have available when using Photon.
Photon is a type-safe database client auto-generated based on your [data model definition](../data-modeling.md#data-model-definition) (which is a declarative representation of your database schema). This page explains the generated API operations you have available when using Photon.

- [Overview](#overview)
- [CRUD](#crud)
Expand All @@ -17,10 +17,13 @@ Photon is a type-safe database client auto-generated based on your [data model d

Using Photon typically follows this high-level workflow:

1. Add Photon.js to your project using: `npm install @prisma/photon`
1. Define/update your data model definition (e.g. by manually adding a new model or by (re)introspecting your database)
2. Generate your Photon database client based on the changes in the data model definition
1. Generate your Photon database client based on the changes in the data model definition

Your `Photon` instance can then be imported from `node_modules/@generated`.
Note that steps 2. and 3. might happen repeatedly as you evolve your application.

The `Photon` constructor can then be imported from `node_modules/@prisma/photon`.

Assume you have the following data model definition:

Expand Down Expand Up @@ -60,7 +63,7 @@ Your generated Photon API will expose the following CRUD operations for the `Use
You can access each function via the respective model property on your generated `Photon` instance, e.g. `users` for the `User` model:

```ts
import { Photon } from '@generated/photon'
import { Photon } from '@prisma/photon'

const photon = new Photon()

Expand Down
2 changes: 2 additions & 0 deletions docs/photon/codegen-and-node-setup.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Code generation & Node.js setup

> ⚠ This page is outdated since the [`2.0.0-preview017`](https://github.com/prisma/prisma2/releases/tag/2.0.0-preview017) release and will be updated very soon. Photon.js is not generated into `node_modules/@generated/photon` any more, but into `node_modules/@prisma/photon`. Since version `2.0.0-preview017`, you also must add `@prisma/photon` to your project dependencies: `npm install @prisma/photon`. Learn more about the change in this [GitHub issue](https://github.com/prisma/photonjs/issues/261).
## TLDR

Photon.js is generated into `node_modules/@generated` by default. While this approach has a number of [benefits](#why-is-photon-js-generated-into-node_modulesgenerated-by-default), it is also unconventional and can be a source confusion for developers new to Photon.js.
Expand Down
2 changes: 1 addition & 1 deletion docs/photon/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Photon.js depends on a query engine that's running as a binary on the same host as your application. When deploying your Photon-based application to production, you need to ensure that the binary used by Photon can run in your production environment, i.e. it needs to be compatible with the runtime of your deployment provider.

The query engine binary is downloaded when you run `prisma2 generate`, it is then stored alongside the generated Photon code inside `node_modules/@generated` (or the [custom `output` path](./codegen-and-node-setup.md) you specified).
The query engine binary is downloaded when you run `prisma2 generate`, it is then stored alongside the generated Photon code inside `node_modules/@prisma` (or the [custom `output` path](./codegen-and-node-setup.md) you specified).

**IMPORTANT**: To ensure the query engine binary is compatible with your production environment, you have to [specify the right platform for Photon.js](../core/generators/photonjs.md#specifying-the-right-platform-for-photon-js).

Expand Down
60 changes: 31 additions & 29 deletions docs/photon/migrating-from-sequelize.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ In this tutorial, we will compare both approaches for working with databases and
## Goals

This tutorial will show you how to achieve the following in your migration process:

1. [Obtaining the Prisma schema from your database](#1-Introspecting-the-existing-database-schema-from-the-Sequelize-project)
2. [Setting up your TypeScript project](#2-Setting-up-your-TypeScript-project)
3. [Specifying the data source](#3-Specifying-the-data-source)
Expand All @@ -33,43 +34,37 @@ You will use **TypeScript** with a **PostgreSQL** database in this tutorial. You
- Know your database server credentials
- Have a database created for the tutorial

You will be migrating a REST API built with the [Express](https://expressjs.com/) framework. The example project can be found in this [repository](https://github.com/infoverload/migration_sequelize_photon).
You will be migrating a REST API built with the [Express](https://expressjs.com/) framework. The example project can be found in this [repository](https://github.com/infoverload/migration_sequelize_photon).

Clone the repository and navigate to it:

```sh
```
git clone https://github.com/infoverload/migration_sequelize_photon
cd migration_sequelize_photon
```

The Sequelize version of the project can be found in the [`sequelize`](https://github.com/infoverload/migration_sequelize_photon/tree/sequelize) branch. To switch to the branch, type:

```sh
```
git checkout sequelize
```

The finished Photon.js version of the project is in the [`master`](https://github.com/infoverload/migration_sequelize_photon/tree/master) branch. To switch to this branch, type:

```sh
```
git checkout master
```

## 1. Introspecting the existing database schema from the Sequelize project

Follow the instructions in the [README file](https://github.com/infoverload/migration_sequelize_photon/blob/sequelize/README.md) in the [`sequelize`](https://github.com/infoverload/migration_sequelize_photon/tree/sequelize) branch and get the project running against your PostgreSQL database. This sets up your database and defines the schema as defined in the TypeORM [entities](https://github.com/infoverload/migration_typeorm_photon/tree/typeorm/src/entity) of the project.

Make sure that you have the [Prisma Framework CLI](https://github.com/prisma/prisma2/blob/master/docs/prisma2-cli.md) installed. The Prisma Framework CLI is available as the [`prisma2`](https://www.npmjs.com/package/prisma2) package on npm. You can install it as a global package on your machine by typing the following command in your terminal:

```sh
npm install -g prisma2
```

Prisma lets you [introspect](https://github.com/prisma/prisma2/blob/master/docs/introspection.md) your database to derive a data model definition from the current database schema.

Now you are ready to introspect the database from the Sequelize project. Navigate outside of the current project directory so you can start a new project. In your terminal, type the command:
Now you are ready to introspect the database from the Sequelize project. Navigate outside of the current project directory so you can start a new project. In your terminal, type the command:

```sh
prisma2 init photonjs_app
```
npx prisma2 init photonjs_app
```

This will initialize a new Prisma project name "photonjs_app" and start the init process:
Expand Down Expand Up @@ -134,9 +129,9 @@ model User {

Now in your terminal, type:

```sh
```
cd photonjs_app
prisma2 dev
npx prisma2 dev
```

This launches the [development mode](https://github.com/prisma/prisma2/blob/master/docs/development-mode.md). When in development mode, the Prisma Framework runs a development server in the background that watches your [Prisma schema file](https://github.com/prisma/prisma2/blob/master/docs/prisma-schema-file.md).
Expand All @@ -148,28 +143,27 @@ Whenever any changes are made in the schema file, the development server:

Go to the endpoint (i.e. http://localhost:5555 ) and explore the generated Prisma schema visually in your browser.

![](https://i.imgur.com/5vSzHaAr.png
)
![](https://i.imgur.com/5vSzHaAr.png)

## 2. Setting up your TypeScript project

### 2.1. Initialize your project and install dependencies

In your project root, initialize a new npm project:

```sh
```
npm init -y
```

Install the `typescript` and `ts-node` packages locally:

```sh
```
npm install --save-dev typescript ts-node
```

Install the `express` and `body-parser` packages:

```sh
```
npm install express body-parser
```

Expand Down Expand Up @@ -229,15 +223,23 @@ datasource db {

## 4. Installing and importing the library

Sequelize is installed as a [node module](https://www.npmjs.com/package/sequelize) with `npm install`, whereas Photon.js is generated by the Prisma CLI (which invokes a Photon.js generator) and provides a type-safe data access API for your data model.
Sequelize is installed as a [node module](https://www.npmjs.com/package/sequelize) with `npm install`, whereas Photon.js is generated by the Prisma CLI (which invokes a Photon.js generator) and provides a type-safe data access API for your data model.

### Installing the Prisma depedencies

Be sure to add the `@prisma/photon` package to your project dependencies as well as the `prisma2` package as a development dependency. Note that both package versions must be kept in sync:

```
npm install @prisma/photon
npm install prisma2 --save-dev
```

### Using the Photon constructor

Make sure you are in your `photonjs_app` project directory. Then, in your terminal, run:

```sh
prisma2 generate
```
npx prisma2 generate
```

This parses the Prisma schema file to generate the right data source client code (from reading the `generator` definition):
Expand All @@ -249,11 +251,11 @@ generator photon {
}
//...
```
and generates a Photon.js database client and a `photon` directory inside `node_modules/@generated`:
and generates a Photon.js database client and a `photon` directory inside `node_modules/@generatedprisma`:

```
node_modules
└── @generated
└── @prisma
└── photon
└── runtime
├── index.d.ts
Expand All @@ -265,7 +267,7 @@ This is the default path but can be [customized](https://github.com/prisma/prism
Now you can import Photon.js in your project. Create a main application file, `index.ts`, inside the `src` directory and import the `Photon` constructor:

```ts
import { Photon } from '@generated/photon'
import { Photon } from '@prisma/photon'
```

## 5. Setting up a connection
Expand All @@ -290,7 +292,7 @@ sequelize.sync({ force: eraseDatabaseOnSync }).then(async () => {
To achieve this in your Photon.js project, in your `index.ts` file, import `Photon` and create a new instance of it like this:

```ts
import { Photon } from '@generated/photon'
import { Photon } from '@prisma/photon'

const photon = new Photon()
```
Expand Down Expand Up @@ -475,7 +477,7 @@ So to implement the same route and endpoint in your Photon.js project, go to you
```ts
import * as express from 'express'
import * as bodyParser from 'body-parser'
import { Photon } from '@generated/photon'
import { Photon } from '@prisma/photon'

const app = express()
app.use(bodyParser.json())
Expand Down Expand Up @@ -607,7 +609,7 @@ In your [package.json](https://github.com/infoverload/migration_sequelize_photon

When finished with your project, run it like this:

```sh
```
npm start
```

Expand Down
51 changes: 26 additions & 25 deletions docs/photon/migrating-from-typeorm.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,33 @@ You will be migrating a REST API built with the [Express](https://expressjs.com/

Clone the repository and navigate to it:

```sh
```
git clone https://github.com/infoverload/migration_typeorm_photon
cd migration_typeorm_photon
```

The TypeORM version of the project can be found in the [`typeorm`](https://github.com/infoverload/migration_typeorm_photon/tree/typeorm) branch. To switch to the branch, type:

```sh
```
git checkout typeorm
```

The finished Photon.js version of the project is in the [`master`](https://github.com/infoverload/migration_typeorm_photon/tree/master) branch. To switch to this branch, type:

```sh
```
git checkout master
```

## 1. Introspecting the existing database schema from the TypeORM project

Follow the instructions in the [README file](https://github.com/infoverload/migration_typeorm_photon/blob/typeorm/README.md) in the [`typeorm`](https://github.com/infoverload/migration_typeorm_photon/tree/typeorm) branch and get the project running against your PostgreSQL database. This sets up your database and defines the schema as defined in the TypeORM [entities](https://github.com/infoverload/migration_typeorm_photon/tree/typeorm/src/entity) of the project.

Make sure that you have the [Prisma Framework CLI](https://github.com/prisma/prisma2/blob/master/docs/prisma2-cli.md) installed. The Prisma Framework CLI is available as the [`prisma2`](https://www.npmjs.com/package/prisma2) package on npm. You can install it as a global package on your machine by typing the following command in your terminal:

```sh
npm install -g prisma2
```

Prisma lets you [introspect](https://github.com/prisma/prisma2/blob/master/docs/introspection.md) your database to derive a data model definition from the current database schema. This feature is typically used in [Photon-only](https://github.com/prisma/prisma2/blob/master/docs/photon/use-only-photon.md) projects where database migrations are not performed via [Lift](https://lift.prisma.io), so the data model needs to be updated manually after each database schema change.
Prisma lets you [introspect](https://github.com/prisma/prisma2/blob/master/docs/introspection.md) your database to derive a data model definition from the current database schema.

Now you are ready to introspect the database from the TypeORM project. Navigate outside of the current project directory so you can start a new project. In your terminal, type the command:

```sh
prisma2 init photonjs_app
```
npx prisma2 init photonjs_app
```

This will initialize a new Prisma project name "photonjs_app" and start the init process:
Expand Down Expand Up @@ -147,15 +141,14 @@ model PostCategoriesCategory {

Now in your terminal, type:

```sh
prisma2 dev
```
npx prisma2 dev
```

This launches the [development mode](https://github.com/prisma/prisma2/blob/master/docs/development-mode.md) and creates a [Prisma Studio](https://github.com/prisma/studio) endpoint for you. Go to the endpoint (i.e. http://localhost:5555 ) and explore the generated Prisma schema visually in your browser.

![](https://i.imgur.com/nnnfql9r.png)


## 2. Specifying the data source

In the TypeORM project example, the data source and credentials can be defined in the [`ormconfig.json`](https://github.com/infoverload/migration_typeorm_photon/blob/typeorm/ormconfig.json) file:
Expand Down Expand Up @@ -189,13 +182,21 @@ datasource db {

TypeORM is installed as a [node module](https://www.npmjs.com/package/typeorm) with `npm install`, whereas Photon.js is generated by the Prisma CLI (which invokes a Photon.js generator) and provides a type-safe data access API for your data model.

### Installing the Prisma depedencies

Be sure to add the `@prisma/photon` package to your project dependencies as well as the `prisma2` package as a development dependency. Note that both package versions must be kept in sync:

```
npm install @prisma/photon
npm install prisma2 --save-dev
```

### Migrating to the Photon constructor

Make sure you are in your `photonjs_app` project directory. Then, in your terminal, run:

```sh
prisma2 generate
```
npx prisma2 generate
```

This parses the Prisma schema file to generate the right data source client code (from reading the `generator` definition):
Expand All @@ -207,11 +208,11 @@ generator photon {
}
//...
```
and generates a Photon.js client and a `photon` directory inside `node_modules/@generated`:
and generates a Photon.js client and a `photon` directory inside `node_modules/@prisma`:

```
node_modules
└── @generated
└── @prisma
└── photon
└── runtime
├── index.d.ts
Expand All @@ -223,7 +224,7 @@ This is the default path but can be [customized](https://github.com/prisma/prism
Now you can import Photon.js in your project. Create a main application file, `index.ts`, inside the `src` directory and import the `Photon` constructor:

```ts
import { Photon } from '@generated/photon'
import { Photon } from '@prisma/photon'
```

## 4. Setting up a connection
Expand All @@ -242,7 +243,7 @@ const connection = createConnection()
To achieve this in your Photon.js project, in your `index.ts` file, import `Photon` and create a new instance of it like this:

```ts
import { Photon } from '@generated/photon'
import { Photon } from '@prisma/photon'

const photon = new Photon()
```
Expand Down Expand Up @@ -383,7 +384,7 @@ So to implement the same route and endpoint in your Photon.js project, go to you
```ts
import * as express from 'express'
import * as bodyParser from 'body-parser'
import { Photon } from '@generated/photon'
import { Photon } from '@prisma/photon'

const app = express()
app.use(bodyParser.json())
Expand Down Expand Up @@ -508,13 +509,13 @@ After you have implemented the routes in your main application file, it's time t

In your project root, initialize a new npm project:

```sh
```
npm init -y
```

Install the `typescript` and `ts-node` packages locally:

```sh
```
npm install --save-dev typescript ts-node
```

Expand Down Expand Up @@ -554,7 +555,7 @@ In your [package.json](https://github.com/infoverload/migration_typeorm_photon/b

With everything in place, you can run the project!

```sh
```
npm start
```

Expand Down

0 comments on commit 5649cfd

Please sign in to comment.