Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
999 changes: 999 additions & 0 deletions orm/postgis-express/bun.lock

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions orm/postgis-express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
"version": "1.0.0",
"license": "MIT",
"scripts": {
"dev": "ts-node src/index.ts",
"dev": "tsx src/index.ts",
"test": "jest"
},
"dependencies": {
"@prisma/client": "6.9.0",
"@types/node": "22.18.12",
"@prisma/adapter-pg": "^6.18.0",
"@prisma/client": "^6.18.0",
"@prisma/extension-accelerate": "^2.0.2",
"dotenv": "^16.4.7",
"express": "5.1.0"
},
"devDependencies": {
Expand All @@ -19,11 +21,11 @@
"@types/supertest": "6.0.3",
"jest": "29.7.0",
"jest-environment-node": "29.7.0",
"prisma": "6.9.0",
"prisma": "^6.18.0",
"randomstring": "1.3.1",
"supertest": "7.1.4",
"ts-jest": "29.4.5",
"ts-node": "10.9.2",
"tsx": "^4.20.6",
"typescript": "5.8.2"
}
}
}
4 changes: 3 additions & 1 deletion orm/postgis-express/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
generator client {
provider = "prisma-client-js"
provider = "prisma-client"
output = "./generated"
engineType = "client"
}

datasource db {
Expand Down
60 changes: 43 additions & 17 deletions orm/postgis-express/prisma/test-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,69 @@ const NodeEnvironment = require('jest-environment-node').default
const randomString = require('randomstring')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const { PrismaClient } = require('@prisma/client')
const { PrismaClient } = require('./generated/client')
const { PrismaPg } = require('@prisma/adapter-pg')

class PrismaTestEnvironment extends NodeEnvironment {
constructor(config) {
super(config)

// Generate a unique schema identifier for this test context
this.schema = `test_${randomString.generate({
// Generate a unique database identifier for this test context
this.databaseName = `test_${randomString.generate({
length: 16,
charset: 'alphanumeric',
capitalization: 'lowercase',
})}`

// Generate the pg connection string for the test schema
this.databaseUrl = 'postgres://postgres:password@localhost:5432/testing'
// Generate the pg connection string for the test database
this.databaseUrl = `postgres://postgres:password@localhost:5432/${this.databaseName}`

// Set the environment variable early so imports use the correct URL
process.env.DB_URL = this.databaseUrl
this.global.process.env.DB_URL = this.databaseUrl
this.client = new PrismaClient()
}

async setup() {
// Set the required environment variable to contain the connection string
// to our database test schema
const url = `${this.databaseUrl}?schema=${this.schema}`
process.env.DB_URL = url
this.global.process.env.DB_URL = url
// Connect to the default postgres database to create the test database
const adminUrl = 'postgres://postgres:password@localhost:5432/postgres'
const adminAdapter = new PrismaPg({ connectionString: adminUrl })
const adminClient = new PrismaClient({ adapter: adminAdapter })

// Create the test database
await adminClient.$executeRawUnsafe(`CREATE DATABASE "${this.databaseName}"`)
await adminClient.$disconnect()

// Create the client for the test database
const adapter = new PrismaPg({ connectionString: this.databaseUrl })
this.client = new PrismaClient({ adapter })

// Ensure PostGIS extension exists
await this.client.$executeRawUnsafe(`CREATE EXTENSION IF NOT EXISTS postgis`)

// Run migrations before the tests start
const { stdout, stderr } = await exec('npx prisma migrate deploy')
if (stderr && !stderr.includes('warn')) {
console.error('Migration error:', stderr)
}
if (stdout) {
console.log('Migration output:', stdout)
}

await exec('yarn prisma migrate deploy')
return super.setup()
}

async teardown() {
// Drop the schema after the tests have completed
await this.client.$executeRawUnsafe(
`drop schema if exists "${this.schema}" cascade`,
)
await this.client.$disconnect()
// Drop the database after the tests have completed
if (this.client) {
await this.client.$disconnect()
}

// Connect to the default postgres database to drop the test database
const adminUrl = 'postgres://postgres:password@localhost:5432/postgres'
const adminAdapter = new PrismaPg({ connectionString: adminUrl })
const adminClient = new PrismaClient({ adapter: adminAdapter })
await adminClient.$executeRawUnsafe(`DROP DATABASE IF EXISTS "${this.databaseName}"`)
await adminClient.$disconnect()
}
}

Expand Down
12 changes: 8 additions & 4 deletions orm/postgis-express/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { PrismaClient } from '@prisma/client'
import 'dotenv/config'
import { PrismaClient } from '../prisma/generated/client'
import { PrismaPg } from "@prisma/adapter-pg"
import express from 'express'

export const prisma = new PrismaClient()
const adapter = new PrismaPg({ connectionString: process.env.DB_URL })
export const prisma = new PrismaClient({ adapter })

const app = express()

app.use(express.json())
Expand Down Expand Up @@ -53,8 +57,8 @@ app.get(`/:userId/nearby-places`, async (req, res) => {
try {
const locations = await prisma.$queryRaw`
select * from "locations_near_user"(${parseInt(
userId,
)}::int, ${distance}::int)
userId,
)}::int, ${distance}::int)
`
res.json({ data: { locations } })
} catch (e) {
Expand Down
6 changes: 4 additions & 2 deletions orm/prisma-mocking-javascript/client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { PrismaClient } = require('@prisma/client')
const { PrismaClient } = require('./prisma/generated/client')
const { PrismaBetterSQLite3 } = require('@prisma/adapter-better-sqlite3')

const prisma = new PrismaClient()
const adapter = new PrismaBetterSQLite3({ url: process.env.DATABASE_URL })
const prisma = new PrismaClient({ adapter });

module.exports = {
prisma,
Expand Down
9 changes: 5 additions & 4 deletions orm/prisma-mocking-javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
"license": "MIT",
"scripts": {
"test": "jest",
"dev": "node ./script.js"
"dev": "node ./client.js"
},
"dependencies": {
"@prisma/client": "6.9.0"
"@prisma/adapter-better-sqlite3": "^6.17.1",
"@prisma/client": "^6.17.1"
},
"devDependencies": {
"jest-mock-extended": "3.0.7",
"prisma": "6.9.0"
"prisma": "^6.17.1"
}
}
}
14 changes: 8 additions & 6 deletions orm/prisma-mocking-javascript/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
generator client {
provider = "prisma-client-js"
provider = "prisma-client"
output = "./generated"
engineType = "client"
}

datasource db {
Expand All @@ -16,9 +18,9 @@ model User {
}

model Post {
id Int @id @default(autoincrement())
title String
content String
author User @relation(fields: [authorId], references: [id])
authorId Int
id Int @id @default(autoincrement())
title String
content String
author User @relation(fields: [authorId], references: [id])
authorId Int
}
15 changes: 8 additions & 7 deletions orm/script/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
"name": "script",
"license": "MIT",
"scripts": {
"dev": "ts-node ./script.ts"
"dev": "tsx ./script.ts"
},
"dependencies": {
"@prisma/client": "6.9.0",
"@types/node": "22.18.12",
"@prisma/extension-accelerate": "2.0.2"
"@prisma/client": "^6.18.0",
"@prisma/extension-accelerate": "^2.0.2",
"dotenv": "^16.4.7"
},
"devDependencies": {
"prisma": "6.9.0",
"ts-node": "10.9.2",
"@types/node": "22.15.32",
"prisma": "^6.18.0",
"tsx": "^4.20.6",
"typescript": "5.8.2"
}
}
}
4 changes: 3 additions & 1 deletion orm/script/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
generator client {
provider = "prisma-client-js"
provider = "prisma-client"
output = "./generated"
engineType = "client"
}

datasource db {
Expand Down
3 changes: 2 additions & 1 deletion orm/script/script.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PrismaClient } from '@prisma/client'
import 'dotenv/config'
import { PrismaClient } from './prisma/generated/client'
import { withAccelerate } from '@prisma/extension-accelerate'

const prisma = new PrismaClient().$extends(withAccelerate())
Expand Down
9 changes: 5 additions & 4 deletions orm/starter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
"devDependencies": {
"@types/node": "22.18.12",
"nodemon": "3.1.10",
"prisma": "6.9.0",
"tsx": "4.20.6",
"prisma": "^6.17.1",
"tsx": "^4.20.6",
"typescript": "5.8.2"
},
"dependencies": {
"@prisma/client": "6.9.0",
"@prisma/extension-accelerate": "2.0.2"
"@prisma/client": "^6.17.1",
"@prisma/extension-accelerate": "^2.0.2",
"dotenv": "^16.4.7"
},
"prettier": {
"trailingComma": "all",
Expand Down
4 changes: 3 additions & 1 deletion orm/starter/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
provider = "prisma-client"
output = "./generated"
engineType = "client"
}

datasource db {
Expand Down
3 changes: 2 additions & 1 deletion orm/starter/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PrismaClient } from '@prisma/client';
import 'dotenv/config';
import { PrismaClient } from '../prisma/generated/client';
import { withAccelerate } from '@prisma/extension-accelerate';

const prisma = new PrismaClient().$extends(withAccelerate());
Expand Down
16 changes: 8 additions & 8 deletions orm/starter/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist",
"target": "es6",
"module": "commonjs",
"rootDir": ".",
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,
"typeRoots": [
"./node_modules/@types"
],
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true
},
}
"include": [
"src"
]
}
1 change: 1 addition & 0 deletions orm/testing-express/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE"
Loading