npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

superb-model.js

v1.0.1

Published

Model classes that automatically connects with server

Downloads

3

Readme

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.