{"id":19745415,"url":"https://github.com/btd/moco","last_synced_at":"2025-07-16T09:43:13.184Z","repository":{"id":11133271,"uuid":"13496674","full_name":"btd/moco","owner":"btd","description":"Small idiomatic model and collection for modern javascript.","archived":false,"fork":false,"pushed_at":"2014-06-21T09:11:13.000Z","size":210,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-08-09T20:51:33.191Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/btd.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-10-11T10:52:33.000Z","updated_at":"2015-01-12T21:38:07.000Z","dependencies_parsed_at":"2022-09-03T20:12:38.748Z","dependency_job_id":null,"html_url":"https://github.com/btd/moco","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/btd%2Fmoco","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/btd%2Fmoco/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/btd%2Fmoco/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/btd%2Fmoco/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/btd","download_url":"https://codeload.github.com/btd/moco/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224202863,"owners_count":17272807,"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-11-12T02:08:15.945Z","updated_at":"2024-11-12T02:08:16.514Z","avatar_url":"https://github.com/btd.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Moco\n\nIt is a small library that create object-like and array-like models and collections. I do not want to use anything that already in es5. So this will work only in modern browsers (IE8 100% not supported).\n\n## Installation\n\nNode:\n\n\tnpm install --save moco\n    \nBrowser:\n\n\tbower install btd/moco\n    \n## Usage\n\nApi exports just 2 functions: `model` and `collection`.\n\n## Models\n\n```js\nvar model = require('moco').model;\n\nvar User = model()\n  .attr('_id', { primary: true }) // this attribute will be used as primary key\n  .attr('name') // just simple attribute\n  .attr('email', { get: function() {\n    return this.name + '@example.com';\n  }}) // this is a getter and it called each time you try access this model\n  .attr('title', { default: 'Unknow' })// this will be used to fill undefined attribute while creation\n    \nvar u1 = new User({ _id: 1, name: 'den'});\nconsole.log(u1._id); // 1\nconsole.log(u1.primary); // 1\n\nconsole.log(u1.email); // den@example.com\nconsole.log(u1.title); // Unknow\n\nu1.name = 'daniel';\n\nconsole.log(u1.email); // daniel@example.com\n```\n\nWhen you call `model()` it return new predefined constructor function.\n\n### .attr(name[, options])\n\nDefine new attribute on this model type. Only defined attributes will be saved to model (used `Object.seal`). By default created getter and setter for this attribute.\n\nSupported options:\n\n`get` - it is a function that will be used for getter, if it is defined for this attibute setter will not be defined.\n\n`default` - it can be anything, that will be assigned to attribute, while creation if its values is undefined. If `default` is a function, it will be called, in context of model instance.\n\n`primary` - it is a boolean field, when it is set to true you will can use this methods:\n\n`#isNew()` - it will return true|false if primary attribute filled. If primary is false throw exception.\n\n`#primary` - it is an alias for attribute with primary: true.\n\nIt is not possible to create 2 attributes with primary: true and all redefenitions of attributes ignored.\n\n### .use(fn)\n\nThis method can be used to mix something general to model.\n\n### .create(obj)\n\nThis method wrap obj to model if it is not and return it.\n\n### #[attr]\n\nFor each defined with `.attr` attributes will be created getter and setter.\n\n### Enumerable properties and defined attributes\n\nEach defined attribute created enumerable, so you can use this model with any library that work with objects and it will be just works.\n\n```js\nObject.keys(u1) // ['_id', 'name', 'email', 'title']\n```\nIf you will try to save anything that is not defined it will not be saved:\n```js\nu1.position = 'developer';\nu1.position // undefined\n```\n\n### Events\n\nWhen model attribute changed it emit event 'change' and 'change:'+attributeName with value and previous value. Also this event can be handled on Model level, as each model type also can emit events.\n\nModel type emit event 'initialize' with create model.\n\nSee [component/emitter](https://github.com/component/emitter) about how to handle events.\n\n## Collection\n\nCollection mimic javascript native `Array` everywhere it is possible. It have all methods that `Array` has and they do everything that native method do. But also it wrap object to Model instance if it is not and emit events:\n\n`add` emitted with instance when model added to collection.\n\n`remove` emitter with instance when model removed from colleciton.\n\n### .use(fn) \n\nIt used in the same way as in model types.\n\n### Example\n\n```js\nvar collection = require('moco').collection;\n\nvar Users = collection(User).use(utils.byId);\n\nvar users = new Users([u1, { _id: 2, name: 'lisa' }]);\nusers.length; // 2\n// utils.byId add 2 methods byId, removeById, of course Model should have primary attribute\nusers.byId(1).name // 'daniel'\n\nusers.byId(2).name // 'lisa'\n\nusers.forEach(function(user) {\n\tconsole.log(user.email);\n})\n// daniel@example.com\n// lisa@example.com\n```\n\n## Collection plugins\n\nThere are 3 built in collection plugins:\n\n### collection.byId\n\nIt create internal cache for primary attribute of models it contains and add methods `#byId(primary)` and `#removeById(primary)`.\nNothing in this plugin checks that collection contains 2 model with the same primary attribute.\n\n### collection.byField(attr)\n\nIt create internal cache for specified attribute and add method `#by[Attr](attr)` [Attr] there it is name with capital letter.\n\n### collection.modelsChanges\n\nIt add ability to collection emit `change` event when nested model changed.\n\n### model.nestedObjects\n\nIt emit change event every time nested model or collection changed. It can works together with `collection.modelsChanges` and proxy these events too.\n\nSmall example:\n\n```javascript\nvar NestedModel1 = moco.model()\n    .attr('a');\n\nvar NestedCollection = moco.collection(NestedModel1)\n    .use(moco.collection.modelsChanges);\n\nvar NestedModel2 = moco.model()\n    .attr('a', { type: NestedModel1 })\n    .attr('b', { type: NestedCollection })\n    .use(moco.model.nestedObjects);\n\nvar nm = new NestedModel1({ a: 'a' });\n\nvar c = new NestedCollection([ nm ]);\n\nvar m = new NestedModel2({ b: c });\n\n/*\n    this will emit 4 events on m:\n    change:b.a with value 'b' and prev 'a'\n    change:b with value === prev === c\n    change with previous 2 events\n*/\nnm.a = 'b';\n```\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2013 Denis Bardadym\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbtd%2Fmoco","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbtd%2Fmoco","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbtd%2Fmoco/lists"}