|
1 | | -# Simple Node.js Script Example |
| 1 | +# Using CrateDB with Prisma |
2 | 2 |
|
3 | | -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). |
4 | 3 |
|
5 | | -## Getting started |
| 4 | +## About |
6 | 5 |
|
7 | | -### 1. Download example and install dependencies |
| 6 | +[Prisma] is a next-generation Node.js and TypeScript ORM. |
8 | 7 |
|
9 | | -Download this example: |
10 | 8 |
|
11 | | -``` |
12 | | -npx try-prisma@latest --template javascript/script |
13 | | -``` |
14 | | - |
15 | | -Install npm dependencies: |
16 | | -``` |
17 | | -cd script |
18 | | -npm install |
19 | | -``` |
20 | | - |
21 | | -<details><summary><strong>Alternative:</strong> Clone the entire repo</summary> |
| 9 | +## Details |
22 | 10 |
|
23 | | -Clone this repository: |
| 11 | +This example shows how to use [Prisma Client] in a **simple Node.js script**, to |
| 12 | +read and write data in a CrateDB database. |
24 | 13 |
|
25 | | -``` |
26 | | -git clone git@github.com:prisma/prisma-examples.git --depth=1 |
27 | | -``` |
28 | | - |
29 | | -Install npm dependencies: |
30 | | - |
31 | | -``` |
32 | | -cd prisma-examples/javascript/script |
33 | | -npm install |
| 14 | +The folder has been scaffolded using this command: |
| 15 | +```shell |
| 16 | +npx try-prisma@latest --template javascript/script --install npm --name . --path . |
34 | 17 | ``` |
35 | 18 |
|
36 | | -</details> |
37 | 19 |
|
38 | | -### 2. Create the database |
| 20 | +## Usage |
39 | 21 |
|
40 | | -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): |
| 22 | +### Create the database |
41 | 23 |
|
42 | | -``` |
| 24 | +Run the following command to submit the SQL DDL to the database. This will create |
| 25 | +database tables for the `User` and `Post` entities that are defined in |
| 26 | +[`prisma/schema.prisma`](./prisma/schema.prisma). |
| 27 | +```shell |
43 | 28 | npx prisma migrate dev --name init |
44 | 29 | ``` |
45 | 30 |
|
46 | | -### 3. Run the script |
47 | | - |
48 | | -Execute the script with this command: |
49 | | - |
50 | | -``` |
| 31 | +### Execute the script |
| 32 | +```shell |
51 | 33 | npm run dev |
52 | 34 | ``` |
53 | 35 |
|
54 | | -## Switch to another database (e.g. PostgreSQL, MySQL, SQL Server, MongoDB) |
55 | | - |
56 | | -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. |
57 | | - |
58 | | -Learn more about the different connection configurations in the [docs](https://www.prisma.io/docs/reference/database-reference/connection-urls). |
59 | | - |
60 | | -<details><summary>Expand for an overview of example configurations with different databases</summary> |
61 | | - |
62 | | -### PostgreSQL |
63 | | - |
64 | | -For PostgreSQL, the connection URL has the following structure: |
65 | | - |
66 | | -```prisma |
67 | | -datasource db { |
68 | | - provider = "postgresql" |
69 | | - url = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA" |
70 | | -} |
71 | | -``` |
72 | | - |
73 | | -Here is an example connection string with a local PostgreSQL database: |
74 | | - |
75 | | -```prisma |
76 | | -datasource db { |
77 | | - provider = "postgresql" |
78 | | - url = "postgresql://janedoe:mypassword@localhost:5432/notesapi?schema=public" |
79 | | -} |
80 | | -``` |
81 | | - |
82 | | -### MySQL |
83 | | - |
84 | | -For MySQL, the connection URL has the following structure: |
85 | | - |
86 | | -```prisma |
87 | | -datasource db { |
88 | | - provider = "mysql" |
89 | | - url = "mysql://USER:PASSWORD@HOST:PORT/DATABASE" |
90 | | -} |
91 | | -``` |
92 | | - |
93 | | -Here is an example connection string with a local MySQL database: |
94 | | - |
95 | | -```prisma |
96 | | -datasource db { |
97 | | - provider = "mysql" |
98 | | - url = "mysql://janedoe:mypassword@localhost:3306/notesapi" |
99 | | -} |
100 | | -``` |
101 | | - |
102 | | -### Microsoft SQL Server |
103 | | - |
104 | | -Here is an example connection string with a local Microsoft SQL Server database: |
105 | | - |
106 | | -```prisma |
107 | | -datasource db { |
108 | | - provider = "sqlserver" |
109 | | - url = "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;" |
110 | | -} |
111 | | -``` |
112 | | - |
113 | | -### MongoDB |
114 | | - |
115 | | -Here is an example connection string with a local MongoDB database: |
116 | | - |
117 | | -```prisma |
118 | | -datasource db { |
119 | | - provider = "mongodb" |
120 | | - url = "mongodb://USERNAME:PASSWORD@HOST/DATABASE?authSource=admin&retryWrites=true&w=majority" |
121 | | -} |
122 | | -``` |
123 | | - |
124 | | -</details> |
125 | 36 |
|
126 | | -## Next steps |
127 | 37 |
|
128 | | -- Check out the [Prisma docs](https://www.prisma.io/docs) |
129 | | -- Share your feedback in the [`#product-wishlist`](https://prisma.slack.com/messages/CKQTGR6T0/) channel on the [Prisma Slack](https://slack.prisma.io/) |
130 | | -- Create issues and ask questions on [GitHub](https://github.com/prisma/prisma/) |
131 | | -- Watch our biweekly "What's new in Prisma" livestreams on [Youtube](https://www.youtube.com/channel/UCptAHlN1gdwD89tFM3ENb6w) |
| 38 | +[Prisma]: https://www.prisma.io/ |
| 39 | +[Prisma Client]: https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client |
| 40 | +[Simple Node.js Script Example]: https://github.com/prisma/prisma-examples/tree/latest/javascript/script |
0 commit comments