{"id":16957965,"url":"https://github.com/joshnuss/supabase-active-record","last_synced_at":"2025-10-14T04:48:33.893Z","repository":{"id":66379955,"uuid":"336092538","full_name":"joshnuss/supabase-active-record","owner":"joshnuss","description":"ActiveRecord pattern for supabase.io","archived":false,"fork":false,"pushed_at":"2021-02-12T02:30:34.000Z","size":120,"stargazers_count":32,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T22:20:05.040Z","etag":null,"topics":["activerecord","supabase"],"latest_commit_sha":null,"homepage":"","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/joshnuss.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-04T21:54:13.000Z","updated_at":"2024-07-22T08:47:07.000Z","dependencies_parsed_at":"2023-02-22T08:30:13.260Z","dependency_job_id":null,"html_url":"https://github.com/joshnuss/supabase-active-record","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/joshnuss%2Fsupabase-active-record","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joshnuss%2Fsupabase-active-record/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joshnuss%2Fsupabase-active-record/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joshnuss%2Fsupabase-active-record/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joshnuss","download_url":"https://codeload.github.com/joshnuss/supabase-active-record/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245140830,"owners_count":20567455,"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":["activerecord","supabase"],"created_at":"2024-10-13T22:20:48.570Z","updated_at":"2025-10-14T04:48:33.827Z","avatar_url":"https://github.com/joshnuss.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Supabase ActiveRecord\n----------------\n\nActiveRecord implementation for Supabase (experimental).\n\n## Features\n\n- CRUD\n- Validations\n- Relationships `belongsTo()`, `hasMany()`\n- Named scopes\n- Filtering\n- Joining\n- Isomorphic\n\n# Examples\n\n## Models\n\nA model is a class that inherits from `ActiveRecord`:\n\n```javascript\nimport { ActiveRecord } from 'supabase-active-record'\n\nclass Person extends ActiveRecord {\n  // define table name and fields\n  static config = {\n    table: 'people',\n    fields: {\n      id: 'serial',\n      firstName: 'string',\n      lastName: 'string'\n    }\n  }\n}\n```\n\n## Querying\n\nTo get all records:\n\n```javascript\nconst people = await Person.all()\n```\n\nTo find a single record:\n\n```javascript\nconst person = await Person.findBy({firstName: \"Josh\", lastName: \"Nussbaum\"})\n```\n\nTo find by `id`:\n\n```javascript\nconst person = await Person.find(41)\n```\n\n`ActiveRecord.find()` and `ActiveRecord.findBy()` return `null` when no record is found. If you perfer to raise an `NotFoundError`, use `ActiveRecord.get()` or  `ActiveRecord.getBy()`:\n\n```javascript\ntry {\n  const person = await Person.get(41)\n} catch (error) {\n  console.error(error.name) // RecordNotFound\n}\n```\n\n### Filters\n\nFilters can be added by chaining `.where()` calls together:\n\n```javascript\n// single filter\nawait Person.where({lastName: 'Jobs'})\n\n// multiple filters\nawait Person\n  .where({firstName: 'Steve', lastName: 'Jobs'})\n\n// equivalent\nawait Person\n  .where('firstName', 'Steve')\n  .where('lastName', 'Jobs')\n\n// equivalent, using operators\nawait Person\n  .where('firstName', '=', 'Steve')\n  .where('lastName', '=', 'Jobs')\n\n// supported operators: =, !=, \u003e, \u003c, \u003c=, \u003e=, like, ilike\nawait Product\n  .where('price', '\u003e', 100)\n  .where('name', 'like', '*shirt*')\n```\n\n### Ordering\n\nData can be ordered by one or more columns:\n\n```javascript\n// sort by column\nawait Product\n  .all()\n  .order('name')\n\n// sort by multiple columns\nawait Product\n  .all()\n  .order(['price', 'name'])\n\n// ascending and descending can be specified as `asc` and `desc`\nawait Product\n  .all()\n  .order({price: 'desc', name: 'asc'})\n```\n\n### Limiting\n\nTo limit the number of records returned:\n\n```javascript\nawait Product\n  .all()\n  .limit(10) // 10 records max\n```\n\n### Joining\n\n(not yet working)\n\nMultiple models can be joined together\n\n```javascript\nimport { ActiveRecord, belongsTo } from 'supabase-active-record'\n\nclass Product extends ActiveRecord {\n  static config = [\n    table: 'products',\n    fields: {\n      id: 'serial',\n      name: 'string',\n      category: belongsTo(Category)\n    }\n  ]\n}\n\nconst product = await Product\n  .find(1)\n  .join('category')\n\nconsole.log(product.category) // Category { name: 'T-Shirts' }\n```\n\n## Scopes\n\nNamed scopes can be defined using `static` functions:\n\n```javascript\nclass Product extends ActiveRecord {\n  static config = {\n    table: 'products'\n  }\n\n  static expensive() {\n    return this\n      .where('price', '\u003e', 100)\n      .order({price: 'desc'})\n      .limit(3)\n  }\n}\n```\n\nAnd then call the scope like this:\n\n```javascript\nconst products = await Product.expensive()\n```\n\n## Validation\n\nMultiple validations are supported. They are defined in `config.validate`:\n\n```javascript\nimport { ActiveRecord, is } from 'supabase-active-record'\n\nclass Product extends ActiveRecord {\n  static config = {\n    table: 'products',\n    fields: {\n      name: 'string',\n      price: 'number'\n    },\n    validates: {\n      name: is.required()\n      price: is.type('number')\n    }\n  }\n}\n```\n\nSupported validations: `is.required()`, `is.type()`, `is.length()`, `is.format()`\n\n### Custom validation\n\nA validation is a function that takes an `object` (the record) and returns a `string` (the error message).\n\n```javascript\nimport { ActiveRecord, is } from 'supabase-active-record'\n\nclass Product extends ActiveRecord {\n  static config = {\n    // ...\n    validates: {\n      name: record =\u003e {\n        if (record.name.length \u003c 3)\n          return 'is too short'\n      }\n    }\n  }\n}\n```\n\n## Creating\n\nTo create a single record:\n\n```javascript\nconst {valid, errors, record} = await Product.create({name: 'Shirt'})\n```\n\nor using an instance:\n\n```javascript\nconst product = new Product()\n\nproduct.name = 'Shirt'\nproduct.price = 100\n\nconst { valid, errors } = await product.save()\n```\n\nTo create multiple records at once:\n\n```javascript\nawait Product.create([\n  {name: 'Shirt', price: 20},\n  {name: 'Pants', price: 100}\n])\n```\n\n## Updating\n\nTo update a record, call `record.save()`:\n\n```javascript\n// get existing record\nconst product = await Product.find(1)\n\n// change record\nproduct.price++\n\n// save record\nconst {valid, errors} = await product.save()\n```\n\nor update multiple records at once:\n\n```javascript\n// update all records where price=1 to price=2\nawait Product\n  .where('price', '=', 1)\n  .update({price: 2})\n```\n\n## Deleting\n\nTo delete a record:\n\n```javascript\n// get existing record\nconst product = await Product.find(1)\n\n// delete it\nawait product.delete()\n```\n\nor delete multiple records at once:\n\n```javascript\n// delete all records where price \u003e 1000\nawait Product\n  .where('price', '\u003e', 1000)\n  .delete()\n```\n\n## Instance methods\n\nInstance methods, getters and setters can be added to the model:\n\n```javascript\nclass Person extends ActiveRecord {\n  // define a getter\n  get fullName() {\n    return `${this.firstName} ${this.lastName}`\n  }\n\n  // define a setter\n  set fullName(value) {\n    const [firstName, lastName] = value.split(' ')\n\n    this.firstName = firstName\n    this.lastName = lastName\n  }\n\n  // define a method to update name\n  anonymize() {\n    this.firstName = 'Anonymous'\n    this.lastName = 'Anonymous'\n  }\n}\n\nconst person = new Person({firstName: \"Steve\", lastName: \"Jobs\"})\nconsole.log(person.fullName) // Steve Jobs\nperson.anonymize()\nconsole.log(person.fullName) // Anonymous Anonymous\n```\n\n## Change Tracking\n\nWhen a record is changed, `record.isChanged` is set to `true`. There is also `record.isPersisted` which is the opposite of `record.isChanged`.\n\n```javascript\nconst product = new Product()\n\nconsole.log(product.isNewRecord) // true\nconsole.log(product.isChanged) // true\nconsole.log(product.isPersisted) // false (opposite of isChanged)\n\nawait product.save()\n\n// now it's no longer a new record or dirty\nconsole.log(product.isNewRecord) // false\nconsole.log(product.isChanged) // false\nconsole.log(product.isPersisted) // true\n```\n\n# Setup\n\nInstall the [npm package](https://www.npmjs.com/package/supabase-active-record):\n\n```bash\nyarn add supabase-active-record\n```\n\nSetup the supabase client:\n\n```js\nimport { createClient } from '@supabase/supabase-js'\nimport { ActiveRecord } from 'supabase-active-record'\n\nActiveRecord.client = createClient(\"\u003cyour supabase url\u003e\", \"\u003cyour supabase key\u003e\")\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoshnuss%2Fsupabase-active-record","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoshnuss%2Fsupabase-active-record","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoshnuss%2Fsupabase-active-record/lists"}