1.0.8 • Published 9 days ago

dynamodb-dao-generator v1.0.8

Weekly downloads
-
License
MIT
Repository
github
Last release
9 days ago

dynamodb-dao-generator

Generate data-access-objects from your domain-objects.

Generates type definitions, query functions, terraform resources, and bundles it all up into daos with a single command!

oclif Version Codecov Downloads/week License

Table of Contents

Goals

The goal of dynamodb-dao-generator is to use the domain-objects you've already defined in order to speed up development and eliminate errors.

Powered by:

This enables:

  • creating a fully functional data-access-object, using best practices, simply by defining your domain-objects
  • easily extending your generated data-access-objects, because you control the code completely
  • instantly leveraging the best practices and safety features finetuned against countless deployments in production

Like an ORM, but without any magic or limitations - the code is in your hands and ready to mod as needed.

Installation

1. Save the package as a dev dependency

npm install --save-dev dynamodb-dao-generator

2. Define a config file

This file will define which domain-objects you want to generate data-access-objects for

For example:

// codegen.dynamodb.dao.ts

import {
  DeclaredDaoSpecification,
  DeclaredDomainObjectIntrospectionPaths,
  DeclaredOutputDirectories,
} from 'dynamodb-dao-generator';

import { Sensor, Address } from './src/domain';

export const introspect: DeclaredDomainObjectIntrospectionPaths = [
  './src/domain/index.ts',
];

export const directories: DeclaredOutputDirectories = {
  terraform: `provision/aws/product`,
  dao: `src/data/dao`,
};

export const specifications: DeclaredDaoSpecification[] = [
  {
    domainObject: Sensor,
    supplementalIndexes: [
      { filterByKey: ['addressUuid'] },
      { filterByKey: ['ownerUuid'], sortByKey: ['createdAt'] },
    ],
  },
  {
    domainObject: Address,
    supplementalIndexes: [
      { filterByKey: ['city', 'state'] },
      { filterByKey: ['postal'] },
    ],
  },
];

3. Test it out!

  $ npx dynamodb-dao-generator version
  $ npx dynamodb-dao-generator generate

Examples

a domain object dao

Input: Say you have the following domain object

export interface Geocode {
  id?: number;
  latitude: number;
  longitude: number;
}
export class Geocode extends DomainValueObject<Geocode> implements Geocode {}

Output: Running this dynamodb-dao-generator on this domain object will:

  1. generate the dao files

    1. src/data/dao/geocodeDao/index.ts
    2. src/data/dao/geocodeDao/findByUuid.ts
    3. src/data/dao/geocodeDao/findByUnique.ts
    4. src/data/dao/geocodeDao/castToDatabaseObject.ts
    5. src/data/dao/geocodeDao/castFromDatabaseObject.ts
    6. src/data/dao/geocodeDao/.maintenance/migrateAllRecordsToNewSchema.ts
  2. generate the terraform table provisioning file

    1. provision/aws/product/dynamodb.table.geocode.tf

Commands

dynamodb-dao-generator generate

generate data-access-objects by parsing domain-objects

USAGE
  $ dynamodb-dao-generator generate

OPTIONS
  -c, --config=config  (required) [default: codegen.sql.dao.yml] path to config yml
  -h, --help           show CLI help

See code: dist/contract/commands/generate.ts

dynamodb-dao-generator help [COMMAND]

display help for dynamodb-dao-generator

USAGE
  $ dynamodb-dao-generator help [COMMAND]

ARGUMENTS
  COMMAND  command to show help for

OPTIONS
  --all  see all commands in CLI

See code: @oclif/plugin-help

Contribution

Team work makes the dream work! Please create a ticket for any features you think are missing and, if willing and able, draft a PR for the feature :)