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

portable-db-ts

v1.0.2

Published

A TypeScript library that provides a simple, in-memory database with a flexible API. It's designed to be portable, making it easy to integrate into your projects.

Downloads

38

Readme

portable-db-ts

portable-db-ts is a TypeScript library that provides a simple, in-memory database with a flexible API. It's designed to be portable, making it easy to integrate into your projects.

npm version License

Installation

npm install portable-db-ts
# or
yarn add portable-db-ts

Usage

// Import the necessary modules
import { DB, StoreInMemory } from 'portable-db-ts';

// Define your model structure
type TTask = {
  id: string; // Every collection entry must have a string id
  title: string;
  isDone: boolean;
  tags: string[];
  description: string;
  dateCreated: string;
  effort: number | null;
};

const db = new DB<
  { task: TTask }, // Define your collections
  { isDarkMode: boolean; } // Define your records
>(new StoreInMemory());

// Create a new task
function newTask(
  id: string,
  isDone: boolean,
  effort: number | null,
  tags: string[] = [],
) {
  return {
    id,
    title: `Task #${id}`,
    description: `Task #${id}`,
    isDone,
    tags,
    effort,
    dateCreated: Date.now(),
  };
}

// ... (continue with other usage examples)

Examples

Using records

// Get a database record for managing dark mode status
const isDarkModeRecord = db.record('isDarkMode', () => false);

// Get the current dark mode status
const isDarkMode = await isDarkModeRecord.get();

// Toggle the dark mode status and update the database record
await isDarkModeRecord.set(!isDarkMode);

Using Index Rules to Build Collections

// Create a collection with index rules
const taskCollection = await db.collection('task', {
  // This defines indexes for the collection. They are required for query capabilities.
  // You only need to index the fields used within queries
  title: 'stringNum',
  description: 'string',
  isDone: 'boolean',
  tags: 'string', // Array fields get indexed by their entry type
  dateCreated: 'number',
  effort: 'number',
});

Setting Data (upsert)

// Add a new task
const newTask = newTask('42', false, 3, ['tag1', 'tag2']);
await taskCollection.set('42', newTask);

Deleting Data

// Delete a task
await taskCollection.delete('42');

Retrieving a Single Entry

// Retrieve a single task by ID
const taskId = '42';
const task = await taskCollection.get(taskId);
console.log(task);

Select Methods

Select Ids

// Select ids of all inserted tasks
const tasks = await taskCollection.selectIds();
console.log(tasks);

Select Ids Sequentially

// Select ids of all inserted tasks sequentially
const taskIds = [];
for await (const taskId of taskCollection.selectIdsSequential()) {
  taskIds.push(taskId);
}
console.log(taskIds);

Select Data

// Select all inserted values
const tasks = await taskCollection.select();
console.log(tasks);

Select Data with filters and sorting

// Select tasks using the select method
const tasks = await taskCollection.select({
  where: { isDone: { eq: false } },
  orderBy: { dateCreated: -1 },
  limit: 5,
  offset: 2,
});
console.log(tasks);

Select Data Sequentially

// Select tasks sequentially
const tasks = [];
for await (const task of taskCollection.selectSequential()) {
  tasks.push(task);
}
console.log(tasks);

Filtering Data

Equality Filter

// Select tasks where 'isDone' is false
const tasks = await taskCollection.select({
  where: { isDone: { eq: false } },
});
console.log(tasks);

Greater Than Filter

// Select tasks where 'effort' is greater than or equal to 1
const tasks = await taskCollection.select({
  where: { effort: { gte: 1 } },
});
console.log(tasks);

Less Than Filter

// Select tasks where 'effort' is less than 0
const tasks = await taskCollection.select({
  where: { effort: { lt: 0 } },
});
console.log(tasks);

In Filter

// Select tasks where 'effort' is either -2 or 2
const tasks = await taskCollection.select({
  where: { effort: { in: [-2, 2] } },
});
console.log(tasks);

Out Filter

// Select tasks where 'effort' is neither -2 nor 2
const tasks = await taskCollection.select({
  where: { effort: { out: [-2, 2] } },
});
console.log(tasks);

Prefix Filter

// Select tasks where 'description' has a prefix 'Task #10'
const tasks = await taskCollection.select({
  where: { description: { prefix: 'Task #10' } },
});
console.log(tasks);

Every Equal Filter

// Select tasks where every tag is 'tag1' and 'tag2'
const tasks = await taskCollection.select({
  where: { tags: { everyEq: ['tag1', 'tag2'] } },
});
console.log(tasks);

Empty Filter

// Select tasks where 'tags' is empty
const tasks = await taskCollection.select({
  where: { tags: { empty: true } },
});
console.log(tasks);

Sorting Data

Simple Sort

// Select tasks with orderBy
const tasks = await taskCollection.select({
  orderBy: { title: 1 },
});
console.log(tasks);

Reverse Sort

// Select tasks with orderBy and reverse sort
const tasks = await taskCollection.select({
  orderBy: { title: -1 },
});
console.log(tasks);

Sort on Multiple Fields

// Select tasks with orderBy and reverse sort on multiple fields
const tasks = await taskCollection.select({
  orderBy: { isDone: -1, title: 1 },
});
console.log(tasks);

License

portable-db-ts is licensed under the MIT License. Feel free to use and contribute!

For issues or suggestions, please open an issue.

Happy querying! 🚀

Authors