3.0.0 • Published 9 months ago

@trxn/hapify-core v3.0.0

Weekly downloads
-
License
-
Repository
github
Last release
9 months ago

hapify-core

Library to describe data models in TypeScript. It also includes helpers to test and filter data models and their fields.

Data models definition

const idField = new NumberBasicField('Id').setPrimary(true).makeNotWritable();

const userModel = new Model('User')
    .addField(new KeyStringField('Id'))
    .addField(new KeyStringField('RoleId'))
    .addField(new RelationField('Role'))
    .addField(new StringBasicField('FirstName').setMaxLength(50))
    .addField(new StringBasicField('LastName').setMaxLength(50))
    .addField(new StringEmailField('Email'))
    .addField(
      new StringPasswordField('Password')
        .setValidationRegex('/([0-9a-z]+)/')
        .makeNotReadable(),
    )
    .addField(
      new StringTextField('Description')
        .setNullable(true)
        .addMetadata('foo', 'bar'),
    )
    .addField(new BooleanField('Enabled').setDefaultValue(false))
    .addField(
      new NumberIntegerField('Credits')
        .setDefaultValue(10)
        .setMax(1000)
        .setNotes('Amount of credits remaining'),
    ),
    
const roleModel = new Model('Role').addField(idField).addField(
  new StringBasicField('Name')
    .setMaxLength(50)
    .setUnique(true)
    .setNotes('Role name'),
)
    .addField(new KeyStringField('Id'))
    .addField(new RelationField('Users'))

const userRoleRelation = new Relation({
  name: 'UserRole',
  referer: {
    model: userModel,
    scalarField: userModel.keyField('RoleId'),
    virtualField: userModel.relationField('Role'),
    cardinality: 'one',
  },
  referee: {
    model: roleModel,
    scalarField: roleModel.keyField('Id'),
    virtualField: roleModel.relationField('users'),
    cardinality: 'many',
  },
})

getRelationFields(model)
getRelationField(model, name);

const project = new Project('app')
  .addModel(userModel)
  .addModel(roleModel)
  .addRelation(userRoleRelation);

Playing with data models

const modelsWithPublicSearchAndCount = project.models
  .filter(and(isModelActionPublic('search'), isModelActionPublic('count')));
const modelsWithDependencyOrSelfReferenced = project.models
  .filter(or(hasDependencies, isSelfReferenced));
const firstModel = project.models[0];
if (hasOwner(firstModel)) {
  const ownerChain = getOwners(firstModel);
}

Playing with data models fields

const firstModel = project.models[0];
const publicStringAndNumberNames = firstModel.fields
  .filter(and(or(isString, isNumber()), isFieldActionPublic('read')))
  .map((field) => kebab(field.name))
  .join(', ');
3.0.0

9 months ago

2.2.7

10 months ago

2.2.5

11 months ago

2.2.1

11 months ago

2.2.0

12 months ago

2.2.3

11 months ago

2.2.2

11 months ago

2.2.4

11 months ago

2.1.22-next.5

12 months ago

2.1.22-next.3

12 months ago

2.1.22-next.4

12 months ago

2.1.22-next.1

12 months ago

2.1.22-next.2

12 months ago

2.1.22-next.0

12 months ago

2.1.12

1 year ago

2.1.11

1 year ago

2.1.10

1 year ago

2.1.9

1 year ago

2.1.8

1 year ago

2.1.7

1 year ago