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

tdmnco-model

v0.7.0

Published

Model.js is a simple model layer for web applications.

Downloads

16

Readme

Model.js NPM Version

What is Model.js?

Model.js is a simple model layer for web applications. It supports a dynamic model definition scheme based on JSON properties.

It is used internally at Tidemann&Co for all our web applications that require a model layer in JavaScript.

↑ Back to top

Installation

Installation is done via npm:

$ npm install tdmnco-model

↑ Back to top

Documentation

Using Model.js is simple and easy. Consider the following example:

import Model from 'tdmnco-model'

class Post extends Model {}

let post = new Post({
  author: '',
  content: 'I love cherries!',
  id: '1',
  title: 'A tribute to cherries'
})

post.author = 'Kasper Tidemann'

post.save()

↑ Back to top

Prevent uglifying model names

If you use Parcel, Webpack or a similar bundler, your model names will be uglified when minimized for production.

This results in data stored in localStorage or sessionStorage with names like i-1 and y-2 instead of Post-1 and User-2.

In order to prevent this, prototype the name of a given model:

import Model from 'tdmnco-model'

class Post extends Model {}

Post.prototype.modelName = 'Post'

export { Post }

↑ Back to top

Build (distributable)

In order to build the Model.js, issue the following command in your terminal:

$ npm run dist

After running the above command, the distributable version of model.js will be available in the dist/js/ folder.

Example of building Model.js

↑ Back to top

Test

Model.js makes use of Jest for its test suite. In order to run the development test suite, issue the following command in your terminal:

$ npm run test:dev

Example of testing Model.js

↑ Back to top

API

Model.js exposes the following functions for working with models and instances. In the following, Model refers to the static class and instance refers to an instance of the model in question:

new Model([contents])

Constructor used to create a new instance of a model:

new Car({
  id: '4',
  manufacturer: 'BMW',
  model: '530d'
})

↑ Back to top

Model.delete()

Function for deleting all instances of a model:

Car.delete()

Note that this effectively empties the memory cache containing all instances of the given model.

↑ Back to top

Model.first([query])

Function for getting the first instance of a model:

const car = Car.first()

The function can be called with a query in order to return the first instance that matches the query:

const band = new Band({
  bandName: 'Metallica',
  id: '17'
})

band.save()

const metallica = Band.first({ bandName: 'Metallica' })

↑ Back to top

Model.get([query])

Function for getting instances of a model. It supports getting an instance by passing an id, by passing an array of ids or by passing a query. Additionally, it supports getting all instances of a model by not passing any arguments to the function:

Model.get([id])

const apple = Apple.get('17')

↑ Back to top

Model.get([ids])

const apples = Apple.get(['17', '18', '19'])

↑ Back to top

Model.get([query])

const bmws = Car.get({ manufacturer: 'BMW' })

↑ Back to top

Model.get()

const cars = Car.get()

↑ Back to top

Model.loadJSON()

Function for loading JSON data into a model, typically used after fetching JSON from an endpoint:

const data = [
  {
    abv: 6.2,
    brewery: 'Alefarm Brewing',
    id: '1',
    name: 'Elevate',
    type: 'IPA'
  },
  {
    abv: 8,
    brewery: 'Alefarm Brewing',
    id: '2',
    name: 'Because Babylon',
    type: 'DIPA'
  }
]

Beer.loadJSON(data)

↑ Back to top

instance.delete()

Function for deleting an instance of a model from the memory cache:

const animal = new Animal({
  id: '392',
  type: 'rabbit'
})

animal.save()

animal.delete()

↑ Back to top

instance.onbeforedelete([callback])

Function for adding a callback to the deletion of an instance, calling the callback function before the instance is deleted:

const phone = new Phone({
  brand: 'iPhone'
  id: '944',
  model: 'X'
})

phone.onbeforedelete(() => {
  alert('Instance will be deleted now!')
})

↑ Back to top

instance.onbeforeupdate([callback])

Function for adding a callback to the update of an instance property, calling the callback function before the property is updated:

const phone = Phone.get('944')

phone.onbeforeupdate((property, before, after) => {
  console.log(property + ' is ' + before + ' but will be changed to ' + after)
})

↑ Back to top

instance.save()

Function for saving an instance of a model to the memory cache:

const building = new Building({
  id: '2359',
  type: 'house'
})

building.save()

↑ Back to top

Getting Help

We believe in an open and welcoming community for all. Please post your questions in the Issues section here at GitHub or contact Kasper Tidemann directly at [email protected].

Note that if your question has general relevance, it might be worth sharing with others.


Thanks for reading!

🎁