npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

powership

v5.8.0

Published

Powership / [Modules](../../docs/modules.md)

Downloads

1,585

Readme

Powership / Modules

Powership

All-in-one full-stack package for managing complex web applications.

Powership is the full-stack package of choice for creating, managing, and scaling complex web applications with support for single-table design.

Using Powership you can quickly create types that can be easily extended, transformed into GraphQL, TypeScript, and used in both frontend and backend applications.

Table of Contents

  1. Typescript Infer
  2. Extending Types
  3. Static typescript Infer
  4. Printing Types as Typescript
  5. Validation
  6. Entity & CRUD
  7. GraphQL

Creating and extending types

import {
  createGraphQLSchema,
  createResolver,
  createType,
  createEntity,
  Infer,
} from 'powership';

const AddressType = createType('Address', {
  object: {
    street: 'string',
    number: {
      union: [
        'string',
        'int?',
      ],
    },
  },
});

const UserType = createType(
  {
    object: {
      name: 'string', // any string
      email: 'email?', // email type - will validate against email regex
      age: 'int?', // optional integer
      notes: '[int]?',

      // declaring a union field - will infer as `string | undefined | number[]`
      unionField: {
        union: [
          'string?',
          '[int]?',
        ],
      },

      // represents an enum
      letter: {
        enum: [
          'a',
          'b',
          'c',
        ],
      },

      // more detailed way to define enums
      letterOptionalList: {
        enum: [
          'x',
          'y',
          'z',
        ],
        optional: true,
        list: true,
      },

      // using a previous object as field type
      addresses: {
        type: AddressType,
        list: true,
      },

      // another way to define object fields
      deliveryAddress: {
        object: {
          street: 'string',
          number: 'int?',
        },
      },
    },
  } as const // "as const" is needed to TS to infer types correctly
);

Extending types

const StoreType = UserType.clone((it) =>
  it.exclude(['addresses']).extendObjectDefinition({ storeId: 'ID', ownerId: 'string' }).graphType('Store')
);

Static typescript infer

type TStoreType = Infer<typeof StoreType>;

Printing types as Typescript

const storeTS = await StoreType.typescriptPrint();

expect(storeTS.split('\n')).toEqual([
  'export interface Store {',
  '  name: string;',
  '  email?: Email;',
  '  age?: number;',
  '  notes?: number[];',
  '  unionField?: string | number[];',
  '  letter: "a" | "b" | "c";',
  '  letterOptionalList?: ("x" | "y" | "z")[];',
  '  deliveryAddress: {',
  '    street: string;',
  '    number?: number;',
  '  };',
  '  storeId: ID;',
  '  ownerId: string;',
  '}',
  '',
]);

Validation

try {
  const validStoreData = StoreType.parse({});
  console.log(validStoreData);
} catch (e) {
  /*
   *  Error: Store: ➤ field "ownerId": RequiredField.
   *           ➤ field "storeId": RequiredField.
   *           ➤ field "deliveryAddress": RequiredField.
   *           ➤ field "letter": RequiredField.
   *           ➤ field "name": RequiredField.
   */
}

Entity and CRUD

const StoreEntity = createEntity({
  name: 'Store',
  type: StoreType,
  indexes: [
    {
      name: 'id1', // index in database to be used
      PK: ['.storeId'],
    },
    {
      name: 'id2',
      PK: [
        '.ownerId',
        '.storeId',
      ],
    },
  ],
});

const findStoreResolver = createResolver({
  name: 'findStore',
  type: StoreEntity.edgeType,
  args: {
    storeId: 'string',
  },
}).resolver(async (_, { storeId /* ✨ automaticly typed as string */ }, requestContext) => {
  const filter = {
    storeId,
  };

  return StoreEntity.findOne({ filter, context: requestContext });
});

GraphQL

const graphqlSchema = createGraphQLSchema([findStoreResolver]);

const schemaTXT = graphqlSchema.utils.print();

expect(schemaTXT.split('\n')).toEqual([
  'type Query {',
  '  findStore(storeId: String!): Store_Edge!',
  '}',
  '',
  'type Store_Edge {',
  '  cursor: String!',
  '  node: StoreEntity!',
  '}',
  '',
  'type StoreEntity {',
  '  createdAt: Date!',
  '  createdBy: String',
  '  id: String!',
  '  ulid: Ulid!',
  '  updatedAt: Date!',
  '  updatedBy: String',
  '',
  '  """',
  '  The full string value of the first index following the RegExp format "^store⋮id1⋮.*"',
  '  """',
  '  _id: String!',
  '',
  '  """',
  '  The id1PK field in the RegExp format "^store⋮id1⋮.*"',
  '  """',
  '  id1PK: String!',
  '',
  '  """',
  '  The id2PK field in the RegExp format "^store⋮id2⋮.*"',
  '  """',
  '  id2PK: String!',
  '  name: String!',
  '  email: String',
  '  age: Int',
  '  notes: [Int]',
  '  unionField: StoreEntity_unionField',
  '  letter: StoreEntity_letter!',
  '  letterOptionalList: [StoreEntity_letterOptionalList]',
  '  deliveryAddress: StoreEntity_deliveryAddress!',
  '  storeId: ID!',
  '  ownerId: String!',
  '}',
  '',
  'scalar Date',
  '',
  'scalar Ulid',
  '',
  '"""',
  'Union of { optional:true, type: string } | { list:true, optional:true, type: int }',
  '"""',
  'scalar StoreEntity_unionField',
  '',
  'enum StoreEntity_letter {',
  '  a',
  '  b',
  '  c',
  '}',
  '',
  'enum StoreEntity_letterOptionalList {',
  '  x',
  '  y',
  '  z',
  '}',
  '',
  'type StoreEntity_deliveryAddress {',
  '  street: String!',
  '  number: Int',
  '}',
]);