Skip to content

Commit 8338baa

Browse files
No external data dependency (#2)
* wip * major progress! * fix up some styles * format
1 parent debf07c commit 8338baa

File tree

12 files changed

+505
-101
lines changed

12 files changed

+505
-101
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This is an example of how to use [`@harperdb/nextjs`](https://github.com/HarperD
44

55
The Next.js application can interact with the database through the [Resource API](https://docs.harperdb.io/docs/technical-details/reference/resource) directly instead of relying on network operations. This is significantly more efficient and enables a better application development experience.
66

7-
<!--
7+
<!--
88
TODO: Re-record video with new application steps
99
> [!TIP]
1010
> Watch a walkthrough of this example here: [Next.js on HarperDB | Step-by-Step Guide for Next Level Next.js Performance](https://youtu.be/GqLEwteFJYY)
@@ -26,7 +26,7 @@ The easiest way to demonstrate this application remotely is to use the `prebuilt
2626
1. Locally or in a CI environment, load the necessary data ([Harper Basic Data Example](https://github.com/HarperDB/basic-data-example))
2727
2. Create a build using `npm run build`
2828
3. Modify `config.yaml` to include `prebuilt: true` under the `@harperdb/nextjs` component
29-
5. Then deploy the prebuilt application using the Harper CLI:
29+
4. Then deploy the prebuilt application using the Harper CLI:
3030

3131
```bash
3232
harperdb deploy \

app/actions.js

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,67 @@
11
'use server';
22

3+
import { revalidatePath } from 'next/cache';
34
import('harperdb');
45

56
export async function listDogs() {
6-
const dogs = [];
7-
for await (const dog of tables.Dog.search()) {
8-
dogs.push({ id: dog.id, name: dog.name });
7+
// Original database logic - uncomment when ready
8+
try {
9+
const dogs = [];
10+
if (tables?.Dog) {
11+
for await (const dog of tables.Dog.search()) {
12+
dogs.push({ id: dog.id, name: dog.name, breed: dog.breed, age: dog.age, color: dog.color });
13+
}
14+
}
15+
return dogs;
16+
} catch (error) {
17+
console.error('Error listing dogs:', error);
18+
return [];
919
}
10-
return dogs;
1120
}
1221

1322
export async function getDog(id) {
14-
return tables.Dog.get(id);
23+
try {
24+
if (typeof tables === 'undefined' || !tables.Dog) {
25+
return null;
26+
}
27+
return await tables.Dog.get(id);
28+
} catch (error) {
29+
console.error('Error getting dog:', error);
30+
return null;
31+
}
32+
}
33+
34+
export async function createDog(formData) {
35+
// Extract form values
36+
const name = formData.get('name');
37+
const breed = formData.get('breed');
38+
const age = parseInt(formData.get('age'));
39+
const color = formData.get('color');
40+
41+
// Validate required fields
42+
if (!name || !breed || !age || !color) {
43+
throw new Error('All fields are required');
44+
}
45+
46+
if (typeof tables === 'undefined' || !tables.Dog) {
47+
throw new Error('Database not available');
48+
}
49+
50+
await tables.Dog.create({ name, breed, age, color });
51+
52+
// Revalidate the dogs page to show updated data
53+
revalidatePath('/dogs');
54+
}
55+
56+
export async function deleteDog(dogId) {
57+
console.log('Deleting dog with id:', dogId);
58+
59+
if (typeof tables === 'undefined' || !tables.Dog) {
60+
throw new Error('Database not available');
61+
}
62+
63+
await tables.Dog.delete(dogId);
64+
65+
// Revalidate the dogs page to show updated data
66+
revalidatePath('/dogs');
1567
}

app/client-component.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

app/dogs/[id]/page.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

app/dogs/layout.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)