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

@bithero/simple-rest-orm

v1.0.3

Published

Simple ORM for REST APIs

Downloads

7

Readme

Simple-rest-ORM

An Simple ORM for rest API's

License

AGPL-3.0-or-later; see LICENSE file for more details

Usage

import { jsonName, jsonProperty } from 'ts-serializable';
import { entity, Entity, registerClient } from '@bithero/simple-rest-orm';
import 'reflect-metadata';

// Defining an entity by extending Entity; with the @entity decorator you can specify extra options
// such as the entity name to use or the clientname
@entity({name: 'my-entity', client: 'myClient'})
class MyEntity extends Entity {
    @jsonName('_name')
    @jsonProperty(String)
    private name: string;

    @jsonProperty(String)
    public city: string;

    // Constructors need to have default arguments because the serlizer needs an
    //  default constructor (i.e. `new MyEntity();`)
    public constructor(name: string = "") {
        super();
        this.name = name;
    }

    public getName(): string {
        return this.name;
    }
}

// Registering an client to use; the name (second argument) is optional and defaults to 'default'
registerClient({
    // each buildRequest* function can either return an Request object, an URL or an string containing an url
    buildRequestFind(entityName, id) {
        return `http://localhost:3000/api/${entityName}/${id}`;
    },
    buildRequestAll(entityName, query) {
        return new URL(`http://localhost:3000/api/${entityName}`);
    },
    buildRequestQuery(entityName, query) {
        return new Request(`http://localhost:3000/api/${entityName}/query`, { method: 'POST', body: JSON.stringify(query) });
    },
    buildRequestDelete(entityName, id) {
        return new Request(`http://localhost:3000/api/${entityName}/${id}`, { method: 'DELETE' });
    },
    buildRequestCreate(entityName, data) {
        return new Request(`http://localhost:3000/api/${entityName}`, { method: 'POST', body: JSON.stringify(data) });
    },
    buildRequestUpdate(entityName, data) {
        return new Request(`http://localhost:3000/api/${entityName}/${data.id}`, { method: 'PUT', body: JSON.stringify(data) });
    },
    sendRequest: fetch,
}, 'myClient');

// retriving an single entitiy
let result = await MyEntity.find("123");

// retrieving all entities
let all = await MyEntity.all();

// quering multiple entities; the query can be completly custom and is defined by your API
let results = await MyEntity.query({ name: { $regex: "^Peter" } });

// deleting either works on an entity
result.delete();
// ... or via the entity
MyEntity.delete("1234");

// creating entities is as simple as instantiate an object and calling `save()`
let newEntity = new MyEntity("Dan");
newEntity.save();

// ... and updateing is even simpler!
newEntity.city = "London";
newEntity.save();

Associations

import { jsonProperty } from 'ts-serializable';
import { entity, Entity, oneToMany, oneToOne } from '@bithero/simple-rest-orm';
import 'reflect-metadata';

@entity({ name: 'ref-entity' })
class RefEntity extends Entity {}

@entity({name: 'my-entity'})
class MyEntity extends Entity {

    // Uses an subroute to lookup the references; i.e.:
    //  ==> GET /my-entity/<this.id>/refs
    // Note: this field is generated and completly overwritten after de-serialization
    @oneToMany({ type: 'subroute', subroute: 'refs', entity: RefEntity })
    private refs_a: RefEntity[];

    // Uses an array of id's inside this entity to lookup the references:
    //  this['refs_b'] = [ '1234', '5678' ]
    //      ==> GET /ref-entity/1234
    //      ==> GET /ref-entity/5678
    @oneToMany({ type: 'arrayOfIds', entity: RefEntity })
    @jsonProperty([String])     // Needed in order to de-serialize the array of id's
    private refs_b: RefEntity[];

    // Uses an single id inside this entity to lookup the reference
    //  this['ref'] = '1234'
    //    ==>
    //  this['ref'] = fetch( GET /ref-entity/1234 )
    @oneToOne({ entity: RefEntity })
    @jsonProperty(String)       // Needed in order to de-serialize the id
    private ref: RefEntity;
}