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

petsdb

v0.5.2

Published

File-based embedded data store for node.js

Downloads

28

Readme

PeTSDB - Pet's TypeScript DataBase

GitHub license codecov npm version Known Vulnerabilities Dependency count npm bundle size nodejs version Github CI GitHub Workflow Status Type definitions Website CodeFactor Package Quality GitHub stars

Small database for prototyping and pet projects. Not for production.

Embedded persistent database for Node.js, 100% TypeScript/JavaScript, no binary dependency.

Installation

Module name on npm is petsdb.

$ npm install petsdb --save     # Put latest version in your package.json
$ npm run test:unit             # You'll need the dev dependencies to run tests

Creating/loading a database

You can use Petsdb as a persistent datastore. One datastore is the equivalent of a collection\array. The constructor is used as follows new Petsdb(config) where config is an object with the following fields (actually one field only):

  • dbPath (required): path to the file where the data is persisted.
import {Petsdb} from 'petsdb';

type ExampleDataType = {
    listOfNumber: Array<number>;
    listOfString: Array<string>;
    someData: {
        data: {
            isExists: boolean;
            text: string;
        };
    };
    someNumber: number;
    someString: string;
};

// create dataBase
const petsdb: Petsdb<ExampleDataType> = new Petsdb<ExampleDataType>({dbPath: 'path/to/your/file'});

// run dataBase, use async/await OR Promises
await petsdb.run();

Creating documents

Petsdb uses JSON.stringify to place document. Also, Petsdb will automatically generate _id (a 16-characters alphanumerical string). The _id of a document, once set, cannot be modified.

const someDocument: ExampleDataType = {
    listOfNumber: [1, 2, 3],
    listOfString: ['one', 'two', 'three'],
    someData: {
        data: {
            isExists: false,
            text: 'lorem ipsum',
        },
    },
    someNumber: 1,
    someString: 'the string',
};

// create document into dataBase, use async/await OR Promises
await petsdb.create(someDocument);

Reading documents

Use read to look for multiple documents matching you query. Or use readOne to look for one specific document. Or use readPage to look for multiple documents matching you query by pagination.

You can use regular expressions in basic querying in place of a string and an array of string.

You can sort paginated result (readPage only) using sort. You can use nested properties to navigate inside nested documents (see below).

Reading documents: read\readOne

Method .read() read database by query. It returns promise with array of items. Method .readOne() works the same as .read(), but returns promise with one item or null.

// search by key\value
await petsdb.read({someString: 'the string'});

// search by nested object
await petsdb.read({someData: {data: {isExists: false}}});

// search by value(s) of array
await petsdb.read({listOfString: ['one']});

// search by RegExp instead of string
await petsdb.read({someString: /the/});

// search by RegExp instead of array of string
await petsdb.read({listOfString: /thr/});

Reading documents: readPage

Method .readPage() read database by query and sort. It returns promise with page of items.

// get page by index 0, set page's size as 10 and sort by `someNumber`
await petsdb.readPage({someString: /the/}, {pageIndex: 0, pageSize: 10, sort: {someNumber: 1}});

// the same, but use for sort nested object
await petsdb.readPage({someString: /the/}, {pageIndex: 0, pageSize: 10, sort: {someData: {data: {text: -1}}}});

Updating documents

Method .update() updates documents by query. All data of document needed. No partial update.

const newDocument: ExampleDataType = {
    listOfNumber: [100, 200, 300],
    listOfString: ['not one', 'not two', 'not three'],
    someData: {
        data: {
            isExists: true,
            text: 'dolor',
        },
    },
    someNumber: 1,
    someString: 'new string',
};

// fully update document, all data needed
await petsdb.update({someNumber: 1}, newDocument);

Deleting documents

Method .delete() delete documents by query.

await petsdb.delete({someNumber: 1});

Basic querying

Basic querying means are looking for documents whose fields match the ones you specify. You can use regular expression to match strings. To check your query use PetsdbQueryType.

import type {PetsdbQueryType} from 'petsdb';

const myQuery: PetsdbQueryType<ExampleDataType> = {
    someData: {data: {isExists: true}},
    someString: /one/,
};

Basic sorting

Basic sorting means are sorting for documents whose fields match the ones you specify. You can use 1 or -1 to sort. You can use only one field to sort. To check your sort use PetsdbSortType.

import type {PetsdbSortType} from 'petsdb';

const mySortByNumber: PetsdbSortType<ExampleDataType> = {
    someString: 1,
};

const mySortByNestedObject: PetsdbSortType<ExampleDataType> = {
    someData: {data: {text: -1}},
};
Full export: class and types
import {Petsdb} from 'petsdb';
import type {
    PetsdbInitialConfigType,
    PetsdbItemType,
    PetsdbQueryType,
    PetsdbReadPageConfigType,
    PetsdbReadPageResultType,
    PetsdbSortDirectionType,
    PetsdbSortType,
    PetsdbSortValueType,
} from 'petsdb';

License

See license.