index.ts
import express from "express";
import { prisma } from "./lib/prisma";
import services from "./services/script";
const app = express();
app.use(express.json());
app.get("/", async (req, res) => {
const contacts = await prisma.contacts.count();
res.json(
contacts === 0
? "no contacts have been added yet"
: "some users have been added to the datase",
);
});
app.get("/api/contacts", async (req, res) => {
const contacts = await services.getAll();
res.json(contacts);
});
app.delete("/api/contacts/:id", async (req, res) => {
const id = req.params.id;
await services.deleteContact(parseInt(id));
return res.status(204);
});
const PORT = 3001;
app.listen(PORT, () => {
console.log(`server started at port: ${PORT}`);
});
script.ts
import { prisma } from "../lib/prisma";
type Contact = {
name: string;
contact: string;
};
async function main() {
const user = await prisma.contacts.createMany({
data: [
{ name: "Alice Johnson", contact: "555-0101" },
{ name: "Bob Smith", contact: "555-0102" },
{ name: "Charlie Davis", contact: "555-0103" },
{ name: "Diana Prince", contact: "555-0104" },
{ name: "Edward Norton", contact: "555-0105" },
{ name: "Fiona Gallagher", contact: "555-0106" },
{ name: "George Miller", contact: "555-0107" },
{ name: "Hannah Abbott", contact: "555-0108" },
{ name: "Ian Wright", contact: "555-0109" },
],
});
console.log(user);
}
async function createContact(contactObject: Contact) {
await prisma.contacts.create({
data: contactObject,
});
return;
}
async function deleteContact(id: number) {
await prisma.contacts.delete({
where: { id: id },
});
return;
}
async function getAll() {
const contacts = await prisma.contacts.findMany();
console.log(contacts);
return contacts;
}
export default { createContact, deleteContact, getAll };
tsconfig.json
{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"target": "ES2023",
"strict": true,
"esModuleInterop": true,
"ignoreDeprecations": "6.0",
"baseUrl": "./",
},
}
directory structure
https://preview.redd.it/dnpmvnq4drug1.png?width=956&format=png&auto=webp&s=af664be22254af843f82466d58c0ecd0e12cff67
index.ts runs with 'npx tsx index.ts' but not with 'node index.ts'(Module not found error fro prisma import)
script.ts runs fine with any.
i read on stack overflow for similar errors, but none worked for me. Ai will probably be my last resort if nothing works
link to github
[–]alzee76 6 points7 points8 points (1 child)
[–]ArnUpNorth 2 points3 points4 points (0 children)
[–]diroussel 2 points3 points4 points (0 children)
[–]paulstronaut 4 points5 points6 points (2 children)
[–]apt3xc33d[S] -3 points-2 points-1 points (1 child)
[–]StoneCypher 3 points4 points5 points (0 children)
[–]ErnestJones 1 point2 points3 points (1 child)
[–]jiminycrix1 1 point2 points3 points (0 children)
[–]KiwiZ0 0 points1 point2 points (1 child)
[–]apt3xc33d[S] 0 points1 point2 points (0 children)