-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(authorization): add separate fields in indexes to improve se…
…arch and utilize db index
- Loading branch information
1 parent
e532e7c
commit d0bb09c
Showing
14 changed files
with
253 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
modules/authorization/src/migrations/actorIndex.migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import ConduitGrpcSdk from '@conduitplatform/grpc-sdk'; | ||
import { ActorIndex } from '../models'; | ||
|
||
export const migrateActorIndex = async (grpcSdk: ConduitGrpcSdk) => { | ||
const count = await ActorIndex.getInstance().countDocuments({ | ||
entityType: '', | ||
}); | ||
if (count === 0) { | ||
return; | ||
} | ||
let actorIndexes = await ActorIndex.getInstance().findMany( | ||
{ | ||
entityType: '', | ||
}, | ||
undefined, | ||
0, | ||
100, | ||
); | ||
let iterator = 0; | ||
while (actorIndexes.length > 0) { | ||
for (const actorIndex of actorIndexes) { | ||
await ActorIndex.getInstance().findByIdAndUpdate(actorIndex._id, { | ||
entityType: actorIndex.entity.split(':')[0], | ||
subjectType: actorIndex.subject.split(':')[0], | ||
relation: actorIndex.subject.split('#')[1], | ||
}); | ||
} | ||
actorIndexes = await ActorIndex.getInstance().findMany( | ||
{ | ||
entityType: '', | ||
}, | ||
undefined, | ||
++iterator * 100, | ||
100, | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
import ConduitGrpcSdk from '@conduitplatform/grpc-sdk'; | ||
import { migrateObjectIndex } from './objectIndex.migration'; | ||
import { migrateActorIndex } from './actorIndex.migration'; | ||
import { migrateRelationships } from './relationship.migration'; | ||
import { migratePermission } from './permission.migration'; | ||
|
||
export async function runMigrations(grpcSdk: ConduitGrpcSdk) { | ||
// ... | ||
await Promise.all([ | ||
migrateObjectIndex(grpcSdk), | ||
migrateActorIndex(grpcSdk), | ||
migrateRelationships(grpcSdk), | ||
migratePermission(grpcSdk), | ||
]); | ||
} |
38 changes: 38 additions & 0 deletions
38
modules/authorization/src/migrations/objectIndex.migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import ConduitGrpcSdk from '@conduitplatform/grpc-sdk'; | ||
import { ObjectIndex } from '../models'; | ||
|
||
export const migrateObjectIndex = async (grpcSdk: ConduitGrpcSdk) => { | ||
const count = await ObjectIndex.getInstance().countDocuments({ | ||
entityType: '', | ||
}); | ||
if (count === 0) { | ||
return; | ||
} | ||
let objectIndexes = await ObjectIndex.getInstance().findMany( | ||
{ | ||
entityType: '', | ||
}, | ||
undefined, | ||
0, | ||
100, | ||
); | ||
let iterator = 0; | ||
while (objectIndexes.length > 0) { | ||
for (const objectIndex of objectIndexes) { | ||
await ObjectIndex.getInstance().findByIdAndUpdate(objectIndex._id, { | ||
subjectType: objectIndex.subject.split(':')[0], | ||
subjectPermission: objectIndex.subject.split('#')[1], | ||
entityType: objectIndex.entity.split(':')[0], | ||
relation: objectIndex.subject.split('#')[1], | ||
}); | ||
} | ||
objectIndexes = await ObjectIndex.getInstance().findMany( | ||
{ | ||
entityType: '', | ||
}, | ||
undefined, | ||
++iterator * 100, | ||
100, | ||
); | ||
} | ||
}; |
36 changes: 36 additions & 0 deletions
36
modules/authorization/src/migrations/permission.migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import ConduitGrpcSdk from '@conduitplatform/grpc-sdk'; | ||
import { Permission } from '../models'; | ||
|
||
export const migratePermission = async (grpcSdk: ConduitGrpcSdk) => { | ||
const count = await Permission.getInstance().countDocuments({ | ||
resourceType: '', | ||
}); | ||
if (count === 0) { | ||
return; | ||
} | ||
let permissions = await Permission.getInstance().findMany( | ||
{ | ||
resourceType: '', | ||
}, | ||
undefined, | ||
0, | ||
100, | ||
); | ||
let iterator = 0; | ||
while (permissions.length > 0) { | ||
for (const permission of permissions) { | ||
await Permission.getInstance().findByIdAndUpdate(permission._id, { | ||
subjectType: permission.subject.split(':')[0], | ||
resourceType: permission.resource.split(':')[0], | ||
}); | ||
} | ||
permissions = await Permission.getInstance().findMany( | ||
{ | ||
resourceType: '', | ||
}, | ||
undefined, | ||
++iterator * 100, | ||
100, | ||
); | ||
} | ||
}; |
36 changes: 36 additions & 0 deletions
36
modules/authorization/src/migrations/relationship.migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import ConduitGrpcSdk from '@conduitplatform/grpc-sdk'; | ||
import { Relationship } from '../models'; | ||
|
||
export const migrateRelationships = async (grpcSdk: ConduitGrpcSdk) => { | ||
const count = await Relationship.getInstance().countDocuments({ | ||
resourceType: '', | ||
}); | ||
if (count === 0) { | ||
return; | ||
} | ||
let relationships = await Relationship.getInstance().findMany( | ||
{ | ||
resourceType: '', | ||
}, | ||
undefined, | ||
0, | ||
100, | ||
); | ||
let iterator = 0; | ||
while (relationships.length > 0) { | ||
for (const objectIndex of relationships) { | ||
await Relationship.getInstance().findByIdAndUpdate(objectIndex._id, { | ||
subjectType: objectIndex.subject.split(':')[0], | ||
resourceType: objectIndex.resource.split(':')[0], | ||
}); | ||
} | ||
relationships = await Relationship.getInstance().findMany( | ||
{ | ||
resourceType: '', | ||
}, | ||
undefined, | ||
++iterator * 100, | ||
100, | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.