{"id":16844751,"url":"https://github.com/stephanhoyer/hedy","last_synced_at":"2025-09-12T03:37:46.444Z","repository":{"id":57239532,"uuid":"42722009","full_name":"StephanHoyer/hedy","owner":"StephanHoyer","description":"Functional ORM","archived":false,"fork":false,"pushed_at":"2023-12-15T02:39:55.000Z","size":53,"stargazers_count":7,"open_issues_count":1,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-02-16T21:34:51.679Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/StephanHoyer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-09-18T12:53:09.000Z","updated_at":"2022-02-23T04:17:56.000Z","dependencies_parsed_at":"2024-10-12T06:30:46.344Z","dependency_job_id":"66ae151a-a7d5-4903-9bb8-fc11fb69ad17","html_url":"https://github.com/StephanHoyer/hedy","commit_stats":{"total_commits":46,"total_committers":2,"mean_commits":23.0,"dds":"0.021739130434782594","last_synced_commit":"5781968e06213027d3534707c05a205b0dd78a97"},"previous_names":["stephanhoyer/fnorm"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StephanHoyer%2Fhedy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StephanHoyer%2Fhedy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StephanHoyer%2Fhedy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StephanHoyer%2Fhedy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/StephanHoyer","download_url":"https://codeload.github.com/StephanHoyer/hedy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239554769,"owners_count":19658278,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T12:56:30.720Z","updated_at":"2025-02-18T21:32:54.322Z","avatar_url":"https://github.com/StephanHoyer.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hedy - a functional ORM\n\n[![Join the chat at https://gitter.im/StephanHoyer/hedy](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/StephanHoyer/hedy?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n[![Build Status](https://travis-ci.org/StephanHoyer/hedy.svg)](https://travis-ci.org/StephanHoyer/hedy)\n\nLet's face it: Node's ORMs are crap. Mostly at least. But JavaScript is an\nawesome language. So why? Because we did it wrong. To much class-ish stuff like\nbackbone. And prototypal inheritance, which is bad.\n\nWe want to fix this!\n\n## Initialisation\n\n```javascript\nconst hedy = require('hedy')\nconst mem = require('hedy/adapter/mem')\n\nconst store = hedy(adapter)\n```\n\nThe `adapter` is a function that is called when a query is fired. It gets\nthe query options-object as parameter. This is a database-specific function. In\nthis case we user the build in memory adapter. In the future we want to create\nadapter for various databases. The goal is that it's really easy to create one for you\nspecific datastore.\n\nSee the existing adapters how this works.\n\n## Creating Models/Collections/Whatever-you-call-them\n\n..., we just call it `query`.\n\n```javascript\nconst userQuery = store('user') // 'user' is the tablename\n```\n\nThe `query` is a monad-ish structure. You can call methods on it and it returns\nanother query with the method applied. The original query remains untouched.\nUnter the hood this is done using\n[patchinko](https://github.com/barneycarroll/patchinko).\n\n## Fetch array of things\n\n```javascript\n// Array of POJOs containing user data\nconst users = await userQuery.where(where).load()\n```\n\nThe where can be omitted, in this case all items where fetched. The query will\nonly run, if you call `load` on it.\n\nThere are also some utility functions that might be usefull. They can be called\non the query without actually fetching it.\n\n```javascript\n// Array of POJOs containing user data\nconst users = await userQuery\n  .where(where)\n  .map(fn1)\n  .filter(fn2)\n  .load()\n})\n```\n\nThe `map` and `filter` will be applied onto the fetched collection in the order as\nthey are applied to the query. They may return a promise.\n\nBecause all that is lazy you might do the following:\n\n```javascript\nconst usersWithLongNames = userQuery.filter(user =\u003e user.name.length \u003e 10)\n\nconst femaleUsersWithLongNames = usersWithLongNames.filter(\n  user =\u003e user.gender === 'female'\n)\n\nconst kidsWithLongNames = usersWithLongNames.filter(user =\u003e user.age \u003c 10)\n\nconst namesOfKidsWithLongNames = kidsWithLongNames\n  .columns(['name'])\n  .map(user =\u003e user.name)\n\n// if you now need the names of the kids:\n// there you have it.\nconst names = await namesOfKidsWithLongNames.load()\n```\n\nSure, in some of those cases the database might do the heavy lifting, but there are\ncases where code can express much more then a database query. Also code reuse\nand composition can lead to great improvements here.\n\nCurrently there are only a few methods build in: `map`, `reduce`, `groupBy`,\n`indexBy`. It's easy to add your own method there. In the upper example lodashs\n`pluck` might be a good choice to get the user name:\n\n```javascript\nconst store = hedy(adapter, {\n  methods: {\n    pluck: require('lodash/collection/pluck'),\n  },\n})\n```\n\nIn this case we add the `pluck`-function of\n[lodash](https://lodash.com/docs#pluck). Now we can do\n\n```javascript\nconst usernames = await userQuery.pluck('name').load()\n// usernames = ['heiner', 'klaus', 'birgit']\n```\n\n## Fetch one thing by pk\n\n```javascript\nconst user = await userQuery.get(id)\n// user = { id: 1, name: 'Heiner' }\n```\n\n## Fetch one first\n\n```javascript\nconst user = await userQuery.where(where).first()\n// user = { id: 1, name: 'Heiner' }\n```\n\n## select columns (with aliasing)\n\n```javascript\nconst user = await userQuery\n  .columns({ username: 'name' })\n  .where(where)\n  .get(1)\n// user = { id: 1, username: 'Heiner' }\n```\n\n## Counting\n\n```javascript\nconst count = await userQuery.count()\n// count = 5\n```\n\n## Create one thing\n\n```javascript\nconst user = await userQuery.post(data)\n// user = { id: 1, name: 'Heiner' }\n```\n\n## Update one thing\n\n```javascript\nconst user = await userQuery.put(id, data)\n// user = { id: 1, name: 'Heiner' }\n```\n\nYou can also patch stuff:\n\n```javascript\nconst user = await userQuery.patch(id, data)\n// user = { id: 1, name: 'Heiner' }\n```\n\n## delete one thing\n\n```javascript\nawait userQuery.del(id)\n```\n\n## Relations\n\nRelations are a key part of ORMs. In most ORMs relations can only be in the same\ndatabase. _Hedy_ as a different approach on this. Relations are defined as\nquerys. Let's look at an example:\n\n```javascript\nconst data = {\n  user: [\n    { id: 1, name: 'heiner' },\n    { id: 2, name: 'klaus' },\n    { id: 3, name: 'manfred' },\n  ],\n  friend: [{ user1Id: 1, user2Id: 2 }, { user1Id: 2, user2Id: 3 }],\n  comment: [\n    { id: 1, userId: 2, text: 'gorgeous' },\n    { id: 2, userId: 3, text: 'nice' },\n    { id: 4, userId: 1, text: 'splended' },\n    { id: 5, userId: 2, text: 'awesome' },\n  ],\n}\nconst adapter = memAdapter(data)\nconst store = hedy(adapter)\n\nconst userQuery = store('user')\nconst commentQuery = store('comment')\nconst friendQuery = store('friend')\n```\n\nHere we have a memory db containing users and their comments.\n\nTo fetch the users with the comments we do:\n\n```javascript\nuserQuery.withRelated(hedy.hasMany(commentQuery)).then(users =\u003e ...)\n```\n\nAs you see, we use a helper to declare a to-many-relation and give a query as\nparameter. The query does not have to request to the same database, so this is\nperfectly possible.\n\n```javascript\nconst memQuery = hedy(memAdapter(data))\nconst pgQuery = hedy(pgAdapter(config))\n\nconst userQuery = memQuery('user')\nconst commentQuery = pgQuery('comment')\n\nuserQuery.withRelated(hedy.hasMany(commentQuery)).then(users =\u003e ...)\n```\n\nFor a more advanced example see the examples in the examples folder.\n\n### Has-many\n\n```javascript\nconst users = await userQuery.withRelated(hedy.hasMany(commentQuery)).load()\n```\n\n### Has-one\n\n**TODO** Add example\n\n### Belongs-to\n\n```javascript\nconst comments = await commentQuery\n  .withRelated(hedy.belongsTo(userQuery))\n  .load()\n```\n\n### Many-to-many\n\n```javascript\nconst users = await user\n  .withRelated(hedy.hasManyThrough(userQuery, friendQuery))\n  .load()\n```\n\nFor _many-to-many_-Relations there are two helper functions to create and delete\nthem:\n\n```javascript\nconst friendship = await friends.link(userA, userB)\nawait friends.unlink(userA, userB)\n```\n\n## Current state\n\nThis is pretty much WIP. The basics are done. Now it's time to write the\nadapters. First one will be a memory-adapter. Then we first add a postgres\nadapter since this is the DB we're using in our project.\n\nHope you like it.\n\nIf you have any ideas, feel free to create a PR/Issue.\n\n## links\n\n- Pretty similar attempt to _hedy_ by the bookshelf maintainer: https://github.com/rhys-vdw/data-mapper\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstephanhoyer%2Fhedy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstephanhoyer%2Fhedy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstephanhoyer%2Fhedy/lists"}