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

sampledb

v0.0.13

Published

Sample data in a database style format.

Downloads

5

Readme

SampleDB

Sample data in a database-like structure. A core set of data is translated into Node-consumable data, IndexedDB data, and MongoDB data. Alternatively, you can load custom data into IndexedDB or MongoDB with a consistent interface accross database types.

Form of data

A 'row' ('record', 'line', etc) is an object with properties.

{ id: 1, fullname: "Jane Doe" }

A 'table' ('collection, 'objectStore', 'dataset', etc) is an array of such objects, usually, though not always, with shared properties.

[
    { id: 1, fullname: "Jane Doe" },
    { id: 2, fullname: "John Doe" }
]  

A database is an object with tables as properties.

{

    customers: [
        { id: 1, fullname: "Jane Doe" },
        { id: 2, fullname: "John Doe" }
    ],

    products: [
        { id: 123456, price: 5 },
        { id: 123457, price: 2 },
        { id: 123458, price: 1.5 },
        { id: 123459, price: 4 }
    ]

}

Getting Started

This is realy a package to help with development and testing, so, in your console, do:

npm install sampledb --save-dev

General Usage

The general idea is that you ...

let db = 
    sampledbConnector(dbName-or-url)
    .reset(json, keysToInclude, deleteIfKeyNotFound) // data resetting
    .connect() // connect to the source after any resetting
    .then(data => do something with data);
  • json: json data or retriever of json data (such as a filepath or url route)
  • keysToInclude: Which keys in json to include? Leave null or undefined to include all keys.
  • deleteIfKeyNotFound: boolean determining whether to drop tables not in the filter. Set to false or ignore to keep tables, but leave them unrefreshed.

For sampledb.server.js and sampledb.client.js, there's no source you connect to, so, resetting parameters exist right in the initialization. Also, you aren't left with a promise. Instead, just call it's data property and get working:

let rows = sampledbConnector(
    json, 
    keysToInclude,
    deleteIfKeysNotFound
).data;

sampledb.server.js

let sample = require('sampledb');
// or './node_modules/sampledb/dist/sampledb.server.js'

let connector = sampleServer(
    './test/SampleDB.json', // or just pass string of json data
    'students, scores' // or leave undefined for everything
);

let customersTable = connector.data.customers;
// do something with customersTable

sampledb.client.js

// This assumes your server is set up to server a 
// json file when './test/SampleDB.json' is routed 

import sample from './node_modules/sampledb/dist/sampledb.client.js'; 

async function getCustomers () {

    let connector = await sampleServer(
        './test/SampleDB.json', 
        'students, scores'
    );

    return connector.data.customers;

}

// do something with getCustomers

sampledb.mongo.js

let sampleMongo = require('./node_modules/sampledb/dist/sampledb.mongo.js');

async function listCollections() {
        
    // The third parameter to 'reset' means that any table not
    // named 'students' or 'scores' will be dropped.
    // Pass false or omit the parameter to keep the tables, but
    // to leave them unrefreshed.

    let db = await 
        sampleMongo('mongodb://localhost:27017/SampleDB')
        .reset('./test/SampleDB.json', 'students, scores', true)
        .connect();

    let collectionNames = 
        (await db.collections())
        .map(c => c.s.name);

    return collectionNames;

}

sampledb.idb.js

import sampleIdb from './node_modules/sampledb/dist/sampledb.idb.js';

async function getCustomerRows () {
        
    let db = await 
        sampleIdb('SampleDB')
        .reset('/test/SampleDB.json', null, true)
        .connect();

    let storeReqest = 
        db.transaction('customers')
        .objectStore('customers')
        .getAll();

    return new Promise((res,rej) => {
        storeReqest.onsuccess = event => {
            let rows = event.target.result;
            res(rows);
        };
        storeReqest.onerror = event => rej(storeReqest.error);
    });

}