# local_orm

> A simple ORM-like layer on top of localStorage

Latest version **0.6.1** (published 2016-11-13) · ISC license · 0 weekly downloads

## Install

```sh
npm install local_orm
pnpm add local_orm
yarn add local_orm
bun add local_orm
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.1 |
| Published | 2016-11-13 |
| First published | 2016-07-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Yanis |
| Maintainers | yanis |
| Keywords | orm, localStorage |

## Links

- npm: https://www.npmjs.com/package/local_orm
- npm.io page: https://npm.io/package/local_orm

## Dependencies (2)

- [node-uuid](https://npm.io/package/node-uuid.md) ^1.4.7
- [underscore](https://npm.io/package/underscore.md) ^1.8.3

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.6.1 (latest) — 2016-11-13
- 0.6.0 — 2016-08-11
- 0.5.9 — 2016-07-13
- 0.5.8 — 2016-07-12
- 0.5.7 — 2016-07-12
- 0.5.6 — 2016-07-11
- 0.5.5 — 2016-07-07
- 0.5.4 — 2016-07-07
- 0.5.3 — 2016-07-07
- 0.5.2 — 2016-07-06
- 0.5.1 — 2016-07-04
- 0.5.0 — 2016-07-04

## README

# local_orm
[![CodeShip](https://codeship.com/projects/0876cb80-2453-0134-45d7-7a46f2e0a594/status?branch=master)](https://codeship.com/projects/0876cb80-2453-0134-45d7-7a46f2e0a594/status?branch=master)
[![Code Climate](https://codeclimate.com/github/hiquest/local_orm/badges/gpa.svg)](https://codeclimate.com/github/hiquest/local_orm)

A simple ORM-like wrapper around localStorage with functional interface, types and validations.

Installation
------
```
npm install local_orm --save
```

Usage
------
Define a schema (yes, we call it a schema):

```javascript
const { define: define, types: t, validations: v } = require("local_orm");

const Store = define({
  name: "books_schema",
  schema: {
    books: {
      title: {
        type: t.string,
        validations: [v.present, v.maxLength(32)]
      },
      year: {
        type: t.integer,
        validations: [v.min(1900), v.max(2999)]
      },
      genre: {
        type: t.string,
        validations: [v.present, v.oneOf('fiction', 'non-fiction')],
        defaultVal: 'fiction'
      }
    }
  }
});

```

Let's save some books.

```javascript

// Create a book
let [err, book] = Store.books.save({ title: "War And Peace" });
console.log(book);
// => { id: "0326d5ce-d3db-4bf7-853f-37d4d5adf6a8", title: "War And Peace", genre: 'fiction' }
// ( Note that we have an id now, and that genre was populated with a default value )

// Let's try another one
let [err, book] = Store.books.save({ year: "1984" });
console.log(book); // => null

// Was there some errors?
console.log(err); // => { 'year': ['should be an integer'], 'title': ['should be present'] }

// Oh, I see now...
let [err, book] = Store.books.save({ title: "So Long, and Thanks for all the Fish", year: 1984 });
```

We can load books from localStorage now.

```javascript
// Find a book by id
let book = Store.books.find(id);

// Load all books
let books = Store.books.all();

// Filter by title
let books = Store.books.where({title: 'War And Peace'});

// Any function is also accepted
let books = Store.books.where((b) => b.year > 1980);
```

Store API
------
* `build` builds a new entity with set default values. Returns a new `entity`

* `validate` validates an entity. Returns an array `[errors, isValid]`, where errors is an object like this one:
```javascript
{
  "title": [ "should be present", "should have more than 5 characters" ],
  "year": [ "should be less of equal to 2999" ]
}
```

* `save` creates a new entity or updates the existing one (if it has an id). Returns an array `[errors, entity]`

* `find` finds an entity by id. Throws an error if it does not exist. Returns an `entity`

* `destroy` destroys an entity by id. Throws an error if it does not exist. Returns `true`

* `all` loads all entities. Returns an `array of entities`

* `where` filters out entities. Accepts `object` or `function`. Returns an `array of entities`

Validations
------
Validation is a simple plain JavaScript function that takes a value and returns an array like this one [error, valid]. Local_orm comes with several predefined validations.

* `present` requires a value to be present (not undefined and not null)
```javascript
validations: [v.present]
```

* `min`, `max` define a range for integer types (inclusive)
```javascript
validations: [v.min(0), v.max(127)]
```

* `minLength`, `maxLength` define a length range for strings or arrays (really, anything that has length)
```javascript
validations: [v.maxLength(32)]
```

* `oneOf` require a value to be in a particular set of values (like enum)
```javascript
validations: [v.oneOf("sun", "moon")]
```

Also, note that when you define a type, under the curtains the corresponding validation is added to the list.

As noted earlier, validations are just functions, so it is easy to define your own:
```javascript
const positive = (val) => {
  if (val > 0) {
    return [null, true];
  } else {
    return ["should be positive", false];
  }
};

validations: [ positive ]
```

Contributing
------
Feel free to fork, add features and send pull requests. Please make sure to add corresponding tests.

---
_Source: https://npm.io/package/local_orm · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
