From f4cc98b8eaef285637a0292bb9c0adff6807b87c Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Wed, 6 Dec 2023 02:01:15 +0100 Subject: [PATCH 1/3] JavaScript/Prisma: Add example project The code has been scaffolded like this: npx try-prisma@latest --template javascript/script --install npm --name . --path . This commit adds the result unmodified. --- by-language/javascript-prisma/.gitignore | 1 + by-language/javascript-prisma/README.md | 131 ++++++++++++++++++ .../javascript-prisma/package-lock.json | 67 +++++++++ by-language/javascript-prisma/package.json | 14 ++ .../javascript-prisma/prisma/schema.prisma | 24 ++++ by-language/javascript-prisma/script.js | 101 ++++++++++++++ 6 files changed, 338 insertions(+) create mode 100644 by-language/javascript-prisma/.gitignore create mode 100644 by-language/javascript-prisma/README.md create mode 100644 by-language/javascript-prisma/package-lock.json create mode 100644 by-language/javascript-prisma/package.json create mode 100644 by-language/javascript-prisma/prisma/schema.prisma create mode 100644 by-language/javascript-prisma/script.js diff --git a/by-language/javascript-prisma/.gitignore b/by-language/javascript-prisma/.gitignore new file mode 100644 index 00000000..c2658d7d --- /dev/null +++ b/by-language/javascript-prisma/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/by-language/javascript-prisma/README.md b/by-language/javascript-prisma/README.md new file mode 100644 index 00000000..a0435ca3 --- /dev/null +++ b/by-language/javascript-prisma/README.md @@ -0,0 +1,131 @@ +# Simple Node.js Script Example + +This example shows how to use [Prisma Client](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client) in a **simple Node.js script** to read and write data in a SQLite database. You can find the database file with some dummy data at [`./prisma/dev.db`](./prisma/dev.db). + +## Getting started + +### 1. Download example and install dependencies + +Download this example: + +``` +npx try-prisma@latest --template javascript/script +``` + +Install npm dependencies: +``` +cd script +npm install +``` + +
Alternative: Clone the entire repo + +Clone this repository: + +``` +git clone git@github.com:prisma/prisma-examples.git --depth=1 +``` + +Install npm dependencies: + +``` +cd prisma-examples/javascript/script +npm install +``` + +
+ +### 2. Create the database + +Run the following command to create your SQLite database file. This also creates the `User` and `Post` tables that are defined in [`prisma/schema.prisma`](./prisma/schema.prisma): + +``` +npx prisma migrate dev --name init +``` + +### 3. Run the script + +Execute the script with this command: + +``` +npm run dev +``` + +## Switch to another database (e.g. PostgreSQL, MySQL, SQL Server, MongoDB) + +If you want to try this example with another database than SQLite, you can adjust the the database connection in [`prisma/schema.prisma`](./prisma/schema.prisma) by reconfiguring the `datasource` block. + +Learn more about the different connection configurations in the [docs](https://www.prisma.io/docs/reference/database-reference/connection-urls). + +
Expand for an overview of example configurations with different databases + +### PostgreSQL + +For PostgreSQL, the connection URL has the following structure: + +```prisma +datasource db { + provider = "postgresql" + url = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA" +} +``` + +Here is an example connection string with a local PostgreSQL database: + +```prisma +datasource db { + provider = "postgresql" + url = "postgresql://janedoe:mypassword@localhost:5432/notesapi?schema=public" +} +``` + +### MySQL + +For MySQL, the connection URL has the following structure: + +```prisma +datasource db { + provider = "mysql" + url = "mysql://USER:PASSWORD@HOST:PORT/DATABASE" +} +``` + +Here is an example connection string with a local MySQL database: + +```prisma +datasource db { + provider = "mysql" + url = "mysql://janedoe:mypassword@localhost:3306/notesapi" +} +``` + +### Microsoft SQL Server + +Here is an example connection string with a local Microsoft SQL Server database: + +```prisma +datasource db { + provider = "sqlserver" + url = "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;" +} +``` + +### MongoDB + +Here is an example connection string with a local MongoDB database: + +```prisma +datasource db { + provider = "mongodb" + url = "mongodb://USERNAME:PASSWORD@HOST/DATABASE?authSource=admin&retryWrites=true&w=majority" +} +``` + +
+ +## Next steps + +- Check out the [Prisma docs](https://www.prisma.io/docs) +- Share your feedback in the [`#product-wishlist`](https://prisma.slack.com/messages/CKQTGR6T0/) channel on the [Prisma Slack](https://slack.prisma.io/) +- Create issues and ask questions on [GitHub](https://github.com/prisma/prisma/) +- Watch our biweekly "What's new in Prisma" livestreams on [Youtube](https://www.youtube.com/channel/UCptAHlN1gdwD89tFM3ENb6w) diff --git a/by-language/javascript-prisma/package-lock.json b/by-language/javascript-prisma/package-lock.json new file mode 100644 index 00000000..d77e4f31 --- /dev/null +++ b/by-language/javascript-prisma/package-lock.json @@ -0,0 +1,67 @@ +{ + "name": "script", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "script", + "version": "1.0.0", + "license": "MIT", + "dependencies": { + "@prisma/client": "5.6.0" + }, + "devDependencies": { + "prisma": "5.6.0" + } + }, + "node_modules/@prisma/client": { + "version": "5.6.0", + "resolved": "https://registry.npmjs.org/@prisma/client/-/client-5.6.0.tgz", + "integrity": "sha512-mUDefQFa1wWqk4+JhKPYq8BdVoFk9NFMBXUI8jAkBfQTtgx8WPx02U2HB/XbAz3GSUJpeJOKJQtNvaAIDs6sug==", + "hasInstallScript": true, + "dependencies": { + "@prisma/engines-version": "5.6.0-32.e95e739751f42d8ca026f6b910f5a2dc5adeaeee" + }, + "engines": { + "node": ">=16.13" + }, + "peerDependencies": { + "prisma": "*" + }, + "peerDependenciesMeta": { + "prisma": { + "optional": true + } + } + }, + "node_modules/@prisma/engines": { + "version": "5.6.0", + "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-5.6.0.tgz", + "integrity": "sha512-Mt2q+GNJpU2vFn6kif24oRSBQv1KOkYaterQsi0k2/lA+dLvhRX6Lm26gon6PYHwUM8/h8KRgXIUMU0PCLB6bw==", + "devOptional": true, + "hasInstallScript": true + }, + "node_modules/@prisma/engines-version": { + "version": "5.6.0-32.e95e739751f42d8ca026f6b910f5a2dc5adeaeee", + "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-5.6.0-32.e95e739751f42d8ca026f6b910f5a2dc5adeaeee.tgz", + "integrity": "sha512-UoFgbV1awGL/3wXuUK3GDaX2SolqczeeJ5b4FVec9tzeGbSWJboPSbT0psSrmgYAKiKnkOPFSLlH6+b+IyOwAw==" + }, + "node_modules/prisma": { + "version": "5.6.0", + "resolved": "https://registry.npmjs.org/prisma/-/prisma-5.6.0.tgz", + "integrity": "sha512-EEaccku4ZGshdr2cthYHhf7iyvCcXqwJDvnoQRAJg5ge2Tzpv0e2BaMCp+CbbDUwoVTzwgOap9Zp+d4jFa2O9A==", + "devOptional": true, + "hasInstallScript": true, + "dependencies": { + "@prisma/engines": "5.6.0" + }, + "bin": { + "prisma": "build/index.js" + }, + "engines": { + "node": ">=16.13" + } + } + } +} diff --git a/by-language/javascript-prisma/package.json b/by-language/javascript-prisma/package.json new file mode 100644 index 00000000..64983d4d --- /dev/null +++ b/by-language/javascript-prisma/package.json @@ -0,0 +1,14 @@ +{ + "name": "script", + "version": "1.0.0", + "license": "MIT", + "scripts": { + "dev": "node ./script.js" + }, + "dependencies": { + "@prisma/client": "5.6.0" + }, + "devDependencies": { + "prisma": "5.6.0" + } +} \ No newline at end of file diff --git a/by-language/javascript-prisma/prisma/schema.prisma b/by-language/javascript-prisma/prisma/schema.prisma new file mode 100644 index 00000000..1dee3021 --- /dev/null +++ b/by-language/javascript-prisma/prisma/schema.prisma @@ -0,0 +1,24 @@ +generator client { + provider = "prisma-client-js" +} + +datasource db { + provider = "sqlite" + url = "file:./dev.db" +} + +model User { + id Int @id @default(autoincrement()) + email String @unique + name String? + posts Post[] +} + +model Post { + id Int @id @default(autoincrement()) + title String + content String? + published Boolean @default(false) + author User? @relation(fields: [authorId], references: [id]) + authorId Int? +} diff --git a/by-language/javascript-prisma/script.js b/by-language/javascript-prisma/script.js new file mode 100644 index 00000000..14d0b089 --- /dev/null +++ b/by-language/javascript-prisma/script.js @@ -0,0 +1,101 @@ +const { PrismaClient } = require('@prisma/client') + +const prisma = new PrismaClient() + +// A `main` function so that we can use async/await +async function main() { + // Seed the database with users and posts + const user1 = await prisma.user.create({ + data: { + email: 'alice@prisma.io', + name: 'Alice', + posts: { + create: { + title: 'Watch the talks from Prisma Day 2019', + content: 'https://www.prisma.io/blog/z11sg6ipb3i1/', + published: true, + }, + }, + }, + include: { + posts: true, + }, + }) + const user2 = await prisma.user.create({ + data: { + email: 'bob@prisma.io', + name: 'Bob', + posts: { + create: [ + { + title: 'Subscribe to GraphQL Weekly for community news', + content: 'https://graphqlweekly.com/', + published: true, + }, + { + title: 'Follow Prisma on Twitter', + content: 'https://twitter.com/prisma/', + published: false, + }, + ], + }, + }, + include: { + posts: true, + }, + }) + console.log( + `Created users: ${user1.name} (${user1.posts.length} posts) and ${user2.name} (${user2.posts.length} posts) `, + ) + // Retrieve all published posts + const allPosts = await prisma.post.findMany({ + where: { published: true }, + }) + console.log(`Retrieved all published posts: `, allPosts) + + // Create a new post (written by an already existing user with email alice@prisma.io) + const newPost = await prisma.post.create({ + data: { + title: 'Join the Prisma Slack community', + content: 'http://slack.prisma.io', + published: false, + author: { + connect: { + email: 'alice@prisma.io', // Should have been created during initial seeding + }, + }, + }, + }) + console.log(`Created a new post: `, newPost) + + // Publish the new post + const updatedPost = await prisma.post.update({ + where: { + id: newPost.id, + }, + data: { + published: true, + }, + }) + console.log(`Published the newly created post: `, updatedPost) + + // Retrieve all posts by user with email alice@prisma.io + const postsByUser = await prisma.user + .findUnique({ + where: { + email: 'alice@prisma.io', + }, + }) + .posts() + console.log(`Retrieved all posts from a specific user: `, postsByUser) +} + +main() + .then(async () => { + await prisma.$disconnect() + }) + .catch(async (e) => { + console.error(e) + await prisma.$disconnect() + process.exit(1) + }) From 7123f5794dd144d517a78d9c898d90efce2f4c88 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Wed, 6 Dec 2023 02:22:38 +0100 Subject: [PATCH 2/3] JavaScript/Prisma: Adjust settings for CrateDB --- by-language/javascript-prisma/prisma/schema.prisma | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/by-language/javascript-prisma/prisma/schema.prisma b/by-language/javascript-prisma/prisma/schema.prisma index 1dee3021..ac26ddb4 100644 --- a/by-language/javascript-prisma/prisma/schema.prisma +++ b/by-language/javascript-prisma/prisma/schema.prisma @@ -3,8 +3,8 @@ generator client { } datasource db { - provider = "sqlite" - url = "file:./dev.db" + provider = "postgresql" + url = "postgresql://crate@localhost:5432/?schema=doc" } model User { From 77c4e000204512f1f20687fa441e789b5f6f7713 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Thu, 7 Dec 2023 02:35:57 +0100 Subject: [PATCH 3/3] JavaScript/Prisma: Update README --- by-language/javascript-prisma/README.md | 131 ++++-------------------- 1 file changed, 20 insertions(+), 111 deletions(-) diff --git a/by-language/javascript-prisma/README.md b/by-language/javascript-prisma/README.md index a0435ca3..d8bf6ab3 100644 --- a/by-language/javascript-prisma/README.md +++ b/by-language/javascript-prisma/README.md @@ -1,131 +1,40 @@ -# Simple Node.js Script Example +# Using CrateDB with Prisma -This example shows how to use [Prisma Client](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client) in a **simple Node.js script** to read and write data in a SQLite database. You can find the database file with some dummy data at [`./prisma/dev.db`](./prisma/dev.db). -## Getting started +## About -### 1. Download example and install dependencies +[Prisma] is a next-generation Node.js and TypeScript ORM. -Download this example: -``` -npx try-prisma@latest --template javascript/script -``` - -Install npm dependencies: -``` -cd script -npm install -``` - -
Alternative: Clone the entire repo +## Details -Clone this repository: +This example shows how to use [Prisma Client] in a **simple Node.js script**, to +read and write data in a CrateDB database. -``` -git clone git@github.com:prisma/prisma-examples.git --depth=1 -``` - -Install npm dependencies: - -``` -cd prisma-examples/javascript/script -npm install +The folder has been scaffolded using this command: +```shell +npx try-prisma@latest --template javascript/script --install npm --name . --path . ``` -
-### 2. Create the database +## Usage -Run the following command to create your SQLite database file. This also creates the `User` and `Post` tables that are defined in [`prisma/schema.prisma`](./prisma/schema.prisma): +### Create the database -``` +Run the following command to submit the SQL DDL to the database. This will create +database tables for the `User` and `Post` entities that are defined in +[`prisma/schema.prisma`](./prisma/schema.prisma). +```shell npx prisma migrate dev --name init ``` -### 3. Run the script - -Execute the script with this command: - -``` +### Execute the script +```shell npm run dev ``` -## Switch to another database (e.g. PostgreSQL, MySQL, SQL Server, MongoDB) - -If you want to try this example with another database than SQLite, you can adjust the the database connection in [`prisma/schema.prisma`](./prisma/schema.prisma) by reconfiguring the `datasource` block. - -Learn more about the different connection configurations in the [docs](https://www.prisma.io/docs/reference/database-reference/connection-urls). - -
Expand for an overview of example configurations with different databases - -### PostgreSQL - -For PostgreSQL, the connection URL has the following structure: - -```prisma -datasource db { - provider = "postgresql" - url = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA" -} -``` - -Here is an example connection string with a local PostgreSQL database: - -```prisma -datasource db { - provider = "postgresql" - url = "postgresql://janedoe:mypassword@localhost:5432/notesapi?schema=public" -} -``` - -### MySQL - -For MySQL, the connection URL has the following structure: - -```prisma -datasource db { - provider = "mysql" - url = "mysql://USER:PASSWORD@HOST:PORT/DATABASE" -} -``` - -Here is an example connection string with a local MySQL database: - -```prisma -datasource db { - provider = "mysql" - url = "mysql://janedoe:mypassword@localhost:3306/notesapi" -} -``` - -### Microsoft SQL Server - -Here is an example connection string with a local Microsoft SQL Server database: - -```prisma -datasource db { - provider = "sqlserver" - url = "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;" -} -``` - -### MongoDB - -Here is an example connection string with a local MongoDB database: - -```prisma -datasource db { - provider = "mongodb" - url = "mongodb://USERNAME:PASSWORD@HOST/DATABASE?authSource=admin&retryWrites=true&w=majority" -} -``` - -
-## Next steps -- Check out the [Prisma docs](https://www.prisma.io/docs) -- Share your feedback in the [`#product-wishlist`](https://prisma.slack.com/messages/CKQTGR6T0/) channel on the [Prisma Slack](https://slack.prisma.io/) -- Create issues and ask questions on [GitHub](https://github.com/prisma/prisma/) -- Watch our biweekly "What's new in Prisma" livestreams on [Youtube](https://www.youtube.com/channel/UCptAHlN1gdwD89tFM3ENb6w) +[Prisma]: https://www.prisma.io/ +[Prisma Client]: https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client +[Simple Node.js Script Example]: https://github.com/prisma/prisma-examples/tree/latest/javascript/script