@live-change/dao 0.4.6 → 0.4.9

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.
package/lib/DaoProxy.js CHANGED
@@ -57,7 +57,7 @@ class DaoProxy extends EventEmitter {
57
57
  const newObservable = this.observables.get(spath)
58
58
  if(newObservable && newObservable !== observable) {
59
59
  observable.observable = newObservable
60
- } else if(observable.observable.isDisposed()) {
60
+ } else if(!observable.observable || observable.observable.isDisposed()) {
61
61
  if(this.dao) {
62
62
  observable.observable = this.dao.observable(what)
63
63
  } else {
@@ -91,13 +91,17 @@ class ObservableProxy extends Observable {
91
91
  }
92
92
 
93
93
  bindProperty(object, property) {
94
- if(this.isDisposed()) this.respawn()
94
+ if(this.isDisposed()) {
95
+ this.respawn()
96
+ }
95
97
  this.properties.push([object, property])
96
- if(this.observable) this.observable.bindProperty(object, property)
98
+ if(this.observable) {
99
+ this.observable.bindProperty(object, property)
100
+ }
97
101
  }
98
102
  unbindProperty(object, property) {
99
- for(var i = 0; i < this.properties.length; i++) {
100
- var prop = this.properties[i]
103
+ for(let i = 0; i < this.properties.length; i++) {
104
+ const prop = this.properties[i]
101
105
  if(prop[0] === object && prop[1] === property) {
102
106
  this.properties.splice(i, 1)
103
107
  if(this.observable) this.observable.unbindProperty(object, property)
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,21 @@ 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(paramsFunc) {
75
+ let newActions = this.actions ? this.actions.slice() : []
76
+ const source = sourceProxy()
77
+ const actionObject = paramsFunc(source)
78
+ const path = actionObject.slice(0, -1)
79
+ const params = actionObject[actionObject.length - 1]
80
+ let processedParams = processParams(params)
81
+ const action = {
82
+ path,
83
+ params: { object: processedParams }
84
+ }
85
+ newActions.push(action)
86
+ return new Path(this.what, this.more, this.to, newActions)
66
87
  }
67
88
  get(func) {
68
89
  const source = sourceProxy()
@@ -74,7 +95,7 @@ class Path {
74
95
  }
75
96
 
76
97
  bind(to) {
77
- return new Path(this.what, this.more, to)
98
+ return new Path(this.what, this.more, to, this.actions)
78
99
  }
79
100
  }
80
101
 
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "url": "https://github.com/live-change/live-change-dao.git"
14
14
  },
15
15
  "dependencies": {
16
- "debug": "^4.3.2"
16
+ "debug": "^4.3.4"
17
17
  },
18
18
  "description": "live data access object",
19
19
  "devDependencies": {
@@ -35,6 +35,6 @@
35
35
  "scripts": {
36
36
  "test": "NODE_ENV=test blue-tape tests/*"
37
37
  },
38
- "version": "0.4.6",
39
- "gitHead": "29c98c353b6f8cc902d3bf2f67cdfc47e313ef58"
38
+ "version": "0.4.9",
39
+ "gitHead": "ec7e7187e4d9c3888b399b6eebd672e6c992e1a0"
40
40
  }
package/tests/path.js ADDED
@@ -0,0 +1,60 @@
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(task => deleteTask({ task: task.id }))
53
+ t.deepEqual(clean(path), {"what":["tasks",{}],"actions":[
54
+ {"path":["tasks"],"params":{"object":{"task":{"property":"id"}}}}
55
+ ]})
56
+ })
57
+
58
+ })
59
+
60
+ test.onFinish(() => process.exit(0))