# @trxn/hapify-core

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

Latest version **3.0.0** (published 2023-08-17) · 0 weekly downloads

## Install

```sh
npm install @trxn/hapify-core
pnpm add @trxn/hapify-core
yarn add @trxn/hapify-core
bun add @trxn/hapify-core
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2023-08-17 |
| First published | 2023-03-15 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 640.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 52 |
| Maintainers | floross, edouarddemotes, dt-tractr |

## Links

- npm: https://www.npmjs.com/package/@trxn/hapify-core
- Repository: https://github.com/tractr/traxion
- Homepage: https://github.com/tractr/traxion#readme
- Issues: https://github.com/tractr/traxion/issues
- npm.io page: https://npm.io/package/@trxn/hapify-core

## Dependencies (3)

- [util](https://npm.io/package/util.md) 0.12.4
- [pluralize](https://npm.io/package/pluralize.md) 8.0.0
- [ts-toolbelt](https://npm.io/package/ts-toolbelt.md) 9.6.0

## Recent versions

- 3.0.0 (latest) — 2023-08-17
- 2.1.22-next.5 (next) — 2023-05-24
- 2.2.7 — 2023-07-18
- 2.2.5 — 2023-06-15
- 2.2.4 — 2023-06-02
- 2.2.3 — 2023-06-02
- 2.2.2 — 2023-06-02
- 2.2.1 — 2023-06-02
- 2.2.0 — 2023-05-29
- 2.1.22-next.4 — 2023-05-23
- 2.1.22-next.3 — 2023-05-22
- 2.1.22-next.2 — 2023-05-22
- 2.1.22-next.1 — 2023-05-14
- 2.1.22-next.0 — 2023-05-11
- 2.1.12 — 2023-03-24
- … 5 more at https://npm.io/package/@trxn/hapify-core/versions

## README

# 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

```typescript
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

```typescript
const modelsWithPublicSearchAndCount = project.models
  .filter(and(isModelActionPublic('search'), isModelActionPublic('count')));
```

```typescript
const modelsWithDependencyOrSelfReferenced = project.models
  .filter(or(hasDependencies, isSelfReferenced));
```

```typescript
const firstModel = project.models[0];
if (hasOwner(firstModel)) {
  const ownerChain = getOwners(firstModel);
}
```

## Playing with data models fields

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

---
_Source: https://npm.io/package/@trxn/hapify-core · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
