Getting Started
This guide will help you set up your first project with Arkos. We'll cover installation, basic configuration, and creating your first API endpoints.
Pre-requisites
Before you begin, ensure you have:
- Node.js 16+ installed
- A package manager (npm, yarn, or pnpm)
- Basic knowledge of Express.js and Prisma
Installation
- First, create a new project and initialize it:
mkdir my-arkos-project
cd my-arkos-project
npm init -y
- Install required dependencies including arkos itself:
- JavaScript
- TypeScript
npm install arkos
npm install --save-dev prisma
npm install arkos
npm install --save-dev prisma typescript @types/node @types/express
Setting Up Prisma
- Initialize Prisma:
npx prisma init
- Configure your database connection in
.env
:
DATABASE_URL="your-database-connection-string"
- Create your Prisma schema under
prisma/schema/schema.prisma
:
datasource db {
provider = "postgresql" // or "mongodb", "mysql", "sqlite"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["prismaSchemaFolder"]
}
model Post {
id String @id @default(uuid())
title String
content String
authordId String
author Author @relation(fields: [authorId], references: [id])
}
model Author {
id String @id @default(uuid())
name String
country String
posts Post[]
}
- Generate Prisma Client:
npx prisma generate
Project Structure
Create the following directory structure:
my-arkos-project/
├── src/
│ ├── utils/
│ │ └── prisma.ts
│ └── app.ts
├── prisma/
└──schema/
└──schema.prisma
Basic Configuration
- Create the Prisma client instance (
src/utils/prisma.ts
):
import { PrismaClient } from "@prisma/client";
export const prisma = new PrismaClient();
- Create your main application file (
src/app.ts
):
- TypeScript
- JavaScript
import arkos from "arkos";
arkos.init();
const arkos = require("arkos");
arkos.init();
Testing Your API
After setting up, Arkos automatically generates these endpoints for your Post model and Author model (Including pagination, nested fields handling and more.):
Author Endpoints
GET /api/authors
- List many authorsGET /api/authors/:id
- Get a single authorPOST /api/authors
- Create a new authorPATCH /api/authors/:id
- Update a authorDELETE /api/authors/:id
- Delete a author
Post Endpoins
GET /api/posts
- List many postsGET /api/posts/:id
- Get a single postPOST /api/posts
- Create a new postPATCH /api/posts/:id
- Update a postDELETE /api/posts/:id
- Delete a post
Test creating an author and a post:
Testing Your API
After setting up, test your API with these examples:
- Curl
- JavaScript & TypeScript
# Create an author
curl -X POST http://localhost:8000/api/authors \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "country": "USA"}'
# Grab the author ID from the response and create a post
curl -X POST http://localhost:8000/api/posts \
-H "Content-Type: application/json" \
-d '{"title": "My First Post", "content": "Hello, Arkos!", "authorId": "author-id-from-response"}'
// Create an author and then a post
fetch("http://localhost:8000/api/authors", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
name: "John Doe",
country: "USA",
}),
})
.then((response) => response.json())
.then((data) => {
const authorId = data.data.id;
return fetch("http://localhost:8000/api/posts", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
title: "My First Post",
content: "Hello, Arkos!",
author: {
id: authorId,
},
}),
});
})
.then((response) => response.json())
.then((data) => console.log("Post created:", data))
.catch((error) => console.error("Error:", error));
If you are familiar with Prisma
you probably wondering why was passed { id: authorId }
in author field instead of { connect: { where: { id: authorId } } }
. The point is that Arkos will handle this for you, so when you pass a relation/scalar field with only then id Arkos will understand that you want to connect to it, for more references see more.
Next Steps
Now that you have a basic setup, you might want to:
Troubleshooting
If you encounter any issues:
- Ensure all dependencies are installed correctly
- Check your database connection string
- Verify your Prisma schema is valid
- Make sure your TypeScript configuration is correct
- Check the Arkos logs for detailed error messages
For more help, visit our GitHub repository or join our community.
Your API is now set up and running. Happy coding! 🚀