Socket
Socket
Sign inDemoInstall

memory-orm

Package Overview
Dependencies
212
Maintainers
1
Versions
80
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    memory-orm

client side ORM + map reduce


Version published
Weekly downloads
88
increased by2833.33%
Maintainers
1
Install size
67.2 MB
Created
Weekly downloads
 

Readme

Source

Build Status

Install

yarn add memory-orm

Quick Start

const { Rule, Set, Query } = require('memory-orm')

new Rule('todo').schema(function () {
  this.has_many('checks')
})

new Rule('check').schema(function () {
  this.belongs_to('todo')
})

Set.todo.merge([
  {
    _id: 1,
    label: '歯を磨く',
  },
  {
    _id: 2,
    label: '宿題をする',
  },
  {
    _id: 3,
    label: 'お風呂はいる',
  },
])

Set.check.merge([
  {
    _id: 1,
    todo_id: 1,
    label: '右上',
    checked: true,
  },
  {
    _id: 2,
    todo_id: 1,
    label: '左上',
    checked: true,
  },
  {
    _id: 3,
    todo_id: 3,
    label: 'シャワー浴びる',
    checked: false,
  },
  {
    _id: 4,
    todo_id: 3,
    label: '肩まで入って10数える',
    checked: true,
  },
])

Query.todos.pluck('label')
Query.todos.find(3).checks.where({ checked: true }).list
Query.todos.find(3).checks.where({ checked: false }).list

map reduce

nametargetaction
countcountreduce +
allallreduce +
all, countavgall / count
all, countvariancevariance sample.
all, countsdstandard deviation.
powpowreduce *
pow, countavgpow / count
listlistlistup object ( key is not use )
setset, hashhash has item by key. set is unique keys.
minmin, min_ispick min key. min_is is item.
maxmax, max_ispick max key. max_is is item.
min, maxrangemax - min
min, max, allrange, densityall / range
const { Rule, Set, Query } = require("memory-orm");

new Rule("position").schema(function() {
  this.model = class model extends this.model {
    static map_reduce (o, emit) {
      for ( const p of o.position ) {
        emit("position", { count: 1, all: p, min: p, max: p})
      }
    }
  }
});

Set.position.merge([{
  "_id": "x1",
  "position": [40,60,80,100,200]
},{
  "_id": "y1",
  "position": [70,190,220,160,240]
},{
  "_id": "x2",
  "position": [40,60,80,100,200]
},{
  "_id": "y2",
  "position": [20,90,20,60,40]
}])

{ count, all, avg, density, min, min_is, max, max_is, range } = Query.where({_id: "x1"}).reduce.position

order

nametargetaction
belongs_todata's prototype is Query[key].find( object index or item.id )
pageseparate by page-size. see: Query.page(size)
sortlodash.orderBy(...key)
diffdiffcalculate differential. use with sort.
coverremain, coverkey has full index. cover has index. remain not has index.
pluckget path data by list values.
indexgroup by item[key].
const { Rule, Set, Query } = require('memory-orm')

new Rule('check').schema(function () {
  this.model = class model extends this.model {
    static map_reduce(o, emit) {
      emit('asc', { list: true })
      emit('desc', { list: true })
    }
    static order(o, emit) {
      emit('asc', 'list', { sort: ['label', 'asc'] })
      emit('desc', 'list', { sort: ['label', 'desc'] })
    }
  }
})

Set.check.merge([
  {
    _id: 1,
    todo_id: 1,
    label: '右上',
    checked: true,
  },
  {
    _id: 2,
    todo_id: 1,
    label: '左上',
    checked: true,
  },
  {
    _id: 3,
    todo_id: 3,
    label: 'シャワー浴びる',
    checked: false,
  },
  {
    _id: 4,
    todo_id: 3,
    label: '肩まで入って10数える',
    checked: true,
  },
])

Query.checks.reduce.asc.list.pluck('label')
Query.checks.reduce.desc.list.pluck('label')

class list

Model

stylenameaction
getidsame as _id
staticdeploy(model)event when Set item. this is item. model is class.
staticupdate(item, old_item)event when add data exist.
staticcreate(item)event when add data not exist.
staticdelete(old_item)event when del data exist.
staticbless(item)value become extends this
staticmap_partition(item, emit)define map reduce. emit is function.
staticmap_reduce(item, emit)define map reduce. emit is function.
staticorder(item, emit)define order process for reduced value.

List

stylenameaction
.pluck(...keys)listup by keys for item.
.where(...)create query. see Query#where
.in(...)create query. see Query#in
getfirst[0]
gethead[0]
gettail[length - 1]
getlast[length - 1]
getuniqget unique values
const { list } = Query.checks.reduce.asc
list.pluck('label')
list.first
list.head
list.tail
list.last

Rule

stylenameaction
.schema(dsl)execute schema definition dsl.
.key_by(keys)id value. default: _id
.key_by(callback)callback return id. default: this._id
.deploy(callback)data adjust before Set.
.scope(callback)define query shorthand and cached.
.property(...)define property shorthand.
.default_scope(callback)root Query replace.
.shuffle()root Query replace. and replace sort order by Math.random.
.order(...)root Query replace. and replace order.
.sort(...)root Query replace. and replace order.
.path(...keys)set name property. for id separate by '-'. add argument '*', tree structure by id.
.belongs_to(to, options)set target property. find by ${target}_id
.habtm(to, option)set target property. finds by ${target}_ids
.has_many(to, option)set target property. find from ${target}_id by _id
.tree(option)set 'nodes' method. scan recursivery by ${target}_id
.graph(option)set 'path' method. scan recursivery by ${target}_id
.modelModel base class ( need extends )
.listList base class ( need extends )
.setSet base class ( need extends )
.mapMap base class ( need extends )
new Rule('todo').schema(function () {
  this.key_by(function () {
    return this._id
  })
  this.deploy(function (model) {
    this.search_words = this.label
  })
  this.scope(function (all) {
    return {
      scan: (word) => all.where({ checked: true }).search(word),
    }
  })
})

State

stylenameaction
.transaction(callback, meta)get transaction diff data.
.store(meta)merge transaction diff data.
.step.< plural name >countup if data manipulation.
getmixinfor vue.js mixin.

Set.< base name >

stylenameaction
.setset data. and old data cleanup.
.resetset data. and old data cleanup.
.mergeset data.
.addset datum.
.appendset datum.
.rejectremove data.
.delremove datum.
.removeremove datum.
.clear_cacherecalculate query caches.
.refreshrecalculate query caches.
.rehashrecalculate query caches.
.findpick first data from all memory by ids. and mark for transaction.
const {
  checks: {
    $sort,
    $memory,
    $format,
  }
} = State.transaction(=>{
  Set.check.add({
    _id: 10,
    todo_id: 1,
    label: "新しい項目",
    checked: true
  })
  Set.check.del({ _id: 10 })
})
State.store(JSON.parse(JSON.stringify({ checks: { $sort, $memory, $format }})))

Query.< plural name >

stylenameaction
.where({ [key]: val })copy Query and add conditions. same (o)=> val === o[key]
.where({ [key]: /regexp/ })copy Query and add conditions. same (o)=> (/regexp/).test( o[key] )
.where({ [key]: [...args] })copy Query and add conditions. same (o)=> args.includes( o[key] )
.in({ [key]: val })copy Query and add conditions. same (o)=> o[key].includes( val )
.in({ [key]: /regexp/ })copy Query and add conditions. same (o)=> o[key].find((oo)=> (/regexp/).test( oo ))
.in({ [key]: [...args] })copy Query and add conditions. same (o)=> o[key].find((oo)=> args.find((arg)=> oo == arg))
.partition(keys)copy Query and replace partition keys. default: ["set"]
.search(text)copy Query and add conditions. for "q.search_words"
.shuffle()copy Query and replace sort order by Math.random.
.distance(key, "asc", [...point])copy Query and replace order. near o[key] is top.
.distance(key, "desc", [...point])copy Query and replace order. far o[key] is top.
.order(keys, order)copy Query and replace order.
.sort(...sort)copy Query and replace order's sort parameter. same this.order({ sort })
.page(size)copy Query and replace page_by. if order option set page: true, resuls page separated list.
.find(...ids)pick first data from hash by ids.
.finds(ids)pick all data from hash by ids.
.pluck(...keys)get path data by keys.
getreducecalculate map reduce.
getlistcalculate list. ( same reduce.list )
gethashcalculate hash. ( same reduce.hash )
getidscalculate hash and get keys.
getmemoryall stored data.
Query.positions.in({ position: 100 }).pluck('_id')
Query.positions.in({ position: [100, 90] }).pluck('_id')
Query.checks.shuffle().pluck('label')
Query.checks.sort('label').pluck('label')
Query.checks.sort('label', 'desc').pluck('label')
Query.checks.order({ sort: ['label', 'desc'] }).pluck('label')
Query.checks.page(3).list[0][0]
Query.checks.page(3).list[0][1]
Query.checks.page(3).list[0][2]
Query.checks.page(3).list[1][0]

Keywords

FAQs

Last updated on 05 Nov 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc