superb-model.js v1.0.1
superb-model.js
Helps to create classes that can communicate to REST endpoints using AJAX for CRUD.
Installation
You can install from npm.
npm install superb-model.js --saveExamples
Fetching a model from server
import { Model, model, field } from 'superb-model.js';
@model('todos')
class Todo extends Model {
@field({ idKey: true })
id = null;
@field('detail')
description = null;
constructor(id, description) {
super();
this.id = id;
this.description = description;
}
}
const todo = new Todo(1);
await todo.get();Creating a new model
const todo = new Todo();
todo.description = 'Develop `model.js` Library';
await todo.post();Updating an existing model
const todo = new Todo(1);
await todo.get();
todo.description = 'Description updated';
await todo.put();Deleting a model
const todo = new Todo(1);
await todo.get();
await todo.remove();Using collections
import { Collection, collection } from 'superb-model.js';
import { Todo } from './todo';
@collection('todos')
class Todos extends Collection {
constructor() {
super(Todo);
}
}
const myTodos = new Todos()
await myTodos.query({ pagination: [1, 10], sort: { field: 'description', order: 'asc' } });API
@model decorator
Helps to define metadata information to a model class.
@model(string) - The passed string can be either an absolute or relative url to which the model is connected with.
@model(object)
The different parameters you can pass in the options object are:
url | string | Required - no | Absolute or relative url to which the model is connected with.
appendBase | boolean | Required - no | True to append the base url.
map | function | Required - no | The function that returns the model instance from the passed data.
emit | function | Required - no | The function that returns data from the model state.
validators | Array<function> | Required - no | Array of synchronous model validators.
asyncValidators | Array<function> | Required - no | Array of asynchronous model validators.
@field decorator
Helps to define metadata information to a property.
@field(string) - The alias name of the field.
@field(object)
The different parameters you can pass in the options object are listed below.
idKey | boolean | Required - no | True if the property is an id field.
alias | string | Required - no | The alias name of the field.
dataType | DataType | Required - no | The data type.
objectType| class | Required - no | The class type.
map | function | Required - no | The function that maps the data.
emit | function | Required - no | The function that returns data from the property state.
ignore | boolean,string | Required - no | Setting true ignores map/emit. Setting "in" ignores map and "out" ignores emit.
validators | Array<function> | Required - no | Array of validators.
asyncValidators | Array<function> | Required - no | Array of async validators.
required | boolean | Required - no | True if the field is required.
minLength | number | Required - no | The minimum length of characters required for the field.
maxLength | number | Required - no | The maximum length of characters acceptable for the field.
min | number | Required - no | The minimum value for the field.
max | number | Required - no | The maximum value for the field.
pattern | RegExp | Required - no | The regex pattern the field value should match.
@collection decorator
Helps to define metadata information to a collection class.
@collection(class) - The model class constructor.
@collection(object)
The different parameters you can pass in the options object are listed below.
type | class | Required - yes | The model class.
url | string | Required - no | Absolute or relative url to which the model is connected with.
appendBase | boolean | Required - no | True to append the base url.
map | function | Required - no | The function that returns the model instance from the passed data.
emit | function | Required - no | The function that returns data from the model state.
validators | Array<function> | Required - no | Array of synchronous model validators.
asyncValidators | Array<function> | Required - no | Array of asynchronous model validators.
Model class properties and methods
Properties
metadata | ModelMetadata | Returns the metadata defined in the model.
Methods
get(requestOptions)
put(requestOptions)
post(requestOptions)
save(requestOptions)
remove(requestOptions)
validate()
validateField(field)
valid(field)
errors(field)
toModel(data)
toData()
Collection class properties and methods
Credits
Inspired from Backbone.Model.