async create(request) {
        const data = await request.getData();

        if (!data) request.response().status(400).send(`Missing request body!`);
        else if (!type.object(data)) request.response().status(400).send(`Request body must be a json object!`);
        else if (!type.string(data.dataSet)) request.response().status(400).send(`Missing parameter 'dataSet' in request body!`);
        else if (!type.array(data.shards)) request.response().status(400).send(`Missing parameter 'shards' in request body!`);
        else {


            // get the shards from the db
            const shards = await Promise.all(data.shards.map(shardIdentifier => this.getShard(shardIdentifier)));


            if (!shards.length) throw new Error(`Canont create clsuter with 0 shards!`);


            // load he data groups for the given data set
            const groups = await this.db.dataGroup('id').getDataVersion().fetchDataVersionStatus({
                identifier: this.db.getORM().in(['active', 'preview'])
            }).getDataSet({
                identifier: data.dataSet
            }).raw().find();



            // rendezvous hash the groups so that they can
            // be assigned to shards
            const mappings = [];
            
            for (const {id} of groups) {
                mappings.push({
                    id_shard: this.getShardForGroup(id, shards),
                    id_dataGroup: id,
                });
            }


            // save to db
            await Promise.all(mappings.map((mapping) => {
                return new this.db.dataGroup_shard(mapping).save();
            }));


            return {
                groupCount: groups.length
            };
        }
    }
/**
    * create data shards for the data groups of a data set
    */
create data shards for the data groups of a data set
create data shards for the data groups of a data set
async create(request) {
        const data = await request.getData();
        if (!data) request.response().status(400).send(`Missing request body!`);
        else if (!type.object(data)) request.response().status(400).send(`Request body must be a json object!`);
        else if (!type.string(data.dataSet)) request.response().status(400).send(`Missing parameter 'dataSet' in request body!`);
        else if (!type.array(data.shards)) request.response().status(400).send(`Missing parameter 'shards' in request body!`);
        else {
            const shards = await Promise.all(data.shards.map(shardIdentifier => this.getShard(shardIdentifier)));
            if (!shards.length) throw new Error(`Canont create clsuter with 0 shards!`);
            const groups = await this.db.dataGroup('id').getDataVersion().fetchDataVersionStatus({
                identifier: this.db.getORM().in(['active', 'preview'])
            }).getDataSet({
                identifier: data.dataSet
            }).raw().find();
            const mappings = [];
            for (const {id} of groups) {
                mappings.push({
                    id_shard: this.getShardForGroup(id, shards),
                    id_dataGroup: id,
                });
            }
            await Promise.all(mappings.map((mapping) => {
                return new this.db.dataGroup_shard(mapping).save();
            }));
            return {
                groupCount: groups.length
            };
        }
    }
async create ( request ) { const data = await request . getData ( ) ; if ( ! data ) request . response ( ) . status ( 400 ) . send ( ` ` ) ; else if ( ! type . object ( data ) ) request . response ( ) . status ( 400 ) . send ( ` ` ) ; else if ( ! type . string ( data . dataSet ) ) request . response ( ) . status ( 400 ) . send ( ` ` ) ; else if ( ! type . array ( data . shards ) ) request . response ( ) . status ( 400 ) . send ( ` ` ) ; else { const shards = await Promise . all ( data . shards . map ( shardIdentifier => this . getShard ( shardIdentifier ) ) ) ; if ( ! shards . length ) throw new Error ( ` ` ) ; const groups = await this . db . dataGroup ( 'id' ) . getDataVersion ( ) . fetchDataVersionStatus ( { identifier : this . db . getORM ( ) . in ( [ 'active' , 'preview' ] ) } ) . getDataSet ( { identifier : data . dataSet } ) . raw ( ) . find ( ) ; const mappings = [ ] ; for ( const { id } of groups ) { mappings . push ( { id_shard : this . getShardForGroup ( id , shards ) , id_dataGroup : id , } ) ; } await Promise . all ( mappings . map ( ( mapping ) => { return new this . db . dataGroup_shard ( mapping ) . save ( ) ; } ) ) ; return { groupCount : groups . length } ; } }
create data shards for the data groups of a data set
create data shards for the data groups of a data set
// get the shards from the db // load he data groups for the given data set // rendezvous hash the groups so that they can // be assigned to shards // save to db