乐闻世界logo
搜索文章和话题

What are popular libraries and tools in Deno's ecosystem?

2月21日 16:08

Deno's ecosystem, though relatively young, is developing rapidly and offers a wealth of third-party libraries and tools. Understanding Deno's ecosystem helps developers better leverage existing resources.

Ecosystem Overview

Deno's ecosystem mainly consists of the following parts:

  1. deno.land/x - Third-party module registry
  2. deno.land/std - Official standard library
  3. nest.land - Another popular module registry
  4. Deno Deploy - Official edge computing platform
  5. Community tools and frameworks - Various development tools and frameworks

1. Web Frameworks

Oak

typescript
import { Application, Router } from "https://deno.land/x/oak@v12.6.1/mod.ts"; const app = new Application(); const router = new Router(); router.get("/", (ctx) => { ctx.response.body = "Hello, Oak!"; }); router.get("/users/:id", (ctx) => { const { id } = ctx.params; ctx.response.body = { id, name: `User ${id}` }; }); app.use(router.routes()); app.use(router.allowedMethods()); await app.listen({ port: 8000 });

Fresh

typescript
import { Fresh } from "https://deno.land/x/fresh@1.4.0/server.ts"; import { handler } from "./routes/index.tsx"; const app = new Fresh(); app.get("/", handler); await app.listen({ port: 8000 });

Hono

typescript
import { Hono } from "https://deno.land/x/hono@v3.11.0/mod.ts"; const app = new Hono(); app.get("/", (c) => c.text("Hello, Hono!")); app.get("/users/:id", (c) => { const id = c.req.param("id"); return c.json({ id, name: `User ${id}` }); }); Deno.serve(app.fetch);

2. Database Clients

PostgreSQL (postgres)

typescript
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts"; const client = new Client({ user: "user", database: "database", hostname: "localhost", port: 5432, password: "password", }); await client.connect(); const result = await client.queryArray("SELECT * FROM users"); console.log(result.rows); await client.end();

MySQL (mysql)

typescript
import { Client } from "https://deno.land/x/mysql@v2.12.0/mod.ts"; const client = await new Client().connect({ hostname: "127.0.0.1", username: "root", password: "password", db: "test", }); const users = await client.query("SELECT * FROM users"); console.log(users); await client.close();

MongoDB (mongo)

typescript
import { MongoClient } from "https://deno.land/x/mongo@v0.31.0/mod.ts"; const client = new MongoClient(); await client.connect("mongodb://localhost:27017"); const db = client.database("test"); const users = db.collection<User>("users"); const user = await users.findOne({ name: "John" }); console.log(user); await client.close();

Redis (redis)

typescript
import { connect } from "https://deno.land/x/redis@v0.31.0/mod.ts"; const redis = await connect({ hostname: "127.0.0.1", port: 6379, }); await redis.set("key", "value"); const value = await redis.get("key"); console.log(value); await redis.close();

3. ORM and Query Builders

DenoDB

typescript
import { Database } from "https://deno.land/x/denodb@v1.0.0/mod.ts"; const db = new Database("postgres", { host: "127.0.0.1", username: "user", password: "password", database: "test", }); class User extends Model { static fields = { id: { primaryKey: true, autoIncrement: true }, name: { type: VARCHAR }, email: { type: VARCHAR, unique: true }, }; } db.link([User]); await db.sync(); const user = await User.create({ name: "John", email: "john@example.com" }); console.log(user);

Drizzle ORM

typescript
import { drizzle } from "https://deno.land/x/drizzle@v0.5.0/mod.ts"; import { pgTable, serial, text, varchar } from "https://deno.land/x/drizzle@v0.5.0/pg-core/mod.ts"; const users = pgTable("users", { id: serial("id").primaryKey(), name: text("name").notNull(), email: varchar("email", { length: 255 }).notNull().unique(), }); const db = drizzle("postgresql://user:password@localhost:5432/test"); const user = await db.insert(users).values({ name: "John", email: "john@example.com", }).returning().get(); console.log(user);

4. Authentication and Authorization

JWT (djwt)

typescript
import { create, verify, getNumericDate } from "https://deno.land/x/djwt@v3.0.1/mod.ts"; const key = await crypto.subtle.generateKey( { name: "HMAC", hash: "SHA-256" }, true, ["sign", "verify"] ); const payload = { iss: "my-app", exp: getNumericDate(60 * 60), // 1 hour data: { userId: 123 }, }; const token = await create({ alg: "HS256", typ: "JWT" }, payload, key); console.log("Token:", token); const verified = await verify(token, key); console.log("Verified:", verified);

OAuth (oauth2_client)

typescript
import { OAuth2Client } from "https://deno.land/x/oauth2_client@v1.0.2/mod.ts"; const oauth2Client = new OAuth2Client({ clientId: "your-client-id", clientSecret: "your-client-secret", authorizationEndpointUri: "https://github.com/login/oauth/authorize", tokenUri: "https://github.com/login/oauth/access_token", }); const authUrl = oauth2Client.code.getAuthorizationUri({ redirectUri: "http://localhost:8000/callback", scope: "read:user", }); console.log("Visit:", authUrl);

5. Utility Libraries

Lodash (lodash)

typescript
import _ from "https://deno.land/x/lodash@4.17.19-es/mod.ts"; const users = [ { name: "John", age: 30 }, { name: "Jane", age: 25 }, ]; const sorted = _.orderBy(users, ["age"], ["asc"]); console.log(sorted); const names = _.map(users, "name"); console.log(names);

Date-fns (date_fns)

typescript
import { format, addDays, isAfter } from "https://deno.land/x/date_fns@v2.29.3/mod.ts"; const now = new Date(); const future = addDays(now, 7); console.log(format(now, "yyyy-MM-dd")); console.log(format(future, "yyyy-MM-dd")); console.log(isAfter(future, now));

Zod (zod)

typescript
import { z } from "https://deno.land/x/zod@v3.21.4/mod.ts"; const UserSchema = z.object({ id: z.number(), name: z.string().min(2), email: z.string().email(), age: z.number().min(0).max(120), }); const userData = { id: 1, name: "John", email: "john@example.com", age: 30, }; const user = UserSchema.parse(userData); console.log(user);

6. Testing Tools

Supabase (supabase)

typescript
import { createClient } from "https://deno.land/x/supabase@2.0.0/mod.ts"; const supabase = createClient( "your-project-url", "your-anon-key" ); const { data, error } = await supabase .from("users") .select("*") .eq("id", 1); console.log(data);

Development Tools

1. Code Formatting

Deno has built-in formatting tools:

bash
deno fmt .

2. Code Linting

bash
deno lint .

3. Documentation Generation

bash
deno doc --html --output=./docs ./src

4. Dependency Check

bash
deno info

Package Management Tools

1. Denoify

Convert Node.js packages to Deno-compatible packages:

bash
denoify

2. Deno 2 NPM (d2n)

Convert npm packages to Deno modules:

bash
d2n express

3. Import Map Generator

Automatically generate import map:

bash
deno run --allow-read --allow-write https://deno.land/x/import_map_generator/main.ts

Deployment Platforms

1. Deno Deploy

Official edge computing platform:

typescript
// main.ts import { serve } from "https://deno.land/std@0.208.0/http/server.ts"; serve((req) => { return new Response("Hello from Deno Deploy!"); });

2. Vercel

Supports Deno deployment:

json
{ "builds": [ { "src": "main.ts", "use": "@vercel/deno" } ] }

3. Railway

Supports Deno deployment:

toml
[build] builder = "NIXPACKS" [deploy] startCommand = "deno run --allow-net main.ts"

Community Resources

1. Official Resources

2. Community Forums

3. Learning Resources

Best Practices

1. Choosing the Right Library

  • Prioritize using standard library
  • Choose actively maintained third-party libraries
  • Check library documentation and examples
  • Check library update frequency

2. Version Management

typescript
// Specify specific version import { serve } from "https://deno.land/std@0.208.0/http/server.ts"; import { Application } from "https://deno.land/x/oak@v12.6.1/mod.ts";

3. Security

  • Check library security
  • Use principle of least privilege
  • Regularly update dependencies

4. Performance

  • Choose performance-optimized libraries
  • Avoid unnecessary dependencies
  • Use caching strategies

Migration Guide

1. Migrating from Node.js

typescript
// Node.js const express = require('express'); const app = express(); // Deno import { Application } from "https://deno.land/x/oak@v12.6.1/mod.ts"; const app = new Application();

2. Using Compatibility Layer

typescript
import { polyfill } from "https://deno.land/x/node_polyfills@v0.1.48/main.ts"; polyfill(); // Now you can use Node.js APIs const fs = require('fs');

Future Development

Deno's ecosystem is developing rapidly, here are some trends worth watching:

  1. More third-party libraries: Ecosystem continues to expand
  2. Better tool support: Development tools continuously improving
  3. Enterprise features: More enterprise-level features
  4. Performance optimization: Continuous performance improvements
  5. Cross-platform support: Better cross-platform compatibility

Deno's ecosystem, though young, is developing rapidly and offers rich resources and tools. By reasonably leveraging these resources, developers can build applications more efficiently.

标签:Deno