项目作者: mediapeers

项目描述 :
same breed, different species.
高级语言: TypeScript
项目地址: git://github.com/mediapeers/chipmunk.git
创建时间: 2019-05-08T21:10:29Z
项目社区:https://github.com/mediapeers/chipmunk

开源协议:

下载


CHIPMUNK

main goals

  • slim & simple compared to chinchilla
  • better suited for react apps
  • functional with hopefully no memory leaks
  • tested

interface

setup, run blocks (always use run blocks!)

  1. const chipmunk = createChipmunk({
  2. errorInterceptor: (err) => true,
  3. headers: { 'Affiliation-Id': 'mpx' }
  4. })
  5. // to change config
  6. chipmunk.updateConfig({ headers: { 'Session-Id': '345dfgsdfgw43..' } })
  7. chipmunk.run(async (ch) => {
  8. // requests.. e.g.
  9. await ch.context('um.user')
  10. // an error happens
  11. throw new Error('foo')
  12. }, (err) => {
  13. // error handler is optional
  14. console.log(err.message) // would print 'foo'
  15. })

optional configuration options

verbose mode

  1. ch.updateConfig({ verbose: true })

contexts

  1. // get context
  2. await ch.context('um.user')

actions

examples

  1. // get user, default method
  2. await ch.action('um.user', 'get', { params: { user_id: 3 } })
  1. // get user with associations resolved & limited attribute set
  2. await ch.action('um.user', 'get', {
  3. params: { user_id: 3 },
  4. schema: `
  5. id, first_name,
  6. organization { name },
  7. `
  8. })
  1. // get user with associations resolved & limited attribute set
  2. // proxied through tuco (node server) -> only one request, better performance
  3. // will throw if no schema was provided
  4. await ch.action('um.user', 'get', {
  5. params: { user_id: 3 },
  6. proxy: true,
  7. schema: `
  8. id, first_name,
  9. organization { name },
  10. `
  11. })
  1. // create new user
  2. await ch.action('um.user', 'create', {
  3. body: {
  4. first_name: 'john',
  5. last_name: 'doe',
  6. ...rest,
  7. }
  8. })
  1. // update existing user
  2. await ch.action('um.user', 'update', {
  3. params: { user_id: 3 },
  4. body: {
  5. first_name: 'johnny',
  6. }
  7. })
  8. // update existing users
  9. await ch.action('um.user', 'update', {
  10. body: [
  11. { id: 3, first_name: 'johnny' },
  12. { id: 5, first_name: 'hermine' },
  13. ]
  14. })

optional action options

  1. // convert to Ruby on Rails compatible 'accepts nested attributes' body
  2. await ch.action('um.user', 'update', {
  3. params: { user_id: 3 },
  4. ROR: true,
  5. body: {
  6. first_name: 'johnny',
  7. organization: {
  8. name: 'walker'
  9. }
  10. }
  11. })
  12. // => converts to
  13. // {
  14. // first_name: 'johnny',
  15. // organization_attributes: {
  16. // name: 'walker'
  17. // }
  18. // }
  19. // convert to 'multi' update format body (our backends support)
  20. await ch.action('um.user', 'update', {
  21. multi: true,
  22. body: [
  23. { id: 3, first_name: 'johnny' },
  24. { id: 5, first_name: 'hermine' },
  25. ]
  26. })
  27. // converts to:
  28. // {
  29. // '3': { id: 3, first_name: 'johnny' },
  30. // '5': { id: 5, first_name: 'hermine' },
  31. // }
  32. // return RAW results
  33. // this does not move association references nor does it support resolving a schema
  34. await ch.action('um.user', 'query', {
  35. raw: true,
  36. })

associations

to manually resolve associations:

  1. // fetch organizations for given users
  2. let users = [user1, user2, user3] // ..requested earlier
  3. user1['@associations']['organization'] // => https://url.to/organization/2'
  4. user1.organization // => NotLoadedError!
  5. const orgResult = await ch.fetch(users, 'organization') // => returns all associated organizations as ChipmunkResult
  6. users = ch.assign(users, orgResult.objects, 'organization')
  7. users[0].organization // => returns org of first user
  8. // alternatively..
  9. users = await ch.fetchAndAssign(users, 'organization')
  10. users[0].organization // => returns org of first user

cache

by default, chipmunk prefixes all cache keys with

  • affiliation-id and role-id, if present
  • role-id only, if present
  • session-id only, if present
  • ‘anonymous’, if none of the above
  1. // use 'runtime' cache
  2. ch.updateConfig({ cache: { enabled: true, engine: 'runtime' } })
  3. // use 'storage' cache
  4. ch.updateConfig({ cache: { enabled: true, engine: 'storage' } })
  5. // EXAMPLE 1, write to cache for current user role
  6. ch.updateConfig({ headers: { 'Role-Id': 5 }, cache: { enabled: true, engine: 'storage' } })
  7. ch.cache.set('foo', 'bar')
  8. ch.cache.get('foo') // => bar
  9. ch.updateConfig({ headers: { 'Role-Id': 8 } })
  10. ch.cache.get('foo') // => null
  11. // EXAMPLE 2, write to cache, ignoring session id, role or affiliation, using runtime cache
  12. ch.updateConfig({ headers: { 'Role-Id': 5 } })
  13. ch.cache.set('foo', 'bar', { noPrefix: true, engine: 'runtime' })
  14. ch.cache.get('foo', { noPrefix: true, engine: 'runtime' }) // => bar
  15. ch.cache.get('foo', { engine: 'runtime' }) // => null
  16. ch.updateConfig({ headers: { 'Role-Id': 8 } })
  17. ch.cache.get('foo', { noPrefix: true, engine: 'runtime' }) // => bar

‘perform later’ jobs

chipmunk (as chinchilla did previously) offers convenience functionality to run second level priority code after the important stuff has been processed.
this allows for example to lazy load less important data after all important data has been gathered.

an example:

  1. const notImportant = () => {
  2. console.log('this really was not that important')
  3. }
  4. ch.performLater(notImportant)
  5. const users = (await ch.action('um.user', 'query')).objects
  6. console.log(users)
  7. // => [user1, user2, ...]
  8. // => this really was not that important