Created
September 16, 2020 14:10
-
-
Save kedoska/eab2179c0532df77892a59a158da77ef to your computer and use it in GitHub Desktop.
Resty DataAdapter Skeleton
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
import { DataAdapter, Pagination, Query } from '@kedoska/resty' | |
const deleteAll = <T>(): Promise<T[]> => | |
new Promise((resolve, reject) => { | |
try { | |
// Implement your delete logic here. | |
// Return the empty list. | |
resolve([]) | |
} catch ({message}) { | |
reject(Error(`could not "delete" the collection, ${message}`)) | |
} | |
}) | |
const selectMany = <T>(pagination: Pagination, query?: Query): Promise<T[]> => | |
new Promise((resolve, reject) => { | |
try { | |
// Implement your select logic here. | |
// Return the list. | |
resolve([]) | |
} catch ({message}) { | |
reject(Error(`could not "select" the resources, ${message}`)) | |
} | |
}) | |
const createOne = <T>(resource: T): Promise<T> => | |
new Promise((resolve, reject) => { | |
try { | |
// Implement your create logic here. | |
// Return the created object. | |
resolve({} as T) | |
} catch ({message}) { | |
reject(Error(`could not "create" the resource, ${message}`)) | |
} | |
}) | |
const selectOne = <T>(id: string): Promise<T> => | |
new Promise((resolve, reject) => { | |
try { | |
// Implement your select logic here. | |
// Return a single object. | |
resolve({} as T) | |
} catch ({message}) { | |
reject(Error(`could not "select" the resource ${id}, ${message}`)) | |
} | |
}) | |
const deleteOne = (id: string): Promise<void> => | |
new Promise((resolve, reject) => { | |
try { | |
// Implement your single delete logic here. | |
// Exit without error | |
resolve() | |
} catch ({message}) { | |
reject(Error(`could not "delete" the resource ${id}, ${message}`)) | |
} | |
}) | |
const updateOne = <T>(id: string, resource: T): Promise<T> => | |
new Promise((resolve, reject) => { | |
try { | |
// Implement your update one logic here. | |
// Return a single object. | |
resolve({} as T) | |
} catch ({message}) { | |
reject(Error(`could not "update" the resource ${id}, ${message}`)) | |
} | |
}) | |
export = (): DataAdapter => ({ | |
deleteAll, | |
selectMany, | |
createOne, | |
selectOne, | |
deleteOne, | |
updateOne, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment