Last active
September 30, 2021 15:36
-
-
Save xieyuheng/0aa40aaad4b1609808e1dd9e6099b95c to your computer and use it in GitHub Desktop.
Comparing generic `Resource` class API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Resource1
should be namedRepository
orResource
forEntity
withid
.Resource2
should be namedStore
for "key value store" forValue
withoutid
.