{"id":21592931,"url":"https://github.com/jonabrams/synth-db","last_synced_at":"2025-04-10T23:26:07.715Z","repository":{"id":34807575,"uuid":"38793408","full_name":"JonAbrams/synth-db","owner":"JonAbrams","description":"An ORM with support for the latest JavaScript features","archived":false,"fork":false,"pushed_at":"2017-02-11T19:41:10.000Z","size":28,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-06T19:58:56.228Z","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":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JonAbrams.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":"2015-07-09T02:58:18.000Z","updated_at":"2024-02-21T13:02:57.000Z","dependencies_parsed_at":"2022-08-17T22:50:13.677Z","dependency_job_id":null,"html_url":"https://github.com/JonAbrams/synth-db","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JonAbrams%2Fsynth-db","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JonAbrams%2Fsynth-db/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JonAbrams%2Fsynth-db/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JonAbrams%2Fsynth-db/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JonAbrams","download_url":"https://codeload.github.com/JonAbrams/synth-db/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248313818,"owners_count":21082923,"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-24T17:10:33.979Z","updated_at":"2025-04-10T23:26:07.695Z","avatar_url":"https://github.com/JonAbrams.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# synth-db\n\nAn ORM for Node and SQL databases. Heavily inspired by [ActiveRecord](http://guides.rubyonrails.org/active_record_basics.html) with a goal of being compatible with the same db that a Rails app would use.\n\nRequires Node 0.12 or IO.js so it can make use of ES6/ES2015 features of JavaScript. Requires classes and arrow functions.\n\nBeing developed readme first (tests second). There's still lots of missing implementation.\n\n[![Build Status](https://travis-ci.org/JonAbrams/synth-db.svg?branch=master)](https://travis-ci.org/JonAbrams/synth-db)\n\n## Install\n\n```\nnpm install --save synth-db\nnpm install --save knex pg      # needed too\n```\n\n## API\n\n### init\n\nCreate a connection to your db using [knex](https://github.com/tgriesser/knex), then pass it to synth-db.\n\n```\nlet knex = require('knex')('postgres://localhost/mydb');\nlet sdb = require('synth-db');\nsdb.setKnex(knex);\n```\n\nsynth-db is a base class that your models will extend.\n\n### Declaring Models\n\nCreate a model and have it extend the `Base` class.\n\n```javascript\n// Assuming you called setKnex previously in the app\nvar sdb = require('synth-db');\n\nclass User extends sdb.Base {\n  constructor () {\n    super();\n  }\n}\n\n/* init will populate the attributes with fields from the `users` table */\nUser.init().then(function () {\n  return User.first;\n}).then(function (user) {\n  console.log(user.name);\n})\n```\n\nThe above will look to the database for a `users` table, and add setters and getters for each column detected.\n\nWhat can you do with a model? You can use it to find records from the table is represents.\n\n### Querying\n\n#### .find(id:string|number):Record\n\n.find takes in the primary key used to look up a record. The `id` field will be used for lookups. A promise to the record is returned, if no record is found, the promise will be rejected.\n\n```javascript\nvar userId = 1235;\nUser.find(userId).then(function (currentUser) {\n  console.log(`Email: ${currentUser.email}`);\n}, function () {\n  console.log(\"Sorry, no record was found\");\n});\n```\n\n#### .where([equalities:object]|[searchString:string][, ...vars]):Relation\n\n.where can either take an equalities object or a search string. The equality object is a set of keys and values that need to all be true for a record to be included.\n\nAlternatively, you can pass in a search string that will be passed through to the db. Don't put user provided data into the search string, instead insert a `?` into the string, and pass the data as an extra argument, this will avoid SQL injection attacks.\n\n```javascript\nvar query = User.where({ confirmed: true });\n// or\nquery = User.where('confirmed = ?', true);\nquery.toString(); // \"select * from users where confirmed = true;\"\n// Only once .then is called on a relation is the db hit\nquery.then(function (users) {\n  return user.email;\n});\n```\n\n#### .all:Array[Record]\n\nExecutes the current relation by turning it into a query, returning a promise to an array of records.\n\nNote: Instead of using `.all` you can also use `.then()`.\n\n```javascript\nUser.order('created_at').limit(10).all.then(function (users) {\n  users.forEach(function (user) {\n    console.log(user.name);\n  });\n});\n\n// or\n\nUser.order('created_at').limit(10).then(function (users) {\n  …\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonabrams%2Fsynth-db","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonabrams%2Fsynth-db","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonabrams%2Fsynth-db/lists"}