├── .gitignore ├── LICENSE.md ├── README.md ├── example.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015, Huy Tran 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ObjectModel 2 | 3 | **ObjectModel** is a tiny package that help you use any object as a Model. 4 | 5 | ## How to install 6 | 7 | Add **ObjectModel** to your project with the following command: 8 | 9 | ``` 10 | npm install object-model 11 | ``` 12 | 13 | Include it when you needed: 14 | 15 | ```js 16 | var OM = require('object-model'); 17 | ``` 18 | 19 | ## How to use 20 | 21 | Let's say, we want to define `carModel`, as a model to define `cars`: 22 | 23 | ```js 24 | var carModel = { 25 | make: '', 26 | year: '', 27 | name: '', 28 | mpg: '' 29 | }; 30 | ``` 31 | 32 | Create the model from `carModel` object with: 33 | 34 | ```js 35 | OM.createModel(carModel); 36 | ``` 37 | 38 | From now on, you can create any data object with `carModel` model: 39 | 40 | ```js 41 | var rav4 = carModel.init('toyota', 2000, 'RAV4', 27); 42 | console.log(rav4); 43 | ``` 44 | 45 | or if you want to write more meaningful code: 46 | 47 | ```js 48 | var crv = carModel.init({ 49 | make: 'honda', 50 | year: 2005, 51 | name: 'CRV' 52 | }); 53 | console.log(crv); 54 | ``` 55 | 56 | You can skip some key, it will automatically takes the default value from model. 57 | 58 | ## Real-life usage 59 | 60 | For example, we use **ObjectModel** with **MongoDB** in a simple **To Do** app. 61 | 62 | Firstly, we define our `task` model: 63 | 64 | ```js 65 | var taskModel = { 66 | task: '', 67 | timeCreated: 0, 68 | done: false 69 | } 70 | ``` 71 | 72 | Then we create a model from `task` object with **ObjectManager**: 73 | 74 | ```js 75 | OM.createModel(taskModel); 76 | ``` 77 | 78 | Now the `taskModel` is available to use. Let's insert a new task to **MongoDB**: 79 | 80 | ```js 81 | db.collection('tasks').insertOne( 82 | taskModel.init('Pay gas bill', Date.now()), 83 | function(err, result) { 84 | if (err) console.error(err); 85 | else console.log(result); 86 | } 87 | ); 88 | ``` 89 | 90 | Or insert many document at a time: 91 | 92 | ```js 93 | db.collection('tasks').insertMany( 94 | [ 95 | taskModel.init('Pay gas bill', Date.now()), 96 | taskModel.init('Pay phone bill', Date.now()), 97 | taskModel.init('Pay car loan', Date.now()), 98 | ], 99 | function(err, result) { 100 | if (err) console.error(err); 101 | else console.log(result); 102 | } 103 | ); 104 | ``` 105 | 106 | # Contribute 107 | 108 | The project is available on Github here: [https://github.com/huytd/object-model](https://github.com/huytd/object-model) 109 | 110 | I know my code isn't cool yet, there are many potential issues with such a logic. Please help me improve it if you can. Feel free to create _Pull Request_ or do code review for other _PR_. 111 | 112 | # License 113 | 114 | This project published under **MIT License**, please refer `LICENSE.md` for more detail. 115 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var OM = require('object-model'); 2 | 3 | var carModel = { 4 | make: '', 5 | year: 0, 6 | name: '', 7 | mpg: 0, 8 | preowned: false 9 | }; 10 | 11 | OM.createModel(carModel); 12 | 13 | var rav4 = carModel.init('toyota', 2000, 'RAV4', 27); 14 | console.log(rav4); 15 | 16 | var crv = carModel.init({ 17 | make: 'honda', 18 | year: 2005, 19 | name: 'CRV', 20 | preowned: true 21 | }); 22 | console.log(crv); 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | ;(function(){ 2 | function ObjectModel() { } 3 | 4 | ObjectModel.prototype.createModel = function(o) { 5 | if (typeof(o) == 'object') { 6 | o.init = function() { 7 | var obj = {}; 8 | var keys = Object.keys(o); 9 | var arg = arguments; 10 | 11 | if (typeof(arg[0]) == 'object') { 12 | var prm = arg[0]; var prmKeys = Object.keys(prm); 13 | var paramMatched = true; 14 | for (var i = 0; i < prmKeys.length; i++) { 15 | if (keys.indexOf(prmKeys[i]) == -1) { 16 | paramMatched = false; 17 | break; 18 | } 19 | } 20 | if (paramMatched) { 21 | // parse from object 22 | for (var i = 0; i < keys.length; i++) { 23 | var key = keys[i]; 24 | if (key != 'init') { 25 | if (prmKeys.indexOf(key) != -1) { 26 | obj[key] = prm[key]; 27 | } else { 28 | obj[key] = o[key]; 29 | } 30 | } 31 | } 32 | return obj; 33 | } 34 | } 35 | 36 | // parse from arguments 37 | for (var i = 0; i < keys.length; i++) { 38 | var key = keys[i]; 39 | if (key != 'init') { 40 | if (i < arguments.length) { 41 | obj[keys[i]] = arguments[i]; 42 | } else { 43 | obj[keys[i]] = o[keys[i]]; 44 | } 45 | } 46 | } 47 | return obj; 48 | } 49 | } 50 | }; 51 | 52 | module.exports = new ObjectModel; 53 | })(); 54 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "object-model", 3 | "version": "1.0.4", 4 | "description": "Using any object as a model", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/huytd/object-model.git" 12 | }, 13 | "keywords": [ 14 | "object", 15 | "model", 16 | "orm" 17 | ], 18 | "author": "Huy Tran", 19 | "license": "MIT", 20 | "tonicExampleFilename": "example.js" 21 | } 22 | --------------------------------------------------------------------------------