{"id":34231714,"url":"https://github.com/knobz-io/memdb","last_synced_at":"2026-03-10T08:33:33.056Z","repository":{"id":65451040,"uuid":"592586858","full_name":"knobz-io/memdb","owner":"knobz-io","description":"Easy-to-use in-memory database for Go","archived":false,"fork":false,"pushed_at":"2023-02-05T22:58:27.000Z","size":31,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-06-20T22:33:42.490Z","etag":null,"topics":["database","generics","go","in-memory"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/knobz-io.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2023-01-24T03:45:47.000Z","updated_at":"2024-06-20T22:33:42.491Z","dependencies_parsed_at":"2023-02-13T18:01:01.025Z","dependency_job_id":null,"html_url":"https://github.com/knobz-io/memdb","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/knobz-io/memdb","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knobz-io%2Fmemdb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knobz-io%2Fmemdb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knobz-io%2Fmemdb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knobz-io%2Fmemdb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/knobz-io","download_url":"https://codeload.github.com/knobz-io/memdb/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knobz-io%2Fmemdb/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30328251,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-10T05:25:20.737Z","status":"ssl_error","status_checked_at":"2026-03-10T05:25:17.430Z","response_time":106,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["database","generics","go","in-memory"],"created_at":"2025-12-16T01:20:09.136Z","updated_at":"2026-03-10T08:33:32.668Z","avatar_url":"https://github.com/knobz-io.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# memdb\n\nmemdb is an easy-to-use in-memory database for Go programming language. It provides a simple and efficient way to store and retrieve data in memory, with features such as type safety, transactions, rich indexing, and sorting.\n\n## Features\n\n* **Type Safety** - memdb uses generics to provide strong type safety, reducing issues that can be found at compile time. This ensures that the data you are working with is of the correct type, and minimizes the chance of runtime errors.\n* **Transactions** - Transactions in memdb can span multiple tables, and are applied atomically. This means that all changes made within a transaction are either applied together or not at all, ensuring data consistency and integrity.\n* **Rich Indexing** - memdb supports single and compound indexes, allowing you to filter and sort data based on multiple properties. This makes it easy to retrieve specific data from the database quickly and efficiently.\n* **Sorting** - memdb allows you to sort query results by different entry properties, making it easy to organize and present data in a specific order.\n\n## Project status\n\nCurrently, this library is not stable and **should not be used for production**. It is still under development and may contain bugs or unfinished features. However, it is open for contributions, bugs reporting and feature requests.\n\n## Installation\n\nTo install memdb, you can use the go get command:\n\n```sh\ngo get github.com/knobz-io/memdb\n```\n\nThen, you can import the package in your Go code:\n\n```go\nimport \"github.com/knobz-io/memdb\"\n```\n\n## Usage\n\nTo start using memdb, you'll need to first create a table schema for your data models using the `memdb.Table[V]` struct. This schema will define the structure of your data, and specify any indexes that you want to create on it. Once the table schema is created, you can use it to initialize a new `*memdb.DB` instance.\n\n\n### Creating table schema\n\nIn order to use memdb, you'll need to define your data models in Go. These will be the types that you'll store in the database. For example, you could have a `User` struct like this:\n\n```go\ntype Status int\n\nconst (\n    Active Status = iota\n    Suspended\n    Banned\n)\n\ntype User struct {\n    ID       int\n    Status   Status\n    Email    string\n    FullName string\n}\n```\n\nOnce you have your data models defined, you can create a table schema for them using the `memdb.Table[V]` struct. The schema will specify how the data is stored in the database, and will define any indexes that you want to create on it. For example, you could create a `UserTable` schema like this::\n\n```go\n\ntype UserTable struct {\n\tmemdb.Table[*User]\n\t// indexes\n\tstatus   *memdb.IntIndex[*User]\n\temail    *memdb.StringIndex[*User]\n\tfullName *memdb.StringIndex[*User]\n}\n\n// ID is a helper function for preparing\n// user primary key from int\nfunc (UserTable) ID(id int) memdb.Key {\n\treturn memdb.IntKey(id)\n}\n\nfunc makeUserTable() UserTable {\n\ttable := memdb.NewTable(func(usr *User) memdb.Key {\n\t\treturn memdb.IntKey(usr.ID)\n\t})\n\t// preparing indexes\n\ttable, status := table.IndexInt(func(usr *User) int {\n\t\treturn int(usr.Status)\n\t})\n\ttable, email := table.IndexString(func(usr *User) string {\n\t\treturn usr.Email\n\t})\n\ttable, fullName := table.IndexString(func(usr *User) string {\n\t\treturn usr.FullName\n\t})\n\treturn UserTable{\n\t\tTable:    table,\n\t\tstatus:   status,\n\t\temail:    email,\n\t\tfullName: fullName,\n\t}\n}\n```\n\n### Initializing database\n\nOnce you have created a table schema, you can use it to initialize a new `*memdb.DB` instance. The `Init` function takes a variable number of table schemas as arguments, allowing you to create multiple tables in a single database.\n\n```go\nusers := makeUserTable()\ndb, err := memdb.Init(users)\n```\n\n### Inserting data\n\nTo insert data into the database, you'll need to start a write transaction using the `db.WriteTx()` method. Once you have a transaction, you can use the `Set` or `SetMulti` method on the table schema to insert one or multiple entries into the table.\n\n```go\n// start a write transaction\ntx := db.WriteTx()\n\n// insert one entry\nusers.Set(tx, \u0026User{\n    ID:       1,\n    Status:   Active,\n    Email:    \"john.doe@example.com\",\n    FullName: \"John Doe\",\n})\n\n// or insert multiple entires\nusers.SetMulti(tx, []*User{\n    {\n        ID:       2,\n        Status:   Active,\n        Email:    \"david.rich@example.com\",\n        FullName: \"David Rich\",\n    },\n    {\n        ID:       3,\n        Status:   Suspended,\n        Email:    \"matt.smith@example.com\",\n        FullName: \"Matt Smith\",\n    },\n    {\n        ID:       4,\n        Status:   Active,\n        Email:    \"jack.thompson@example.com\",\n        FullName: \"Jack Thompson\",\n    },\n})\n\n// commit changes\ntx.Commit()\n```\n\n### Deleting data\n\nTo delete data from the database, you'll need to start a write transaction using the `db.WriteTx()` method. Once you have a transaction, you can use the `Del` or `DelMulti` method on the table schema to delete one or multiple entries from the table.\n\n```go\n// start a write transaction\ntx := db.WriteTx()\n\n// delete single entry where ID=1\nerr := users.Del(tx, users.ID(1))\n\n// or delete multiple entries where ID=1, ID=2\nerr := users.DelMulti(tx, []memdb.Key{\n    users.ID(1),\n    users.ID(2),\n})\n\n// commit changes\ntx.Commit()\n```\n\n### Retrieving single entry\n\nTo retrieve a specific entry from the table using its primary key, you'll need to start a read-only transaction using the `db.ReadTx()` method. Once you have a transaction, you can use the `Get` method on the table schema to retrieve the entry.\n\n```go\n// start read-only transaction\ntx := db.ReadTx()\n// retrieving entry with ID=3\nusr, err := users.Get(tx, users.ID(3))\nif err == memdb.ErrNotFound {\n    // handle not found\n} else if err != nil {\n    panic(err)\n}\n```\n\n### Retrieving multiple entries\n\nThe `Select` method on the table schema can be used to retrieve multiple entries from the table. The returned value is a query object that can be used to filter and sort the results.\n\n```go\n// start read-only transaction\ntx := db.ReadTx()\n\n// retrieve all entries\nlist, err := users.Select(tx).All()\n```\n\nThis will retrieve all entries from the table, without any filtering or sorting.\n\nYou can also use the `Page` method to retrieve a specific page of entries from the table.\n\n```go\n// return limited number of entries\n// from the speciffic point\nlimit := 10\noffset := 5\nlist, err := users.Select(tx).Page(limit, offset)\n```\n\nThis will retrieve a specific page of the entries with `limit` number of entries starting from `offset`.\n\nFor iterating over the table entries one by one, you can create a cursor and iterate over it.\n\n```go\n// create new cursor\nc, err := users.Select(tx).Cursor()\nif err != nil {\n    panic(err)\n}\n// iterate over table entries\nfor usr, ok := c.First(); ok; usr, ok = c.Next() {\n    fmt.Println(\"id:\", usr.ID, \"email:\", usr.Email)\n}\n```\nThis will allow you to retrieve entries one by one and do something with them. This can be useful for large tables that you don't want to load into memory all at once.\n\nPlease note that, these examples are for retriving all the entries, if you want to filter the entries based on certain condition, you can use the `Where` method on the query object.\n\n### Filtering\n\nTo retrieve a set of entries from the table based on certain conditions, you can use the `Select` method on the table schema to create a query, and then use various filter methods to specify the conditions.\n\n```go\n// start read-only transaction\ntx := db.ReadTx()\n// filter all entries where Status=Active\nlist, err := users.Select(tx).\n    Where(\n        users.status.Is(int(Active)),\n    ).\n    All()\n```\n\n### Sorting\n\nTo retrieve a set of entries from the table and sort them based on certain properties, you can use the `Select` method on the table schema to create a query, and then use various sort methods to specify the sorting order.\n\n```go\n// start read-only transaction\ntx := db.ReadTx()\n// return a list of all users sorted all users\n// sorted by FullName property in ascending order\nlist, err := users.Select(tx).\n    OrderBy(users.fullName.Asc()).\n    All()\n```\n\nIn this example, the `OrderBy` method is used to sort the query results by the `FullName` property in ascending order. The `Asc()` method is used to specify the sorting order.\n\nYou can chain multiple sorting conditions by adding them to `OrderBy` method.\n\n```go\nlist, err := users.Select(tx).\n    OrderBy(\n        users.fullName.Asc(),\n        users.email.Desc(),\n    ).\n    All()\n```\n\nThis will sort the query results by the `FullName` property in ascending order, then if two or more entries have the same `FullName` it will sort them by the `Email` property in descending order.\n\nFor more information on how to use memdb, please refer to the [Godoc](https://pkg.go.dev/github.com/knobz-io/memdb).\n\n## Contributing\n\nIf you're interested in contributing to memdb, please read the [contributing guidelines](CONTRIBUTING.md) before submitting a pull request.\n\n## License\n\nmemdb is licensed under the [MIT License](LICENSE).\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fknobz-io%2Fmemdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fknobz-io%2Fmemdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fknobz-io%2Fmemdb/lists"}