{"id":28466248,"url":"https://github.com/iolo/node-nobatis","last_synced_at":"2025-06-29T21:32:13.356Z","repository":{"id":6745667,"uuid":"7991984","full_name":"iolo/node-nobatis","owner":"iolo","description":"simple \"mybatis-like\" dao for nodejs","archived":false,"fork":false,"pushed_at":"2014-09-27T11:02:46.000Z","size":228,"stargazers_count":5,"open_issues_count":0,"forks_count":3,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-06-07T03:37:10.194Z","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/iolo.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":"2013-02-03T15:08:58.000Z","updated_at":"2021-01-22T01:12:15.000Z","dependencies_parsed_at":"2022-09-02T11:50:14.940Z","dependency_job_id":null,"html_url":"https://github.com/iolo/node-nobatis","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/iolo/node-nobatis","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iolo%2Fnode-nobatis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iolo%2Fnode-nobatis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iolo%2Fnode-nobatis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iolo%2Fnode-nobatis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iolo","download_url":"https://codeload.github.com/iolo/node-nobatis/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iolo%2Fnode-nobatis/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262671621,"owners_count":23346443,"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":"2025-06-07T06:08:47.889Z","updated_at":"2025-06-29T21:32:13.351Z","avatar_url":"https://github.com/iolo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"nobatis\n=======\n\nThis is a simple [mybatis](http://mybatis.org)-like DAO(NOT ORM) for node.js.\n\nFeatures\n--------\n\n* query mapper and builder\n* TODO: connection management\n* TODO: promise support(kriskowal's Q)\n* TODO: caching\n* TODO: validation \n* TODO: logging \n* TODO: error handling\n* DAO(NOT ORM)\n\nInstall\n-------\n\n```\n$ npm install nobatis\n```\n\nor\n\n```\n$ npm install git@github.com:iolo/node-nobatis.git\n```\n\nHow to Get DataSource\n---------------------\n\n* prepare configurations:\n\n```javascript\nvar config = {\n  \"dataSource\": {\n    \"driver\": \"mariasql\",\n    \"host\": \"localhost\",\n    \"port\": 3306,\n    \"user\": \"root\",\n    \"password\": \"\",\n    \"db\": \"test\"\n  },\n  \"queries\": {\n    \"test1.selectAll\": \"SELECT * FROM test1\",\n    \"test1.select\": \"SELECT * FROM test1 WHERE id=?\",\n    \"test1.insert\": \"INSERT INTO test1(name) VALUES(:name)\",\n    \"test1.update\": \"UPDATE test1 SET name=:name WHERE id=:id\",\n    \"test1.delete\": \"DELETE FROM test1 WHERE id=?\"\n  }\n};\n```\n\n* or you can write configurations to a file(json module)\n\n* import nobatis module\n\n```javascript\nvar nobatis = require('nobatis');\n```\n\n* create ```DataSource``` with configutaion:\n\n```javascript\nvar dataSource = nobatis.createDataSource(config);\n```\n\n* or create one with a configuration file(json module):\n\n```javascript\nvar dataSource = nobatis.createDataSource(require('./config'));\n```\n\n* or get the default one:\n\n```javascript\nvar dataSource = nobatis.createDataSource();\n```\n\n* now you can ```openSession()```:\n\n```javascript\nvar session = null;\ntry {\n  session = dataSource.openSession();\n  // use session here …\n} finally {\n  session \u0026\u0026 session.close();\n}\n```\n\n* or work ```withSession()```:\n\n```javascript\ndataSource.withSession(function (session) {\n  // use session here ...\n});\n```\n\n* work ```withSession()``` and ```promise```:\n\n```javascript\ndataSource.withSession(function (session) {\n  // use session and return promise ...\n  return session.select(...)\n    .then(function (select_result) {\n      return session.insert(...);\n    })\n    .then(function (insert_result) {\n      return session.update(...);\n    })\n    .then(function (update_result) {\n      return session.destroy(...);\n    });\n})\n.then(function (destroy_result) {\n  ...\n})\n.catch(function (err) {\n  ...\n});\n```\n\nHow to Execute Queries\n----------------------\n\n* select multiple rows\n\n```javascript\nsession.select('test1.selectAll', [])\n  .then(function (rows) {\n    ...\n  })\n  .catch(function (err) {\n    ...\n  });\n```\n\n* select multiple rows with row bounds\n\n```javascript\nsession.select('test1.selectAll', [], {offset:2, limit:2})\n  .then(function (rows) {\n    ...\n  })\n  .catch(function (err) {\n    ...\n  });\n```\n\n* select a single row\n\n```javascript\nsession.selectOne('test1.select', \\[1])\n  .then(function (row) {\n    ...\n  .catch(function (err) {\n    ...\n  });\n```\n\n* insert new row\n\n```javascript\nsession.insert('test1.insert', {name:'a'})\n  .then(function (insertId) {\n    ...\n  .catch(function (err) {\n    ...\n  });\n```\n\n* update row(s)\n\n```javascript\nsession.update('test1.update', {id:1, name:'a'})\n  .then(function (affectedRows) {\n    ...\n  .catch(function (err) {\n    ...\n  });\n```\n\n* delete row(s)\n\n```javascript\nsession.destroy('test1.delete', \\[1])\n  .then(function (affectedRows) {\n    ...\n  .catch(function (err) {\n    ...\n  });\n```\n\nHow to Create DAO\n-----------------\n\n* prepare dao object\n\n```javascript\nvar nobatis = require('nobatis');\nvar dataSource = require('nobatis').createDataSource(config);\nvar dao = nobatis.createDao(dataSource, {\n  table: 'test1',\n  primaryKey: 'id',\n  primaryKeyGenerated: true,\n  defaults: function () {\n    return {\n      id: 0,\n      name: '',\n      created: new Date()\n    };\n  }\n});\n```\n\n* create new object with default attributes\n```javascript\nvar obj = dao.createNew();\n```\n\n* create new object with custom attributes\n```javascript\nvar obj = dao.createNew({name:'foo'});\n```\n\n* check the object is saved or not\n```javascript\ndao.isNew(obj);\n```\n\n* select an object by primary key\n```javascript\ndao.load(pk)\n  .then(function (obj) {\n    ...\n  .catch(function (err) {\n    ...\n  });\n```\n\n* insert/update an object\n```javascript\ndao.save(obj)\n  .then(function (affectedRow-or-insertId) {\n    ...\n  .catch(function (err) {\n    ...\n  });\n```\n\n* insert/update an object and reload it\n```javascript\ndao.save(obj, true)\n  .then(function (obj) {\n    ...\n  .catch(function (err) {\n    ...\n  });\n```\n\n* delete an object by primary key\n```javascript\ndao.destroy(pk)\n  .then(function (success_or_not) {\n    ...\n  .catch(function(err) {\n    ...\n  });\n```\n\n* select all rows\n```javascript\ndao.all()\n  .then(function (rows) {\n    ...\n  .progress(function (row) {\n    ...\n  .catch(function(err) {\n    ...\n  });\n```\n\n* select all rows with bounds\n```javascript\ndao.all({offset:10, limit:10})\n  .then(function (rows, numRows) {\n    ...\n  .progress(function (row) {\n    ...\n  .catch(function(err) {\n    ...\n  });\n```\n\nMore\n----\n\n* see also\n    - [kriskowal's Q](https://github.com/kriskowal/q)\n    - [mscdex's mariasql](https://github.com/mscdex/node-mariasql)\n    - [felixge's mysql](https://github.com/felixge/node-mysql)\n\n* may the *SOURCE* be with you...\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiolo%2Fnode-nobatis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiolo%2Fnode-nobatis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiolo%2Fnode-nobatis/lists"}