Skip to content
Draft
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
132 changes: 115 additions & 17 deletions tests/integration/commands/dev/dev.programmatic-netlify-dev.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import fetch from 'node-fetch'
import { describe, test } from 'vitest'

import { callCli } from '../../utils/call-cli.js'
import { withDevServer } from '../../utils/dev-server.js'
import { withSiteBuilder } from '../../utils/site-builder.js'

describe('@netlify/dev integration', () => {
test('Makes DB available to functions', async (t) => {
await withSiteBuilder(t, async (builder) => {
builder
.withPackageJson({
packageJson: {
dependencies: { '@netlify/database': '0.7.0' },
},
})
.withCommand({ command: ['npm', 'install'] })
.withContentFile({
path: 'netlify/functions/db-test.mjs',
content: `
describe('Makes Netlify Database available to functions', () => {
test('When using @netlify/database directly', async (t) => {
await withSiteBuilder(t, async (builder) => {
builder
.withPackageJson({
packageJson: {
dependencies: { '@netlify/database': '0.7.0' },
},
})
.withCommand({ command: ['npm', 'install'] })
.withContentFile({
path: 'netlify/functions/db-test.mjs',
content: `
import { getDatabase } from "@netlify/database";

export default async () => {
Expand All @@ -31,14 +33,110 @@ describe('@netlify/dev integration', () => {

export const config = { path: "/db-test" };
`,
})

await builder.build()

await withDevServer({ cwd: builder.directory }, async (server) => {
const response = await fetch(`${server.url}/db-test`)
const body = await response.text()
t.expect(body).toEqual(JSON.stringify({ sum: 2 }))
})
})
})

test('when using drizzle-orm/netlify-db', async (t) => {
await withSiteBuilder(t, async (builder) => {
builder
.withPackageJson({
packageJson: {
dependencies: { '@netlify/database': '0.7.0', 'drizzle-orm': 'beta', 'drizzle-kit': 'beta' },
},
})
.withCommand({ command: ['npm', 'install'] })
.withContentFile({
path: 'db/schema.ts',
content: `
import { doublePrecision, integer, pgTable, serial, text } from 'drizzle-orm/pg-core'

export const planets = pgTable('planets', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
massKg: doublePrecision('mass_kg').notNull(),
temperatureCelsius: integer('temperature_celsius').notNull(),
})
`,
})
.withContentFile({
path: 'drizzle.config.ts',
content: `
import { defineConfig } from 'drizzle-kit'

export default defineConfig({
dialect: 'postgresql',
schema: './db/schema.ts',
out: 'netlify/database/migrations',
})
`,
})
.withCommand({ command: ['npx', 'drizzle-kit', 'generate'] })
.withContentFile({
path: 'netlify/database/migrations/99999999999999_seed_planets/migration.sql',
content: `
-- Seed data.
INSERT INTO planets (name, mass_kg, temperature_celsius) VALUES
('Mercury', 3.30e23, 167),
('Venus', 4.87e24, 464),
('Earth', 5.97e24, 15),
('Mars', 6.42e23, -65),
('Jupiter', 1.898e27, -110),
('Saturn', 5.68e26, -140),
('Uranus', 8.68e25, -195),
('Neptune', 1.02e26, -200);
`,
})
.withContentFile({
path: 'netlify/functions/database-drizzle/database-drizzle.mjs',
content: `
import { drizzle } from 'drizzle-orm/netlify-db'

import { planets } from '../../../db/schema'

await builder.build()
export default async () => {
try {
const db = drizzle()
const rows = await db.select({ name: planets.name }).from(planets)

return Response.json({ planets: rows })
} catch (error) {
return Response.json({ error: error.message }, { status: 500 });
}
};

export const config = { path: "/database-drizzle" };
`,
})

await withDevServer({ cwd: builder.directory }, async (server) => {
const response = await fetch(`${server.url}/db-test`)
const body = await response.text()
t.expect(body).toEqual(JSON.stringify({ sum: 2 }))
await builder.build()

await callCli(['database', 'migrations', 'apply'], { cwd: builder.directory })

await withDevServer({ cwd: builder.directory }, async (server) => {
const response = await fetch(`${server.url}/database-drizzle`)
const body = await response.json()
t.expect(body).toEqual({
planets: [
{ name: 'Mercury' },
{ name: 'Venus' },
{ name: 'Earth' },
{ name: 'Mars' },
{ name: 'Jupiter' },
{ name: 'Saturn' },
{ name: 'Uranus' },
{ name: 'Neptune' },
],
})
})
})
})
})
Expand Down
Loading