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

db-mycro

v0.3.7

Published

A node module with a json database that saves data in a specific directory, similar to sqlite, but in JSON

Downloads

1

Readme

A node module with a json database that saves data in a specific directory, similar to sqlite, but in JSON

import { Database, Repository } from 'db-mycro';

const db_video = new Database('video', __dirname)
const Video = new Repository({
  name: 'video',
  columns: [
    { name: 'id', type: 'number', isPrimary: true, autoIncrement: true },
    { name: 'minutes', type: 'number' }
  ],
  db: db_video
})

Where to use?

Important: db-mycro IS NOT a database made to hold all your application data! read the text below to better understand.

This is just a database created for small applications, small things, or even just for educational purposes, if you don't want to configure a whole database just use db-mycro

Install

npm

  npm i --save db-mycro

yarn

  yarn add db-mycro

Docs

Our database consists of 2 main methods, the database and the repository.

IMPORTANT: read the documentation until the end, and never forget the save method

Database

the database method creates a database in which you will store your repositories (if you don't know what a repository in db-mycro is, read on).

import

Javascript

  const { Database } = require('db-mycro');

Typescript

  import { Database } from 'db-mycro';

Creating a new database

  const db_video = new Database('video', __dirname)

the Database function takes two parameters the first is the name which is the database name and the second which is the path this is where your database should be saved and then the function returns an instance of the database created (instance in which it will be necessary to assign to the repositories)

Repository

If you are familiar with MySQL, repositories are like a table in MySQL, if you are not familiar with MySQL, we will explain what a repository is in the text below.

A repository is an object that is created with a specific structure to store your data, for example, if you want to store the number of minutes a user spends watching a video, you can create a repository called video with a field called minutes which is the type number and whenever you need to save new data of the time the user spent watching your video, just save it in the minutes field of the video repository.

import

Javascript

  const { Repository } = require('db-mycro');

Typescript

  import { Repository } from 'db-mycro';

Creating a new repository

const Video = new Repository({
  name: 'video',
  columns: [
    { name: 'id', type: 'number', isPrimary: true, autoIncrement: true },
    { name: 'minutes', type: 'number' }
  ],
  db: db_video
})

the repository receives an object with 3 mandatory parameters and returns an instance of the created repository.

parameters:

  • name The repository name.

  • columns is column type.

  • db An instance of the database where the repository belongs

Repository columns

  columns: [
    { name: 'id', type: 'number', isPrimary: true, autoIncrement: true },
    { name: 'minutes', type: 'number' }
  ]

the columns of a repository are the fields that the repository will have. They get an array of objects where each object has the essential settings for that column. and each of the objects has the following parameters:

name (string mandatory) the column name.

type (string mandatory) the column type, can be: 'string' | 'number' | 'array' | 'object' | 'boolean'

isPrimary (boolean optional) indicates if the field is the id of that data. default false

autoIncrement (boolean optional) if it is true for each insertion in the repository it will add 1 in this field. only works with type number. default false

default (optional and of the same type that was entered in the type parameter) this is a default value for if the column value is null. default null

allowNull (boolean optional) if true the field cannot be null. default false

IMPORTANT: this is optional if the column was undefined or a empty array your repository will allow all data type, without validation

Repository Methods

repositories have some methods that allow you to add, list, update, and delete data. never forget the save method.

.add(datas: object[])

Video.add([{
 minutes: 10
}])

add data to a repository, receive an object array where each object must be of the explicit type in the type parameter in the column property

.find(params?: { where?: string; offset?: number; limit?: number; })

Video.find({
where: "data.minutes == 10",
offset: 2,
limit: 10
})

returns the data registered in the repository. the only one that doesn't need the save method to be executed after its execution

receive optional parameters:

where (string) a condition of what data will be returned. you can access each data from the data prefix.

offset (number) after which index the data will be fetched.

limit (number) a limit on how much data will be fetched

.update({ where: string, data: object })

Video.update({
 where: "data.id == 10",
 data: { minutes: 15 }
})

update data based on the data property where a where property is true, you can access each data within the where property string by the data prefix

.delete(where?: string)

Video.delete('data.id == 10')

delete data based on where the where property is true, you can access each data within the where property string by the data prefix.

.save()

Video.save()

all changes that are made by any method in a repository ARE NOT SAVED they just change the local memory of the repository to ensure better performance of db-mycro. that's why you should always, after making the changes in the repository, execute the save method as in the example above (replacing "Video" by the variable that stores the instance of your repository)