Skip to content

Commit

Permalink
JavaScript/Prisma: Adjust settings for CrateDB
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Dec 6, 2023
1 parent f4cc98b commit 8380f79
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 114 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ __pycache__
.coverage
coverage.xml
mlruns/
node_modules/
archive/
logs.log
logs.log
131 changes: 20 additions & 111 deletions by-language/javascript-prisma/README.md
Original file line number Diff line number Diff line change
@@ -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
```

<details><summary><strong>Alternative:</strong> Clone the entire repo</summary>
## 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 .
```

</details>

### 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).

<details><summary>Expand for an overview of example configurations with different databases</summary>

### 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"
}
```

</details>

## 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
4 changes: 2 additions & 2 deletions by-language/javascript-prisma/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ generator client {
}

datasource db {
provider = "sqlite"
url = "file:./dev.db"
provider = "postgresql"
url = "postgresql://crate@localhost:5432/mydb?schema=public"
}

model User {
Expand Down

0 comments on commit 8380f79

Please sign in to comment.