memory-orm
Version:
client side ORM + map reduce
88 lines (87 loc) • 2.08 kB
JavaScript
'use strict'
Object.defineProperty(exports, '__esModule', { value: true })
exports.List = void 0
const tslib_1 = require('tslib')
const at_1 = tslib_1.__importDefault(require('lodash/at'))
const uniq_1 = tslib_1.__importDefault(require('lodash/uniq'))
const orderBy_1 = tslib_1.__importDefault(require('lodash/orderBy'))
const groupBy_1 = tslib_1.__importDefault(require('lodash/groupBy'))
const property_1 = tslib_1.__importDefault(require('lodash/property'))
class List extends Array {
constructor(query) {
super()
if (query && query.where && query.in) {
this.query = query
}
}
get first() {
return this[0]
}
get last() {
return this[this.length - 1]
}
get head() {
return this[0]
}
get tail() {
return this[this.length - 1]
}
get uniq() {
return this.constructor.bless(uniq_1.default(this), this.query)
}
pluck(...keys) {
let cb
switch (keys.length) {
case 0:
cb = function () {
return null
}
break
case 1:
cb = property_1.default(keys[0])
break
default:
cb = function (o) {
return at_1.default(o, ...keys)
}
break
}
return this.constructor.bless(this.map(cb), this.query)
}
static bless(list, query) {
Reflect.setPrototypeOf(list, this.prototype)
if (query && query.where && query.in) {
list.query = query
}
return list
}
sort(...cmd) {
const o = orderBy_1.default(this, cmd[0], cmd[1])
Reflect.setPrototypeOf(o, Reflect.getPrototypeOf(this))
return o
}
group_by(cb) {
const o = groupBy_1.default(this, cb)
for (const key in o) {
const oo = o[key]
Reflect.setPrototypeOf(oo, Reflect.getPrototypeOf(this))
}
return o
}
page_by(per) {
let idx = 0
return Object.values(
this.group_by(function (o) {
return Math.floor(idx++ / per)
})
)
}
where(req) {
return this.query.where(req)
}
in(req) {
return this.query.in(req)
}
}
exports.List = List
//# sourceMappingURL=list.js.map