memory-orm
Advanced tools
Comparing version
@@ -13,2 +13,3 @@ import { Datum } from './datum' | ||
NameBase, | ||
CLASS, | ||
} from './type' | ||
@@ -27,5 +28,5 @@ import { Model } from './model' | ||
} | ||
declare type ReduceContext<O extends MODEL_DATA> = { | ||
declare type ReduceContext<O extends MODEL_DATA, R> = { | ||
map: typeof Map | ||
query: Query<O> | ||
query: Query<O, R> | ||
memory: Memory | ||
@@ -37,7 +38,7 @@ cache: Cache['$format'] | ||
} | ||
export declare class Finder<O extends MODEL_DATA> { | ||
export declare class Finder<O extends MODEL_DATA, R> { | ||
$name: NameBase | ||
all: Query<O> | ||
all: Query<O, R> | ||
model: typeof Model | typeof Struct | ||
list: typeof List | ||
list: CLASS<List<O, R>> | ||
map: typeof Map | ||
@@ -53,13 +54,13 @@ constructor() | ||
$name: NameBase | ||
all: Query<O> | ||
all: Query<O, R> | ||
map: typeof Map | ||
list: typeof List | ||
list: CLASS<List<O, R>> | ||
model: typeof Model | typeof Struct | ||
}): void | ||
calculate(query: Query<O>, memory: Memory): void | ||
reduce({ map, cache, paths, query, memory }: ReduceContext<O>, ids: string[]): void | ||
finish({ map, paths, query }: ReduceContext<O>): void | ||
calculate(query: Query<O, R>, memory: Memory): void | ||
reduce({ map, cache, paths, query, memory }: ReduceContext<O, R>, ids: string[]): void | ||
finish({ map, paths, query }: ReduceContext<O, R>): void | ||
data_set(type: string, from: PlainData<O>, parent: Object | undefined): any | ||
data_emitter( | ||
{ base, journal }: SetContext<O>, | ||
{ base, journal }: SetContext<O, R>, | ||
{ | ||
@@ -74,12 +75,12 @@ item, | ||
data_init( | ||
{ model, parent, deploys }: SetContext<O>, | ||
{ model, parent, deploys }: SetContext<O, R>, | ||
{ item }: Datum, | ||
{ reduce, order }: Emitters | ||
): void | ||
data_entry({ model }: SetContext<O>, { item }: Datum, { reduce, order }: Emitters): void | ||
reset(ctx: SetContext<O>): boolean | ||
merge(ctx: SetContext<O>): boolean | ||
remove(ctx: SetContext<O>): boolean | ||
update(ctx: SetContext<O>, parent: Object): boolean | ||
data_entry({ model }: SetContext<O, R>, { item }: Datum, { reduce, order }: Emitters): void | ||
reset(ctx: SetContext<O, R>): boolean | ||
merge(ctx: SetContext<O, R>): boolean | ||
remove(ctx: SetContext<O, R>): boolean | ||
update(ctx: SetContext<O, R>, parent: Object): boolean | ||
} | ||
export {} |
import { Query } from './query' | ||
import { NameBase, MODEL_DATA, DIC, ID } from './type' | ||
export declare class List<O extends MODEL_DATA> extends Array { | ||
query: Query<O> | ||
export declare class List<O extends MODEL_DATA, R> extends Array { | ||
query: Query<O, R> | ||
static $name: NameBase | ||
@@ -11,10 +11,10 @@ get first(): O | ||
get uniq(): this | ||
pluck(...keys: ID[]): List<any> | ||
static bless<O extends MODEL_DATA>(list: any[], query: Query<O>): List<O> | ||
constructor(query: Query<O>) | ||
pluck(...keys: ID[]): List<any, any> | ||
static bless<O extends MODEL_DATA, R>(list: any[], query: Query<O, R>): List<O, R> | ||
constructor(query: Query<O, R>) | ||
sort(...cmd: any[]): this | ||
group_by(cb: any): DIC<List<O>> | ||
page_by(per: any): List<O>[] | ||
where(req: any): Query<O> | ||
in(req: any): Query<O> | ||
group_by(cb: any): DIC<List<O, R>> | ||
page_by(per: any): List<O, R>[] | ||
where(req: any): Query<O, R> | ||
in(req: any): Query<O, R> | ||
} |
import { Query } from './query' | ||
import { LeafCmd, ReduceLeaf, OrderCmd, Reduce, ReduceOrder, MODEL_DATA, NameBase } from './type' | ||
import { | ||
LeafCmd, | ||
ReduceLeaf, | ||
OrderCmd, | ||
Reduce, | ||
ReduceOrder, | ||
MODEL_DATA, | ||
NameBase, | ||
CLASS, | ||
} from './type' | ||
import { List } from './list' | ||
@@ -8,4 +17,4 @@ export declare class Map<O extends MODEL_DATA> { | ||
static init(o: ReduceLeaf, cmd: LeafCmd): void | ||
static reduce<O extends MODEL_DATA>( | ||
query: Query<O>, | ||
static reduce<O extends MODEL_DATA, R>( | ||
query: Query<O, R>, | ||
path: string, | ||
@@ -16,10 +25,10 @@ item: any, | ||
): void | ||
static finish<O extends MODEL_DATA>( | ||
query: Query<O>, | ||
static finish<O extends MODEL_DATA, R>( | ||
query: Query<O, R>, | ||
path: string, | ||
o: ReduceLeaf, | ||
list: typeof List | ||
list: CLASS<List<O, R>> | ||
): void | ||
static order<O extends MODEL_DATA>( | ||
query: Query<O>, | ||
static order<O extends MODEL_DATA, R>( | ||
query: Query<O, R>, | ||
path: string, | ||
@@ -29,20 +38,20 @@ from: Reduce, | ||
cmd: OrderCmd, | ||
list: typeof List | ||
): ReduceOrder<O> | ||
static dash<O extends MODEL_DATA>( | ||
query: Query<O>, | ||
list: CLASS<List<O, R>> | ||
): ReduceOrder<O, R> | ||
static dash<O extends MODEL_DATA, R>( | ||
query: Query<O, R>, | ||
path: string, | ||
from: ReduceOrder<O>, | ||
from: ReduceOrder<O, R>, | ||
origin: any, | ||
cmd: OrderCmd, | ||
list: typeof List | ||
): ReduceOrder<O> | ||
static post_proc<O extends MODEL_DATA>( | ||
query: Query<O>, | ||
list: CLASS<List<O, R>> | ||
): ReduceOrder<O, R> | ||
static post_proc<O extends MODEL_DATA, R>( | ||
query: Query<O, R>, | ||
path: string, | ||
from: ReduceOrder<O>, | ||
from: ReduceOrder<O, R>, | ||
origin: any, | ||
cmd: OrderCmd, | ||
list: typeof List | ||
): ReduceOrder<O> | ||
list: CLASS<List<O, R>> | ||
): ReduceOrder<O, R> | ||
} |
@@ -1,5 +0,5 @@ | ||
import { Reduce, Memory, Filter, OrderCmd, ID, DIC, MODEL_DATA, QUERY, SortCmd } from './type' | ||
import { Reduce, Memory, Filter, OrderCmd, ID, DIC, MODEL_DATA, QUERY_ARGS, SortCmd } from './type' | ||
import { Finder } from './finder' | ||
import { List } from './list' | ||
export declare class Query<O extends MODEL_DATA> { | ||
export declare class Query<O extends MODEL_DATA, R> { | ||
_is_uniq: boolean | ||
@@ -10,3 +10,3 @@ _all_ids: string[] | ||
_filters: Filter[] | ||
_finder: Finder<O> | ||
_finder: Finder<O, R> | ||
_group: any | ||
@@ -22,10 +22,16 @@ _cache: { | ||
$memory: Memory | ||
all: Query<O> | ||
all: Query<O, R> | ||
get reduce(): Reduce | ||
get list(): List<O> | ||
get list(): List<O, R> | ||
get hash(): DIC<O> | ||
get memory(): Memory | ||
get ids(): string[] | ||
static build<O extends MODEL_DATA>({ $sort, $memory }: { $sort: any; $memory: any }): Query<O> | ||
constructor(base: any, tap: (this: Query<O>) => void) | ||
static build<O extends MODEL_DATA, R>({ | ||
$sort, | ||
$memory, | ||
}: { | ||
$sort: any | ||
$memory: any | ||
}): Query<O, R> | ||
constructor(base: any, tap: (this: Query<O, R>) => void) | ||
_copy({ | ||
@@ -50,17 +56,17 @@ all, | ||
}): void | ||
in(req: QUERY): Query<O> | ||
where(req: (o: O) => any): Query<O> | ||
where(req: QUERY): Query<O> | ||
partition(...ary: string[]): Query<O> | ||
distinct(b?: boolean): Query<O> | ||
distance(key: any, order: any, point: any): Query<O> | ||
search(text: any, target?: string): Query<O> | ||
shuffle(): Query<O> | ||
order(keys: string | string[], order: OrderCmd): Query<O> | ||
sort(...sort: SortCmd): Query<O> | ||
page(page_by: number): Query<O> | ||
in(req: QUERY_ARGS): Query<O, R> | ||
where(req: (o: O) => any): Query<O, R> | ||
where(req: QUERY_ARGS): Query<O, R> | ||
partition(...ary: string[]): Query<O, R> | ||
distinct(b?: boolean): Query<O, R> | ||
distance(key: any, order: any, point: any): Query<O, R> | ||
search(text: any, target?: string): Query<O, R> | ||
shuffle(): Query<O, R> | ||
order(keys: string | string[], order: OrderCmd): Query<O, R> | ||
sort(...sort: SortCmd): Query<O, R> | ||
page(page_by: number): Query<O, R> | ||
form(...ids: ID[]): any | ||
find(...ids: ID[]): O | null | ||
finds(ids: ID[]): O[] | ||
pluck(...keys: ID[]): List<any> | ||
pluck(...keys: ID[]): List<any, any> | ||
} |
@@ -0,1 +1,2 @@ | ||
import { List } from './list' | ||
import { | ||
@@ -5,3 +6,2 @@ Cache, | ||
RelationCmd, | ||
DIC, | ||
CLASS, | ||
@@ -13,14 +13,40 @@ MODEL_DATA, | ||
OrderCmd, | ||
SCHEMA, | ||
SCOPE, | ||
QUERY, | ||
} from './type' | ||
import { Set } from './set' | ||
import { Map } from './map' | ||
import { Query } from './query' | ||
export declare class Rule<O extends MODEL_DATA, M extends CLASS<O>> { | ||
export declare class Rule<O extends MODEL_DATA, Q, R> { | ||
$name: NameBase | ||
state: Cache | ||
all: Query<O> | ||
model: any | ||
list: any | ||
set: any | ||
map: any | ||
constructor(base: string, modelClass?: CLASS<O>) | ||
schema(cb: (this: Rule<O, M>) => void): this | ||
all: QUERY<O, Q, R> | ||
model: CLASS<O> | ||
list: CLASS<List<O, R>> | ||
set: CLASS<Set<O>> | ||
map: CLASS<Map<O>> | ||
constructor( | ||
base: string, | ||
{ | ||
model, | ||
list, | ||
set, | ||
map, | ||
scope, | ||
scope_without_cache, | ||
schema, | ||
deploy, | ||
}?: { | ||
model?: CLASS<O> | ||
list?: CLASS<List<O, R>> | ||
set?: CLASS<Set<O>> | ||
map?: CLASS<Map<O>> | ||
scope?: SCOPE<O, Q, R> | ||
scope_without_cache?: SCOPE<O, Q, R> | ||
schema?: SCHEMA<O, Q, R> | ||
deploy?: DEPLOY<O> | ||
} | ||
) | ||
schema(cb: SCHEMA<O, Q, R>): this | ||
key_by( | ||
@@ -34,8 +60,8 @@ keys?: | ||
): void | ||
deploy(cb: DEPLOY<O, M>): void | ||
deploy(cb: DEPLOY<O>): void | ||
depend_on(parent: any): void | ||
scope_without_cache(cb: (all: Query<O>) => DIC<any>): void | ||
scope(cb: (all: Query<O>) => DIC<any>): void | ||
scope_without_cache(cb: SCOPE<O, Q, R>): void | ||
scope(cb: SCOPE<O, Q, R>): void | ||
property(type: any, o: any): void | ||
default_scope(scope: (all: Query<O>) => Query<O>): void | ||
default_scope(scope: (all: Query<O, R>) => Query<O, R>): void | ||
shuffle(): void | ||
@@ -42,0 +68,0 @@ sort(...sort: SortCmd): void |
@@ -30,7 +30,19 @@ 'use strict' | ||
} | ||
function method(r, key, o) { | ||
Object.defineProperty(r.model.prototype, key, o) | ||
function method({ prototype }, key, o) { | ||
Object.defineProperty(prototype, key, o) | ||
} | ||
class Rule { | ||
constructor(base, modelClass) { | ||
constructor( | ||
base, | ||
{ | ||
model = class model extends model_1.Model {}, | ||
list = class list extends list_1.List {}, | ||
set = class set extends set_1.Set {}, | ||
map = class map extends map_1.Map {}, | ||
scope, | ||
scope_without_cache, | ||
schema, | ||
deploy, | ||
} = {} | ||
) { | ||
this.$name = rename(base) | ||
@@ -43,6 +55,18 @@ this.state = mem_1.State.base(this.$name.list) | ||
this.depend_on(this.$name.list) | ||
this.model = modelClass || class model extends model_1.Model {} | ||
this.list = class list extends list_1.List {} | ||
this.set = class set extends set_1.Set {} | ||
this.map = class map extends map_1.Map {} | ||
this.model = model | ||
this.list = list | ||
this.set = set | ||
this.map = map | ||
if (scope_without_cache) { | ||
this.scope_without_cache(scope_without_cache) | ||
} | ||
if (scope) { | ||
this.scope(scope) | ||
} | ||
if (deploy) { | ||
this.deploy(deploy) | ||
} | ||
if (schema) { | ||
this.schema(schema) | ||
} | ||
} | ||
@@ -76,3 +100,3 @@ schema(cb) { | ||
})() | ||
method(this, 'id', { | ||
method(this.model, 'id', { | ||
enumerable: true, | ||
@@ -91,4 +115,3 @@ get, | ||
for (const key in cmd) { | ||
const val = cmd[key] | ||
this.all[key] = val | ||
this.all[key] = cmd[key] | ||
} | ||
@@ -99,4 +122,3 @@ } | ||
for (const key in cmd) { | ||
const val = cmd[key] | ||
this.use_cache(key, val) | ||
this.use_cache(key, cmd[key]) | ||
} | ||
@@ -122,3 +144,3 @@ } | ||
relation_to_one(key, target, ik, else_id) { | ||
method(this, key, { | ||
method(this.model, key, { | ||
enumerable: true, | ||
@@ -134,3 +156,3 @@ get() { | ||
this.use_cache(key, (id) => Mem.Query[target].distinct(false)[cmd]({ [qk]: id })) | ||
method(this, key, { | ||
method(this.model, key, { | ||
enumerable: true, | ||
@@ -152,3 +174,3 @@ get() { | ||
}) | ||
method(this, key, { | ||
method(this.model, key, { | ||
enumerable: true, | ||
@@ -180,3 +202,3 @@ value(n) { | ||
}) | ||
method(this, key, { | ||
method(this.model, key, { | ||
enumerable: true, | ||
@@ -227,3 +249,3 @@ value(n) { | ||
const pk = `${tail_key}_id` | ||
method(this, 'siblings', { | ||
method(this.model, 'siblings', { | ||
get() { | ||
@@ -230,0 +252,0 @@ return all.where({ [pk]: this[pk] }) |
@@ -7,4 +7,4 @@ import { Query } from './query' | ||
$name: NameBase | ||
all: Query<O> | ||
finder: Finder<O> | ||
all: Query<O, any> | ||
finder: Finder<O, any> | ||
model: CLASS<O> | ||
@@ -11,0 +11,0 @@ constructor({ $name, all, model }: { $name: any; all: any; model: any }) |
@@ -1,2 +0,2 @@ | ||
import { Metadata } from './mem' | ||
import { Metadata, Rule } from './mem' | ||
import { Model } from './model' | ||
@@ -19,3 +19,3 @@ import { Struct } from './struct' | ||
} | ||
export declare type DEPLOY<O, M> = { | ||
export declare type DEPLOY<O> = { | ||
( | ||
@@ -25,3 +25,3 @@ this: O, | ||
o: O | ||
model: M | ||
model: CLASS<O> | ||
reduce: LeafEmitter | ||
@@ -32,2 +32,9 @@ order: OrderEmitter | ||
} | ||
export declare type SCHEMA<O extends MODEL_DATA, Q, R> = { | ||
(this: Rule<O, Q, R>): void | ||
} | ||
export declare type QUERY<O extends MODEL_DATA, Q extends DIC<any>, R> = Query<O, R> & Q | ||
export declare type SCOPE<O extends MODEL_DATA, Q, R> = { | ||
(all: Query<O, R> & Q): DIC<any> | ||
} | ||
export declare type Emitter<T> = { | ||
@@ -41,3 +48,3 @@ (keys: PATH, cmd: T): void | ||
ids: string | ||
deploys: DEPLOY<any, any>[] | ||
deploys: DEPLOY<any>[] | ||
depends: (() => void)[] | ||
@@ -85,6 +92,6 @@ } | ||
}> | ||
export declare type ReduceOrder<O extends MODEL_DATA> = List<O> & | ||
export declare type ReduceOrder<O extends MODEL_DATA, R> = List<O, R> & | ||
Partial<{ | ||
id: ID | ||
query: Query<O> | ||
query: Query<O, R> | ||
from: ReduceLeaf | ||
@@ -94,3 +101,3 @@ all: number | ||
cover: string[] | ||
quantile: ReduceOrder<O> | ||
quantile: ReduceOrder<O, R> | ||
page_idx(this: Reduce[][], item: Object): number | null | ||
@@ -145,9 +152,9 @@ }> | ||
} | ||
export interface SetContext<O extends MODEL_DATA> { | ||
export interface SetContext<O extends MODEL_DATA, R> { | ||
model: typeof Model | typeof Struct | ||
all: Query<O> | ||
all: Query<O, R> | ||
base: Cache | ||
journal: Cache | ||
meta: Metadata | ||
deploys: DEPLOY<any, any>[] | ||
deploys: DEPLOY<any>[] | ||
from: PlainData<O> | ||
@@ -162,3 +169,3 @@ parent: Object | undefined | ||
} | ||
export declare type QUERY = { | ||
export declare type QUERY_ARGS = { | ||
[path: string]: (string | number)[] | RegExp | string | number | boolean | null | ||
@@ -165,0 +172,0 @@ } |
{ | ||
"name": "memory-orm", | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"description": "client side ORM + map reduce", | ||
@@ -5,0 +5,0 @@ "author": "7korobi", |
@@ -18,2 +18,3 @@ import _ from 'lodash' | ||
PATH, | ||
CLASS, | ||
} from './type' | ||
@@ -32,5 +33,5 @@ import { Model } from './model' | ||
} | ||
type ReduceContext<O extends MODEL_DATA> = { | ||
type ReduceContext<O extends MODEL_DATA, R> = { | ||
map: typeof Map | ||
query: Query<O> | ||
query: Query<O, R> | ||
memory: Memory | ||
@@ -43,3 +44,3 @@ cache: Cache['$format'] | ||
function each_by_id<O extends MODEL_DATA>({ from }: SetContext<O>, process: IdProcess) { | ||
function each_by_id<O extends MODEL_DATA, R>({ from }: SetContext<O, R>, process: IdProcess) { | ||
if (from instanceof Array) { | ||
@@ -52,3 +53,3 @@ for (const item of from) { | ||
function each<O extends MODEL_DATA>({ from }: SetContext<O>, process: PlainProcess<O>) { | ||
function each<O extends MODEL_DATA, R>({ from }: SetContext<O, R>, process: PlainProcess<O>) { | ||
if (from instanceof Array) { | ||
@@ -79,7 +80,7 @@ for (const item of from) { | ||
export class Finder<O extends MODEL_DATA> { | ||
export class Finder<O extends MODEL_DATA, R> { | ||
$name!: NameBase | ||
all!: Query<O> | ||
all!: Query<O, R> | ||
model!: typeof Model | typeof Struct | ||
list!: typeof List | ||
list!: CLASS<List<O, R>> | ||
map!: typeof Map | ||
@@ -97,5 +98,5 @@ | ||
$name: NameBase | ||
all: Query<O> | ||
all: Query<O, R> | ||
map: typeof Map | ||
list: typeof List | ||
list: CLASS<List<O, R>> | ||
model: typeof Model | typeof Struct | ||
@@ -111,3 +112,3 @@ }) { | ||
calculate(query: Query<O>, memory: Memory) { | ||
calculate(query: Query<O, R>, memory: Memory) { | ||
if (query._step >= State.step[this.$name.list]) { | ||
@@ -119,3 +120,3 @@ return | ||
query._step = step() | ||
const ctx: ReduceContext<O> = { | ||
const ctx: ReduceContext<O, R> = { | ||
map: this.map, | ||
@@ -157,3 +158,3 @@ query, | ||
reduce({ map, cache, paths, query, memory }: ReduceContext<O>, ids: string[]) { | ||
reduce({ map, cache, paths, query, memory }: ReduceContext<O, R>, ids: string[]) { | ||
if (!ids) { | ||
@@ -177,3 +178,3 @@ return | ||
finish({ map, paths, query }: ReduceContext<O>) { | ||
finish({ map, paths, query }: ReduceContext<O, R>) { | ||
for (const path in paths) { | ||
@@ -217,3 +218,3 @@ const o = paths[path] | ||
data_emitter({ base, journal }: SetContext<O>, { item, $group }): Emitters { | ||
data_emitter({ base, journal }: SetContext<O, R>, { item, $group }): Emitters { | ||
if (!base.$format) { | ||
@@ -264,3 +265,3 @@ throw new Error('bad context.') | ||
data_init( | ||
{ model, parent, deploys }: SetContext<O>, | ||
{ model, parent, deploys }: SetContext<O, R>, | ||
{ item }: Datum, | ||
@@ -277,3 +278,3 @@ { reduce, order }: Emitters | ||
data_entry({ model }: SetContext<O>, { item }: Datum, { reduce, order }: Emitters) { | ||
data_entry({ model }: SetContext<O, R>, { item }: Datum, { reduce, order }: Emitters) { | ||
model.map_partition(item, reduce) | ||
@@ -287,3 +288,3 @@ model.map_reduce(item, reduce) | ||
reset(ctx: SetContext<O>) { | ||
reset(ctx: SetContext<O, R>) { | ||
ctx.journal.$memory = PureObject() | ||
@@ -304,3 +305,3 @@ const news = (ctx.base.$memory = ctx.all.$memory = PureObject()) | ||
merge(ctx: SetContext<O>) { | ||
merge(ctx: SetContext<O, R>) { | ||
let is_hit = false | ||
@@ -331,3 +332,3 @@ each(ctx, (item) => { | ||
remove(ctx: SetContext<O>) { | ||
remove(ctx: SetContext<O, R>) { | ||
let is_hit = false | ||
@@ -346,3 +347,3 @@ each_by_id(ctx, (id) => { | ||
update(ctx: SetContext<O>, parent: Object) { | ||
update(ctx: SetContext<O, R>, parent: Object) { | ||
let is_hit = false | ||
@@ -349,0 +350,0 @@ each_by_id(ctx, (id) => { |
@@ -5,4 +5,4 @@ import _ from 'lodash' | ||
export class List<O extends MODEL_DATA> extends Array { | ||
query!: Query<O> | ||
export class List<O extends MODEL_DATA, R> extends Array { | ||
query!: Query<O, R> | ||
static $name: NameBase | ||
@@ -29,3 +29,3 @@ get first(): O { | ||
pluck(...keys: ID[]): List<any> { | ||
pluck(...keys: ID[]): List<any, any> { | ||
let cb | ||
@@ -50,3 +50,3 @@ switch (keys.length) { | ||
static bless<O extends MODEL_DATA>(list: any[], query: Query<O>) { | ||
static bless<O extends MODEL_DATA, R>(list: any[], query: Query<O, R>) { | ||
Reflect.setPrototypeOf(list, this.prototype) | ||
@@ -56,6 +56,6 @@ if (query && query.where && query.in) { | ||
} | ||
return (list as any) as List<O> | ||
return (list as any) as List<O, R> | ||
} | ||
constructor(query: Query<O>) { | ||
constructor(query: Query<O, R>) { | ||
super() | ||
@@ -74,3 +74,3 @@ if (query && query.where && query.in) { | ||
group_by(cb) { | ||
const o = (_.groupBy(this, cb) as any) as DIC<List<O>> | ||
const o = (_.groupBy(this, cb) as any) as DIC<List<O, R>> | ||
for (const key in o) { | ||
@@ -77,0 +77,0 @@ const oo = o[key] |
import _ from 'lodash' | ||
import { Query } from './query' | ||
import { LeafCmd, ReduceLeaf, OrderCmd, Reduce, ReduceOrder, MODEL_DATA, NameBase } from './type' | ||
import { | ||
LeafCmd, | ||
ReduceLeaf, | ||
OrderCmd, | ||
Reduce, | ||
ReduceOrder, | ||
MODEL_DATA, | ||
NameBase, | ||
CLASS, | ||
} from './type' | ||
import { List } from './list' | ||
@@ -102,4 +111,4 @@ | ||
static reduce<O extends MODEL_DATA>( | ||
query: Query<O>, | ||
static reduce<O extends MODEL_DATA, R>( | ||
query: Query<O, R>, | ||
path: string, | ||
@@ -160,7 +169,7 @@ item: any, | ||
static finish<O extends MODEL_DATA>( | ||
query: Query<O>, | ||
static finish<O extends MODEL_DATA, R>( | ||
query: Query<O, R>, | ||
path: string, | ||
o: ReduceLeaf, | ||
list: typeof List | ||
list: CLASS<List<O, R>> | ||
) { | ||
@@ -206,4 +215,4 @@ if (!o) { | ||
static order<O extends MODEL_DATA>( | ||
query: Query<O>, | ||
static order<O extends MODEL_DATA, R>( | ||
query: Query<O, R>, | ||
path: string, | ||
@@ -213,3 +222,3 @@ from: Reduce, | ||
cmd: OrderCmd, | ||
list: typeof List | ||
list: CLASS<List<O, R>> | ||
) { | ||
@@ -237,5 +246,5 @@ let o1 = from | ||
let o = o1 as ReduceOrder<O> | ||
let o = o1 as ReduceOrder<O, R> | ||
if (cmd.sort) { | ||
o = (_.orderBy(o, cmd.sort[0], cmd.sort[1]) as any) as ReduceOrder<O> | ||
o = (_.orderBy(o, cmd.sort[0], cmd.sort[1]) as any) as ReduceOrder<O, R> | ||
} | ||
@@ -246,3 +255,3 @@ | ||
const pad = (o.length - 1) / size | ||
const box = ([] as any) as ReduceOrder<O> | ||
const box = ([] as any) as ReduceOrder<O, R> | ||
const end = size + 1 | ||
@@ -317,9 +326,9 @@ for (let i = 0; i < end; i++) { | ||
static dash<O extends MODEL_DATA>( | ||
query: Query<O>, | ||
static dash<O extends MODEL_DATA, R>( | ||
query: Query<O, R>, | ||
path: string, | ||
from: ReduceOrder<O>, | ||
from: ReduceOrder<O, R>, | ||
origin, | ||
cmd: OrderCmd, | ||
list: typeof List | ||
list: CLASS<List<O, R>> | ||
) { | ||
@@ -338,9 +347,9 @@ if (!(from instanceof Array)) { | ||
static post_proc<O extends MODEL_DATA>( | ||
query: Query<O>, | ||
static post_proc<O extends MODEL_DATA, R>( | ||
query: Query<O, R>, | ||
path: string, | ||
from: ReduceOrder<O>, | ||
from: ReduceOrder<O, R>, | ||
origin, | ||
cmd: OrderCmd, | ||
list: typeof List | ||
list: CLASS<List<O, R>> | ||
) { | ||
@@ -364,6 +373,6 @@ let per | ||
if (cmd.page && (per = query.$page_by)) { | ||
o = ([] as any) as ReduceOrder<O> | ||
o = ([] as any) as ReduceOrder<O, R> | ||
o.all = from.length | ||
for (let idx = 0; idx < from.length; idx++) { | ||
let c: ReduceOrder<O> | ||
let c: ReduceOrder<O, R> | ||
if (!(idx % per)) { | ||
@@ -370,0 +379,0 @@ c = new list(query) |
import _ from 'lodash' | ||
import { Reduce, Memory, Filter, OrderCmd, ID, DIC, MODEL_DATA, QUERY, SortCmd } from './type' | ||
import { Reduce, Memory, Filter, OrderCmd, ID, DIC, MODEL_DATA, QUERY_ARGS, SortCmd } from './type' | ||
import { Model } from './model' | ||
@@ -17,6 +17,11 @@ import { Struct } from './struct' | ||
function query_parser<O extends MODEL_DATA>( | ||
base: Query<O>, | ||
function query_parser<O extends MODEL_DATA, R>( | ||
base: Query<O, R>, | ||
req: any, | ||
cb: (q: Query<O>, target: string | null, request: any, path: (o: Model | Struct) => any) => void | ||
cb: ( | ||
q: Query<O, R>, | ||
target: string | null, | ||
request: any, | ||
path: (o: Model | Struct) => any | ||
) => void | ||
) { | ||
@@ -27,3 +32,3 @@ if (!req) { | ||
return new Query<O>(base, function (this: Query<O>) { | ||
return new Query<O, R>(base, function (this: Query<O, R>) { | ||
this._filters = base._filters.concat() | ||
@@ -44,3 +49,3 @@ if (req instanceof Function || req instanceof Array || 'string' === typeof req) { | ||
export class Query<O extends MODEL_DATA> { | ||
export class Query<O extends MODEL_DATA, R> { | ||
_is_uniq!: boolean | ||
@@ -51,3 +56,3 @@ _all_ids!: string[] | ||
_filters!: Filter[] | ||
_finder!: Finder<O> | ||
_finder!: Finder<O, R> | ||
_group: any | ||
@@ -63,3 +68,3 @@ _cache!: { | ||
$memory!: Memory | ||
all!: Query<O> | ||
all!: Query<O, R> | ||
@@ -71,3 +76,3 @@ get reduce() { | ||
get list(): List<O> { | ||
get list(): List<O, R> { | ||
return this.reduce.list as any | ||
@@ -88,3 +93,3 @@ } | ||
static build<O extends MODEL_DATA>({ $sort, $memory }) { | ||
static build<O extends MODEL_DATA, R>({ $sort, $memory }) { | ||
const _group = null | ||
@@ -95,9 +100,12 @@ const _all_ids = null | ||
const $partition = ['set'] | ||
return new Query<O>({ _all_ids, _group, _is_uniq, _filters, $sort, $partition }, function () { | ||
this.all = this | ||
this.$memory = $memory | ||
}) | ||
return new Query<O, R>( | ||
{ _all_ids, _group, _is_uniq, _filters, $sort, $partition }, | ||
function () { | ||
this.all = this | ||
this.$memory = $memory | ||
} | ||
) | ||
} | ||
public constructor(base, tap: (this: Query<O>) => void) { | ||
public constructor(base, tap: (this: Query<O, R>) => void) { | ||
this._step = 0 | ||
@@ -119,3 +127,3 @@ this._copy(base) | ||
in(req: QUERY): Query<O> | ||
in(req: QUERY_ARGS): Query<O, R> | ||
in(req) { | ||
@@ -151,4 +159,4 @@ return query_parser(this, req, function (q, target, req, path) { | ||
where(req: (o: O) => any): Query<O> | ||
where(req: QUERY): Query<O> | ||
where(req: (o: O) => any): Query<O, R> | ||
where(req: QUERY_ARGS): Query<O, R> | ||
where(req) { | ||
@@ -181,3 +189,3 @@ return query_parser(this, req, function (q, target, req, path) { | ||
partition(...ary: string[]) { | ||
return new Query<O>(this, function () { | ||
return new Query<O, R>(this, function () { | ||
this.$partition = ary | ||
@@ -191,3 +199,3 @@ }) | ||
} | ||
return new Query<O>(this, function () { | ||
return new Query<O, R>(this, function () { | ||
this._is_uniq = b | ||
@@ -243,3 +251,3 @@ if (b && this._all_ids) { | ||
order(keys: string | string[], order: OrderCmd): Query<O> { | ||
order(keys: string | string[], order: OrderCmd): Query<O, R> { | ||
if (!keys.length) { | ||
@@ -256,3 +264,3 @@ keys = ['list'] | ||
} | ||
return new Query<O>(this, function () { | ||
return new Query<O, R>(this, function () { | ||
this.$sort = _.cloneDeep(this.$sort) | ||
@@ -263,8 +271,8 @@ this.$sort[path] = order | ||
sort(...sort: SortCmd): Query<O> { | ||
return this.order([], { sort }) as Query<O> | ||
sort(...sort: SortCmd): Query<O, R> { | ||
return this.order([], { sort }) as Query<O, R> | ||
} | ||
page(page_by: number) { | ||
return new Query<O>(this, function () { | ||
return new Query<O, R>(this, function () { | ||
this.$page_by = page_by | ||
@@ -271,0 +279,0 @@ }) |
106
src/rule.ts
@@ -10,3 +10,2 @@ import _ from 'lodash' | ||
RelationCmd, | ||
DIC, | ||
CLASS, | ||
@@ -18,2 +17,5 @@ MODEL_DATA, | ||
OrderCmd, | ||
SCHEMA, | ||
SCOPE, | ||
QUERY, | ||
} from './type' | ||
@@ -44,37 +46,71 @@ import { Set } from './set' | ||
function method<O extends MODEL_DATA, M extends CLASS<O>>(r: Rule<O, M>, key: string, o: Object) { | ||
Object.defineProperty(r.model.prototype, key, o) | ||
function method({ prototype }: any, key: string, o: Object) { | ||
Object.defineProperty(prototype, key, o) | ||
} | ||
export class Rule<O extends MODEL_DATA, M extends CLASS<O>> { | ||
export class Rule<O extends MODEL_DATA, Q, R> { | ||
$name: NameBase | ||
state: Cache | ||
all: Query<O> | ||
all: QUERY<O, Q, R> | ||
model!: any | ||
list!: any | ||
set!: any | ||
map!: any | ||
model!: CLASS<O> | ||
list!: CLASS<List<O, R>> | ||
set!: CLASS<Set<O>> | ||
map!: CLASS<Map<O>> | ||
constructor(base: string, modelClass?: CLASS<O>) { | ||
constructor( | ||
base: string, | ||
{ | ||
model = class model extends Model {} as any, | ||
list = class list extends List<O, R> {} as any, | ||
set = class set extends Set<O> {} as any, | ||
map = class map extends Map<O> {} as any, | ||
scope, | ||
scope_without_cache, | ||
schema, | ||
deploy, | ||
}: { | ||
model?: CLASS<O> | ||
list?: CLASS<List<O, R>> | ||
set?: CLASS<Set<O>> | ||
map?: CLASS<Map<O>> | ||
scope?: SCOPE<O, Q, R> | ||
scope_without_cache?: SCOPE<O, Q, R> | ||
schema?: SCHEMA<O, Q, R> | ||
deploy?: DEPLOY<O> | ||
} = {} | ||
) { | ||
this.$name = rename(base) | ||
this.state = State.base(this.$name.list) | ||
this.all = Query.build<O>(this.state) | ||
this.all = Query.build<O, R>(this.state) as Query<O, R> & Q | ||
this.all.$sort['_reduce.list'] = {} | ||
this.all._cache = {} | ||
this.all._finder = new Finder<O>() | ||
this.all._finder = new Finder<O, R>() | ||
this.depend_on(this.$name.list) | ||
this.model = modelClass || class model extends Model {} | ||
this.list = class list extends List<O> {} | ||
this.set = class set extends Set<O> {} | ||
this.map = class map extends Map<O> {} | ||
this.model = model | ||
this.list = list | ||
this.set = set | ||
this.map = map | ||
if (scope_without_cache) { | ||
this.scope_without_cache(scope_without_cache) | ||
} | ||
if (scope) { | ||
this.scope(scope) | ||
} | ||
if (deploy) { | ||
this.deploy(deploy) | ||
} | ||
if (schema) { | ||
this.schema(schema) | ||
} | ||
} | ||
schema(cb: (this: Rule<O, M>) => void) { | ||
schema(cb: SCHEMA<O, Q, R>) { | ||
cb.call(this) | ||
this.model.$name = this.list.$name = this.set.$name = this.map.$name = this.$name | ||
this.all._finder.join(this) | ||
this.all._finder.join(this as any) | ||
@@ -106,3 +142,3 @@ Mem.Set[this.$name.base] = new this.set(this) | ||
method(this, 'id', { | ||
method(this.model, 'id', { | ||
enumerable: true, | ||
@@ -113,3 +149,3 @@ get, | ||
deploy(cb: DEPLOY<O, M>) { | ||
deploy(cb: DEPLOY<O>) { | ||
this.$name.deploys.push(cb) | ||
@@ -122,15 +158,13 @@ } | ||
scope_without_cache(cb: (all: Query<O>) => DIC<any>) { | ||
scope_without_cache(cb: SCOPE<O, Q, R>) { | ||
const cmd = cb(this.all) | ||
for (const key in cmd) { | ||
const val = cmd[key] | ||
this.all[key] = val | ||
;(this.all as any)[key] = cmd[key] | ||
} | ||
} | ||
scope(cb: (all: Query<O>) => DIC<any>) { | ||
scope(cb: SCOPE<O, Q, R>) { | ||
const cmd = cb(this.all) | ||
for (const key in cmd) { | ||
const val = cmd[key] | ||
this.use_cache(key, val) | ||
this.use_cache(key, cmd[key]) | ||
} | ||
@@ -143,3 +177,3 @@ } | ||
default_scope(scope: (all: Query<O>) => Query<O>) { | ||
default_scope(scope: (all: Query<O, R>) => Query<O, R>) { | ||
this.all._copy(scope(this.all)) | ||
@@ -163,3 +197,3 @@ const base = State.base(this.$name.list) | ||
relation_to_one(key: string, target: string, ik: ID, else_id?: ID) { | ||
method(this, key, { | ||
method(this.model, key, { | ||
enumerable: true, | ||
@@ -177,3 +211,3 @@ get() { | ||
method(this, key, { | ||
method(this.model, key, { | ||
enumerable: true, | ||
@@ -197,3 +231,3 @@ get() { | ||
method(this, key, { | ||
method(this.model, key, { | ||
enumerable: true, | ||
@@ -212,3 +246,3 @@ value(this: Model | Struct, n: number) { | ||
const ids: any[] = [] | ||
for (const a of q.pluck(ik) as List<any>) { | ||
for (const a of q.pluck(ik) as List<any, any>) { | ||
if (a != null) { | ||
@@ -229,3 +263,3 @@ for (let k of a) { | ||
method(this, key, { | ||
method(this.model, key, { | ||
enumerable: true, | ||
@@ -240,3 +274,3 @@ value(this: Model | Struct, n: number) { | ||
if (val instanceof Function) { | ||
this.all[key] = (...args: string[]) => { | ||
;(this.all as any)[key] = (...args: string[]) => { | ||
const name = `${key}:${JSON.stringify(args)}` | ||
@@ -246,3 +280,3 @@ return this.all._cache[name] || (this.all._cache[name] = val(...args)) | ||
} else { | ||
this.all[key] = val | ||
;(this.all as any)[key] = val | ||
} | ||
@@ -285,3 +319,3 @@ } | ||
const pk = `${tail_key}_id` | ||
method(this, 'siblings', { | ||
method(this.model, 'siblings', { | ||
get() { | ||
@@ -323,3 +357,3 @@ return all.where({ [pk]: this[pk] }) | ||
leaf: { | ||
get(this: Query<O>) { | ||
get(this: Query<O, R>) { | ||
const not_leaf = _.uniq(this.pluck(fk)) | ||
@@ -326,0 +360,0 @@ return this.where((o) => !not_leaf.includes(o.id)) |
@@ -11,4 +11,4 @@ import _ from 'lodash' | ||
$name: NameBase | ||
all: Query<O> | ||
finder: Finder<O> | ||
all: Query<O, any> | ||
finder: Finder<O, any> | ||
model: CLASS<O> | ||
@@ -15,0 +15,0 @@ |
@@ -24,6 +24,16 @@ import { Metadata, Rule } from './mem' | ||
export type DEPLOY<O, M> = { | ||
(this: O, cmd: { o: O; model: M; reduce: LeafEmitter; order: OrderEmitter }): void | ||
export type DEPLOY<O> = { | ||
(this: O, cmd: { o: O; model: CLASS<O>; reduce: LeafEmitter; order: OrderEmitter }): void | ||
} | ||
export type SCHEMA<O extends MODEL_DATA, Q, R> = { | ||
(this: Rule<O, Q, R>): void | ||
} | ||
export type QUERY<O extends MODEL_DATA, Q extends DIC<any>, R> = Query<O, R> & Q | ||
export type SCOPE<O extends MODEL_DATA, Q, R> = { | ||
(all: Query<O, R> & Q): DIC<any> | ||
} | ||
export type Emitter<T> = { | ||
@@ -38,3 +48,3 @@ (keys: PATH, cmd: T): void | ||
ids: string | ||
deploys: DEPLOY<any, any>[] | ||
deploys: DEPLOY<any>[] | ||
depends: (() => void)[] | ||
@@ -87,6 +97,6 @@ } | ||
export type ReduceOrder<O extends MODEL_DATA> = List<O> & | ||
export type ReduceOrder<O extends MODEL_DATA, R> = List<O, R> & | ||
Partial<{ | ||
id: ID | ||
query: Query<O> | ||
query: Query<O, R> | ||
from: ReduceLeaf | ||
@@ -96,3 +106,3 @@ all: number | ||
cover: string[] | ||
quantile: ReduceOrder<O> | ||
quantile: ReduceOrder<O, R> | ||
page_idx(this: Reduce[][], item: Object): number | null | ||
@@ -153,9 +163,9 @@ }> | ||
export interface SetContext<O extends MODEL_DATA> { | ||
export interface SetContext<O extends MODEL_DATA, R> { | ||
model: typeof Model | typeof Struct | ||
all: Query<O> | ||
all: Query<O, R> | ||
base: Cache | ||
journal: Cache | ||
meta: Metadata | ||
deploys: DEPLOY<any, any>[] | ||
deploys: DEPLOY<any>[] | ||
from: PlainData<O> | ||
@@ -172,3 +182,3 @@ parent: Object | undefined | ||
export type QUERY = { | ||
export type QUERY_ARGS = { | ||
[path: string]: (string | number)[] | RegExp | string | number | boolean | null | ||
@@ -175,0 +185,0 @@ } |
@@ -10,4 +10,4 @@ import { DIC, NameBase } from './type' | ||
export const Map: DIC<MapBase<any>> = {} | ||
export const Query: DIC<QueryBase<any>> = {} | ||
export const Finder: DIC<FinderBase<any>> = {} | ||
export const Query: DIC<QueryBase<any, any>> = {} | ||
export const Finder: DIC<FinderBase<any, any>> = {} | ||
@@ -14,0 +14,0 @@ export function merge(o: DIC<any>) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
820323
0.39%4099
3.25%