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

@apparts/model

v5.2.0

Published

A model for reading and writing to the database. It stores data.

Downloads

86

Readme

#+TITLE: Apparts-Model #+DATE: [2020-08-06 Thu] #+AUTHOR: Philipp Uhl

  • Usage

You can either work with one, multiple, or no elements. The respective classes are ~OneModel~, ~ManyModel~, and ~NoneModel~. You can extend these classes by using them:

#+BEGIN_SRC js const { useModel, makeModel } = require("@apparts/model");

const types = { id: { /* Types as defined by @apparts/types / type: "id", / Will be shown in output of getPublic*/ public: true, /* Auto increment this field / auto: true, / This is a key. Combined keys are possible, too, by setting { key: true } on multiple fields. / key: true }
optionalVal: { type: "string", /
This field can be NULL / optional: true
}, derivedValue: { type: "string", public: true, /
This field will not be persisted. It's value is derived from other values and will be generated on the fly. When using the getPublic method, while having derived values, you must first call generateDerived. / derived: async (c, model) => { / c is the content of the model, model is the whole Model object / return (await model._dbs._getSomeRelation()).text; } }, createdOn: { type: "time", / This field will be set to the functions result, if the field is NULL. The function recieves the element as parameter. / default: (c) => c.optionalVal || Date.now() }, fixedDefault: { type: "int", / This field will be set to the given value, if the field is NULL. / default: 7, / This field will be named "someNumber" in the output of getPublic */ mapped: "someNumber", public: true }, };

const [Users, _User, NoUser] = useModel(types, "users");

// You can extend the classes like this: class User extends _User { constructor(dbs, content) { super(dbs, content); // do something here } additionalFunc() { return this.content; } }

module.exports = makeModel("User", [Users, User, NoUser]); #+END_SRC

The extended classes can then be used:

#+BEGIN_SRC js // get dbs from "@apparts/db" somehow const peter = await new User(dbs, { optionalVal: "Peter"}).store(); console.log(peter.content); // => { id: 1, optionalVal: "Peter", createdOn: 12345, fixedDefault: 7 } #+END_SRC

** =useModel= and =makeModel=

@apparts/model exports two functions: =useModel= and =makeModel=:

  • =useModel : (, ) => <[ManyModel, OneModel, NoneModel]>= :: A function that takes the type definition of the model and the name for the collection (with Postgresql that would be the table name). It returns an array with a =ManyModel=, an =OneModel= and a =NoneModel= for the given type and collection name.
  • =makeModel : (, [ManyModel, OneModel, NoneModel]) => { use, , s, No }= :: A function that is intended to generate what a file exports, that defines a model. It takes the overloaded classes as input and the models name. It returns an object with a function that works similar to =useModel= and the respective Many-, One-, NoneModel but named according to the =name=-parameter.
    • =use : (?dbs) => [s, , No]= :: The =use= function can be either invoked with a [[https://github.com/phuhl/apparts-db][Dbs]] or without. When invoked with a Dbs, the returned Models will have the Dbs set already and have a changed constructor signature: The ManyModel and OneModel take the content as first and only parameter, the NoneModel doesn't take a parameter at all.

** ManyModel

Values:

  • ~contents~ :: Array, that contains the values. The values can be edited. To save the changes, call ~update~.

Functions:

  • ~constructor(dbs, ?[contents])~ :: Constructor, when used via =use()=.
  • ~constructor(?[contents])~ :: Constructor, when used via =use(dbs)=.
  • ~async load(filter, ?limit, ?offset, ?order)~ :: Load data, filter grammar is as defined by the =find= function of the =dbs=. For more information, see below at in the section "Filter Grammar".
  • ~async loadByIds(ids, ?limit, ?offset)~ :: Load data by IDs. For models with only one key, you can use ~[, , ...]~, for a model with multiple keys, you have to use ~{ : [ , ...], : [, ...], ...}~.
  • ~async store()~ :: Saves to the database
  • ~async update()~ :: Updates the values, if you updated the contents.
  • ~length()~ :: Length of ~contents~
  • ~set(field, val)~ :: Set a field on all elements
  • ~setF(field, f)~ :: Set a field on all elements through a function, that receives the element and returns the new value
  • ~async deleteAll()~ :: Delete all from the loaded collection from the database. If any of the items is referenced by another item, =IsReference= will be thrown.
  • ~getPublic()~ :: Get the public representation (marked as public in the types definition by the key ~public (bool)~). If there are derived values in the type, =generateDerived= must be called first!
  • =async generateDerived()= :: Generate derived values. The derived values will be saved in =_derived=. This function must be called before =getPublic= is called, if derived values exist in the type.
  • =static getTypes()= :: Returns the type of the model

** OneModel

Values:

  • ~content~ :: Object, that contains the values. The values can be edited. To save the changes, call ~update~.

Functions:

  • ~constructor(dbs, ?content)~ :: Constructor, when used via =use()=.
  • ~constructor(?content)~ :: Constructor, when used via =use(dbs)=.
  • ~async load(filter)~ :: Load one item. If more than one item was found that satisfies the filter, a =NotUnique= error will be thrown. If no item was found, a =NotFound= error will be thrown. The filter grammar is as defined by the =find= function of the =dbs=. For more information, see below at in the section "Filter Grammar".
  • ~async loadById(id)~ :: Load data by ID. For models with only one key, you can use ~~, for a model with multiple keys, you have to use ~{ : , : , ... }~.
  • ~async store()~ :: Saves to the database
  • ~async update()~ :: Updates the values, if you updated the contents.
  • ~set(field, val)~ :: Set a field on all elements
  • ~async delete()~ :: Delete this element from the database. If the item is referenced by another item, =IsReference= will be thrown.
  • ~getPublic()~ :: Get the public representation (marked as public in the types definition by the key ~public (bool)~). If there are derived values in the type, =generateDerived= must be called first!
  • =async generateDerived()= :: Generate derived values. The derived values will be saved in =_derived=. This function must be called before =getPublic= is called, if derived values exist in the type.
  • =static getTypes()= :: Returns the type of the model

** NoneModel

Functions:

  • ~constructor(dbs)~ :: Constructor
  • ~async loadNone(filter)~ :: Throws an ~DoesExist~ error, if something was loaded, does nothing if nothing was loaded. The filter grammar is as defined by the =find= function of the =dbs=. For more information, see below at in the section "Filter Grammar".
  • =static getTypes()= :: Returns the type of the model

** Errors

  • ~DoesExist~
  • ~NotFound~
  • ~NotUnique~
  • =IsReference=
  • =ConstraintFailed=

#+BEGIN_SRC js const { NotUnique, NotFound, DoesExist, IsReference, ConstraintFailed } = require("@apparts/model"); #+END_SRC

** Filter Grammar

The filter syntax is like this:

#+BEGIN_SRC js const filter = { : , ...}; // where is a key from the type and // where matcher is = | { op: , val: } | { op: , val: } | { op: "and", val: } // logical and for all subconditions = lte // less than or equals | lt // less than | gte // greater than or equals | gt // greater than = like // sql like, a string comparison where the "%" character // will be matched against anything. E.g. "bread%crumb" // matches "bread crumb" or "bread eating crumb". = | | | null #+END_SRC