This example shows how to use Photon.js in a simple Node.js script to read and write data in a database.
Clone the prisma2 branch of this repository:
git clone --single-branch --branch prisma2 git@github.com:prisma/prisma-examples.git
Install Node dependencies:
cd prisma-examples/javascript/script
npm install
Learn more about the development mode
Prisma's development mode watches your Prisma schema on the file system. Whenever there's a change in the schema, the Prisma Framework CLI performs two major tasks in the background:
- map the Prisma schema to your database schema (i.e., perform a schema migration in the database)
- regenerate the Photon.js database client based on the new Prisma schema
It also runs a web server to host Prisma Studio, typically at http://localhost:5555.
In this case, the command also creates a new SQLite database file at ./prisma/dev.db since that didn't exist in the project yet.
Start the development mode with the following command:
npx prisma2 dev
Note: You're using npx to run the Prisma Framework CLI that's listed as a development dependency in
package.json. Alternatively, you can install the CLI globally usingnpm install -g prisma2. When using Yarn, you can run:yarn prisma2 dev.
You can now open Prisma Studio. Open your browser and navigate to the URL displayed by the CLI output (typically at http://localhost:5555).
Alternative: Connect to your own database
Prisma supports MySQL and PostgreSQL at the moment. If you would like to connect to your own database, you can do so by specifying a different data source in the Prisma schema file.
For a MySQL provider:
datasource mysql {
provider = "mysql"
url = "mysql://johndoe:secret42@localhost:3306/mydatabase"
}
OR
For a PostgreSQL provider:
datasource postgresql {
provider = "postgresql"
url = "postgresql://johndoe:secret42@localhost:5432/mydatabase?schema=public"
}
Note: In the above example connection strings,
johndoewould be the username to your database,secret42the password,mydatabasethe name of your database, andpublicthe PostgreSQL schema.
Then to migrate your database schema, run:
npx prisma2 lift save --name 'init'
npx prisma2 lift upExecute the script with this command:
npm run dev
Note: You need to execute the command in a new terminal window/tab, since the development mode is taking up your currrent terminal session.
The migrations that were generated throughout the development mode are development migrations that are thrown away once the desired schema has been found. In that case, you need to persist the schema using the lift subcommands.
To persist your schema migration with Lift, run:
npx prisma2 lift save --name 'init'
npx prisma2 lift up
The first command, lift save, stores a number of migration files on the file sytem with details about the migration (such as the required migration steps and SQL operations), this doesn't yet affect the database. It also deletes the old development migrations. The second command, lift up, actually performs the schema migration against the database.
Sometimes, e.g. in CI/CD environments, it can be helpful to generate Photon.js with a CLI command. This can be done with the prisma2 generate command. If you want to run it in this project, you need to prepend npx again:
npx prisma2 generate
- Read the holistic, step-by-step Prisma Framework tutorial
- Check out the Prisma Framework docs (e.g. for data modeling, relations or the Photon.js API)
- Share your feedback in the
prisma2-previewchannel on the Prisma Slack - Create issues and ask questions on GitHub
- Track the Prisma Framework's progress on
isprisma2ready.com