@deppi/loader
v0.0.0-development.3
Published
A way to query data
Downloads
5
Readme
@deppi/loader
The loader we were always Promised
Usage
vanilla example
// We import the factory function
const { createLoader } = require('@deppi/loader')
// Create a singleton for loading Users
const userLoader = createLoader({
/**
* Defaults set by createLoader
*/
maxAge: 60 * 1000, /* Items can stay in hash for 1 minute */
maxCacheSize: 100, /* Max items we can hold in state */
createHash: require('object-hash'), /* LoaderQuery => Hash function */
/* We _MUST_ give resolve */
resolve: query => console.log(`called ${query._id}!`)
|| Promise.resolve(query)
})
Promise.all([
userLoader.load({ _id: 1 }),
userLoader.load({ _id: 1}),
userLoader.load({ _id: 2 })
]).then(console.log)
// called 1!
// called 2!
// [{ _id: 1 }, { _id: 1 }, { _id: 2 }]
typescript: types can be found at ./src/index.d.ts
import { createLoader } from '@deppi/loader'
interface User {
name: string;
}
// userLoader: Loader<User>
const userLoader = createLoader<User>({
// resolve: LoaderQuery => Promise<User>
resolve: query => Promise.resolve({
name: 'Tim'
})
})
userLoader
// load: LoaderQuery => Promise<User>
.load({})
// then: User => void
.then(console.log) // { name: 'Tim' }
Scripts
yarn example
: Run the example ( must have installed via$ yarn
)yarn test
: Run test suitesyarn build:prod
: Builds the Typescript for productionyarn clean
: Removes the./dist/
folderyarn build
: Builds a new./dist
folder after removing any previous oneyarn prepublishOnly
: FOR NPM USE ONLY Causes the package to be built right before being published