import getDatabase from '../database' ; import { getSchema } from './get-schema' ; import { CollectionsService , FieldsService , RelationsService } from '../services' ; import { version } from '../../package.json' ; import { SchemaOverview , Snapshot , SnapshotField , SnapshotRelation } from '../types' ; import { Knex } from 'knex' ; import { omit } from 'lodash' ; export async function getSnapshot ( options ? ) { const database = options ?. database ?? getDatabase ( ) ; const schema = options ?. schema ?? ( await getSchema ( { database } ) ) ; const collectionsService = new CollectionsService ( { knex : database , schema } ) ; const fieldsService = new FieldsService ( { knex : database , schema } ) ; const relationsService = new RelationsService ( { knex : database , schema } ) ; const [ collections , fields , relations ] = await Promise . all ( [ collectionsService . readByQuery ( ) , fieldsService . readAll ( ) , relationsService . readAll ( ) , ] ) ; return { version : 1 , directus : version , collections : collections . filter ( ( item ) => excludeSystem ( item ) ) , fields : fields . filter ( ( item ) => excludeSystem ( item ) ) . map ( omitID ) as SnapshotField [ ] , relations : relations . filter ( ( item ) => excludeSystem ( item ) ) . map ( omitID ) as SnapshotRelation [ ] , } ; } function excludeSystem ( item ) { if ( item ?. meta ?. system === true ) return false ; return true ; } function omitID ( item ) { return omit ( item , 'meta.id' ) ; }