Skip to content

Introduction and principles

loveyourstack edited this page Apr 18, 2024 · 3 revisions

Introduction

Thank you for your interest in LoveYourStack!

The lys packages were created

  • to reduce the amount of repetitive database and HTTP handler code in my Go codebases
  • to ensure that API CRUD operations worked the same for all entities: validation, filtering, paging, etc

.. without using a framework, because some codebases I have worked on have quite tricky SQL queries and operations, and I wanted to keep the freedom to handle such cases manually.

Principles

These are the principles underlying these packages and how they are intended to be used.

Principle Comments
Consistent naming Once something is named, use that name everywhere if possible, to reduce cognitive load. For example, use database schema names as URL prefixes, use the table names as endpoints, always prefix views with "v_", always call the id column "id", always suffix foreign key columns with "_fk", etc
Flat data Although tables should be modeled hierarchically in the database, lys functions for simplicity do not return or work with hierarchical data. Instead, use view JOINs to get fields or aggregated values from related entities
Keep JOINs in the database As much as possible, use database views to encapsulate JOIN logic rather than merging data in the application
Keep SQL DDL in the repo Keep current DDL statements in the repo, as shown in the Northwind sample app. This enables the creation of a test database at any time, removing the need for mock database calls
Minimal layers The entity as defined in the store package Input and Model structs is how the entity is used in business logic. There is no subsequent translation into a business-only layer. An exception is when importing data from an external source: a struct should be defined which models exactly how the data arrives from that external source
Use enums Where possible, create database enums instead of type and status lookup tables. They are fully supported by the lys packages

Clone this wiki locally