Socket
Socket
Sign inDemoInstall

superb-model.js

Package Overview
Dependencies
3
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    superb-model.js

Model classes that automatically connects with server


Version published
Weekly downloads
4
increased by33.33%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

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 --save

Examples

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.

FAQs

Last updated on 04 Dec 2020

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc