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

@simonbackx/simple-database

v1.24.0

Published

A simple library that helps you perform basic database operations (save, delete...) but gives you the possibility to write your own queries instead of using automated - non optimized - queries.

Downloads

120

Readme

Simple database

A simple library that helps you perform basic database operations (save, delete...) but gives you the possibility to write your own queries instead of using automated - non optimized - queries.

The idea is to provide a type safe ORM. The problem in most ORMs is that you never know what relation is already loaded and which is not. Simple database will generate compile time errors when a relation is not loaded by using the power of TypeScript.

Again, simplicity also means a bit more boilerplate code. But that will make the code only more customizeable and maintainable in the future.

Installation

Yarn

yarn add @simonbackx/simple-database

NPM

npm install @simonbackx/simple-database

Example

type TestModelWithFriends = TestModel & { friends: TestModel[] };

class TestModel extends Model {
    static table = "testModels";

    @column({ primary: true, type: "integer" })
    id: number | null = null;

    @column({ type: "string" })
    name: string;

    @column({ type: "integer" })
    count: number;

    @column({ type: "boolean" })
    isActive: boolean;

    @column({ type: "datetime" })
    createdOn: Date;

    @column({ type: "date", nullable: true })
    birthDay: Date | null = null;

    @column({ foreignKey: TestModel.partner, type: "integer", nullable: true })
    partnerId: number | null = null; // null = no address

    // Relations are defined as static properties, but you could define them however you like
    static partner = new ManyToOneRelation(TestModel, "partner");
    static friends = new ManyToManyRelation(TestModel, TestModel, "friends");

    static async getWithFriends(id: number): Promise<TestModelWithFriends | undefined> {
        // ... implementation with join here
        // note the type safe return type that indicates that the relation is loaded
    }

    // You can only use this method when the relation 'friends' is loaded
    logFriendCount(this: TestModelWithFriends) {
        console.log("I have " + this.friends.length + " friends");
    }
}

A very basic usage example would be the following code. Please note that we could still make some changes to the API to remove some boilerplate code (if possible). We would be able to make this shorter if TypeScript would support type guards on async methods.

const model = await TestModel.getByID(12);
if (!model) {
    throw new Error("Not found");
}
await TestModel.friends.load(model);
if (TestModel.friends.isLoaded(model)) {
    // model.friends is defined here
    console.log("He has " + model.friends.length + " friends");
    model.logFriendCount();
}

model.logFriendCount(); // will throw a compile error

Running tests

To run tests, make sure you create a .env file first. Setup a test database and run the migrations first.

yarn migrations

Example .env file

# Database
DB_HOST=localhost
DB_USER=root
DB_PASS=root
DB_DATABASE=simple-database-tests