import { FaunaCollectionOptions } from '~/types/fauna' ; import { FrameworkCollectionScaffoldOptions } from '~/types/framework/framework.collection' ; import { Biota } from '~/biota' ; import { query as q } from 'faunadb' ; import { BiotaRoleName } from '~/factory/constructors/role' ; import { execute } from '~/tools/tasks' ; import { collection } from '~/factory/api/collection' ; import { collections } from '~/factory/api/collections' ; import { PrivilegeRights } from '~/factory/constructors/privilege' ; export function scaffold ( this , collectionName ) { const self = this ; return async function scaffoldMethod ( collectionOptions , options = { } ) { const defaultRoles = options . roles || [ 'biota.user' ] ; const defaultSearchable = [ ] ; const { index = defaultSearchable , compute = [ ] , field = [ ] } = options || { } ; const tasks = [ { name : `Upserting collection (${ collectionName })` , async task ( ) { return self . query ( collection ( self . context ) ( collectionName ) . upsert ( collectionOptions ) ) ; } , } , ] ; for ( const indexField of index ) { tasks . push ( { name : `Upserting index field (${ indexField }) on (${ collectionName })` , async task ( ) { return self . collection ( collectionName ) . index ( indexField , { role : BiotaRoleName ( 'user' ) } ) ; } , } ) ; } for ( const computeField of compute ) { tasks . push ( { name : `Upserting viewable field (${ computeField . field }) on (${ collectionName })` , async task ( ) { return self . collection ( collectionName ) . compute ( computeField , { role : BiotaRoleName ( 'user' ) } ) ; } , } ) ; } for ( const fieldField of field ) { tasks . push ( { name : `Upserting viewable field (${ fieldField . field }) on (${ collectionName })` , async task ( ) { return self . collection ( collectionName ) . field ( fieldField ) ; } , } ) ; } for ( const role of defaultRoles ) { tasks . push ( { name : `Adding collection ${ collectionName } to [${ role }] role` , async task ( ) { return self . role ( role ) . privilege . set ( PrivilegeRights ( { resource : q . Collection ( collectionName ) , rights : { insert : true , get : [ 'self' , 'owner' , 'assignee' ] , update : [ 'self' , 'owner' , 'assignee' ] , replace : [ 'self' , 'owner' , 'assignee' ] , delete : [ 'owner' ] , } , } ) ) ; } , } ) ; } return execute ( tasks , { domain : 'Biota.collection.scaffold' , } ) ; } ; }