Skip to content

Commit e20aaa3

Browse files
Update misc examples (#8328)
1 parent f958943 commit e20aaa3

File tree

31 files changed

+2155
-109
lines changed

31 files changed

+2155
-109
lines changed

orm/postgis-express/bun.lock

Lines changed: 999 additions & 0 deletions
Large diffs are not rendered by default.

orm/postgis-express/package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
"version": "1.0.0",
44
"license": "MIT",
55
"scripts": {
6-
"dev": "ts-node src/index.ts",
6+
"dev": "tsx src/index.ts",
77
"test": "jest"
88
},
99
"dependencies": {
10-
"@prisma/client": "6.9.0",
11-
"@types/node": "22.18.12",
10+
"@prisma/adapter-pg": "^6.18.0",
11+
"@prisma/client": "^6.18.0",
12+
"@prisma/extension-accelerate": "^2.0.2",
13+
"dotenv": "^16.4.7",
1214
"express": "5.1.0"
1315
},
1416
"devDependencies": {
@@ -19,11 +21,11 @@
1921
"@types/supertest": "6.0.3",
2022
"jest": "29.7.0",
2123
"jest-environment-node": "29.7.0",
22-
"prisma": "6.9.0",
24+
"prisma": "^6.18.0",
2325
"randomstring": "1.3.1",
2426
"supertest": "7.1.4",
2527
"ts-jest": "29.4.5",
26-
"ts-node": "10.9.2",
28+
"tsx": "^4.20.6",
2729
"typescript": "5.8.2"
2830
}
29-
}
31+
}

orm/postgis-express/prisma/schema.prisma

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
generator client {
2-
provider = "prisma-client-js"
2+
provider = "prisma-client"
3+
output = "./generated"
4+
engineType = "client"
35
}
46

57
datasource db {

orm/postgis-express/prisma/test-environment.js

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,69 @@ const NodeEnvironment = require('jest-environment-node').default
22
const randomString = require('randomstring')
33
const util = require('util')
44
const exec = util.promisify(require('child_process').exec)
5-
const { PrismaClient } = require('@prisma/client')
5+
const { PrismaClient } = require('./generated/client')
6+
const { PrismaPg } = require('@prisma/adapter-pg')
67

78
class PrismaTestEnvironment extends NodeEnvironment {
89
constructor(config) {
910
super(config)
1011

11-
// Generate a unique schema identifier for this test context
12-
this.schema = `test_${randomString.generate({
12+
// Generate a unique database identifier for this test context
13+
this.databaseName = `test_${randomString.generate({
1314
length: 16,
1415
charset: 'alphanumeric',
1516
capitalization: 'lowercase',
1617
})}`
1718

18-
// Generate the pg connection string for the test schema
19-
this.databaseUrl = 'postgres://postgres:password@localhost:5432/testing'
19+
// Generate the pg connection string for the test database
20+
this.databaseUrl = `postgres://postgres:password@localhost:5432/${this.databaseName}`
21+
22+
// Set the environment variable early so imports use the correct URL
2023
process.env.DB_URL = this.databaseUrl
2124
this.global.process.env.DB_URL = this.databaseUrl
22-
this.client = new PrismaClient()
2325
}
2426

2527
async setup() {
26-
// Set the required environment variable to contain the connection string
27-
// to our database test schema
28-
const url = `${this.databaseUrl}?schema=${this.schema}`
29-
process.env.DB_URL = url
30-
this.global.process.env.DB_URL = url
28+
// Connect to the default postgres database to create the test database
29+
const adminUrl = 'postgres://postgres:password@localhost:5432/postgres'
30+
const adminAdapter = new PrismaPg({ connectionString: adminUrl })
31+
const adminClient = new PrismaClient({ adapter: adminAdapter })
32+
33+
// Create the test database
34+
await adminClient.$executeRawUnsafe(`CREATE DATABASE "${this.databaseName}"`)
35+
await adminClient.$disconnect()
36+
37+
// Create the client for the test database
38+
const adapter = new PrismaPg({ connectionString: this.databaseUrl })
39+
this.client = new PrismaClient({ adapter })
40+
41+
// Ensure PostGIS extension exists
42+
await this.client.$executeRawUnsafe(`CREATE EXTENSION IF NOT EXISTS postgis`)
43+
44+
// Run migrations before the tests start
45+
const { stdout, stderr } = await exec('npx prisma migrate deploy')
46+
if (stderr && !stderr.includes('warn')) {
47+
console.error('Migration error:', stderr)
48+
}
49+
if (stdout) {
50+
console.log('Migration output:', stdout)
51+
}
3152

32-
await exec('yarn prisma migrate deploy')
3353
return super.setup()
3454
}
3555

3656
async teardown() {
37-
// Drop the schema after the tests have completed
38-
await this.client.$executeRawUnsafe(
39-
`drop schema if exists "${this.schema}" cascade`,
40-
)
41-
await this.client.$disconnect()
57+
// Drop the database after the tests have completed
58+
if (this.client) {
59+
await this.client.$disconnect()
60+
}
61+
62+
// Connect to the default postgres database to drop the test database
63+
const adminUrl = 'postgres://postgres:password@localhost:5432/postgres'
64+
const adminAdapter = new PrismaPg({ connectionString: adminUrl })
65+
const adminClient = new PrismaClient({ adapter: adminAdapter })
66+
await adminClient.$executeRawUnsafe(`DROP DATABASE IF EXISTS "${this.databaseName}"`)
67+
await adminClient.$disconnect()
4268
}
4369
}
4470

orm/postgis-express/src/server.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import { PrismaClient } from '@prisma/client'
1+
import 'dotenv/config'
2+
import { PrismaClient } from '../prisma/generated/client'
3+
import { PrismaPg } from "@prisma/adapter-pg"
24
import express from 'express'
35

4-
export const prisma = new PrismaClient()
6+
const adapter = new PrismaPg({ connectionString: process.env.DB_URL })
7+
export const prisma = new PrismaClient({ adapter })
8+
59
const app = express()
610

711
app.use(express.json())
@@ -53,8 +57,8 @@ app.get(`/:userId/nearby-places`, async (req, res) => {
5357
try {
5458
const locations = await prisma.$queryRaw`
5559
select * from "locations_near_user"(${parseInt(
56-
userId,
57-
)}::int, ${distance}::int)
60+
userId,
61+
)}::int, ${distance}::int)
5862
`
5963
res.json({ data: { locations } })
6064
} catch (e) {

orm/prisma-mocking-javascript/client.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
const { PrismaClient } = require('@prisma/client')
1+
const { PrismaClient } = require('./prisma/generated/client')
2+
const { PrismaBetterSQLite3 } = require('@prisma/adapter-better-sqlite3')
23

3-
const prisma = new PrismaClient()
4+
const adapter = new PrismaBetterSQLite3({ url: process.env.DATABASE_URL })
5+
const prisma = new PrismaClient({ adapter });
46

57
module.exports = {
68
prisma,

orm/prisma-mocking-javascript/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
"license": "MIT",
55
"scripts": {
66
"test": "jest",
7-
"dev": "node ./script.js"
7+
"dev": "node ./client.js"
88
},
99
"dependencies": {
10-
"@prisma/client": "6.9.0"
10+
"@prisma/adapter-better-sqlite3": "^6.17.1",
11+
"@prisma/client": "^6.17.1"
1112
},
1213
"devDependencies": {
1314
"jest-mock-extended": "3.0.7",
14-
"prisma": "6.9.0"
15+
"prisma": "^6.17.1"
1516
}
16-
}
17+
}

orm/prisma-mocking-javascript/prisma/schema.prisma

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
generator client {
2-
provider = "prisma-client-js"
2+
provider = "prisma-client"
3+
output = "./generated"
4+
engineType = "client"
35
}
46

57
datasource db {
@@ -16,9 +18,9 @@ model User {
1618
}
1719

1820
model Post {
19-
id Int @id @default(autoincrement())
20-
title String
21-
content String
22-
author User @relation(fields: [authorId], references: [id])
23-
authorId Int
21+
id Int @id @default(autoincrement())
22+
title String
23+
content String
24+
author User @relation(fields: [authorId], references: [id])
25+
authorId Int
2426
}

orm/script/package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
"name": "script",
33
"license": "MIT",
44
"scripts": {
5-
"dev": "ts-node ./script.ts"
5+
"dev": "tsx ./script.ts"
66
},
77
"dependencies": {
8-
"@prisma/client": "6.9.0",
9-
"@types/node": "22.18.12",
10-
"@prisma/extension-accelerate": "2.0.2"
8+
"@prisma/client": "^6.18.0",
9+
"@prisma/extension-accelerate": "^2.0.2",
10+
"dotenv": "^16.4.7"
1111
},
1212
"devDependencies": {
13-
"prisma": "6.9.0",
14-
"ts-node": "10.9.2",
13+
"@types/node": "22.15.32",
14+
"prisma": "^6.18.0",
15+
"tsx": "^4.20.6",
1516
"typescript": "5.8.2"
1617
}
17-
}
18+
}

orm/script/prisma/schema.prisma

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
generator client {
2-
provider = "prisma-client-js"
2+
provider = "prisma-client"
3+
output = "./generated"
4+
engineType = "client"
35
}
46

57
datasource db {

0 commit comments

Comments
 (0)