@live-change/session-service 0.1.6 → 0.1.11

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/definition.js CHANGED
@@ -1,8 +1,11 @@
1
1
  const App = require("@live-change/framework")
2
2
  const app = App.app()
3
3
 
4
+ const relationsPlugin = require('@live-change/relations-plugin')
5
+
4
6
  const definition = app.createServiceDefinition({
5
- name: "session"
7
+ name: "session",
8
+ use: [ relationsPlugin ]
6
9
  })
7
10
 
8
11
  module.exports = definition
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/session-service",
3
- "version": "0.1.6",
3
+ "version": "0.1.11",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,6 +21,7 @@
21
21
  "url": "https://www.viamage.com/"
22
22
  },
23
23
  "dependencies": {
24
- "@live-change/framework": "^0.5.7"
24
+ "@live-change/framework": "^0.5.7",
25
+ "@live-change/relations-plugin": "^0.1.10"
25
26
  }
26
27
  }
package/sessionItem.js CHANGED
@@ -12,7 +12,7 @@ definition.processor(function(service, app) {
12
12
  if (model.properties.session) throw new Error('session property already exists!!!')
13
13
  const originalModelProperties = {...model.properties}
14
14
  const modelProperties = Object.keys(model.properties)
15
- const defaults = App.utils.generateDefault(modelProperties)
15
+ const defaults = App.utils.generateDefault(model.properties)
16
16
  const modelPropertyName = modelName.slice(0, 1).toLowerCase() + modelName.slice(1)
17
17
 
18
18
  function modelRuntime() {
@@ -22,28 +22,18 @@ definition.processor(function(service, app) {
22
22
  const config = model.sessionItem
23
23
  const writeableProperties = modelProperties || config.writableProperties
24
24
 
25
- console.log("SESSIONM ITEM", model)
25
+ console.log("SESSION ITEM", model)
26
26
 
27
- model.properties.session = new PropertyDefinition({
28
- type: Session,
29
- validation: ['nonEmpty']
30
- })
31
- if(!model.indexes) model.indexes = {}
32
- model.indexes.bySession = new IndexDefinition({
33
- property: 'session'
34
- })
35
- for(const sortField of config.sortBy) {
36
- const sortFieldUc = sortField.slice(0, 1).toUpperCase() + sortField.slice(1)
37
- model.indexes['bySession' + sortFieldUc] = new IndexDefinition({
38
- property: ['session', sortField]
39
- })
27
+ model.itemOf = {
28
+ what: Session,
29
+ ...config
40
30
  }
41
31
 
42
- if(config.readAccess) {
43
- const viewName = 'session' + modelName + 's'
32
+ if(config.sessionReadAccess) {
33
+ const viewName = 'mySession' + modelName + 's'
44
34
  service.views[viewName] = new ViewDefinition({
45
35
  name: viewName,
46
- access: config.readAccess,
36
+ access: config.sessionReadAccess,
47
37
  properties: App.rangeProperties,
48
38
  daoPath(range, { client, context }) {
49
39
  const path = modelRuntime().indexRangePath('bySession', [client.session], range )
@@ -52,7 +42,7 @@ definition.processor(function(service, app) {
52
42
  })
53
43
  for(const sortField of config.sortBy) {
54
44
  const sortFieldUc = sortField.slice(0, 1).toUpperCase() + sortField.slice(1)
55
- const viewName = 'session' + modelName + 'sBy' + sortFieldUc
45
+ const viewName = 'mySession' + modelName + 'sBy' + sortFieldUc
56
46
  service.views[viewName] = new ViewDefinition({
57
47
  name: viewName,
58
48
  access: config.readAccess,
@@ -63,45 +53,13 @@ definition.processor(function(service, app) {
63
53
  })
64
54
  }
65
55
  }
66
- if(config.publicAccess) {
67
- const viewName = 'publicSession' + modelName + 's'
68
- service.views[viewName] = new ViewDefinition({
69
- name: viewName,
70
- access: config.publicAccess,
71
- properties: App.rangeProperties,
72
- daoPath(range, { client, context }) {
73
- return modelRuntime().sortedIndexRangePath('bySession', [range.session], { ...range, session: undefined } )
74
- }
75
- })
76
- for(const sorfField of config.sortBy) {
77
- const sortFieldUc = sorfField.slice(0, 1).toUpperCase() + sortField.slice(1)
78
- const viewName = 'publicSession' + modelName + 'sBy' + sortFieldUc
79
- service.views[viewName] = new ViewDefinition({
80
- name: viewName,
81
- access: config.publicAccess,
82
- properties: App.rangeProperties,
83
- daoPath(range, { client, context }) {
84
- return modelRuntime().sortedIndexRangePath('bySession' + sortFieldUc, [client.session], range )
85
- }
86
- })
87
- }
88
- }
89
56
 
90
- if(config.createAccess || config.writeAccess) {
91
- const eventName = 'session' + modelName + 'Created'
92
- service.events[eventName] = new EventDefinition({
93
- name: eventName,
94
- execute(properties) {
95
- const data = properties.data
96
- const session = properties.session
97
- const id = properties[modelPropertyName]
98
- return modelRuntime().create({ ...data, session, id })
99
- }
100
- })
101
- const actionName = 'createSession' + modelName
57
+ if(config.sessionCreateAccess || config.sessionWriteAccess) {
58
+ const eventName = 'sessionOwned' + modelName + 'Created'
59
+ const actionName = 'createMySession' + modelName
102
60
  service.actions[actionName] = new ActionDefinition({
103
61
  name: actionName,
104
- access: config.createAccess || config.writeAccess,
62
+ access: config.sessionCreateAccess || config.sessionWriteAccess,
105
63
  properties: {
106
64
  ...originalModelProperties,
107
65
  [modelPropertyName]: {
@@ -109,35 +67,30 @@ definition.processor(function(service, app) {
109
67
  validation: ['localId']
110
68
  }
111
69
  },
112
- queuedBy: (command) => command.client.session,
70
+ //queuedBy: (command) => command.client.session,
113
71
  waitForEvents: true,
114
72
  async execute(properties, { client, service }, emit) {
115
73
  const id = properties[modelPropertyName] || app.generateUid()
74
+ const entity = await modelRuntime().get(id)
75
+ if(entity) throw 'exists'
116
76
  emit({
117
77
  type: eventName,
118
78
  [modelPropertyName]: id,
119
- session: client.session,
79
+ identifiers: {
80
+ session: client.session,
81
+ },
120
82
  data: properties
121
83
  })
122
84
  return id
123
85
  }
124
86
  })
125
87
  }
126
- if(config.updateAccess || config.writeAccess) {
127
- const eventName = 'session' + modelName + 'Updated'
128
- service.events[eventName] = new EventDefinition({
129
- name: eventName,
130
- execute(properties) {
131
- const data = properties.data
132
- const id = properties[modelPropertyName]
133
- const session = properties.session
134
- return modelRuntime().update(id, { ...data, id, session })
135
- }
136
- })
137
- const actionName = 'updateSession' + modelName
88
+ if(config.sessionUpdateAccess || config.sessionWriteAccess) {
89
+ const eventName = 'sessionOwned' + modelName + 'Updated'
90
+ const actionName = 'updateMySession' + modelName
138
91
  service.actions[actionName] = new ActionDefinition({
139
92
  name: actionName,
140
- access: config.updateAccess || config.writeAccess,
93
+ access: config.sessionUpdateAccess || config.sessionWriteAccess,
141
94
  properties: {
142
95
  ...originalModelProperties,
143
96
  [modelPropertyName]: {
@@ -150,8 +103,8 @@ definition.processor(function(service, app) {
150
103
  waitForEvents: true,
151
104
  async execute(properties, { client, service }, emit) {
152
105
  const entity = await modelRuntime().get(properties[modelPropertyName])
153
- if(!entity) throw new Error('not_found')
154
- if(entity.session != client.session) throw new Error('not_authorized')
106
+ if(!entity) throw 'not_found'
107
+ if(entity.session != client.session) throw 'not_authorized'
155
108
  let updateObject = {}
156
109
  for(const propertyName of writeableProperties) {
157
110
  if(properties.hasOwnProperty(propertyName)) {
@@ -159,12 +112,13 @@ definition.processor(function(service, app) {
159
112
  }
160
113
  }
161
114
  const merged = App.utils.mergeDeep({}, entity, updateObject)
162
- console.log("VALIDATE INTERNAL!!!!", merged)
163
115
  await App.validation.validate(merged, validators, { source: action, action, service, app, client })
164
116
  emit({
165
117
  type: eventName,
166
118
  [modelPropertyName]: entity.id,
167
- session: client.session,
119
+ identifiers: {
120
+ session: client.session,
121
+ },
168
122
  data: properties
169
123
  })
170
124
  }
@@ -172,19 +126,12 @@ definition.processor(function(service, app) {
172
126
  const action = service.actions[actionName]
173
127
  const validators = App.validation.getValidators(action, service, action)
174
128
  }
175
- if(config.deleteAccess || config.writeAccess) {
176
- const eventName = 'session' + modelName + 'Deleted'
177
- service.events[eventName] = new EventDefinition({
178
- name: eventName,
179
- execute(properties) {
180
- const id = properties[modelPropertyName]
181
- return modelRuntime().delete(id)
182
- }
183
- })
184
- const actionName = 'deleteSession' + modelName
129
+ if(config.sessionDeleteAccess || config.sessionWriteAccess) {
130
+ const eventName = 'sessionOwned' + modelName + 'Deleted'
131
+ const actionName = 'deleteMySession' + modelName
185
132
  service.actions[actionName] = new ActionDefinition({
186
133
  name: actionName,
187
- access: config.createAccess || config.writeAccess,
134
+ access: config.sessionDeleteAccess || config.sessionWriteAccess,
188
135
  properties: {
189
136
  [modelPropertyName]: {
190
137
  type: model,
@@ -195,12 +142,14 @@ definition.processor(function(service, app) {
195
142
  waitForEvents: true,
196
143
  async execute(properties, { client, service }, emit) {
197
144
  const entity = await modelRuntime().get(properties[modelPropertyName])
198
- if(!entity) throw new Error('not_found')
199
- if(entity.session != client.session) throw new Error('not_authorized')
145
+ if(!entity) throw 'not_found'
146
+ if(entity.session != client.session) throw 'not_authorized'
200
147
  emit({
201
148
  type: eventName,
202
- session: client.session,
203
- [modelPropertyName]: entity.id
149
+ [modelPropertyName]: entity.id,
150
+ identifiers: {
151
+ session: client.session
152
+ }
204
153
  })
205
154
  }
206
155
  })
@@ -7,13 +7,15 @@ definition.processor(function(service, app) {
7
7
 
8
8
  for(let modelName in service.models) {
9
9
  const model = service.models[modelName]
10
+ console.log("SP", modelName)
10
11
 
11
12
  if(model.sessionProperty) {
12
- console.trace("PROCESS MODEL " + modelName)
13
+ console.log("MODEL " + modelName + " IS SESSION PROPERTY, CONFIG:", model.sessionProperty)
13
14
  if (model.properties.session) throw new Error('session property already exists!!!')
15
+
14
16
  const originalModelProperties = {...model.properties}
15
17
  const modelProperties = Object.keys(model.properties)
16
- const defaults = App.utils.generateDefault(modelProperties)
18
+ const defaults = App.utils.generateDefault(model.properties)
17
19
  const modelPropertyName = modelName.slice(0, 1).toLowerCase() + modelName.slice(1)
18
20
 
19
21
  function modelRuntime() {
@@ -21,6 +23,13 @@ definition.processor(function(service, app) {
21
23
  }
22
24
 
23
25
  const config = model.sessionProperty
26
+ const writeableProperties = modelProperties || config.writableProperties
27
+
28
+ model.propertyOf = {
29
+ what: Session,
30
+ ...config
31
+ }
32
+
24
33
  model.properties.session = new PropertyDefinition({
25
34
  type: Session,
26
35
  validation: ['nonEmpty']
@@ -29,78 +38,67 @@ definition.processor(function(service, app) {
29
38
  model.indexes.bySession = new IndexDefinition({
30
39
  property: 'session'
31
40
  })
32
- if(config.readAccess) {
33
- const viewName = 'session' + modelName
34
- service.views[viewName] = new ViewDefinition({
35
- name: viewName,
36
- access: config.readAccess,
37
- daoPath(params, {client, context}) {
38
- return modelRuntime().indexObjectPath('bySession', client.session)
39
- }
40
- })
41
- }
42
- if(config.publicAccess) {
43
- const viewName = 'publicSession' + modelName
41
+ if(config.sessionReadAccess) {
42
+ const viewName = 'mySession' + modelName
44
43
  service.views[viewName] = new ViewDefinition({
45
44
  name: viewName,
46
- access: config.publicAccess,
47
- daoPath({session}, {client, context}) {
48
- return modelRuntime().indexObjectPath('bySession', session)
45
+ access: config.sessionReadAccess,
46
+ daoPath(params, { client, context }) {
47
+ return modelRuntime().path(client.session)
48
+ //return modelRuntime().indexObjectPath('bySession', client.session)
49
49
  }
50
50
  })
51
51
  }
52
52
 
53
- if(config.setAccess || config.writeAccess) {
54
- const eventName = 'session' + modelName + 'Set'
55
- service.events[eventName] = new EventDefinition({
56
- name: eventName,
53
+ if(config.sessionSetAccess || config.sessionWriteAccess) {
54
+ const eventName = 'sessionOwned' + modelName + 'Set'
55
+ const actionName = 'setMySession' + modelName
56
+ service.actions[actionName] = new ActionDefinition({
57
+ name: actionName,
57
58
  properties: {
58
59
  ...originalModelProperties
59
60
  },
60
- execute(properties) {
61
- const data = properties.data
62
- const session = properties.session
63
- return modelRuntime().create({...data, session, id: session})
64
- }
65
- })
66
- const actionName = 'setSession' + modelName
67
- service.actions[actionName] = new ActionDefinition({
68
- name: actionName,
69
- access: config.createAccess || config.writeAccess,
61
+ access: config.sessionSetAccess || config.sessionWriteAccess,
62
+ skipValidation: true,
70
63
  queuedBy: (command) => command.client.session,
71
64
  waitForEvents: true,
72
65
  async execute(properties, {client, service}, emit) {
66
+ let newObject = {}
67
+ for(const propertyName of writeableProperties) {
68
+ if(properties.hasOwnProperty(propertyName)) {
69
+ newObject[propertyName] = properties[propertyName]
70
+ }
71
+ }
72
+ const data = App.utils.mergeDeep({}, defaults, newObject)
73
+ await App.validation.validate(data, validators, { source: action, action, service, app, client })
73
74
  emit({
74
75
  type: eventName,
75
- session: client.session,
76
- data: properties || {}
76
+ identifiers: {
77
+ session: client.session
78
+ },
79
+ data
77
80
  })
78
81
  }
79
82
  })
83
+ const action = service.actions[actionName]
84
+ const validators = App.validation.getValidators(action, service, action)
80
85
  }
81
- if(config.updateAccess || config.writeAccess) {
82
- const eventName = 'session' + modelName + 'Updated'
83
- service.events[eventName] = new EventDefinition({
84
- name: eventName,
85
- execute(properties) {
86
- const data = properties.data
87
- const session = properties.session
88
- return modelRuntime().update(session, { ...data, session, id: session })
89
- }
90
- })
91
- const actionName = 'updateSession' + modelName
86
+
87
+ if(config.sessionUpdateAccess || config.sessionWriteAccess) {
88
+ const eventName = 'sessionOwned' + modelName + 'Updated'
89
+ const actionName = 'updateMySession' + modelName
92
90
  service.actions[actionName] = new ActionDefinition({
93
91
  name: actionName,
94
92
  properties: {
95
93
  ...originalModelProperties
96
94
  },
97
- access: config.updateAccess || config.writeAccess,
95
+ access: config.sessionUpdateAccess || config.sessionWriteAccess,
98
96
  skipValidation: true,
99
97
  queuedBy: (command) => command.client.session,
100
98
  waitForEvents: true,
101
99
  async execute(properties, { client, service }, emit) {
102
100
  const entity = await modelRuntime().get(client.session)
103
- if(!entity) throw new Error('not_found')
101
+ if(!entity) throw 'not_found'
104
102
  let updateObject = {}
105
103
  for(const propertyName of writeableProperties) {
106
104
  if(properties.hasOwnProperty(propertyName)) {
@@ -111,7 +109,9 @@ definition.processor(function(service, app) {
111
109
  await App.validation.validate(merged, validators, { source: action, action, service, app, client })
112
110
  emit({
113
111
  type: eventName,
114
- session: client.session,
112
+ identifiers: {
113
+ session: client.session
114
+ },
115
115
  data: properties || {}
116
116
  })
117
117
  }
@@ -119,30 +119,28 @@ definition.processor(function(service, app) {
119
119
  const action = service.actions[actionName]
120
120
  const validators = App.validation.getValidators(action, service, action)
121
121
  }
122
- if(config.resetAccess || config.writeAccess) {
123
- const eventName = 'session' + modelName + 'Reset'
124
- service.events[eventName] = new EventDefinition({
125
- name: eventName,
126
- execute({session}) {
127
- return modelRuntime().delete(session)
128
- }
129
- })
130
- const actionName = 'resetSession' + modelName
122
+
123
+ if(config.sessionResetAccess || config.sessionWriteAccess) {
124
+ const eventName = 'sessionOwned' + modelName + 'Reset'
125
+ const actionName = 'resetMySession' + modelName
131
126
  service.actions[actionName] = new ActionDefinition({
132
127
  name: actionName,
133
- access: config.createAccess || config.writeAccess,
128
+ access: config.sessionResetAccess || config.sessionWriteAccess,
134
129
  queuedBy: (command) => command.client.session,
135
130
  waitForEvents: true,
136
131
  async execute(properties, {client, service}, emit) {
137
132
  const entity = await modelRuntime().indexObjectGet('bySession', client.session)
138
- if (!entity) throw new Error('not_found')
133
+ if (!entity) throw 'not_found'
139
134
  emit({
140
135
  type: eventName,
141
- session: client.session,
136
+ identifiers: {
137
+ session: client.session
138
+ }
142
139
  })
143
140
  }
144
141
  })
145
142
  }
143
+
146
144
  }
147
145
  }
148
146