import { IGroup , IGroupsStorage } from "@staticdeploy/core" ; import { Knex } from "knex" ; import convertErrors from "./common/convertErrors" ; import tables from "./common/tables" ; @ convertErrors export default class GroupsStorage implements IGroupsStorage { constructor ( private knex ) { } async findOne ( id ) { const [ group = null ] = await this . knex ( tables . groups ) . where ( { id } ) ; return group ; } async findOneByName ( name ) { const [ group = null ] = await this . knex ( tables . groups ) . where ( { name } ) ; return group ; } async findMany ( ) { const groups = await this . knex ( tables . groups ) ; return groups ; } async oneExistsWithName ( name ) { const [ group = null ] = await this . knex ( tables . groups ) . select ( "id" ) . where ( { name } ) ; return group !== null ; } async allExistWithIds ( ids ) { const groups = await this . knex ( tables . groups ) . select ( "id" ) . whereIn ( "id" , ids ) ; return groups . length === ids . length ; } async createOne ( toBeCreatedGroup ) { const [ createdGroup ] = await this . knex ( tables . groups ) . insert ( toBeCreatedGroup ) . returning ( "*" ) ; return createdGroup ; } async updateOne ( id , patch ) { const [ updatedGroup ] = await this . knex ( tables . groups ) . where ( { id } ) . update ( patch ) . returning ( "*" ) ; return updatedGroup ; } async deleteOne ( id ) { await this . knex ( tables . groups ) . where ( { id } ) . delete ( ) ; } }