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..d8bf6ab3 --- /dev/null +++ b/by-language/javascript-prisma/README.md @@ -0,0 +1,40 @@ +# Using CrateDB with Prisma + + +## About + +[Prisma] is a next-generation Node.js and TypeScript ORM. + + +## Details + +This example shows how to use [Prisma Client] in a **simple Node.js script**, to +read and write data in a CrateDB database. + +The folder has been scaffolded using this command: +```shell +npx try-prisma@latest --template javascript/script --install npm --name . --path . +``` + + +## Usage + +### 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 +``` + +### Execute the script +```shell +npm run dev +``` + + + +[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 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..ac26ddb4 --- /dev/null +++ b/by-language/javascript-prisma/prisma/schema.prisma @@ -0,0 +1,24 @@ +generator client { + provider = "prisma-client-js" +} + +datasource db { + provider = "postgresql" + url = "postgresql://crate@localhost:5432/?schema=doc" +} + +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) + })