{"id":16473859,"url":"https://github.com/budiadiono/sqlite-ts","last_synced_at":"2025-03-23T11:32:51.387Z","repository":{"id":57368342,"uuid":"155022886","full_name":"budiadiono/sqlite-ts","owner":"budiadiono","description":"SQLite ORM for Typescript","archived":false,"fork":false,"pushed_at":"2019-02-10T07:46:57.000Z","size":94,"stargazers_count":15,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T14:07:34.370Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/budiadiono.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":"2018-10-28T00:50:55.000Z","updated_at":"2024-05-05T05:50:35.000Z","dependencies_parsed_at":"2022-09-05T19:21:25.664Z","dependency_job_id":null,"html_url":"https://github.com/budiadiono/sqlite-ts","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/budiadiono%2Fsqlite-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/budiadiono%2Fsqlite-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/budiadiono%2Fsqlite-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/budiadiono%2Fsqlite-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/budiadiono","download_url":"https://codeload.github.com/budiadiono/sqlite-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245097158,"owners_count":20560311,"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-11T12:28:33.114Z","updated_at":"2025-03-23T11:32:50.941Z","avatar_url":"https://github.com/budiadiono.png","language":"TypeScript","readme":"# SQLITE-TS\n\nSQLite ORM for Typescript\n\n## Installation\n\nUsing npm:\n\n```\nnpm i -S sqlite-ts\n```\n\nor yarn:\n\n```\nyarn add sqlite-ts\n```\n\n## Usage\n\nIt's easy!\n\n### Define Entities\n\n```ts\nimport { Column, Primary } from 'sqlite-ts'\n\nclass Person {\n  @Primary()\n  id: number = 0\n\n  @Column('NVARCHAR')\n  name: string = ''\n\n  @Column('DATETIME')\n  dob: Date = new Date()\n\n  @Column('INTEGER')\n  age: number = 0\n\n  @Column('BOOLEAN')\n  married: boolean = false\n\n  @Column('MONEY')\n  salary: number = 0\n}\n\nclass Address {\n  @Primary()\n  id: number = 0\n\n  @Column('INTEGER')\n  person: number = 0\n\n  @Column('NVARCHAR')\n  address: string = ''\n}\n```\n\n### Connect to Database\n\n```ts\n// let's use sqlite3 from https://github.com/mapbox/node-sqlite3\nimport Sqlite3 = require('sqlite3')\n\n// define entities object\nconst entities = {\n  Person,\n  Address\n}\n\n// make a connection using SQLite3.\n// you can use other available drivers\n// or create your own\nconst sqlite3Db = new sqlite.Database(':memory:')\nconst db = await Db.init({\n  // set the driver\n  driver: new SQLite3Driver(sqlite3Db),\n\n  // set your entities here\n  entities,\n\n  // set `true` so all tables in entities will automatically created for you\n  // if it does not exists yet in database\n  createTables: false\n})\n```\n\n### Working with Entities\n\nFrom now to work with entities you can access your entities via `db.tables.[entity name].[action function]`.\n\n#### Create\n\nFor example to create table you can simply do this:\n\n```ts\nawait db.tables.Person.create()\nawait db.tables.Address.create()\n```\n\nor\n\n```ts\nawait db.createAllTables()\n```\n\n#### Insert\n\n```ts\n// insert single data\nconst result = await db.tables.Person.insert({\n  name: 'Joey',\n  married: true,\n  dob: new Date(2000, 1, 1, 0, 0, 0),\n  age: 18,\n  salary: 100\n})\n```\n\nThe `Person` entity is using default primary key which is `INTEGER` that is autogenerated.\nYou can get inserted primary key value from the `result` of `insert` action above that returns:\n\n```ts\n{\n  insertId: 1, // generated primary key\n  rowsAffected: 1 // number of created data\n}\n```\n\nYou may want to insert multiple data at once like so:\n\n```ts\n// insert multiple data at once\nconst results = await db.tables.Person.insert([\n  {\n    name: 'Hanna',\n    married: false,\n    dob: new Date(2001, 2, 2, 0, 0, 0),\n    age: 17,\n    salary: 100\n  },\n  {\n    name: 'Mary',\n    married: false,\n    dob: new Date(2002, 3, 3, 0, 0, 0),\n    age: 26,\n    salary: 50\n  }\n])\n```\n\nBut you can't get advantage of getting the generated primary keys for inserted data.\nBecause the `results` only returns the last generated primary key:\n\n```ts\n{\n  insertId: 3, // latest generated primary key\n  rowsAffected: 2 // number of created data\n}\n```\n\nIf you have multiple action that you want to execute under `BEGIN` and `COMMIT` statement,\nyou can use transaction to do this:\n\n```ts\nawait db.transaction(({ exec, tables }) =\u003e {\n  exec(\n    tables.Address.insert({\n      person: 1,\n      address: `Joy's Home`\n    })\n  )\n  exec(\n    tables.Address.insert({\n      person: 2,\n      address: `Hanna's Home`\n    })\n  )\n  exec(\n    tables.Address.insert({\n      person: 3,\n      address: `Marry's Home`\n    })\n  )\n})\n```\n\nNeed to get inserted generated primary key under transaction? Simply do this instead:\n\n```ts\nlet address1: any\nlet address2: any\nlet address3: any\nawait db.transaction(({ exec, tables }) =\u003e {\n  exec(\n    tables.Address.insert({\n      person: 1,\n      address: `Joy's Home`\n    })\n  ).then(r =\u003e {\n    address1 = r\n  })\n\n  exec(\n    tables.Address.insert({\n      person: 2,\n      address: `Hanna's Home`\n    })\n  ).then(r =\u003e {\n    address2 = r\n  })\n\n  exec(\n    tables.Address.insert({\n      person: 3,\n      address: `Marry's Home`\n    })\n  ).then(r =\u003e {\n    address3 = r\n  })\n})\n```\n\nThe actions above should returns:\n\n```ts\n// address1:\n{\n  insertId: 1,\n  rowsAffected: 1\n}\n\n// address2:\n{\n  insertId: 2,\n  rowsAffected: 1\n}\n\n// address3:\n{\n  insertId: 1,\n  rowsAffected: 1\n}\n```\n\nYou can also do same things for `upsert`, `update`, `delete`, `create` and `drop` action.\n\n#### Select\n\n##### Select All\n\n```ts\n// select all\nconst people = await db.tables.Person.select()\n```\n\nreturns:\n\n```\n[ \n  { id: 1,\n    name: 'Joey',\n    dob: 2000-01-31T17:00:00.000Z,\n    age: 18,\n    married: true,\n    salary: 100\n  },\n  { id: 2,\n    name: 'Hanna',\n    dob: 2001-03-01T17:00:00.000Z,\n    age: 17,\n    married: false,\n    salary: 100\n  },\n  { id: 3,\n    name: 'Mary',\n    dob: 2002-04-02T17:00:00.000Z,\n    age: 26,\n    married: false,\n    salary: 50\n  }\n]\n```\n\n##### Select Columns\n\n```ts\n// select columns\nconst people2 = await db.tables.Person.select(c =\u003e [c.id, c.name, c.salary])\n```\n\nreturns:\n\n```\n[\n  { id: 1, name: 'Joey', salary: 100 },\n  { id: 2, name: 'Hanna', salary: 100 },\n  { id: 3, name: 'Mary', salary: 50 }\n]\n```\n\n##### Select Limit\n\n```ts\n// select with limit\nconst people3 = await db.tables.Person.select(c =\u003e [\n  c.id,\n  c.name,\n  c.salary\n]).limit(1)\n```\n\nreturns:\n\n```\n[{ id: 1, name: 'Joey', salary: 100 }]\n```\n\n##### Select Where\n\n```ts\n// select with condition\nconst people4 = await db.tables.Person.select(c =\u003e [c.id, c.name]).where(c =\u003e\n  c.greaterThanOrEqual({ salary: 100 })\n)\n```\n\nreturns:\n\n```\n[ { id: 1, name: 'Joey' }, { id: 2, name: 'Hanna' } ]\n```\n\n##### Select Order\n\n```ts\n// select with order\nconst people5 = await db.tables.Person.select(c =\u003e [c.id, c.name])\n  .where(c =\u003e c.notEquals({ married: true }))\n  .orderBy({ name: 'DESC' })\n```\n\nreturns:\n\n```\n[ { id: 3, name: 'Mary' }, { id: 2, name: 'Hanna' } ]\n```\n\n##### Select Single Data\n\n```ts\n// select single data\nconst person = await db.tables.Person.single(c =\u003e [c.id, c.name])\n```\n\nreturns:\n\n```\n{ id: 1, name: 'Joey' }\n```\n\nFor the rest, you can play around with editor intellisense to get more options.\n\n#### Update\n\n```ts\n// let's prove that she's not married yet\nlet hanna = await db.tables.Person.single(c =\u003e [c.id, c.name, c.married]).where(\n  c =\u003e c.equals({ id: 2 })\n)\n// returns:\n// hanna is not married yet = { id: 2, name: 'Hanna', married: false }\n\n// let's marry her\nawait db.tables.Person.update({ married: true }).where(c =\u003e c.equals({ id: 2 }))\n\nhanna = await db.tables.Person.single(c =\u003e [c.id, c.name, c.married]).where(c =\u003e\n  c.equals({ id: 2 })\n)\n// returns:\n// hanna is now married = { id: 2, name: 'Hanna', married: true }\n```\n\n#### Join\n\n```ts\nconst people6 = await db.tables.Person.join(\n  t =\u003e ({\n    // FROM Person AS self JOIN Address AS address\n    address: t.Address\n  }),\n  (p, { address }) =\u003e {\n    // ON self.id = address.person\n    p.equal({ id: address.person })\n  }\n).map(f =\u003e ({\n  // SELECT self.id AS id, self.name AS name, address.address AS address\n  id: f.self.id,\n  name: f.self.name,\n  address: f.address.address\n}))\n```\n\nresults:\n\n```\n[\n  { id: 1, name: 'Joey', address: \"Joy's Home\" },\n  { id: 2, name: 'Hanna', address: \"Hanna's Home\" },\n  { id: 3, name: 'Mary', address: \"Marry's Home\" }\n]\n```\n\nYou can follow the `join` action with `where`, `limit` and `orderBy` as well:\n\n```ts\n// join where order and limit\nconst people7 = await db.tables.Person.join(\n  t =\u003e ({\n    // FROM Person AS self JOIN Address AS address\n    address: t.Address\n  }),\n  (p, { address }) =\u003e {\n    // ON self.id = address.person\n    p.equal({ id: address.person })\n  }\n)\n  .map(f =\u003e ({\n    // SELECT self.id AS id, self.name AS name, address.address AS address\n    id: f.self.id,\n    name: f.self.name,\n    address: f.address.address\n  }))\n  // WHERE self.married = 1\n  .where(p =\u003e p.self.equals({ married: true }))\n  // ORDER BY address.address ASC\n  .orderBy({ address: { address: 'ASC' } })\n  // LIMIT 1\n  .limit(1)\n```\n\nresult:\n\n```\n[{ id: 2, name: 'Hanna', address: \"Hanna's Home\" }]\n```\n\n#### Delete\n\n```ts\n// delete\nconst delResult = await db.tables.Person.delete().where(c =\u003e\n  c.equals({ id: 3 })\n)\n```\n\nresult:\n\n```\n{\n  insertId: 3,\n  rowsAffected: 1\n}\n```\n\nYou can put `delete` action under `transaction`.\n\n#### Drop\n\n```ts\n// drop\nawait db.tables.Address.drop()\n\n// or drop inside transaction\nawait db.transaction(({ exec, tables }) =\u003e {\n  exec(tables.Address.drop())\n  exec(tables.Person.drop())\n})\n\n// or drop all tables\nawait db.dropAllTables()\n```\n\n## License\n\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbudiadiono%2Fsqlite-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbudiadiono%2Fsqlite-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbudiadiono%2Fsqlite-ts/lists"}