• This repository has been archived on 06/Mar/2021
  • Stars
    star
    140
  • Rank 261,473 (Top 6 %)
  • Language
    JavaScript
  • License
    The Unlicense
  • Created almost 11 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A CRUD JavaScript class.

A CRUD JavaScript Class

Read introduction article on David Walsh's blog.

Here is a CRUD JavaScript class mapping to the storage driver of your choice (localStorage in this demo). Before going any further, a couple of things you should note:

Each record has to be an object. You can't store an array, a primitive or whatever; only objects. If you want to store β€” let's say β€” numbers you should wrap them into objects first; e.g. { value: 42 }.

You shouldn't try to store different entities in the same database. The API being quite simple, it aims at solving simple issues. For a more robust JS Database, I suggest you try Taffy.

What is the driver?

The driver (the thing that actually does the storage) has been externalized from the main class in order to allow you to use the driver you want: sessionStorage, localStorage, Redis... Anything you want as long as it relies on key/value pairs and supports 3 methods: setItem(..), getItem(..), removeItem(..).

The one used per default is a StorageDriver, relying on either localStorage or sessionStorage (in the client or a dummy replication of itself when used on Node), depending on what you pass to the constructor, but you could definitely build your own.

How does it work?

Instanciating a database

The indexedKeys property aims at speeding up the search. By setting some keys to be indexed, searching for those keys will be way faster. In any case, you can search for any key, even those which are not indexed.

var db = new Database({
  name: 'MyDatabase',
  indexedKeys: ['job', 'age']
})

Inserting a new entry

var obj = {
  name: 'Kitty',
  age: 23,
  job: 'dev'
}

var id = db.insert(obj)

Note: you can pass an array to the insert(..) method to add several entries at once.

Updating an entry

If you want to update a specific entry, the easiest way is to pass its ID as the first argument. The ID is being added to the entry when inserted as the id property. You can change the name of this property by setting the uniqueKey option when instanciating the database.

obj['mood'] = 'happy'
db.update(id, obj)

To update a collection of entry based on a search, here is how you would do it:

var dev, i, len,
    devs = this.find({ job: 'dev' })

for(i = 0, len = devs.length; i < len; i++) {
  dev = devs[i]
  dev['mood'] = 'happy'
  dev.job = 'clown'
  db.update(dev.id, dev)
}

Retrieving entries

The find(..) method requires an object to parse and search with.

db.find({ mood: 'happy' })
db.find({ job: 'dev', age: 22 })

Retrieving all entries

To return all existing entries, you can call the find(..) method with no arguments:

db.find()

Deleting an entry

If you want to delete a specific entry, the easiest way is to pass its ID to the function. The ID is being added to the entry when inserted as the id property. You can change the name of this property by setting the uniqueKey option when instanciating the database.

db.delete(id)

If you want to delete a collection of entries based on asearch, you can pass an object to the function. The function will first perform a find, then delete all the returned entries.

db.delete({ job: dev })

Dropping all entries

To remove all existing entries, you can drop the database which basically resets everything to its initial state:

db.drop()

Counting existing entries

You can use the count(..) method to count the number of existing entries:

db.count()

Development

# Installing dependencies
npm install

# Linting the code
npm run lint

# Running the tests
npm run test --silent

# Building the dist file
npm run build

More Repositories

1

sass-boilerplate

A boilerplate for Sass projects using the 7-1 architecture pattern from Sass Guidelines.
SCSS
3,338
star
2

a11y-dialog

A very lightweight and flexible accessible modal dialog script.
TypeScript
2,395
star
3

SJSJ

Simplified JavaScript Jargon
HTML
2,272
star
4

sass-guidelines

Guidelines for writing sane, maintainable and scalable Sass.
HTML
908
star
5

awesome-sass

A curated list of awesome Sass.
751
star
6

selectors-explained

A CSS selector to plain English translator.
JavaScript
211
star
7

react-a11y-dialog

An accessible React component for a11y-dialog
TypeScript
173
star
8

SassyJSON

[UNMAINTAINED] A Sass API for JSON.
SCSS
169
star
9

jekyll-boilerplate

A cleaned up version of the initial Jekyll setup for quick use.
HTML
144
star
10

sass-compatibility

Sass compatibility issues between different engines.
SCSS
129
star
11

site

Sources for kittygiraudel.com
Liquid
103
star
12

Countdown.js

[UNMAINTAINED] A little JavaScript cutstomizable countdown
JavaScript
103
star
13

focusable-selectors

A micro-lib exporting an array of CSS selectors for focusable HTML elements.
JavaScript
74
star
14

SassyCast

[UNMAINTAINED] Type conversion functions for Sass.
SCSS
72
star
15

SassySort

[UNMAINTAINED] Several ways to sort values in Sass using popular sorting algorithms.
SCSS
61
star
16

ImagePreloader

A lightweight JavaScript implementation of an image preloader relying on Promises.
JavaScript
61
star
17

SassyStrings

[UNMAINTAINED] A collection of Sass functions to manipulate strings.
SCSS
59
star
18

react-menu-button

A React component wrapper around inclusive-menu-button.
JavaScript
50
star
19

ama

Ask me anything!
43
star
20

SassyTester

[UNMAINTAINED] A minimalistic function tester in Sass.
SCSS
42
star
21

dotfiles

My configuration files
Shell
40
star
22

react-a11y-disclosure

A small React hook to build accessible disclosure widgets.
JavaScript
35
star
23

react-a11y-footnotes

A reusable React implementation of accessible footnotes.
JavaScript
35
star
24

jekyll-styleguide

This project is an experiment about how we can create a living styleguide in Jekyll.
HTML
32
star
25

eleventy-plugin-footnotes

An 11ty plugin to render accessible footnotes with Liquid
JavaScript
32
star
26

github-release-search

Tiny CLI to search for terms in GitHub releases
JavaScript
28
star
27

SassyMatrix

[UNMAINTAINED] All you ever wanted to deal with matrices in Sass.
SCSS
28
star
28

node-legofy

Node.js version of Legofy.
JavaScript
25
star
29

SassyGradients

[UNMAINTAINED] Sass helpers to manipulate gradients.
SCSS
24
star
30

inline-styler

A teeny tiny JavaScript library to easily manipulate the style attribute of an element.
JavaScript
22
star
31

SassyBitwise

[UNMAINTAINED] Bitwise operators in Sass.
SCSS
18
star
32

dhand

A React hook to guess the dominant hand of the user.
JavaScript
18
star
33

scrabble-helper

A CLI helper for Scrabble.
JavaScript
15
star
34

sass-semver

[UNMAINTAINED] A Sass implementation of node-semver.
SCSS
13
star
35

SassyIteratorsGenerators

[UNMAINTAINED] Iterators and generators implementation in Sass.
SCSS
13
star
36

map-dater

An interactive command line utility to estimate the age of an undated world map.
JavaScript
13
star
37

SassyLogger

[UNMAINTAINED] A logger for Sass.
SCSS
13
star
38

circularray

A fast circular array implementation in JavaScript
JavaScript
13
star
39

hangman.js

Redux powered hangman game.
JavaScript
11
star
40

advent-of-code

TypeScript
6
star
41

sanity-micro-client

JavaScript
6
star
42

dependency-checker

A simple CLI script to check dependencies from a package.json
JavaScript
6
star
43

css3-pratique

CSS 3, Pratique du Design Web
4
star
44

sudoku-solver

JavaScript
4
star
45

PHP-hangman-game

[UNMAINTAINED] A little OOPHP hangman game.
PHP
3
star
46

cypress-5570

JavaScript
2
star
47

teachable-backup

Node.js script to download all content from a Teachable school
JavaScript
1
star