@live-change/relations-plugin 0.9.77 → 0.9.78

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/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@live-change/relations-plugin",
3
- "version": "0.9.77",
3
+ "version": "0.9.78",
4
4
  "description": "",
5
- "main": "index.js",
5
+ "main": "src/index.js",
6
6
  "scripts": {
7
7
  "test": "NODE_ENV=test tape tests/*"
8
8
  },
@@ -22,8 +22,8 @@
22
22
  },
23
23
  "type": "module",
24
24
  "dependencies": {
25
- "@live-change/framework": "^0.9.77",
25
+ "@live-change/framework": "^0.9.78",
26
26
  "pluralize": "^8.0.0"
27
27
  },
28
- "gitHead": "5998bcbd97bda91829bcf57b23e938d95d431144"
28
+ "gitHead": "747a9cb02377d924583925508f082c8dd5031d43"
29
29
  }
package/src/entity.ts ADDED
@@ -0,0 +1,78 @@
1
+ import {
2
+ includeAccessRoles, defineGlobalRangeView
3
+ } from './utils.js'
4
+
5
+ import {
6
+ defineView, defineCreatedEvent, defineUpdatedEvent, defineDeletedEvent, defineCreateAction,
7
+ defineUpdateAction, defineDeleteAction, defineCreateTrigger, defineUpdateTrigger, defineDeleteTrigger
8
+ } from './entityUtils.js'
9
+
10
+ const annotation = 'entity'
11
+
12
+
13
+ export default function(service, app) {
14
+ if (!service) throw new Error("no service")
15
+ if (!app) throw new Error("no app")
16
+
17
+ for(let modelName in service.models) {
18
+ const model = service.models[modelName]
19
+ const config = model[annotation]
20
+ if(!config) continue
21
+
22
+ if (model[annotation + 'Processed']) throw new Error("duplicated processing of " + annotation + " processor")
23
+ model[annotation + 'Processed'] = true
24
+
25
+ const originalModelProperties = { ...model.properties }
26
+ const modelProperties = Object.keys(model.properties)
27
+ const modelPropertyName = modelName.slice(0, 1).toLowerCase() + modelName.slice(1)
28
+
29
+ model.identifiers = [{ name: modelPropertyName, field: 'id' }]
30
+ model.crud = {}
31
+ includeAccessRoles(model, [
32
+ config.readAccessControl, config.writeAccessControl,
33
+ config.createAccessControl, config.updateAccessControl, config.deleteAccessControl
34
+ ])
35
+
36
+ function modelRuntime() {
37
+ return service._runtime.models[modelName]
38
+ }
39
+
40
+ if (!model.indexes) {
41
+ model.indexes = {}
42
+ }
43
+
44
+ const writeableProperties = modelProperties || config.writeableProperties
45
+ //console.log("PPP", others)
46
+ const objectType = service.name + '_' + modelName
47
+
48
+ const context = {
49
+ service, app, model, originalModelProperties, modelProperties, modelPropertyName, modelRuntime,
50
+ modelName, writeableProperties, annotation, objectType
51
+ }
52
+
53
+ defineView(config, context, config.readAccess || config.readAccessControl || config.writeAccessControl)
54
+ defineGlobalRangeView(config, context, config.readAllAccess)
55
+ /// TODO: multiple views with limited fields
56
+
57
+ defineCreatedEvent(config, context)
58
+ defineUpdatedEvent(config, context)
59
+ defineDeletedEvent(config, context)
60
+
61
+ defineCreateTrigger(config, context)
62
+ defineUpdateTrigger(config, context)
63
+ defineDeleteTrigger(config, context)
64
+
65
+ if (config.createAccess || config.writeAccess || config.createAccessControl || config.writeAccessControl) {
66
+ defineCreateAction(config, context)
67
+ }
68
+
69
+ if (config.updateAccess || config.writeAccess || config.updateAccessControl || config.writeAccessControl) {
70
+ defineUpdateAction(config, context)
71
+ }
72
+
73
+ if (config.deleteAccess || config.writeAccess || config.deleteAccessControl || config.writeAccessControl) {
74
+ defineDeleteAction(config, context)
75
+ }
76
+
77
+ }
78
+ }
@@ -4,15 +4,18 @@ import {
4
4
  includeAccessRoles, defineGlobalRangeView
5
5
  } from './utils.js'
6
6
  import { fireChangeTriggers } from "./changeTriggers.js"
7
- import App from '@live-change/framework'
7
+ import App, { ServiceDefinition } from '@live-change/framework'
8
8
  import {
9
9
  PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition, TriggerDefinition
10
10
  } from "@live-change/framework"
11
11
  import pluralize from 'pluralize'
12
12
 
13
- const annotation = 'entity'
13
+ import { ServiceDefinitionSpecification } from "@live-change/framework"
14
+ import { ActionDefinitionSpecificationAC, ViewDefinitionSpecificationAC, TriggerDefinitionSpecificationAC } from "./types.js"
15
+ import { EventDefinitionSpecification } from "@live-change/framework"
14
16
 
15
- function entityAccessControl({service, modelName, modelPropertyName}, accessControl) {
17
+
18
+ export function entityAccessControl({service, modelName, modelPropertyName}, accessControl) {
16
19
  if(!accessControl) return undefined
17
20
  if(Array.isArray(accessControl)) accessControl = { roles: accessControl }
18
21
  return {
@@ -21,11 +24,11 @@ function entityAccessControl({service, modelName, modelPropertyName}, accessCont
21
24
  }
22
25
  }
23
26
 
24
- function defineView(config, context, external) {
27
+ export function defineView(config, context, external) {
25
28
  const { service, modelRuntime, modelPropertyName, modelName, model } = context
26
29
  const viewName = (config.prefix || '' ) + (config.prefix ? modelName : modelPropertyName) + (config.suffix || '')
27
30
  if(external) model.crud.read = viewName
28
- service.views[viewName] = new ViewDefinition({
31
+ const view = service.view({
29
32
  name: viewName,
30
33
  properties: {
31
34
  [modelPropertyName]: {
@@ -48,7 +51,7 @@ function defineView(config, context, external) {
48
51
  }
49
52
 
50
53
 
51
- function defineCreatedEvent(config, context) {
54
+ export function defineCreatedEvent(config, context) {
52
55
  const {
53
56
  service, modelRuntime, modelName, modelPropertyName
54
57
  } = context
@@ -70,7 +73,7 @@ function defineCreatedEvent(config, context) {
70
73
  })
71
74
  }
72
75
 
73
- function defineUpdatedEvent(config, context) {
76
+ export function defineUpdatedEvent(config, context) {
74
77
  const {
75
78
  service, modelRuntime, modelName, modelPropertyName
76
79
  } = context
@@ -92,12 +95,12 @@ function defineUpdatedEvent(config, context) {
92
95
  })
93
96
  }
94
97
 
95
- function defineDeletedEvent(config, context) {
98
+ export function defineDeletedEvent(config, context) {
96
99
  const {
97
100
  service, modelRuntime, modelName, modelPropertyName,
98
101
  } = context
99
- const eventName = modelName + 'Deleted'
100
- service.events[eventName] = new EventDefinition({
102
+ const eventName = modelName + 'Deleted';
103
+ (service as ServiceDefinition<ServiceDefinitionSpecification>).event({
101
104
  name: eventName,
102
105
  properties: {
103
106
  [modelPropertyName]: {
@@ -111,7 +114,7 @@ function defineDeletedEvent(config, context) {
111
114
  })
112
115
  }
113
116
 
114
- function getCreateFunction( validators, validationContext, config, context) {
117
+ export function getCreateFunction( validators, validationContext, config, context) {
115
118
  const {
116
119
  service, app, model, modelPropertyName, modelRuntime, objectType,
117
120
  modelName, writeableProperties
@@ -139,14 +142,14 @@ function getCreateFunction( validators, validationContext, config, context) {
139
142
  return execute
140
143
  }
141
144
 
142
- function defineCreateAction(config, context) {
145
+ export function defineCreateAction(config, context) {
143
146
  const {
144
147
  service, app, model, modelPropertyName, modelRuntime, objectType,
145
148
  modelName, writeableProperties
146
149
  } = context
147
150
  const actionName = 'create' + modelName
148
151
  model.crud.create = actionName
149
- const action = new ActionDefinition({
152
+ const action = (service as ServiceDefinition<ServiceDefinitionSpecification>).action<ActionDefinitionSpecificationAC>({
150
153
  name: actionName,
151
154
  properties: { ...model.properties },
152
155
  access: config.createAccess || config.writeAccess,
@@ -162,14 +165,14 @@ function defineCreateAction(config, context) {
162
165
  service.actions[actionName] = action
163
166
  }
164
167
 
165
- function defineCreateTrigger(config, context) {
168
+ export function defineCreateTrigger(config, context) {
166
169
  const {
167
170
  service, app, model, modelPropertyName, modelRuntime, objectType,
168
171
  modelName, writeableProperties
169
172
  } = context
170
173
  const actionName = 'create' + modelName
171
174
  const triggerName = `${service.name}_${actionName}`
172
- const trigger = new TriggerDefinition({
175
+ const trigger = (service as ServiceDefinition<ServiceDefinitionSpecification>).trigger<TriggerDefinitionSpecificationAC>({
173
176
  name: triggerName,
174
177
  properties: { ...model.properties },
175
178
  skipValidation: true,
@@ -179,10 +182,9 @@ function defineCreateTrigger(config, context) {
179
182
  const validators = App.validation.getValidators(trigger, service, trigger)
180
183
  const validationContext = { source: trigger, trigger }
181
184
  trigger.execute = getCreateFunction( validators, validationContext, config, context)
182
- service.triggers[triggerName] = [trigger]
183
185
  }
184
186
 
185
- function getUpdateFunction( validators, validationContext, config, context) {
187
+ export function getUpdateFunction( validators, validationContext, config, context) {
186
188
  const {
187
189
  service, app, model, modelRuntime, modelPropertyName, objectType,
188
190
  modelName, writeableProperties
@@ -209,7 +211,7 @@ function getUpdateFunction( validators, validationContext, config, context) {
209
211
  return execute
210
212
  }
211
213
 
212
- function defineUpdateAction(config, context) {
214
+ export function defineUpdateAction(config, context) {
213
215
  const {
214
216
  service, app, model, modelRuntime, modelPropertyName, objectType,
215
217
  modelName, writeableProperties
@@ -237,7 +239,7 @@ function defineUpdateAction(config, context) {
237
239
  service.actions[actionName] = action
238
240
  }
239
241
 
240
- function defineUpdateTrigger(config, context) {
242
+ export function defineUpdateTrigger(config, context) {
241
243
  const {
242
244
  service, app, model, modelRuntime, modelPropertyName, objectType,
243
245
  modelName, writeableProperties
@@ -262,7 +264,7 @@ function defineUpdateTrigger(config, context) {
262
264
  service.triggers[triggerName] = [trigger]
263
265
  }
264
266
 
265
- function getDeleteFunction(config, context) {
267
+ export function getDeleteFunction(config, context) {
266
268
  const {
267
269
  service, app, model, modelRuntime, modelPropertyName, objectType,
268
270
  otherPropertyNames, modelName, writeableProperties,
@@ -282,7 +284,7 @@ function getDeleteFunction(config, context) {
282
284
  return execute
283
285
  }
284
286
 
285
- function defineDeleteAction(config, context) {
287
+ export function defineDeleteAction(config, context) {
286
288
  const {
287
289
  service, app, model, modelRuntime, modelPropertyName, objectType,
288
290
  modelName, writeableProperties
@@ -305,7 +307,7 @@ function defineDeleteAction(config, context) {
305
307
  })
306
308
  }
307
309
 
308
- function defineDeleteTrigger(config, context) {
310
+ export function defineDeleteTrigger(config, context) {
309
311
  const {
310
312
  service, app, model, modelRuntime, modelPropertyName, objectType,
311
313
  modelName, writeableProperties
@@ -324,71 +326,4 @@ function defineDeleteTrigger(config, context) {
324
326
  waitForEvents: true,
325
327
  execute: getDeleteFunction(config, context)
326
328
  })]
327
- }
328
-
329
- export default function(service, app) {
330
- if (!service) throw new Error("no service")
331
- if (!app) throw new Error("no app")
332
-
333
- for(let modelName in service.models) {
334
- const model = service.models[modelName]
335
- const config = model[annotation]
336
- if(!config) continue
337
-
338
- if (model[annotation + 'Processed']) throw new Error("duplicated processing of " + annotation + " processor")
339
- model[annotation + 'Processed'] = true
340
-
341
- const originalModelProperties = { ...model.properties }
342
- const modelProperties = Object.keys(model.properties)
343
- const modelPropertyName = modelName.slice(0, 1).toLowerCase() + modelName.slice(1)
344
-
345
- model.identifiers = [{ name: modelPropertyName, field: 'id' }]
346
- model.crud = {}
347
- includeAccessRoles(model, [
348
- config.readAccessControl, config.writeAccessControl,
349
- config.createAccessControl, config.updateAccessControl, config.deleteAccessControl
350
- ])
351
-
352
- function modelRuntime() {
353
- return service._runtime.models[modelName]
354
- }
355
-
356
- if (!model.indexes) {
357
- model.indexes = {}
358
- }
359
-
360
- const writeableProperties = modelProperties || config.writeableProperties
361
- //console.log("PPP", others)
362
- const objectType = service.name + '_' + modelName
363
-
364
- const context = {
365
- service, app, model, originalModelProperties, modelProperties, modelPropertyName, modelRuntime,
366
- modelName, writeableProperties, annotation, objectType
367
- }
368
-
369
- defineView(config, context, config.readAccess || config.readAccessControl || config.writeAccessControl)
370
- defineGlobalRangeView(config, context, config.readAllAccess)
371
- /// TODO: multiple views with limited fields
372
-
373
- defineCreatedEvent(config, context)
374
- defineUpdatedEvent(config, context)
375
- defineDeletedEvent(config, context)
376
-
377
- defineCreateTrigger(config, context)
378
- defineUpdateTrigger(config, context)
379
- defineDeleteTrigger(config, context)
380
-
381
- if (config.createAccess || config.writeAccess || config.createAccessControl || config.writeAccessControl) {
382
- defineCreateAction(config, context)
383
- }
384
-
385
- if (config.updateAccess || config.writeAccess || config.updateAccessControl || config.writeAccessControl) {
386
- defineUpdateAction(config, context)
387
- }
388
-
389
- if (config.deleteAccess || config.writeAccess || config.deleteAccessControl || config.writeAccessControl) {
390
- defineDeleteAction(config, context)
391
- }
392
-
393
- }
394
- }
329
+ }
package/src/types.ts ADDED
@@ -0,0 +1,36 @@
1
+ import {
2
+ ViewContext, ViewDefinitionSpecificationObservable, ViewDefinitionSpecificationDaoPath,
3
+ ViewDefinitionSpecificationFetch,
4
+ ActionDefinitionSpecification,
5
+ TriggerDefinitionSpecification
6
+ } from "@live-change/framework"
7
+
8
+ export type AccessControlSettings = string | string[] | {
9
+ roles: string | string[]
10
+ objects?: (properties: any) => any[]
11
+ }
12
+ export interface ViewDefinitionSpecificationObservableAC extends ViewDefinitionSpecificationObservable {
13
+ accessControl?: AccessControlSettings
14
+ }
15
+ export interface ViewDefinitionSpecificationDaoPathAC extends ViewDefinitionSpecificationDaoPath {
16
+ daoPath: (parameters: Record<string, any>, context: ViewContext) => any[]
17
+ accessControl?: AccessControlSettings
18
+ }
19
+ export interface ViewDefinitionSpecificationFetchAC extends ViewDefinitionSpecificationFetch {
20
+ fetch: (parameters: Record<string, any>, context: ViewContext) => Promise<any>
21
+ accessControl?: AccessControlSettings
22
+ }
23
+
24
+ export type ViewDefinitionSpecificationAC =
25
+ ViewDefinitionSpecificationObservableAC
26
+ | ViewDefinitionSpecificationDaoPathAC
27
+ | ViewDefinitionSpecificationFetchAC
28
+
29
+
30
+ export interface ActionDefinitionSpecificationAC extends ActionDefinitionSpecification {
31
+ accessControl?: AccessControlSettings
32
+ }
33
+
34
+ export interface TriggerDefinitionSpecificationAC extends TriggerDefinitionSpecification {
35
+ accessControl?: AccessControlSettings
36
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "compilerOptions": {
3
+ "composite": true,
4
+ "module": "nodenext",
5
+ "moduleResolution": "nodenext",
6
+ "allowSyntheticDefaultImports": true,
7
+
8
+ "target": "ES2020",
9
+ "useDefineForClassFields": true,
10
+ "lib": ["ES2020", "DOM"],
11
+
12
+ /* Linting */
13
+ "strict": false,
14
+ "noUnusedLocals": false,
15
+ "noUnusedParameters": false,
16
+ "noFallthroughCasesInSwitch": true,
17
+ "outDir": "dist",
18
+ "checkJs": false,
19
+ "esModuleInterop": true, // Enable interop for ES modules
20
+ "resolveJsonModule": true, // Allow importing JSON files
21
+ "skipLibCheck": true,
22
+ "noImplicitAny": false
23
+ },
24
+ "files": ["src/index.js"],
25
+ "include": ["src/**/*.ts", "src/**/*.js"],
26
+ "exclude": ["node_modules/@types", "node_modules/**/*.d.ts", "**/*.test.js", "**/CMakeFiles/**"]
27
+ }
28
+
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes