├── .editorconfig ├── .eslintignore ├── .eslintrc ├── .github ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── stale.yml ├── .gitignore ├── .npmignore ├── .yo-rc.json ├── CODEOWNERS ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── bin ├── automigrate.js └── instance-introspection.js ├── client └── README.md ├── common └── models │ ├── account.js │ └── account.json ├── package.json ├── server ├── boot │ ├── authentication.js │ └── root.js ├── component-config.json ├── config.json ├── datasources.json ├── middleware.json ├── middleware.production.json ├── model-config.json └── server.js └── test └── smoke.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # http://editorconfig.org 4 | 5 | root = true 6 | 7 | [*] 8 | indent_style = space 9 | indent_size = 2 10 | end_of_line = lf 11 | charset = utf-8 12 | trim_trailing_whitespace = true 13 | insert_final_newline = true 14 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strongloop/loopback-example-database/24b484e710d866214c55b0adfc208adc694f306a/.eslintignore -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "loopback", 3 | "rules": { 4 | "max-len": ["error", 80, 4, { 5 | "ignoreComments": true, 6 | "ignoreUrls": true, 7 | "ignorePattern": "^\\s*var\\s.+=\\s*(require\\s*\\()|(/)" 8 | }] 9 | } 10 | } -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 9 | 10 | # Description/Steps to reproduce 11 | 12 | 16 | 17 | # Link to reproduction sandbox 18 | 19 | 24 | 25 | # Expected result 26 | 27 | 30 | 31 | # Additional information 32 | 33 | 38 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ### Description 2 | 3 | 4 | #### Related issues 5 | 6 | 12 | 13 | - connect to 14 | 15 | ### Checklist 16 | 17 | 22 | 23 | - [ ] New tests added or existing tests modified to cover all changes 24 | - [ ] Code conforms with the [style 25 | guide](http://loopback.io/doc/en/contrib/style-guide.html) 26 | -------------------------------------------------------------------------------- /.github/stale.yml: -------------------------------------------------------------------------------- 1 | # Number of days of inactivity before an issue becomes stale 2 | daysUntilStale: 60 3 | # Number of days of inactivity before a stale issue is closed 4 | daysUntilClose: 14 5 | # Issues with these labels will never be considered stale 6 | exemptLabels: 7 | - pinned 8 | - security 9 | - critical 10 | - p1 11 | - major 12 | # Label to use when marking an issue as stale 13 | staleLabel: stale 14 | # Comment to post when marking an issue as stale. Set to `false` to disable 15 | markComment: > 16 | This issue has been automatically marked as stale because it has not had 17 | recent activity. It will be closed if no further activity occurs. Thank you 18 | for your contributions. 19 | # Comment to post when closing a stale issue. Set to `false` to disable 20 | closeComment: > 21 | This issue has been closed due to continued inactivity. Thank you for your understanding. 22 | If you believe this to be in error, please contact one of the code owners, 23 | listed in the `CODEOWNERS` file at the top-level of this repository. 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.csv 2 | *.dat 3 | *.iml 4 | *.log 5 | *.out 6 | *.pid 7 | *.seed 8 | *.sublime-* 9 | *.swo 10 | *.swp 11 | *.tgz 12 | *.xml 13 | .DS_Store 14 | .idea 15 | .project 16 | .strong-pm 17 | coverage 18 | node_modules 19 | npm-debug.log 20 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .project 3 | *.sublime-* 4 | .DS_Store 5 | *.seed 6 | *.log 7 | *.csv 8 | *.dat 9 | *.out 10 | *.pid 11 | *.swp 12 | *.swo 13 | node_modules 14 | coverage 15 | *.tgz 16 | *.xml 17 | -------------------------------------------------------------------------------- /.yo-rc.json: -------------------------------------------------------------------------------- 1 | { 2 | "generator-loopback": {} 3 | } -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Lines starting with '#' are comments. 2 | # Each line is a file pattern followed by one or more owners, 3 | # the last matching pattern has the most precedence. 4 | 5 | # Alumni maintainers 6 | # @kjdelisle @loay @ssh24 @virkt25 7 | 8 | # Core team members from IBM 9 | * @jannyHou @b-admike @dhmlau 10 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ### Contributing ### 2 | 3 | Thank you for your interest in `loopback-example-database`, an open source project 4 | administered by StrongLoop. 5 | 6 | Contributing to `loopback-example-database` is easy. In a few simple steps: 7 | 8 | * Ensure that your effort is aligned with the project's roadmap by 9 | talking to the maintainers, especially if you are going to spend a 10 | lot of time on it. 11 | 12 | * Make something better or fix a bug. 13 | 14 | * Adhere to code style outlined in the [Google C++ Style Guide][] and 15 | [Google Javascript Style Guide][]. 16 | 17 | * Sign the [Contributor License Agreement](https://cla.strongloop.com/agreements/strongloop/loopback-example-database) 18 | 19 | * Submit a pull request through Github. 20 | 21 | 22 | ### Contributor License Agreement ### 23 | 24 | ``` 25 | Individual Contributor License Agreement 26 | 27 | By signing this Individual Contributor License Agreement 28 | ("Agreement"), and making a Contribution (as defined below) to 29 | StrongLoop, Inc. ("StrongLoop"), You (as defined below) accept and 30 | agree to the following terms and conditions for Your present and 31 | future Contributions submitted to StrongLoop. Except for the license 32 | granted in this Agreement to StrongLoop and recipients of software 33 | distributed by StrongLoop, You reserve all right, title, and interest 34 | in and to Your Contributions. 35 | 36 | 1. Definitions 37 | 38 | "You" or "Your" shall mean the copyright owner or the individual 39 | authorized by the copyright owner that is entering into this 40 | Agreement with StrongLoop. 41 | 42 | "Contribution" shall mean any original work of authorship, 43 | including any modifications or additions to an existing work, that 44 | is intentionally submitted by You to StrongLoop for inclusion in, 45 | or documentation of, any of the products owned or managed by 46 | StrongLoop ("Work"). For purposes of this definition, "submitted" 47 | means any form of electronic, verbal, or written communication 48 | sent to StrongLoop or its representatives, including but not 49 | limited to communication or electronic mailing lists, source code 50 | control systems, and issue tracking systems that are managed by, 51 | or on behalf of, StrongLoop for the purpose of discussing and 52 | improving the Work, but excluding communication that is 53 | conspicuously marked or otherwise designated in writing by You as 54 | "Not a Contribution." 55 | 56 | 2. You Grant a Copyright License to StrongLoop 57 | 58 | Subject to the terms and conditions of this Agreement, You hereby 59 | grant to StrongLoop and recipients of software distributed by 60 | StrongLoop, a perpetual, worldwide, non-exclusive, no-charge, 61 | royalty-free, irrevocable copyright license to reproduce, prepare 62 | derivative works of, publicly display, publicly perform, 63 | sublicense, and distribute Your Contributions and such derivative 64 | works under any license and without any restrictions. 65 | 66 | 3. You Grant a Patent License to StrongLoop 67 | 68 | Subject to the terms and conditions of this Agreement, You hereby 69 | grant to StrongLoop and to recipients of software distributed by 70 | StrongLoop a perpetual, worldwide, non-exclusive, no-charge, 71 | royalty-free, irrevocable (except as stated in this Section) 72 | patent license to make, have made, use, offer to sell, sell, 73 | import, and otherwise transfer the Work under any license and 74 | without any restrictions. The patent license You grant to 75 | StrongLoop under this Section applies only to those patent claims 76 | licensable by You that are necessarily infringed by Your 77 | Contributions(s) alone or by combination of Your Contributions(s) 78 | with the Work to which such Contribution(s) was submitted. If any 79 | entity institutes a patent litigation against You or any other 80 | entity (including a cross-claim or counterclaim in a lawsuit) 81 | alleging that Your Contribution, or the Work to which You have 82 | contributed, constitutes direct or contributory patent 83 | infringement, any patent licenses granted to that entity under 84 | this Agreement for that Contribution or Work shall terminate as 85 | of the date such litigation is filed. 86 | 87 | 4. You Have the Right to Grant Licenses to StrongLoop 88 | 89 | You represent that You are legally entitled to grant the licenses 90 | in this Agreement. 91 | 92 | If Your employer(s) has rights to intellectual property that You 93 | create, You represent that You have received permission to make 94 | the Contributions on behalf of that employer, that Your employer 95 | has waived such rights for Your Contributions, or that Your 96 | employer has executed a separate Corporate Contributor License 97 | Agreement with StrongLoop. 98 | 99 | 5. The Contributions Are Your Original Work 100 | 101 | You represent that each of Your Contributions are Your original 102 | works of authorship (see Section 8 (Submissions on Behalf of 103 | Others) for submission on behalf of others). You represent that to 104 | Your knowledge, no other person claims, or has the right to claim, 105 | any right in any intellectual property right related to Your 106 | Contributions. 107 | 108 | You also represent that You are not legally obligated, whether by 109 | entering into an agreement or otherwise, in any way that conflicts 110 | with the terms of this Agreement. 111 | 112 | You represent that Your Contribution submissions include complete 113 | details of any third-party license or other restriction (including, 114 | but not limited to, related patents and trademarks) of which You 115 | are personally aware and which are associated with any part of 116 | Your Contributions. 117 | 118 | 6. You Don't Have an Obligation to Provide Support for Your Contributions 119 | 120 | You are not expected to provide support for Your Contributions, 121 | except to the extent You desire to provide support. You may provide 122 | support for free, for a fee, or not at all. 123 | 124 | 6. No Warranties or Conditions 125 | 126 | StrongLoop acknowledges that unless required by applicable law or 127 | agreed to in writing, You provide Your Contributions on an "AS IS" 128 | BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 129 | EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES 130 | OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY, OR 131 | FITNESS FOR A PARTICULAR PURPOSE. 132 | 133 | 7. Submission on Behalf of Others 134 | 135 | If You wish to submit work that is not Your original creation, You 136 | may submit it to StrongLoop separately from any Contribution, 137 | identifying the complete details of its source and of any license 138 | or other restriction (including, but not limited to, related 139 | patents, trademarks, and license agreements) of which You are 140 | personally aware, and conspicuously marking the work as 141 | "Submitted on Behalf of a Third-Party: [named here]". 142 | 143 | 8. Agree to Notify of Change of Circumstances 144 | 145 | You agree to notify StrongLoop of any facts or circumstances of 146 | which You become aware that would make these representations 147 | inaccurate in any respect. Email us at callback@strongloop.com. 148 | ``` 149 | 150 | [Google C++ Style Guide]: https://google.github.io/styleguide/cppguide.html 151 | [Google Javascript Style Guide]: https://google.github.io/styleguide/javascriptguide.xml 152 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) IBM Corp. 2013,2017. All Rights Reserved. 2 | Node module: loopback-example-database 3 | This project is licensed under the MIT License, full text below. 4 | 5 | -------- 6 | 7 | MIT license 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy 10 | of this software and associated documentation files (the "Software"), to deal 11 | in the Software without restriction, including without limitation the rights 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | copies of the Software, and to permit persons to whom the Software is 14 | furnished to do so, subject to the following conditions: 15 | 16 | The above copyright notice and this permission notice shall be included in 17 | all copies or substantial portions of the Software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # loopback-example-database 2 | 3 | **⚠️ This LoopBack 3 example project is no longer maintained. Please refer to [LoopBack 4 Examples](https://loopback.io/doc/en/lb4/Examples.html) instead. ⚠️** 4 | 5 | A tutorial for basic database related features. 6 | 7 | - [Overview](#overview) 8 | - [Prerequisites](#prerequisites) 9 | - [Running the example](#running-the-example) 10 | - [Tutorial - MongoDB](#tutorial---mongodb) 11 | 12 | ## Overview 13 | 14 | ### Topics covered 15 | 16 | - Data sources 17 | - Creating 18 | - Configuring 19 | - Models 20 | - Creating 21 | - Automigration 22 | - Instance introspection (Discovery) 23 | 24 | ### Database specific tutorials 25 | 26 | Database specific tutorials are on separate branches. The master branch contains 27 | the tutorial for MongoDB. 28 | 29 | |Branch|Connector| 30 | |:--|:--| 31 | |[master](https://github.com/strongloop/loopback-example-database)|MongoDB| 32 | |[mssql](https://github.com/strongloop/loopback-example-database/tree/mssql)|Microsoft SQL Server| 33 | |[mysql](https://github.com/strongloop/loopback-example-database/tree/mysql)|MySQL| 34 | |[oracle](https://github.com/strongloop/loopback-example-database/tree/oracle)|Oracle| 35 | |[postgresql](https://github.com/strongloop/loopback-example-database/tree/postgresql)|PostgreSQL| 36 | 37 | For example, to view the MySQL example: 38 | 39 | ``` 40 | git clone https://github.com/strongloop/loopback-example-database 41 | cd loopback-example-database 42 | git checkout mysql 43 | ``` 44 | 45 | ## Prerequisites 46 | 47 | Before starting this tutorial, make sure you have the following installed: 48 | 49 | - Node 50 | - NPM 51 | - [StrongLoop Controller](https://github.com/strongloop/strongloop) 52 | 53 | ## Running the example 54 | 55 | ``` 56 | git clone https://github.com/strongloop/loopback-example-database 57 | cd loopback-example-database 58 | npm install 59 | npm start 60 | ``` 61 | 62 | ## Tutorial - MongoDB 63 | 64 | ### 1. Create a new LoopBack app 65 | 66 | #### App info 67 | 68 | - Name: `loopback-example-database` 69 | - Dir to contain the project: `loopback-example-database` 70 | 71 | ``` 72 | lb app loopback-example-database 73 | 74 | _-----_ 75 | | | ╭──────────────────────────╮ 76 | |--(o)--| │ Let's create a LoopBack │ 77 | `---------´ │ application! │ 78 | ( _´U`_ ) ╰──────────────────────────╯ 79 | /___A___\ / 80 | | ~ | 81 | __'.___.'__ 82 | ´ ` |° ´ Y ` 83 | 84 | ? What's the name of your application? loopback-example-database 85 | ? Enter name of the directory to contain the project: loopback-example-database 86 | info change the working directory to loopback-example-database 87 | 88 | ? Which version of LoopBack would you like to use? 3.x (current) 89 | ? What kind of application do you have in mind? empty-server (An empty LoopBack API, without any c 90 | onfigured models or datasources) 91 | 92 | ``` 93 | 94 | ### 2. Install the LoopBack MongoDB connector 95 | 96 | ``` 97 | cd loopback-example-database 98 | npm install --save loopback-connector-mongodb 99 | ``` 100 | 101 | ### 3. Create a data source 102 | 103 | #### Data source info 104 | 105 | - Data source name: `accountDS` 106 | - Select the connector for `accountDS`: `MongoDB` 107 | 108 | ``` 109 | lb datasource accountDS 110 | ... # follow the prompts 111 | ``` 112 | 113 | This creates a new data source named `accountDS` that uses the MongoDB 114 | connector. 115 | 116 | ### 4. Configure the data source 117 | 118 | For the purposes of this example, we will use a preconfigured StrongLoop MongoDB 119 | server. Edit `server/datasources.json` to set the MongoDB configs: 120 | 121 | ``` 122 | { 123 | ... 124 | "accountDS": { 125 | "name": "accountDS", 126 | "connector": "mongodb", 127 | "host": "demo.strongloop.com", 128 | "port": 27017, 129 | "database": "demo", 130 | "username": "demo", 131 | "password": "L00pBack" 132 | } 133 | } 134 | ``` 135 | 136 | > Feel free to use your own local MongoDB instance. Simply change the configs 137 | > above to match your own. 138 | 139 | ### 5. Create a new model 140 | 141 | ``` 142 | lb model 143 | ... # follow the prompts 144 | ``` 145 | 146 | #### Model Info 147 | 148 | - Model name: `Account` 149 | - Attach `Account` to: `accountDS (mongodb)` 150 | - Base class: `PersistedModel` 151 | - Expose via REST: `Yes` 152 | - Custom plural form: *Leave blank* 153 | - Properties: 154 | - `email` 155 | - String 156 | - Not required 157 | - `createdAt` 158 | - Date 159 | - Not required 160 | - `lastModifiedAt` 161 | - Date 162 | - Not required 163 | 164 | ``` 165 | slc loopback:model Account 166 | ... # follow the prompts 167 | ``` 168 | 169 | ### 6. Create the collection with sample data - Automigration 170 | 171 | With the `account` model configured, we can generate the corresponding 172 | MongoDB collection using the info from the `Account` metadata in [`common/models/account.json`](common/models/account.json) 173 | via [*auto-migration*](http://loopback.io/doc/en/lb2/Implementing-auto-migration.html). 174 | 175 | Start by creating a dir to store general-purpose scripts: 176 | 177 | ``` 178 | mkdir bin 179 | ``` 180 | 181 | Inside that dir, create a script named [`automigrate.js`](bin/automigrate.js). 182 | To create the `Account` collection and create two sample accounts, run: 183 | 184 | ``` 185 | node bin/automigrate.js 186 | ``` 187 | 188 | > **WARNING** 189 | > 190 | > The `automigrate` function creates a new collection if it doesn't exist. If 191 | > the collection already exists, **it will be destroyed and it's data will be 192 | > deleted**. If you want to keep this data, use `autoupdate` instead. 193 | 194 | You should see: 195 | 196 | ``` 197 | Created: { email: 'baz@qux.com', 198 | createdAt: Thu Oct 22 2015 17:58:09 GMT-0700 (PDT), 199 | lastModifiedAt: Thu Oct 22 2015 17:58:09 GMT-0700 (PDT), 200 | id: 562986213ea33440575c6588 } 201 | Created: { email: 'foo@bar.com', 202 | createdAt: Thu Oct 22 2015 17:58:09 GMT-0700 (PDT), 203 | lastModifiedAt: Thu Oct 22 2015 17:58:09 GMT-0700 (PDT), 204 | id: 562986213ea33440575c6587 } 205 | ``` 206 | 207 | > If you are using Node 4, it is safe to ignore `Swagger: skipping unknown type 208 | > "ObjectId"`. This warning will be addressed in a future update. 209 | 210 | ### 7. View data using the explorer 211 | 212 | Projects scaffolded via `slc loopback` come with `loopback-component-explorer` 213 | preconfigured. From the project root, start the server: 214 | 215 | ``` 216 | node . 217 | ``` 218 | 219 | Then to view the existing account data, browse to `localhost:3000/explorer` and 220 | click: 221 | 222 | - `GET /Accounts` 223 | - `Try it out!` 224 | 225 | You should see: 226 | 227 | ``` 228 | [ 229 | { 230 | "email": "foo@bar.com", 231 | "createdAt": "2015-10-23T00:58:09.280Z", 232 | "lastModifiedAt": "2015-10-23T00:58:09.280Z", 233 | "id": "562986213ea33440575c6587" 234 | }, 235 | { 236 | "email": "baz@qux.com", 237 | "createdAt": "2015-10-23T00:58:09.280Z", 238 | "lastModifiedAt": "2015-10-23T00:58:09.280Z", 239 | "id": "562986213ea33440575c6588" 240 | } 241 | ] 242 | ``` 243 | 244 | > Try out some of the other endpoints to get a feel for how explorer works. 245 | 246 | ### 8. Add a script to perform instance instrospection (Discovery) 247 | 248 | > [*Discovery*](http://loopback.io/doc/en/lb2/Discovering-models-from-relational-databases.html) 249 | > is the process of reverse engineering a LoopBack model from an existing database schema. 250 | 251 | The LoopBack MongoDB connector does not support discovery. However, you can use 252 | *instance instrospection*, which creates a LoopBack model from an existing 253 | JavaScript object. 254 | 255 | To do this, create a script named [`instance-introspections.js`](bin/instance-introspection.js) 256 | in the `bin` dir. Then run: 257 | 258 | ``` 259 | node bin/instance-introspection 260 | ``` 261 | 262 | You should see: 263 | 264 | ``` 265 | Created: { email: 'bob.doe@ibm.com', 266 | createdAt: Thu Oct 22 2015 19:38:20 GMT-0700 (PDT), 267 | lastModifiedAt: Thu Oct 22 2015 19:38:20 GMT-0700 (PDT), 268 | id: 56299d9d71c7f600719ca39f } 269 | ``` 270 | 271 | > See the [official docs](http://loopback.io/doc/en/lb2/Creating-models-from-unstructured-data.html) 272 | > for more info. 273 | 274 | --- 275 | 276 | [More LoopBack examples](https://loopback.io/doc/en/lb3/Tutorials-and-examples.html) 277 | -------------------------------------------------------------------------------- /bin/automigrate.js: -------------------------------------------------------------------------------- 1 | // Copyright IBM Corp. 2015,2016. All Rights Reserved. 2 | // Node module: loopback-example-database 3 | // This file is licensed under the MIT License. 4 | // License text available at https://opensource.org/licenses/MIT 5 | 6 | var path = require('path'); 7 | 8 | var app = require(path.resolve(__dirname, '../server/server')); 9 | var ds = app.datasources.accountDS; 10 | ds.automigrate('Account', function(err) { 11 | if (err) throw err; 12 | 13 | var accounts = [ 14 | { 15 | email: 'john.doe@ibm.com', 16 | createdAt: new Date(), 17 | lastModifiedAt: new Date(), 18 | }, 19 | { 20 | email: 'jane.doe@ibm.com', 21 | createdAt: new Date(), 22 | lastModifiedAt: new Date(), 23 | }, 24 | ]; 25 | var count = accounts.length; 26 | accounts.forEach(function(account) { 27 | app.models.Account.create(account, function(err, model) { 28 | if (err) throw err; 29 | 30 | console.log('Created:', model); 31 | 32 | count--; 33 | if (count === 0) 34 | ds.disconnect(); 35 | }); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /bin/instance-introspection.js: -------------------------------------------------------------------------------- 1 | // Copyright IBM Corp. 2015,2016. All Rights Reserved. 2 | // Node module: loopback-example-database 3 | // This file is licensed under the MIT License. 4 | // License text available at https://opensource.org/licenses/MIT 5 | 6 | var path = require('path'); 7 | 8 | var app = require(path.resolve(__dirname, '../server/server')); 9 | var ds = app.datasources.accountDS; 10 | 11 | var account = { 12 | email: 'bob.doe@ibm.com', 13 | createdAt: new Date(), 14 | lastModifiedAt: new Date(), 15 | }; 16 | var opts = { 17 | idInjection: true, 18 | }; 19 | var Account = ds.buildModelFromInstance('Account', account, opts); 20 | 21 | var instance = new Account(account); 22 | Account.create(instance, function(err, model) { 23 | if (err) throw err; 24 | 25 | console.log('Created:', model); 26 | 27 | ds.disconnect(); 28 | }); 29 | -------------------------------------------------------------------------------- /client/README.md: -------------------------------------------------------------------------------- 1 | ## Client 2 | 3 | This is the place for your application front-end files. 4 | -------------------------------------------------------------------------------- /common/models/account.js: -------------------------------------------------------------------------------- 1 | // Copyright IBM Corp. 2015,2016. All Rights Reserved. 2 | // Node module: loopback-example-database 3 | // This file is licensed under the MIT License. 4 | // License text available at https://opensource.org/licenses/MIT 5 | 6 | module.exports = function(Account) { 7 | 8 | }; 9 | -------------------------------------------------------------------------------- /common/models/account.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Account", 3 | "base": "PersistedModel", 4 | "idInjection": true, 5 | "options": { 6 | "validateUpsert": true 7 | }, 8 | "properties": { 9 | "email": { 10 | "type": "string" 11 | }, 12 | "createdAt": { 13 | "type": "date" 14 | }, 15 | "lastModifiedAt": { 16 | "type": "date" 17 | } 18 | }, 19 | "validations": [], 20 | "relations": {}, 21 | "acls": [], 22 | "methods": {} 23 | } 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "loopback-example-database", 3 | "version": "2.0.0", 4 | "main": "server/server.js", 5 | "scripts": { 6 | "start": "node .", 7 | "lint": "eslint .", 8 | "test": "tape test/*.js", 9 | "posttest": "npm run lint" 10 | }, 11 | "dependencies": { 12 | "compression": "^1.0.3", 13 | "cors": "^2.8.1", 14 | "loopback": "^3.0.0", 15 | "loopback-boot": "^2.6.5", 16 | "loopback-component-explorer": "^6.3.1", 17 | "loopback-connector-mongodb": "^3.9.2", 18 | "serve-favicon": "^2.0.1", 19 | "strong-error-handler": "^1.1.0" 20 | }, 21 | "devDependencies": { 22 | "eslint": "^2.7.0", 23 | "eslint-config-loopback": "^1.0.0", 24 | "tape": "^4.2.2" 25 | }, 26 | "repository": { 27 | "type": "git", 28 | "url": "https://github.com/strongloop/loopback-example-database" 29 | }, 30 | "description": "loopback-example-database", 31 | "license": "MIT", 32 | "author": "IBM Corp." 33 | } 34 | -------------------------------------------------------------------------------- /server/boot/authentication.js: -------------------------------------------------------------------------------- 1 | // Copyright IBM Corp. 2014,2016. All Rights Reserved. 2 | // Node module: loopback-example-database 3 | // This file is licensed under the MIT License. 4 | // License text available at https://opensource.org/licenses/MIT 5 | 6 | module.exports = function enableAuthentication(server) { 7 | // enable authentication 8 | server.enableAuth(); 9 | }; 10 | -------------------------------------------------------------------------------- /server/boot/root.js: -------------------------------------------------------------------------------- 1 | // Copyright IBM Corp. 2014,2016. All Rights Reserved. 2 | // Node module: loopback-example-database 3 | // This file is licensed under the MIT License. 4 | // License text available at https://opensource.org/licenses/MIT 5 | 6 | module.exports = function(server) { 7 | // Install a `/` route that returns server status 8 | var router = server.loopback.Router(); 9 | router.get('/', server.loopback.status()); 10 | server.use(router); 11 | }; 12 | -------------------------------------------------------------------------------- /server/component-config.json: -------------------------------------------------------------------------------- 1 | { 2 | "loopback-component-explorer": { 3 | "mountPath": "/explorer" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /server/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "restApiRoot": "/api", 3 | "host": "0.0.0.0", 4 | "port": 3000, 5 | "remoting": { 6 | "context": false, 7 | "rest": { 8 | "normalizeHttpPath": false, 9 | "xml": false 10 | }, 11 | "json": { 12 | "strict": false, 13 | "limit": "100kb" 14 | }, 15 | "urlencoded": { 16 | "extended": true, 17 | "limit": "100kb" 18 | }, 19 | "cors": false, 20 | "errorHandler": false 21 | }, 22 | "legacyExplorer": false 23 | } 24 | -------------------------------------------------------------------------------- /server/datasources.json: -------------------------------------------------------------------------------- 1 | { 2 | "db": { 3 | "name": "db", 4 | "connector": "memory" 5 | }, 6 | "accountDS": { 7 | "host": "demo.strongloop.com", 8 | "port": 27017, 9 | "database": "demo", 10 | "username": "demo", 11 | "password": "L00pBack", 12 | "name": "accountDS", 13 | "connector": "mongodb" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /server/middleware.json: -------------------------------------------------------------------------------- 1 | { 2 | "initial:before": { 3 | "loopback#favicon": {} 4 | }, 5 | "initial": { 6 | "compression": {}, 7 | "cors": { 8 | "params": { 9 | "origin": true, 10 | "credentials": true, 11 | "maxAge": 86400 12 | } 13 | } 14 | }, 15 | "session": {}, 16 | "auth": {}, 17 | "parse": {}, 18 | "routes": { 19 | "loopback#rest": { 20 | "paths": [ 21 | "${restApiRoot}" 22 | ] 23 | } 24 | }, 25 | "files": {}, 26 | "final": { 27 | "loopback#urlNotFound": {} 28 | }, 29 | "final:after": { 30 | "strong-error-handler": {} 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /server/middleware.production.json: -------------------------------------------------------------------------------- 1 | { 2 | "final:after": { 3 | "loopback#errorHandler": { 4 | "params": { 5 | "includeStack": false 6 | } 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /server/model-config.json: -------------------------------------------------------------------------------- 1 | { 2 | "_meta": { 3 | "sources": [ 4 | "loopback/common/models", 5 | "loopback/server/models", 6 | "../common/models", 7 | "./models" 8 | ], 9 | "mixins": [ 10 | "loopback/common/mixins", 11 | "loopback/server/mixins", 12 | "../common/mixins", 13 | "./mixins" 14 | ] 15 | }, 16 | "User": { 17 | "dataSource": "db" 18 | }, 19 | "AccessToken": { 20 | "dataSource": "db", 21 | "public": false 22 | }, 23 | "ACL": { 24 | "dataSource": "db", 25 | "public": false 26 | }, 27 | "RoleMapping": { 28 | "dataSource": "db", 29 | "public": false 30 | }, 31 | "Role": { 32 | "dataSource": "db", 33 | "public": false 34 | }, 35 | "Account": { 36 | "dataSource": "accountDS", 37 | "public": true 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /server/server.js: -------------------------------------------------------------------------------- 1 | // Copyright IBM Corp. 2014,2016. All Rights Reserved. 2 | // Node module: loopback-example-database 3 | // This file is licensed under the MIT License. 4 | // License text available at https://opensource.org/licenses/MIT 5 | 6 | var loopback = require('loopback'); 7 | var boot = require('loopback-boot'); 8 | 9 | var app = module.exports = loopback(); 10 | 11 | app.start = function() { 12 | // start the web server 13 | return app.listen(function() { 14 | app.emit('started'); 15 | var baseUrl = app.get('url').replace(/\/$/, ''); 16 | console.log('Web server listening at: %s', baseUrl); 17 | if (app.get('loopback-component-explorer')) { 18 | var explorerPath = app.get('loopback-component-explorer').mountPath; 19 | console.log('Browse your REST API at %s%s', baseUrl, explorerPath); 20 | } 21 | }); 22 | }; 23 | 24 | // Bootstrap the application, configure models, datasources and middleware. 25 | // Sub-apps like REST API are mounted via boot scripts. 26 | boot(app, __dirname, function(err) { 27 | if (err) throw err; 28 | 29 | // start the server if `$ node server.js` 30 | if (require.main === module) 31 | app.start(); 32 | }); 33 | -------------------------------------------------------------------------------- /test/smoke.js: -------------------------------------------------------------------------------- 1 | // Copyright IBM Corp. 2015,2016. All Rights Reserved. 2 | // Node module: loopback-example-database 3 | // This file is licensed under the MIT License. 4 | // License text available at https://opensource.org/licenses/MIT 5 | 6 | var test = require('tape'); 7 | 8 | test('smoke test', function(t) { 9 | t.equal(1, 1); 10 | t.end(); 11 | }); 12 | --------------------------------------------------------------------------------