Skip to content
Open
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
133 changes: 133 additions & 0 deletions node-typescript/postgres-with-drizzle/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Directory used by Appwrite CLI for local development
.appwrite
6 changes: 6 additions & 0 deletions node-typescript/postgres-with-drizzle/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true
}
162 changes: 162 additions & 0 deletions node-typescript/postgres-with-drizzle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# 🐘 Node.js (TypeScript) Postgres with Drizzle Function

A todo API backed by Appwrite Postgres and Drizzle ORM.

## 🧰 Usage

### GET /

- Returns all todos.

**Response**

Sample `200` Response:

```json
{
"ok": true,
"todos": [
{
"id": 1,
"title": "Write the docs",
"completed": false,
"createdAt": "2026-09-25T06:12:06.366Z"
}
]
}
```

### POST /

- Creates a todo.

**Parameters**

| Name | Description | Location | Type | Sample Value |
| ----- | ---------------------- | -------- | ------ | -------------- |
| title | Title, 1 to 255 chars. | Body | String | Write the docs |

**Response**

Sample `201` Response:

```json
{
"ok": true,
"todo": {
"id": 1,
"title": "Write the docs",
"completed": false,
"createdAt": "2026-09-25T06:12:06.366Z"
}
}
```

Sample `400` Response:

```json
{ "ok": false, "error": "Title must be 1 to 255 characters." }
```

### GET /:id

- Returns a single todo.

**Response**

Sample `200` Response:

```json
{
"ok": true,
"todo": {
"id": 1,
"title": "Write the docs",
"completed": false,
"createdAt": "2026-09-25T06:12:06.366Z"
}
}
```

Sample `404` Response:

```json
{ "ok": false, "error": "Todo not found." }
```

### PATCH /:id

- Updates the title or completion of a todo.

**Parameters**

| Name | Description | Location | Type | Sample Value |
| --------- | -------------------------- | -------- | ------- | -------------- |
| title | New title, 1 to 255 chars. | Body | String | Write the docs |
| completed | Whether the todo is done. | Body | Boolean | true |

**Response**

Sample `200` Response:

```json
{
"ok": true,
"todo": {
"id": 1,
"title": "Write the docs",
"completed": true,
"createdAt": "2026-09-25T06:12:06.366Z"
}
}
```

Sample `400` Response:

```json
{ "ok": false, "error": "Provide a title or completed value." }
```

### DELETE /:id

- Deletes a todo.

**Response**

Sample `200` Response:

```json
{ "ok": true }
```

## 🗄️ Migrations

The function doesn't create tables. Before the first deployment, apply the migrations in `drizzle/` from your machine:

```bash
npm install
DATABASE_URL="postgresql://..." npm run db:migrate
```

After changing `src/schema.ts`, run `npm run db:generate` to create a new migration, then apply it with `npm run db:migrate`.

## ⚙️ Configuration

| Setting | Value |
| ----------------- | ------------------------------ |
| Runtime | Node (22) |
| Entrypoint | `dist/main.js` |
| Build Commands | `npm install && npm run build` |
| Permissions | `any` |
| Timeout (Seconds) | 15 |

## 🔒 Environment Variables

### DATABASE_URL

Connection string of your Appwrite Postgres database.

| Question | Answer |
| ------------ | ------------------------------------------------------------------------------------------- |
| Required | Yes |
| Sample Value | `postgresql://admin:d1efb...aec35@db-8f2c61a4d7e0.fra.appwrite.center:5432/db-8f2c61a4d7e0` |
10 changes: 10 additions & 0 deletions node-typescript/postgres-with-drizzle/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'drizzle-kit';

export default defineConfig({
dialect: 'postgresql',
schema: './src/schema.ts',
out: './drizzle',
dbCredentials: {
url: process.env.DATABASE_URL!,
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE "todos" (
"id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "todos_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
"title" varchar(255) NOT NULL,
"completed" boolean DEFAULT false NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"id": "998038d9-0766-4634-9fcc-8e8a27c20f2d",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.todos": {
"name": "todos",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"identity": {
"type": "always",
"name": "todos_id_seq",
"schema": "public",
"increment": "1",
"startWith": "1",
"minValue": "1",
"maxValue": "2147483647",
"cache": "1",
"cycle": false
}
},
"title": {
"name": "title",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true
},
"completed": {
"name": "completed",
"type": "boolean",
"primaryKey": false,
"notNull": true,
"default": false
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
}
},
"enums": {},
"schemas": {},
"sequences": {},
"roles": {},
"policies": {},
"views": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
Loading
Loading