{"id":15040654,"url":"https://github.com/samuil4/ts-data-layer-model","last_synced_at":"2025-04-14T18:34:03.866Z","repository":{"id":57380623,"uuid":"141626935","full_name":"samuil4/ts-data-layer-model","owner":"samuil4","description":"Models to help you organize your data layer. Forget about using plain json and modifying it's properties for UI purposes, and then exclude the modified properties so you can save the changes to an API end-point.","archived":false,"fork":false,"pushed_at":"2018-08-20T04:28:54.000Z","size":84,"stargazers_count":5,"open_issues_count":8,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-06T15:04:06.482Z","etag":null,"topics":["typescript","typescript-library","typescript2"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/samuil4.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}},"created_at":"2018-07-19T20:19:18.000Z","updated_at":"2019-10-29T10:05:47.000Z","dependencies_parsed_at":"2022-09-26T16:41:18.791Z","dependency_job_id":null,"html_url":"https://github.com/samuil4/ts-data-layer-model","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samuil4%2Fts-data-layer-model","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samuil4%2Fts-data-layer-model/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samuil4%2Fts-data-layer-model/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samuil4%2Fts-data-layer-model/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samuil4","download_url":"https://codeload.github.com/samuil4/ts-data-layer-model/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248937201,"owners_count":21186184,"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":["typescript","typescript-library","typescript2"],"created_at":"2024-09-24T20:44:52.821Z","updated_at":"2025-04-14T18:34:03.838Z","avatar_url":"https://github.com/samuil4.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Data layer model for typescript\n\nProblem this micro library focuses to solve:\n\n\u003e Better organization of the server returned data, excluding from model UI related data and some helper functions.\n\n## Note:\n\nSome parts of the code will be changed and optimized. Please submit your features request.\n\n## Change log\n\n### v.1.0.6\n\n- added tests\n- added add/remove method on collection `myCollection.add(rawJSON)`, `myCollection.remove(model)`\n- added collection reset method `myCollection.reset()`, `myCollection.reset(rawJSON)`\n- added collection length property `myCollection.length`\n- added model clerCached method to clear cached props `myCollection.clearCached() // clears all`, `myCollection.clearCached('myProp') // clears myProp`\n- added findWhere method to collection `myCollection.findWhere({name: \"Samuil\", admin: true})`\n- added sortBy method to collection `myCollection.sortBy('name') // string sorted`, `myCollection.sortBy('age') // number sorted`\n\n### v.1.0.5\n\n- added clone method on model | deep clone\n\n### v.1.0.2\n\n- added collection class\n- added interfaces related to collection\n- added to core model the following models\n  - toString - export props as JSON string\n  - toStringSession - export session props as JSON string\n  - toStringAll - exports full model state as JSON string\n- removed cached property debug logs from decorator\n\n### v. 1.0.1\n\n- added core model class and bare minimum functionality\n- added dome usefull interfaces\n- added some sort of test and examples\n\n## Why ?\n\n\u003e Assuming you have a list of items returned from an API end point and you have to add UI only related properties (here called session props). When you need to save the modified data to the server we usually do the following:\n\n```javascript\n// get an item\nlet item = items[0];\nconst cleanItem = _.omit(item, [\n  'selected',\n  'visible',\n  'used',\n  \"whatever property you don't want to go to the server\",\n]);\n\napiService.send(cleanItem);\n```\n\n\u003e Now you can do the following\n\n```javascript\n  interface propsDefinition {\n    itemName: string;\n    thisGoesToServer: []\n  }\n\n  interface sessionPropsDefinition {\n    selected: boolean;\n    visible: boolean;\n    whateverYouDontWantToGoToServer: boolean;\n  }\n\n  class MyItem extends Model implements propsDefinition, sessionPropsDefinition {\n    itemName: string;\n    thisGoesToServer: [];\n    whateverYouDontWantToGoToServer: boolean;\n\n    constructor(data: IRawData\u003cpropsDefinition, sessionPropsDefinition\u003e) {\n      super(data);\n    }\n  }\n\n  const newItem = new MyItem({\n    props: {...},\n    session: {...}\n  });\n\n  // And you get the usual autocompleation, type checking,  type guards, etc.\n  newItem.itemName = 'new name';\n  newItem.thisGoesToServer = ['yep'];\n  newItem.visible = true;\n  newItem.selected = false;\n\n  const sendToServer = newItem.toJSON();\n```\n\n## Completed features list:\n\n- 1.  On instance create structure of Props and SessionProps.\n- 2.  Create dynamic getters and setters.\n- 3.  Developper must be able to use MyObject.sessionPropName or MyObject.propName without referencing Props or SessionProps.\n- 5.  For extraProps: false. Show error not found on get and set property.\n- 6.  If extraProps: false -\u003e Seal prototype.\n- 7.  If extraProps: true -\u003e Add property from setter method.\n- 8.  Create method to export props as JSON excluding any sessionProperties.\n- 9.  Ability to cache values (getters) of properties. Example: Math.PI \\* 256 - expected calculation to happen only once\n- 10. Tests `yarn test`\n\n## Pending completion\n\n- 4.  Do not let duplicated keys to be used in Props and SessionProps.\n- 10. Ability to clear on demand cached values\n- 11. Add collection entity\n\n## New features request\n\n\u003e [Submit you feedback fere](https://github.com/samuil4/ts-data-layer-model/issues)\n\n\u003e If you like where this is going, please star this repo :)\n\n## TO DO:\n\n- Documentation and more examples\n- fix typos\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamuil4%2Fts-data-layer-model","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamuil4%2Fts-data-layer-model","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamuil4%2Fts-data-layer-model/lists"}