@live-change/dao 0.4.8 → 0.4.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -65,7 +65,7 @@ class ObservableList extends Observable {
65
65
  if(i == l) this.list.push(element)
66
66
  } else {
67
67
  let i
68
- for(i = this.list.length-1; i >= 0; i--) {
68
+ for(i = this.list.length - 1; i >= 0; i--) {
69
69
  if(this.list[i][field] == value) {
70
70
  oldElement = this.list[i]
71
71
  this.list.splice(i, 1, element)
package/lib/Path.js CHANGED
@@ -26,6 +26,7 @@ function resolve(schema) {
26
26
  return schema.map(resolve)
27
27
  }
28
28
  if(typeof schema == 'object') {
29
+ //console.log("RESOLVE", schema)
29
30
  if(schema.$toPath) return schema.$toPath()
30
31
  const out = {}
31
32
  for(const key in schema) out[key] = resolve(schema[key])
@@ -34,11 +35,24 @@ function resolve(schema) {
34
35
  return schema
35
36
  }
36
37
 
38
+ function processParams(params) {
39
+ let processedParams = {}
40
+ for(const key in params) {
41
+ const param = params[key]
42
+ //console.log("PARAM", key, param)
43
+ const resolvedParam = resolve(param)
44
+ //console.log("RESOLVED PARAM", key, resolvedParam)
45
+ processedParams[key] = resolvedParam
46
+ }
47
+ return processedParams
48
+ }
49
+
37
50
  class Path {
38
- constructor(what, more = undefined, to = undefined) {
51
+ constructor(what, more = undefined, to = undefined, actions = undefined) {
39
52
  this.what = what
40
53
  this.more = more
41
54
  this.to = to
55
+ this.actions = actions
42
56
  }
43
57
  with(...funcs) {
44
58
  let newMore = this.more ? this.more.slice() : []
@@ -47,14 +61,7 @@ class Path {
47
61
  const fetchObject = func(source)
48
62
  const path = fetchObject.what.slice(0, -1)
49
63
  const params = fetchObject.what[fetchObject.what.length - 1]
50
- let processedParams = {}
51
- for(const key in params) {
52
- const param = params[key]
53
- //console.log("PARAM", key, param)
54
- const resolvedParam = resolve(param)
55
- //console.log("RESOLVED PARAM", key, resolvedParam)
56
- processedParams[key] = resolvedParam
57
- }
64
+ let processedParams = processParams(params)
58
65
  const more = {
59
66
  schema: [[...path, { object: processedParams }]],
60
67
  more: fetchObject.more,
@@ -62,7 +69,22 @@ class Path {
62
69
  }
63
70
  newMore.push(more)
64
71
  }
65
- return new Path(this.what, newMore)
72
+ return new Path(this.what, newMore, this.to, this.actions)
73
+ }
74
+ action(name, paramsFunc) {
75
+ let newActions = this.actions ? this.actions.slice() : []
76
+ const source = sourceProxy()
77
+ const actionObject = (paramsFunc || to)(source)
78
+ const path = actionObject.slice(0, -1)
79
+ const params = actionObject[actionObject.length - 1]
80
+ let processedParams = processParams(params)
81
+ const action = {
82
+ name: paramsFunc ? name : undefined,
83
+ path,
84
+ params: { object: processedParams },
85
+ }
86
+ newActions.push(action)
87
+ return new Path(this.what, this.more, this.to, newActions)
66
88
  }
67
89
  get(func) {
68
90
  const source = sourceProxy()
@@ -74,7 +96,7 @@ class Path {
74
96
  }
75
97
 
76
98
  bind(to) {
77
- return new Path(this.what, this.more, to)
99
+ return new Path(this.what, this.more, to, this.actions)
78
100
  }
79
101
  }
80
102
 
package/package.json CHANGED
@@ -35,6 +35,6 @@
35
35
  "scripts": {
36
36
  "test": "NODE_ENV=test blue-tape tests/*"
37
37
  },
38
- "version": "0.4.8",
39
- "gitHead": "7e38d4629d65410f7552d3d69857547caf709e20"
38
+ "version": "0.4.12",
39
+ "gitHead": "49986db53465eb08a50e6090343fc8cbaf230670"
40
40
  }
package/tests/path.js ADDED
@@ -0,0 +1,61 @@
1
+ const test = require('blue-tape')
2
+ const testServerDao = require('./testServerDao.js')
3
+ const { Path } = require("../index.js")
4
+
5
+ const users = (params) => new Path(['users', params])
6
+ const user = (params) => new Path(['user', params])
7
+ const avatar = (params) => new Path(['avatar', params])
8
+ const tasks = (params) => new Path(['tasks', params])
9
+ const deleteTask = (params) => ['tasks', params]
10
+
11
+ const clean = x => JSON.parse(JSON.stringify(x))
12
+
13
+ test("Path", t => {
14
+ t.plan(4)
15
+
16
+ t.test("single user query", t => {
17
+ t.plan(1)
18
+ const path = user({ user: '123' })
19
+ t.deepEqual(clean(path), { what: [ 'user', { user: '123' } ] })
20
+ })
21
+
22
+ t.test("all users with avatars", t => {
23
+ t.plan(1)
24
+ const path = users({ }).with(u => avatar({ user: u.id }))
25
+ t.deepEqual(clean(path), {"what":["users",{}],"more":[{"schema":[["avatar",{"object":{"user":{"property":"id"}}}]]}]})
26
+ })
27
+
28
+ t.test("all tasks with authors and observers with avatars", t => {
29
+ t.plan(1)
30
+ const userWithAvatar = (id) => user({ user: id }).with(u => avatar({ id: u.id }))
31
+ const path = tasks({ })
32
+ .with(task => userWithAvatar(task.author))
33
+ .with(task => userWithAvatar(task.observers))
34
+ t.deepEqual(clean(path), { "what": [ "tasks", {} ], "more": [
35
+ {
36
+ "schema": [["user", { "object": { "user": { "property": "author" } } } ]],
37
+ "more": [
38
+ { "schema": [["avatar", {"object": {"id": {"property": "id"}}}]] }
39
+ ]
40
+ },
41
+ {
42
+ "schema": [["user", {"object": {"user": {"property": "observers"}}}]],
43
+ "more": [
44
+ {"schema": [["avatar", {"object": {"id": {"property": "id"}}}]]}
45
+ ]
46
+ }
47
+ ]})
48
+ })
49
+
50
+ t.test("tasks with delete actions", t => {
51
+ t.plan(1)
52
+ const path = tasks({ }).action('delete', task => deleteTask({ task: task.id }))
53
+ console.log(JSON.stringify(path))
54
+ t.deepEqual(clean(path), {"what":["tasks",{}],"actions":[
55
+ {"name":"delete","path":["tasks"],"params":{"object":{"task":{"property":"id"}}}}
56
+ ]})
57
+ })
58
+
59
+ })
60
+
61
+ test.onFinish(() => process.exit(0))