You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

memory-orm

Package Overview
Dependencies
Maintainers
1
Versions
80
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

memory-orm

client side ORM + map reduce

0.4.7
Source
npmnpm
Version published
Weekly downloads
98
2350%
Maintainers
1
Weekly downloads
 
Created
Source

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
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 size. size already set by Query.page(size)
sortlodash.orderBy(...key)
coverremain, coverkey has full index. cover has index. remain not has index.
pluckget path data by list values.
indexgroup by item[key].
group_bygroup by item[key]. item[key] is String
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
staticupdateevent when add data exist.
staticcreateevent when add data not exist.
staticdeleteevent when del data exist.
staticblessvalue become extends this
staticmap_partitiondefine map reduce.
staticmap_reducedefine map reduce.
staticorderdefine order process for reduced value.

List

stylenameaction
.pluckget path data
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
.schemaexecute schema definition block.
.key_byid value. default: _id
.deploydata adjust before Set.
.scopedefine query shorthand.
.propertydefine property shorthand.
.default_scoperoot Query replace.
.shuffleroot Query replace. and replace sort order by Math.random.
.orderroot Query replace. and replace order.
.sortroot Query replace. and replace order.
.pathset name property. for id separate by '-'.
.belongs_toset target property. find by ${target}_id
.habtmset target property. finds by ${target}_ids
.has_manyset target property. find from ${target}_id by _id
.treeset 'nodes' method. scan recursivery by ${target}_id
.graphset '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
.transactionget transaction diff data.
.storemerge transaction diff data.
.step.< plural name >countup if data manipulation.

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
.wherecopy Query and add conditions.
.incopy Query and add conditions. (includes algorythm.)
.partitioncopy Query and replace partition.
.searchcopy Query and add conditions. for data search_words value.
.shufflecopy Query and replace sort order by Math.random.
.ordercopy Query and replace order.
.sortcopy Query and replace order's sort parameter.
.pagecopy Query and replace page_by.
.findpick first data from hash by ids.
.findspick all data from hash by ids.
.pluckget path data by list values.
getreducecalculate map reduce.
getlistcalculate list. ( same as reduce.list )
gethashcalculate hash. ( same as 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

ORM

FAQs

Package last updated on 18 Aug 2019

Did you know?

Socket

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