<commit_before>/** @flow */
import {createModel} from './createModel'
import type { InsertQuery, UpdateQuery, Repository, Manager, BaseRecord, TableMetadata, Model, Criteria, SelectQuery } from './types'

export function createRepository<T: BaseRecord>(tableName: string, manager: Manager): Repository<T> {
  const repo = {
    async getMetadata(): Promise<TableMetadata> {
      await manager.getMetadataManager().ready()
      return manager.getMetadataManager().getColumns(tableName)
    },
    find(id: number): Promise<Model<T> | null> {
      const query: SelectQuery<T> = this.startQuery()
        .where('id = ?', id)
      return query
        .execute()
        .then((result: Array<T>): Model<T> | null => result.length > 0 ? this.hydrate(result.pop(), true) : null)
    },
    findOneBy(criteria: Criteria): Promise<Model<T> | null> {
      const query: SelectQuery<T> = this.startQuery()
        .limit(1)
        .criteria(criteria)
      return query
        .execute()
        .then((result: Array<T>): Model<T> | null => result.length > 0 ? this.hydrate(result.pop(), true) : null)
    },
    findBy(criteria: Criteria, orderBy?: { [key: string]: string } = {}, limit?: number, offset?: number): Promise<Array<Model<T>>> {
      const query: SelectQuery<T> = this.startQuery()
        .criteria(criteria)
      limit && query.limit(limit)
      offset && query.offset(offset)
      return query.execute().then((result: Array<T>): Array<Model<T>> => result.map((entry: T): Model<T> => this.hydrate(entry, true)))
    },
    startQuery(alias: ?string = null): SelectQuery<T> {
      return manager.startQuery()
        .select()
        .from(tableName, alias)
        .field(alias ? `${alias}.*` : '*')
    },
    create<Q: T>(data: Q): Model<Q> {
      return this.hydrate(data)
    },
    hydrate(data: T, fetched: boolean = false): Model<T> {
      return createModel(this, data, fetched)
    },
    insert(data: T): Promise<number> {
      const query = manager.startQuery().insert()
        .into(tableName)
      Object.keys(data).forEach((key: string): InsertQuery => query.set(key, data[key]))
      return query.execute().then((result: any): number => result.insertId)
    },
    update(selector: number | { [key: string]: any }, changes: { [key: string]: any }): Promise<number> {<commit_msg>Fix for usage of create<commit_after>/** @flow */
import {createModel} from './createModel'
import type { InsertQuery, UpdateQuery, Repository, Manager, BaseRecord, TableMetadata, Model, Criteria, SelectQuery } from './types'

export function createRepository<T: BaseRecord>(tableName: string, manager: Manager): Repository<T> {
  const repo = {
    async getMetadata(): Promise<TableMetadata> {
      await manager.getMetadataManager().ready()
      return manager.getMetadataManager().getColumns(tableName)
    },
    find(id: number): Promise<Model<T> | null> {
      const query: SelectQuery<T> = this.startQuery()
        .where('id = ?', id)
      return query
        .execute()
        .then((result: Array<T>): Model<T> | null => result.length > 0 ? this.hydrate(result.pop(), true) : null)
    },
    findOneBy(criteria: Criteria): Promise<Model<T> | null> {
      const query: SelectQuery<T> = this.startQuery()
        .limit(1)
        .criteria(criteria)
      return query
        .execute()
        .then((result: Array<T>): Model<T> | null => result.length > 0 ? this.hydrate(result.pop(), true) : null)
    },
    findBy(criteria: Criteria, orderBy?: { [key: string]: string } = {}, limit?: number, offset?: number): Promise<Array<Model<T>>> {
      const query: SelectQuery<T> = this.startQuery()
        .criteria(criteria)
      limit && query.limit(limit)
      offset && query.offset(offset)
      return query.execute().then((result: Array<T>): Array<Model<T>> => result.map((entry: T): Model<T> => this.hydrate(entry, true)))
    },
    startQuery(alias: ?string = null): SelectQuery<T> {
      return manager.startQuery()
        .select()
        .from(tableName, alias)
        .field(alias ? `${alias}.*` : '*')
    },
    create<Q: T>(data: any = {}): Model<Q> {
      return this.hydrate(data)
    },
    hydrate(data: T, fetched: boolean = false): Model<T> {
      return createModel(this, data, fetched)
    },
    insert(data: T): Promise<number> {
      const query = manager.startQuery().insert()
        .into(tableName)
      Object.keys(data).forEach((key: string): InsertQuery => query.set(key, data[key]))
      return query.execute().then((result: any): number => result.insertId)
    },
    update(selector: number | { [key: string]: any }, changes: { [key: string]: any }): Promise<number> {
{'fraction_non_alphanumeric': 0.16999785084891467, 'fraction_numerical': 0.0012894906511927789, 'mean_word_length': 3.3293023255813954, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2814963', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1416, 'n_words': 330}