Skip to content

Instantly share code, notes, and snippets.

@xieyuheng
Last active September 30, 2021 15:36
Show Gist options
  • Save xieyuheng/0aa40aaad4b1609808e1dd9e6099b95c to your computer and use it in GitHub Desktop.
Save xieyuheng/0aa40aaad4b1609808e1dd9e6099b95c to your computer and use it in GitHub Desktop.
Comparing generic `Resource` class API
// NOTE `id` in `data`
export abstract class Resource1<T, Pk extends keyof T> {
abstract primary_key: Pk
abstract create(input: Omit<T, Pk>): Promise<T>
abstract create_many(inputs: Array<Omit<T, Pk>>): Promise<Array<T>>
abstract get(pk: T[Pk]): Promise<T | undefined>
abstract get_or_fail(pk: T[Pk]): Promise<T>
abstract keys(): Promise<Array<Pk>>
abstract all(): Promise<Array<T>>
abstract find(query: Partial<T>): Promise<Array<T>>
abstract find_first(query: Partial<T>): Promise<T | undefined>
abstract find_first_or_fail(query: Partial<T>): Promise<T>
abstract has(pk: T[Pk]): Promise<boolean>
abstract put(data: T): Promise<boolean>
abstract patch(data: Partial<T>): Promise<boolean>
abstract delete(pk: T[Pk]): Promise<boolean>
}
// NOTE `id` not in `data`
export abstract class Resource2<T, Key extends string | number> {
abstract create(data: T): Promise<Key>
abstract create_many(array: Array<T>): Promise<Array<Key>>
abstract get(key: Key): Promise<T | undefined>
abstract get_or_fail(key: Key): Promise<T>
abstract keys(): Promise<Array<Key>>
abstract all(): Promise<Record<Key, T>>
abstract find(query: Partial<T>): Promise<Record<Key, T>>
abstract find_first(query: Partial<T>): Promise<[Key, T] | undefined>
abstract find_first_or_fail(query: Partial<T>): Promise<[Key, T]>
abstract has(key: Key): Promise<boolean>
abstract put(key: Key, data: T): Promise<boolean>
abstract patch(key: Key, data: Partial<T>): Promise<boolean>
abstract delete(key: Key): Promise<boolean>
}
@xieyuheng
Copy link
Author

  • Resource1 should be named Repository or Resource for Entity with id.
  • Resource2 should be named Store for "key value store" for Value without id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment