@live-change/scope-service 0.9.62

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/config.js ADDED
@@ -0,0 +1,15 @@
1
+ import definition from './definition.js'
2
+
3
+ const {
4
+
5
+ } = definition.config
6
+
7
+ definition.clientConfig = {
8
+
9
+ }
10
+
11
+ const config = {
12
+
13
+ }
14
+
15
+ export default config
package/definition.js ADDED
@@ -0,0 +1,13 @@
1
+ import App from '@live-change/framework'
2
+ const app = App.app()
3
+
4
+ import userService from '@live-change/user-service'
5
+ import relationsPlugin from '@live-change/relations-plugin'
6
+ import accessControlService from '@live-change/access-control-service'
7
+
8
+ const definition = app.createServiceDefinition({
9
+ name: "scope",
10
+ use: [ relationsPlugin, accessControlService ]
11
+ })
12
+
13
+ export default definition
package/index.js ADDED
@@ -0,0 +1,9 @@
1
+ import App from '@live-change/framework'
2
+ const app = App.app()
3
+
4
+ import definition from './definition.js'
5
+
6
+ import "./scopes.js"
7
+ import "./indexes.js"
8
+
9
+ export default definition
package/indexes.js ADDED
@@ -0,0 +1,61 @@
1
+ import App from '@live-change/framework'
2
+ const app = App.app()
3
+ import definition from './definition.js'
4
+ const config = definition.config
5
+
6
+ import { Scope } from './scopes.js'
7
+
8
+ //*
9
+ const scopesByObjectIndex = definition.index({
10
+ name: 'scopesByObject',
11
+ async function(input, output, { scopesTableName, pathsByAncestorDescendantRelationIndexName }) {
12
+ /// Can be optimized by using a ScopeIndexer for indexing scope using range changes
13
+ const scopesTable = await input.table(scopesTableName)
14
+ const pathsIndex = await input.index(pathsByAncestorDescendantRelationIndexName)
15
+
16
+ const crossed = scopesTable.cross(pathsIndex, scope => ({ /// scope to path range by ancestorType
17
+ gte: `"${scope.id}":`,
18
+ lte: `"${scope.id}"_\xFF\xFF\xFF\xFF`
19
+ }), path => path.ancestorType, 128)
20
+
21
+ const results = crossed.map(async ([scope, path]) => {
22
+ //output.debug('scope', scope, 'path', path)
23
+ if(!(scope && path)) return null
24
+ const { ancestorType, ancestor, descendantType, descendant, intermediate } = path
25
+ return {
26
+ id: [descendantType, descendant, ancestorType, ancestor].map(v => JSON.stringify(v)).join(':'),
27
+ scopeType: ancestorType,
28
+ scope: ancestor,
29
+ objectType: descendantType,
30
+ object: descendant,
31
+ intermediate
32
+ }
33
+ })
34
+
35
+ await results.to(output)
36
+ },
37
+ parameters: {
38
+ scopesTableName: 'scope_Scope',
39
+ pathsByAncestorDescendantRelationIndexName: 'accessControl_pathsByAncestorDescendantRelation'
40
+ }
41
+ })
42
+ //*
43
+ const objectsByScopeIndex = definition.index({
44
+ name: 'objectsByScope',
45
+ async function(input, output, { scopesByObjectIndexName }) {
46
+ (await input.index(scopesByObjectIndexName))
47
+ .map(async ({ scopeType, scope, objectType, object, intermediate }) => ({ // when single parameter, it will ignore null by default
48
+ id: [scopeType, scope, objectType, object].map(v => JSON.stringify(v)).join(':'),
49
+ objectType,
50
+ object,
51
+ scopeType,
52
+ scope,
53
+ intermediate: intermediate.toReversed()
54
+ }))
55
+ .to(output)
56
+ },
57
+ parameters: {
58
+ scopesByObjectIndexName: 'scope_scopesByObject'
59
+ }
60
+ })
61
+ //*/
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@live-change/scope-service",
3
+ "version": "0.9.62",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "NODE_ENV=test tape tests/*"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/live-change/live-change-stack.git"
12
+ },
13
+ "license": "MIT",
14
+ "bugs": {
15
+ "url": "https://github.com/live-change/live-change-stack/issues"
16
+ },
17
+ "homepage": "https://github.com/live-change/live-change-stack",
18
+ "author": {
19
+ "email": "michal@laszczewski.pl",
20
+ "name": "Michał Łaszczewski",
21
+ "url": "https://www.viamage.com/"
22
+ },
23
+ "type": "module",
24
+ "dependencies": {
25
+ "@live-change/access-control-service": "^0.9.62",
26
+ "@live-change/framework": "^0.9.62",
27
+ "@live-change/relations-plugin": "^0.9.62"
28
+ },
29
+ "gitHead": "b1b605b7f1fa4fc3de4720afbb401e2cfff080cf"
30
+ }
package/scopes.js ADDED
@@ -0,0 +1,45 @@
1
+ import App from '@live-change/framework'
2
+ const app = App.app()
3
+ import definition from './definition.js'
4
+ const config = definition.config
5
+
6
+ const scopes = []
7
+
8
+ /// Collect all models that have are scopes
9
+ definition.processor(function(service, app) {
10
+ for(let modelName in service.models) {
11
+ const model = service.models[modelName]
12
+ if(!model.scope) continue
13
+ scopes.push(service.name + '_' + modelName)
14
+ }
15
+ })
16
+
17
+ const Scope = definition.model({
18
+ name: "Scope",
19
+ properties: {
20
+ type: {
21
+ type: String
22
+ }
23
+ }
24
+ })
25
+
26
+ definition.afterStart(async () => {
27
+ const destObjects = scopes.map(type => ({
28
+ id: type,
29
+ type
30
+ }))
31
+ let idsSet = new Set(destObjects.map(obj => obj.id))
32
+ const existingScopes = await Scope.rangeGet({})
33
+ const promises = []
34
+ for(const obj of destObjects) {
35
+ promises.push(Scope.create(obj))
36
+ }
37
+ for(const scope of existingScopes) {
38
+ if(!idsSet.has(scope.id)) {
39
+ promises.push(Scope.delete(scope.id))
40
+ }
41
+ }
42
+ await Promise.all(promises)
43
+ })
44
+
45
+ export { scopes, Scope }