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

layer-data

v0.0.8

Published

Multi-data source data layer for apps.

Downloads

3

Readme

layer-data

Multi-data source data layer for apps.

npm Build Status Coverage Status

Basics

Initialize a SQLite database

// Path of the database file
const db_path = 'data/my-data.db';

// Initialize database
const db = new SQLite(db_path);

// Retrieve a "Hello World"
const data = await db.queryData(`SELECT 'Hello World'`);

// Prints "Hello World"
console.log(data[0]);

Object-Relational Mapping

Aims to be not too complex, just useful enough.

Define Entity, Schema, Mapper and Repo

export interface PersonEntity{
    id: number;
    name: string;
}

export const PersonEntitySchema: SchemaOf<PersonEntity> = {
    properties:{
        "id": {type: "number"},
        "name": {type: "string"},
    },
    required: ["id", "name"]
};

export const PersonEntityMapper: EntityMapper<PersonEntity> = {
    table: "person",
    primaryKey: "id",
    autoIncrement: "id"
};

export class PersonRepo extends EntityRepository<PersonEntity>{
    constructor(readonly db: DataSource){
        super(db, PersonEntitySchema, PersonEntityMapper);
    }
}

Basic Use Cases

Initialize:

// Initialize DB
const db = new SQLite('my-data.db');

// Initialize Repo
const repo = new PersonRepo(db);

Insert an Entity

const person  = repo.insert({id: 0, name: "John Doe"});

// Prints 1, since id is auto-increment
console.log(person.id);

Retrieve an Entity

const person = repo.getOne(1);

// Prints "John Doe"
console.log(person.name);

Update an Entity

person.name = 'John Smith';

// Updates on DB
repo.update(person);

Delete an Entity

repo.delete(person);

const person = repo.getOne(1);

// person is undefined

Migrations Support

Migrations are loaded from a directory on a flat structure of .sql files.

Basic rules:

  • Migrations must be .sql files
  • Migrations need a version identifier
  • Double underscore __ separates version from migration name
  • Migrations should not change once applied
  • Migrations are persisted in a database table called schema_migration
  • It only takes one line to initialize the migrations support:
   migrationManager.makeSureMigrationsAreUpToDate();

Example directory:

/data
  V1__Base_Schema.sql
  V2__Add_Table_Product.sql
  V3__Add_Column_Price.sql

Typical Initialization:

 // Initialize DB
 const db = new SQLite('my-data.db');

// Path to folder where migrations are contained
schemaPath = 'data/migrations/sqlite';

// Initialize MigrationManager
MigrationManager mm = new MigrationManager(db, {schemaPath});

// Run migration manager
mm.makeSureMigrationsAreUpToDate();