Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
@ark7/model
Advanced tools
Readme
One major issue that often results in numerous bugs is the replication of identical models. To mitigate this problem, the @ark7/model library offers a model class layer that can be utilized across various environments, ensuring consistent logic throughout.
Install the dependencies package:
$ npm install @ark7/model
Add transform plugin to tsconfig.json:
// tsconfig.json
{
...
"plugins": [{
"transform": "@ark7/model/transformer"
}],
}
Models are defined by decorating the class with A7Model
, or using
A7Model.provide(ModelClass)
.
// models/users.ts
import { A7Model } from '@ark7/model';
@A7Model({})
export class Name {
readonly first: string;
last: string;
}
export enum Gender {
MALE = 'MALE',
FEMALE = 'FEMALE',
}
@A7Model({})
export class User {
email: string;
name?: Name;
gender?: Gender;
}
// Another way to register User model:
//
// A7Model.provide(User)
Once the model is defined, class metadata, field metadata, and model schema are
retrievable through A7Model.getMetadata(ModelClass)
. For example:
@A7Model({})
class Name {
first: string;
last: string;
}
A7Model.getMetadata(Name).should.be.deepEqual({
modelClass: Name.prototype.constructor,
superClass: null,
configs: {
schema: {
name: 'Name',
props: [
{
modifier: 'PUBLIC',
name: 'first',
optional: false,
readonly: false,
type: 'string',
},
{
modifier: 'PUBLIC',
name: 'last',
optional: false,
readonly: false,
type: 'string',
},
],
},
},
fields: {},
name: 'Name',
});
Model level configuration can be injected by either @A7Model()
or @Config()
:
@A7Model<ModelConfig>({ foo: 'bar' })
class MCModel { }
interface ModelConfig {
foo: string;
}
(A7Model.getMetadata(MCModel).configs as ModelConfig).foo.should.be.equal(
'bar'
);
@A7Model({
discriminatorKey: 'kind',
})
class Event extends StrictModel {
kind?: string;
}
@A7Model({})
class MouseEvent extends Event {
foo: string;
}
const ins = EventModel.modelize({
kind: 'MouseEvent',
foo: 'bar',
} as any);
ins.should.be.instanceof(MouseEvent);
const ins2 = MouseEvent.modelize({
foo: 'bar',
});
ins2.should.be.instanceof(MouseEvent);
ins2.toObject().should.be.deepEqual({
kind: 'MouseEvent',
foo: 'bar',
});
A model can mixin other models.
@A7Model({})
class M1 {
foo: string;
}
@A7Model({})
class M2 {
bar: string;
}
@A7Model({})
@Mixin(M1)
@Mixin(M2)
class CombinedModel extends Model {}
interface CombinedModel extends M1, M2 {}
The required
modifier can be declared on field metadata
or schema
level:
class Name {
first: string; // schema level required
@Required() // field metadata level required
last: string; // schema level required
}
Sometimes, the two levels may have conflict opinions:
class Name {
first?: string; // schema level optional
@Required(false) // field metadata level: optional
last: string; // schema level: required
}
It depends on the adaptor to deal with those conflicts.
The readonly
modifier can be declared on field metadata
or schema
level:
class Name {
readonly first: string; // schema level readonly
@Readonly() // field metadata level readonly
last: string; // schema level non-readonly
}
It depends on the adaptor to deal with the conflicts.
The default
value can be set on field metadata
:
class Name {
@Default('foo')
first: string;
@Default(() => 'bar')
last: string;
}
import { A7Model, StrictModel } from '@ark7/model';
@A7Model({})
class Name extends StrictModel {
first: string;
last: string;
}
@A7Model({})
export class User extends StrictModel {
email: string;
name?: Name;
}
const user = User.modelize({
email: 'test@google.com',
name: {
first: 'foo',
last: 'bar',
}
});
user.should.be.instanceof(User);
user.name.should.be.instanceof(Name);
import { A7Model, StrictModel } from '@ark7/model';
@A7Model({})
class Name extends StrictModel {
first: string;
last: string;
}
@A7Model({})
export class User extends StrictModel {
email: string;
name?: Name;
}
const user = User.modelize({
email: 'test@google.com',
name: {
first: 'foo',
last: 'bar',
}
});
user.toObject().should.be.instanceof({
email: 'test@google.com',
name: {
first: 'foo',
last: 'bar',
}
});
Each field will be assigned a level number. The higher the level number, the more restrict or confidential the field is. We have pre-defined five data levels:
1. BASIC (10) - The basic field that will be used in the most scenarios.
Usually, presented when it's referenced by other model.
2. SHORT (20) - The fields that are useful for displaying as a list or
table. Usually, presented in the find or search endpoints.
3. DETAIL (30) - The fields that contains detail information. Usually,
presented in the get endpoints.
4. CONFIDENTIAL (40) - The fields that contains sensitive information.
Usually, not returning to the client or only to
admins with special privileges.
5. NEVER (1000) - The fields that are never returns.
Projection
We can do the projection by providing a filter level. Any fields with level numbers that are smaller or equal to the filter level will be projected. You can tune the filter level by specifying the passLevelMap in the option.
@A7Model({})
class Name extends StrictModel {
@Basic() first: string;
@Basic() last: string;
}
@A7Model({})
export class User extends StrictModel {
@Basic() email: string;
@Short() name?: Name;
}
const user = User.modelize({
email: 'test@google.com',
name: {
first: 'foo',
last: 'bar',
}
});
user.toObject({ level: DefaultDataLevel.BASIC }).should.be.instanceof({
email: 'test@google.com',
});
user.toObject({ level: DefaultDataLevel.SHORT }).should.be.instanceof({
email: 'test@google.com',
name: {
first: 'foo',
last: 'bar',
},
});
Population
For a reference field, when the filter level is greater than the populateLevel specified by the option, the field will be populated.
@A7Model({})
export class User extends Model {
@Virtual({ ... })
@Level({ populateLevel: DefaultDataLevel.DETAIL })
posts: Post[];
}
@A7Model({})
export class Post extends Model {
author: Ref<User>;
}
Sometimes, we need to attach metadata to an instance.
import { A7Model, StrictModel } from '@ark7/model';
@A7Model({})
class Name extends StrictModel {
first: string;
last: string;
}
const name = Name.modelize({ first: 'foo', last: 'bar'});
name.$attach({ hello: 'world' });
name.$attach().should.be.deepEqual({
__$attach: true,
hello: 'world',
});
// This won't affect toObject() or toJSON():
name.toObject().should.be.deepEqual({
first: 'foo',
last: 'bar',
});
Email address.
UUID.
FAQs
Ark7 model used for both backend and frontend
The npm package @ark7/model receives a total of 140 weekly downloads. As such, @ark7/model popularity was classified as not popular.
We found that @ark7/model demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.