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

sackets

v1.0.0

Published

(Typescript) well tested, easy to use and highly extendable ActiveRecord library for browser and node js with code generation

Downloads

1

Readme

sackets

sackets is an easy to use and highly extendable typed mongo db like query based TypeScript ActiveRecord library for browser and node js with code generation


Quick Start

  • Install with npm
npm install sackets --save
  • Create a models.js (or models.json if you prefer json) file for meta information of your models similar to the following
module.exports = {
    "Item": {
        "id": "number", // required
        "name": "string",
        "price": "number",
        "category": "Category", // belongs to
        "toString": "name" // string representation of Item object
    },
    "Category": {
        "id": "number",
        "name": "string",
        "items": "ItemCollection", // has many
        "toString": "name"
    }
};
  • Create a modelsgen.js file in your project with the following content
var generator = require('sackets/generator');
var models = require('./models'); // get the model meta created above
var path = require('path');

generator.generate({
    models: models,
    path: path.resolve(__dirname + '/app/models'), // the path where code will be generated.
});
  • Run modelsgen script with node
node modelsgen.js
  • Files will be generated as following
- app
    - models
        - base
            - Category.ts // Generated every time overwriting the existing. Contains generated code based on the meta information
            - Item.ts
            - index.ts
        - Category.ts // Generated first time only, if not exists. Contains your custom code
        - CategoryCollection.ts
        - Item.ts
        - ItemCollection.ts
        - index.ts // barrel file exporting all the models
  • Place your custom code in app/models/Item.ts for single item queries or in app/models/ItemCollection.ts for item collection queries
// app/models/ItemCollection.ts
import {ItemCollection as ItemCollectionBase} from './base/index';
import {Category} from './Category';

export class ItemCollection extends ItemCollectionBase {
    findExpensiveByCategory(category: Category){
        return this.find({
            categoryId: category.id,
            price: {$gt: 100},
            $sort: {price: -1}
        }); // typed query filter
    }
}
  • Use the code in your components or services
import {Item, Category} from 'app/models/index';

class MyComponent {
    category = Category.model({id: 20});
    items = Item.collection();
    constructor(){
        this.items.findExpensiveByCategory(category).then(()=> {
            // now `this.items` collection has expensive items
            console.log(this.items);
        }).catch((err: any)=> {
            console.log(err);
        });
    }
}

Note:

Unlike other frameworks, sackets self-fills the object(s) which makes it a perfect data handling library for data binding frameworks like Angular.

<ul>
    <li *ngFor="let item of items">
        {{ item.name }} {{ item.price }}
    </li>
</ul>