quave:definitions

v1.0.5Published 2 years ago

quave:definitions

quave:definitions is a Meteor package that provides a way for you to declare your models and enums.

Why

It is desired to have a centralized way to declare schemas for your MongoDB collection and also your GraphQL schema.

We believe we are not reinventing the wheel in this package but what we are doing is like putting together the wheels in the vehicle :).

Installation

meteor add quave:definitions

Usage

The idea is to be as close as possible to a SimpleSchema schema definition but also supporting additional properties when necessary for GraphQL and other necessities.

createModelDefinition

You provide a name for your model and fields, the fields is your schema definition.

See that you can use custom types from meteor/quave:custom-type-* like DateTime and also in combination with createEnumDefinition (see more below).

We have also special properties:

  • in the root
    • pluralName: if your model has an irregular name
  • in the fields
    • graphQLType: if you need to specify a type (scalar or special type) that has a different name in GraphQL than in MongoDB
    • graphQLOptionalInput: if the input in GraphQL has a different optional value
1import { createModelDefinition } from 'meteor/quave:definitions';
2import { DateTimeType } from 'meteor/quave:custom-type-date-time/DateTimeType';
3import { PlayerPositionDefinition } from './PlayerPositionEnum';
4
5export const PlayerDefinition = createModelDefinition({
6  name: 'Player',
7  fields: {
8    name: {
9      type: String,
10    },
11    birthday: {
12      type: DateTimeType,
13      optional: true,
14    },
15    position: {
16      ...PlayerPositionDefinition.toSimpleSchemaField(),
17      optional: true,
18    },
19  },
20});
21
22export const PlayerSchema = PlayerDefinition.toSimpleSchema();

createEnumDefinition

You provide a name for your enum and options, the options are your static definitions in a limited domain.

Functions:

  • toSimpleSchemaField: transforms the enum definition into field properties, like allowedValues, type, etc
  • toEnum: returns the options adding the option key as a field called value to each item.
1import { createEnumDefinition } from 'meteor/quave:definitions';
2
3export const PlayerPositionDefinition = createEnumDefinition({
4  name: 'PlayerPosition',
5  options: {
6    GOLEIRO: {
7      name: 'Goleiro',
8    },
9    LATERAL_DIREITO: {
10      name: 'Lateral Direito',
11    },
12    LATERAL_ESQUERDO: {
13      name: 'Lateral Esquerdo',
14    },
15    ZAGUEIRO: {
16      name: 'Zagueiro',
17    },
18    VOLANTE: {
19      name: 'Volante',
20    },
21    MEIA: {
22      name: 'Meia',
23    },
24    ATACANTE: {
25      name: 'Atacante',
26    },
27    PONTA_DIREITA: {
28      name: 'Ponta Direita',
29    },
30    PONTA_ESQUERDA: {
31      name: 'Ponta Esquerda',
32    },
33  },
34});
35
36export const PlayerPosition = PlayerPositionDefinition.toEnum();
37

License

MIT