{"id":17349251,"url":"https://github.com/co2-git/narwal","last_synced_at":"2025-03-27T11:44:30.327Z","repository":{"id":27873733,"uuid":"31364779","full_name":"co2-git/narwal","owner":"co2-git","description":"Object Modeling in JavaScript for MySQL","archived":false,"fork":false,"pushed_at":"2015-04-14T15:13:42.000Z","size":472,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-01T16:23:19.275Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":false,"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/co2-git.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2015-02-26T11:53:18.000Z","updated_at":"2017-07-07T20:53:43.000Z","dependencies_parsed_at":"2022-09-03T20:22:15.200Z","dependency_job_id":null,"html_url":"https://github.com/co2-git/narwal","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/co2-git%2Fnarwal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/co2-git%2Fnarwal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/co2-git%2Fnarwal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/co2-git%2Fnarwal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/co2-git","download_url":"https://codeload.github.com/co2-git/narwal/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245841692,"owners_count":20681184,"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-15T16:55:08.787Z","updated_at":"2025-03-27T11:44:30.308Z","avatar_url":"https://github.com/co2-git.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"n    a    r    w    a    l\n==========================\n\n## alpha - do not use (yet)\n\n`narwal` is a **loosely coupled structure-data architecture** for modeling and moving around MySQL data.\n\n# Install\n\n```bash\nnpm install narwal\n```\n\n```js\nvar narwal = require('narwal');\n```\n\n# Model\n\nWith `narwal` it's easy to modelize your tables structure:\n\n```js\n// Model for `employees` table\n\nnew narwal.Model(\"Employee\", {\n\n  // FIELD first_name VARCHAR(255)\n  \n  \"first_name\": {\n    type:       String,\n    required:   true\n  },\n  \n  // FIELD last_name VARCHAR(255)\n  \n  \"last_name\": {\n    type:       String,\n    required:   true\n  },\n  \n  // FIELD email VARCHAR(255)\n  \n  \"email\": {\n    type:       String\n    validate:   /^.+@.+$/\n  },\n  \n  // FIELD dob TIMESTAMP\n  \n  \"dob\": {\n    type:       Date\n  },\n  \n  // FIELD active TINYINT(1) DEFAULT 0\n  \n  \"active\": {\n    type:       Boolean,\n    default:    false\n  }\n});\n```\n\nFind out more about [structuring your data models](docs/Structure.md).\n\n# CRUD Queries\n\n`narwal` models can easily be queried for `SELECT`, `INSERT`, `UPDATE` and `DELETE` queries.\n\n## INSERT INTO\n\n```sql\nINSERT INTO employees (first_name, last_name) VALUES ('John', 'Doe')\n```\n\n```js\nnarwal.models.Employee\n  .insert({ \"first_name\": 'John', \"last_name\": 'Doe' });\n```\n\nFind out more about [inserting data](docs/Insert.md).\n\n## SELECT\n\n```sql\nSELECT email FROM employees WHERE first_name='John' AND last_name='Doe' LIMIT 10 ORDER BY email ASC\n```\n\n```js\nnarwal.models.Employee\n  .find({ \"first_name\": 'John', \"last_name\": 'Doe' })\n  .select(\"email\")\n  .limit(10)\n  .sort(\"email\");\n```\n\nFind out more about [retrieving data](docs/Retrieve.md).\n\n## UPDATE\n\n```sql\nUPDATE employees SET first_name='John' WHERE last_name='Doe'\n```\n\n```js\nnarwal.models.Employee\n  .update({ \"first_name\": 'John' })\n  .where({ \"last_name\": 'Doe' });\n```\n\n## DELETE\n\n```sql\nDELETE FROM employees WHERE first_name='John' AND last_name='Doe'\n```\n\n```js\nnarwal.models.Employee\n  .remove({ \"first_name\": 'John', \"last_name\": 'Doe' });\n```\n\n# Filters\n\nFilters can handle complex `WHERE` statements\n\n```sql\nSELECT * FROM players \n  WHERE \n    first_name != 'John' \n  AND\n    (last_name = 'Jackson' OR last_name REGEXP '^Smith')\n  AND\n    score \u003e 100\n  AND\n    trial_expiration_date \u003e NOW()\n```\n\n```js\nvar is = narwal.is;\nvar sql = narwal.sql;\n\nnarwal.models.Player\n  \n  .filter({\n  \n    \"first_name\":               is.not(\"John\"),\n    \"last_name\":                [ \"Jackson\", /^Smith/ ],\n    \"score\":                    is.above(100),\n    \"trial_expiration_date\":    is.after(sql('NOW()'))\n  \n  });\n```\n\n# Relations\n\nIt is easy to link different models together:\n\n```js\n// Join model Player with model Team\n\nnew narwal.Model(\"Team\", { \"color\": String });\n\nnew narwal.Model(\"Player\", { \"username\": String, \"team\": narwal.models.Team });\n\n// Note that you can do deep-linking search:\n\nnarwal.models.Player\n  // Find players which team's color is red\n  .find({ \"team\": { \"color\": \"red\" } });\n```\n\n# Hooks\n\nYou can also `before` and `after` hooks on any operations:\n\n```js\nnarwal.models.Player.before('insert', function (row, done) {\n  fs.mkdir('users/' + row.id, done);\n});\n\nnarwal.models.Player.after('remove', function (row, done) {\n  fs.rmdir('users/' + row.id, done);\n});\n```\n\n# Transactions\n\n```js\nnew narwal.Transaction(function (done) {\n  \n  // Queries run here will force a rollback on error\n  \n  // Call done() when done to commit the transaction\n  \n  \n  // Example of a transaction:\n  \n  narwal.models.Team                  // Use Model \"Team\"\n    \n    .insert({ \"color\": \"red\" })       // Insert new team which color is red\n    \n    .then(function (newTeam) {       // Once new team created\n      \n      narwal.models.Player            // Use Model \"Player\"\n        \n        .insert({ \"team\": newTeam })  // Insert new player which team is the newly created team\n        \n        .then(                        // Once new player created\n          done);                      // Commit transaction\n      }\n    );\n  \n});\n```\n\nLearn more about transactions [here](docs/Transactions.md)\n\n# Stream support\n\n# Migration\n\nYou can create MySQL tables from models:\n\n```js\nnarwal.models.Player.create();\n```\n\nSpecify `{ \"alter\": true }` to alter the table in case it already exists but has a different structure. If the table does not exists, it will be created.\n\n```js\nnarwal.models.Player.create({ \"alter\": true });\n```\n\n# Connect\n\n```js\nnarwal.\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fco2-git%2Fnarwal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fco2-git%2Fnarwal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fco2-git%2Fnarwal/lists"}