P
Prisma19h ago
Manel

Primeiros passos com Prisma + SQLite usando JS

I tried following the tutorial in the documentation, but I'm having trouble using Prisma ORM without TypeScript, as I want to use it with JavaScript and SQLite. Could someone provide me with a step-by-step guide for installation and integration?
3 Replies
Prisma AI Help
Prisma AI Help19h ago
You chose to compile with the bot. One moment while I piece together your answer… Aqui vai um passo‑a‑passo bem direto para usar Prisma ORM + SQLite com JavaScript (sem TypeScript), adaptando o quickstart de TypeScript para JS. 1. Criar o projeto e instalar dependências
mkdir hello-prisma-js
cd hello-prisma-js
npm init -y

npm install prisma --save-dev
npm install @prisma/client @prisma/adapter-better-sqlite3 dotenv
mkdir hello-prisma-js
cd hello-prisma-js
npm init -y

npm install prisma --save-dev
npm install @prisma/client @prisma/adapter-better-sqlite3 dotenv
O adapter @prisma/adapter-better-sqlite3 é o usado com SQLite no quickstart de TS.[Quickstart SQLite] 2. Inicializar Prisma com SQLite
npx prisma init --datasource-provider sqlite --output ../generated/prisma
npx prisma init --datasource-provider sqlite --output ../generated/prisma
Isso cria prisma/schema.prisma, .env e prisma.config.ts.[Quickstart SQLite] Confirme: prisma/schema.prisma:
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}

datasource db {
provider = "sqlite"
}
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}

datasource db {
provider = "sqlite"
}
.env:
DATABASE_URL="file:./dev.db"
DATABASE_URL="file:./dev.db"
prisma.config.ts:
import 'dotenv/config'
import { defineConfig, env } from 'prisma/config'

export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: { path: 'prisma/migrations' },
datasource: { url: env('DATABASE_URL') },
})
import 'dotenv/config'
import { defineConfig, env } from 'prisma/config'

export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: { path: 'prisma/migrations' },
datasource: { url: env('DATABASE_URL') },
})
3. Definir um modelo Edite prisma/schema.prisma e adicione um modelo simples:
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}
4. Criar a base e gerar o client
npx prisma migrate dev --name init
npx prisma generate
npx prisma migrate dev --name init
npx prisma generate
A migração cria dev.db e gera o Prisma Client no diretório configurado.[Migrate] [[Generating client]](https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/generating-prisma-cli ent) 5. Instanciar Prisma Client (JS) Crie lib/prisma.js:
import 'dotenv/config'
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3'
import { PrismaClient } from '../generated/prisma/client/index.js'

const adapter = new PrismaBetterSqlite3({
url: process.env.DATABASE_URL,
})
export const prisma = new PrismaClient({ adapter })
import 'dotenv/config'
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3'
import { PrismaClient } from '../generated/prisma/client/index.js'

const adapter = new PrismaBetterSqlite3({
url: process.env.DATABASE_URL,
})
export const prisma = new PrismaClient({ adapter })
(Adaptado do exemplo TypeScript, apenas removendo tipos.[Instantiate client]) 6. Escrever e rodar o primeiro script JS Crie script.mjs (para ESM):
import { prisma } from './lib/prisma.js'

async function main() {
const user = await prisma.user.create({
data: {
name: 'Alice',
email: 'alice@prisma.io',
},
})
console.log('Created user:', user)

const allUsers = await prisma.user.findMany()
console.log('All users:', allUsers)
}

main()
.then(() => prisma.$disconnect())
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})
import { prisma } from './lib/prisma.js'

async function main() {
const user = await prisma.user.create({
data: {
name: 'Alice',
email: 'alice@prisma.io',
},
})
console.log('Created user:', user)

const allUsers = await prisma.user.findMany()
console.log('All users:', allUsers)
}

main()
.then(() => prisma.$disconnect())
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})
Rodar:
node script.mjs
node script.mjs
As queries seguem o mesmo padrão mostrado na documentação de Prisma Client.[Client intro]
Manel
ManelOP18h ago
Precisa ser .mjs?
Prisma AI Help
Prisma AI Help18h ago
🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.

Did you find this page helpful?