@devmoods/express-extras
TypeScript icon, indicating that this package has built-in type declarations

0.57.1 • Public • Published

@devmoods/express-extras

Express evolved: Type-safe routing, elegant transactions, and production-ready patterns for modern Node.js

Extremely opinionated set of utilities for cranking out production-ready Node.js apps in a timely manner. We assume you are using PostgreSQL as the main database, Redis for data you don't want in Postgres and Faktory for background jobs.

const router = createRouter();

router.put(
  '/posts/:id',
  {
    request: type({ title: 'string' }),
    response: type({ id: 'number', title: 'string' }),
  },
  async (req) => {
    await delay(1000);
    return {
      id: req.params.id,
      ...req.body,
    };
  },
);
  • Clean graceful shutdown procedures and built-in health checks. Runs perfectly in Kubernetes.
  • PostgreSQL with transactions and migrations
bin/dx manage migrate up
await postgres.transaction(() => {
  const post = await postgres.get<{ title: string }>(
    sql`SELECT title FROM posts WHERE id = ${id} FOR UPDATE`,
  );

  await postgres.query(
    sql`UPDATE posts SET ${sql.spreadUpdate({ title: post.title.split('').reverse().join('') })} WHERE id = ${id}`,
  );

  try {
    // Nested transactions with savepoints
    await postgres.transaction(() => {
    })
  } catch () {
  }

  throw new Error('please rollback');
});
  • CSRF protection
  • Built in auth backends! Email+password, passwordless, easy to extend with more
  • OpenAPI v3 generator
  • Redis
  • Slack/Discord client for sending operational notifications
  • CLI commands inspired by Django's management commands
bin/dx manage --help
registerManagementCommand((command, cli) => {
  command('hello').action(() => {
    cli.writeLine('Hello World');
  });
});
const logger = getLogger();

logger.info('hello world', { foo: 'bar', baz: 10 });
// {"level":"info","hostname":"machine.local","pid":43149,"name":"default","message":"hello world","foo":"bar","baz":10,"time":"2024-01-02T17:25:28.610Z","v":1}

Goals

  • Time-to-market and top-notch developer experience.

Non-goals

  • Support for multiple different database vendors and job processing systems.

Example

See the example/ folder for a complete example.

Configure

Put environment variables in .env at the application will automatically read those on startup. For test configuration in unit and integrations tests, use .env.test.

Readme

Keywords

none

Package Sidebar

Install

npm i @devmoods/express-extras

Weekly Downloads

134

Version

0.57.1

License

ISC

Unpacked Size

164 kB

Total Files

80

Last publish

Collaborators

  • hkkoren