# masterquest-sqlite3

> A NoSQL / SQLite3 Hybrid. All your indices are belong to us. Master Quest.

Latest version **1.3.1** (published 2019-04-04) · (MIT OR Apache-2.0) license · 0 weekly downloads

## Install

```sh
npm install masterquest-sqlite3
pnpm add masterquest-sqlite3
yarn add masterquest-sqlite3
bun add masterquest-sqlite3
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.1 |
| Published | 2019-04-04 |
| First published | 2015-12-02 |
| Weekly downloads | 0 |
| License | (MIT OR Apache-2.0) |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 39.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | AJ ONeal |
| Maintainers | coolaj86 |

## Links

- npm: https://www.npmjs.com/package/masterquest-sqlite3
- Repository: https://git.coolaj86.com:coolaj86/masterquest-sqlite3.js
- npm.io page: https://npm.io/package/masterquest-sqlite3

## Recent versions

- 1.3.1 (latest) — 2019-04-04
- 1.3.0 — 2019-04-04
- 1.2.1 — 2018-09-15
- 1.2.0 — 2018-09-15
- 1.1.1 — 2018-05-17
- 1.1.0 — 2018-05-17
- 1.0.3 — 2016-08-18
- 1.0.2 — 2015-12-09
- 1.0.1 — 2015-12-02

## README

Master Quest SQLite3
============

Master Quest is a brave new attempt at Data Mapping.

It kinda looks like an ORM, but it isn't. It's not SQL, it's NoSQL with benefits.

You get to choose which contraints to keep and which to forget.

Guiding Principles

* NoSQL (well, as far as you care)
* Migrations don't suck
* Harness the awesomeness of indexes
* `deletedAt` to delete a record
* `null` to delete a field
* unindexed fields stored as json
* ids should be one either
  * deterministic (duplicate records not okay, last `updatedAt` wins)
  * cryptographically random (duplicate records consolidated in application)
* avoid mutating data (CRDT-ish style)
* if it won't scale, don't fret it (no foreign key contraints)
* `id` is simply named `id`
* multi-key ids should be `sha256sum(key1 + key2 + key3)`
* JavaScript is `camelCase`y, databases are `snake_case`y. We can handle that.
* join tables are in alphabet order i.e. `foo`, `bar`, `bar_foo`

TODO / In Progress

* Multi-Master Replication
* Relationships
  * currently detaches before saving (most important)
* MongoDB / RethinkDB -ish queries
* RealTime

USAGE
=====

```bash
npm install --save 'https://github.com/coolaj86/node-masterquest-sqlite3.git'
```

```javascript
'use strict';

// works with sqlite3, sqlcipher, and sqlite3-cluster
var db = new (require('sqlite3').Database)('/tmp/data.sqlite3');

require('masterquest-sqlite3').wrap(db, {
  modelname: 'Persons'
, indices: [ 'firstName', 'lastName' ]
, hasMany: [ 'children' ]
}).then(function (mq) {

  // update (or create) deterministic record
  var john = {
    id: 'john.doe@email.com'
  , firstName: 'john'
  , lastName: 'doe'
  , dog: { name: 'ralph', color: 'gold' }
  , children: [ 'stacey@email.com' ]
  };

  mq.Persons.upsert(john.id, john).then(function () {
    // note: if `dog` existed, it will be overwritten, not merged
    // note: `children` will be removed before save

    mq.Persons.get('john.doe@email.com').then(function (data) {
      // dog will be rehydrated from json
      // children will not be fetched and attached
      console.log(data);
    });

  });

});
```

API
===

It's kinda CRUDdy... but don't let that scare you.

* `upsert(id, data[, oldId])` - creates or updates based on existence in DB (use this)
  * modifies `createdAt` and or `updatedAt`
* `create(id, obj)` - same as above, but fails if the object exists
* `save(data[, oldId])` - (just don't use this, please) creates or updates based on presence of ID
* `destroy(id)` - mark a record as `deletedAt` from DB
* `get(id)` - grab one by id
* `find(attrs, opts)` - grab many by indexable attributes
  * attrs
    * explicit `null` will find all (and requires that `limit` be set)
    * `{ foo: 2, bar: 6 }` will find records where `foo` is `2` *and* `bar` is `6`
  * opts
    * `orderBy`
    * `orderByDesc`
    * `limit`

Schema
======

Anything that isn't in the schema

* `indices` specifies an array of strings
  * `[ 'firstName', 'lastName' ]`
* relationships are option and current only exclude during save
  * `hasMany`, `belongsTo`, `hasOne`, `belongsToMany`, `hasAndBelongsToMany`
* `createdAt`, `updatedAt`, `deletedAt` timestamps are always added
  * turn off with `timestamps: false`
* `id` is always `id`
  * change with `idname: 'myId'`

Migrations
----------

You can only add indexes. You cannot rename or remove them.

To add an index, simply change the schema.

```javascript
{ modelname: 'persons'
, indices: [ 'firstName', 'lastName' ]
, hasMany: [ 'children' ]
}
```

LICENSE
=======

Dual-licensed MIT and Apache-2.0

See LICENSE

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