{"id":13412169,"url":"https://github.com/pupizoid/ormlite","last_synced_at":"2026-03-12T12:05:11.481Z","repository":{"id":57494617,"uuid":"139023745","full_name":"pupizoid/ormlite","owner":"pupizoid","description":"Lightweight package containing some ORM-like features and helpers for sqlite databases.","archived":false,"fork":false,"pushed_at":"2023-01-30T09:54:24.000Z","size":171,"stargazers_count":13,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-07-31T20:49:56.780Z","etag":null,"topics":["orm","sql","sqlite3"],"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/pupizoid.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":"2018-06-28T13:42:09.000Z","updated_at":"2024-03-06T20:01:55.000Z","dependencies_parsed_at":"2023-01-30T18:10:18.401Z","dependency_job_id":null,"html_url":"https://github.com/pupizoid/ormlite","commit_stats":null,"previous_names":[],"tags_count":67,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pupizoid%2Formlite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pupizoid%2Formlite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pupizoid%2Formlite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pupizoid%2Formlite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pupizoid","download_url":"https://codeload.github.com/pupizoid/ormlite/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243624995,"owners_count":20321210,"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":["orm","sql","sqlite3"],"created_at":"2024-07-30T20:01:21.744Z","updated_at":"2025-12-15T23:10:19.941Z","avatar_url":"https://github.com/pupizoid.png","language":"Go","readme":"# ormlite\nLightweight package implementing some ORM-like features and helpers for sqlite databases.\n\n[![Build Status](https://app.travis-ci.com/pupizoid/ormlite.svg?branch=master)](https://app.travis-ci.com/pupizoid/ormlite)\n[![codecov](https://codecov.io/gh/pupizoid/ormlite/branch/master/graph/badge.svg?token=44ngQeqfvl)](https://codecov.io/gh/pupizoid/ormlite)\n[![GoDoc](https://godoc.org/github.com/pupizoid/ormlite?status.svg)](https://godoc.org/github.com/pupizoid/ormlite)\n[![Go Report Card](https://goreportcard.com/badge/github.com/pupizoid/ormlite)](https://goreportcard.com/report/github.com/pupizoid/ormlite)\n[![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/avelino/awesome-go)\n\n## Model\n```go\ntype Model interface {\n    Table() string\n}\n```\nThis package operates models which are described by `Model` interface. We call any entry a model if it's a struct and has a table where data is stored.\n\n## CRUD\nThis package provides a bunch of functions to allow you create, read, update and delete data.\n  \n### QueryStruct\nLoads data from table and scans it into provided struct. If query was too broad to load more than one rows, the latest of them will be scanned. Also this function supports loading relations which will be described below.\n\n```go\ntype SimpleStruct struct {\n  IntField int64 `ormlite:\"col=rowid,primary\"`\n  Text string\n  UnusedField bool `ormlite:\"-\"\n}\n\nvar s SimpleStruct\nerr := QueryStruct(db, \"\", nil, \u0026s)\n```\n\nLet's describe some tags used in example struct:\n- `col` - let you specify custom column name to be scanned to the field\n- `primary` - indicates model primary key, it's basically used when saving model\n- `-` - hide field for package so it won't be affected at any kind\n\n### QuerySlice\nThis is very similar to QueryStruct except that it loads multiple rows in a slice.\n\n### Upsert\nThis function is used to save or update existing model, if model has `primary` field and it's value is zero - this model will be inserted to the model's table. Otherwise model's row will be updated according it's current values (except `has-one` relation). This function also supports updating related models except creating or editing `many-to-many` related models.\n```go\nerr := Upsert(db, \u0026s)\n```\n\n### Insert \nFunction used for inserting Models. Despite of `Upsert` it returns an error in case of constraint errors. \n\n### Delete\nThis function... yea, it deletes model from database using it's primary key value. If model does not have primary key or it has zero value an error will ne returned.\nSince sometimes it's useful to know that delete operation is really took place in database, function will check number of affected rows and return a special `ErrNoRowsAffected`\nif it's not positive.\n\n## Options\n\n```go\ntype Options struct {\n   // Add where clause to query\n   Where         Where    \n   Limit         int      \n   Offset        int      \n   OrderBy       *OrderBy \n   // Load relations to specified depth,\n   // if depth is 0 don't load any relations\n   RelationDepth int      \n}\n```\n\nFor most queries is't enough to use `DefaultOptions()` which has relation depth equal to 1. \n\nIf you already have variable containing Options, you can extend them with additional settings with following functions:\n- WithLimit\n- WithOffset\n- WithOrder\n- WithWhere\n\nFor example:\n\n```go\nopts := ormlite.WithWhere(ormlite.DefaultOptions(), ormlite.Where{\"id\": 1})\n```\n\n## Relations\n\nQueryStruct, QuerySlice and Upsert support loading relations between models, the supported relation types are:\n- Has One\n- Has Many\n- Many To Many\n\nSince you can control depth of loaded relations, there is no need to be afraid of cycle loading. But there are several tags to configure relations.\n\n### Has One\n\n```go\ntype Model struct {\n   Related ormlite.Model `ormlite:\"has_one,col=related_model_id\"`\n}\n```\n\n`has_one` indicates that this field represents has one relations type to other model.\n\n`col` is an optional parameter to specify custom column name of foreign id of related model.\n\n### Has Many\n\n```go\ntype Model struct {\n   Related []ormlite.Model `ormlite:\"has_many\"`\n}\n```\n\n`has_many` is the only parameter to indicate has many relation, however there is a requirement that related model must have `primary`\n field.\n \n ### Many To Many\n \n ```go\ntype Model struct {\n   Related       []ormlite.Model `ormlite:\"many_to_many,table=mapping_table,field=model_id\"`\n   RelatedActive []ormlite.Model `ormlite:\"many_to_many,table=mapping_table(active=1),field=model_id\"`\n}\n```\n\n`many_to_many` indicates that field represents many-to-many relation.\n\n`table(additional condition)` should contain mapping table name to retrieve relation information. If it's necessary to map entities with additional conditions you can specify sql describing them in brackets. For now only one additional field is supported.\n\n`field` should specify column in mapping table that has foreign key of original model\n\nAlso there is a requirement to related model primary key field to contain `ref` setting that specifies column name of it's foreign key in mapping table.\n\n### Search by related\n\nSometimes it's useful to search many-to-many model by related ones, so running the following code\n\n```go\ntype Author struct {\n    Id     int      `ormlite:\"primary,ref=author_id\"`\n    Topics []*Topic `ormlite:\"many_to_many,table=author_topics,field=author_id\"`\n    Name   string\n}\n\nfunc (a *Author) Table() string { return \"authors\" }\n\ntype Topic struct {\n    Id      int       `ormlite:\"primary,ref=topic_id\"`\n    Authors []*Author `ormlite:\"many_to_many,table=author_topics,field=topic_id\"`\n    Content string\n}\n\nfunc (p *Topic) Table() string { return \"topics\" }\n\nfunc main() {\n    db, err := sql.Open(\"sqlite3\", \":memory:?_fk=1\")\n    if err != nil {\n        panic(err)\n    }\n\n    _, err = db.Exec(`\n       create table authors(id integer primary key, name text);\n       create table topics(id integer primary key, content text, author_id int);\n       create table author_topics(author_id integer, topic_id integer);\n    `)\n    if err != nil {\n        panic(err)\n    }\n\n    john := \u0026Author{Name: \"John\"}\n    err = ormlite.Upsert(db, john)\n    if err != nil {\n        panic(err)\n    }\n\n    pete := \u0026Author{Name: \"Pete\"}\n    err = ormlite.Upsert(db, pete)\n    if err != nil {\n        panic(err)\n    }\n\n    cars := \u0026Topic{Content: \"Cars\", Authors: []*Author{john, pete}}\n    err = ormlite.Upsert(db, cars)\n    if err != nil {\n        panic(err)\n    }\n\n    bikes := \u0026Topic{Content: \"Bikes\", Authors: []*Author{john}}\n    err = ormlite.Upsert(db, bikes)\n    if err != nil {\n        panic(err)\n    }\n\n    planes := \u0026Topic{Content: \"Plains\", Authors: []*Author{pete}}\n    err = ormlite.Upsert(db, planes)\n    if err != nil {\n        panic(err)\n    }\n\n    var carAuthors []*Author\n    err = ormlite.QuerySlice(db, \u0026ormlite.Options{RelatedTo: []ormlite.IModel{\u0026Topic{Id: cars.Id}}}, \u0026carAuthors)\n    if err != nil {\n        panic(err)\n    }\n\n    var planeAuthors []*Author\n    err = ormlite.QuerySlice(db, \u0026ormlite.Options{RelatedTo: []ormlite.IModel{\u0026Topic{Id: planes.Id}}}, \u0026planeAuthors)\n    if err != nil {\n        panic(err)\n    }\n\n    fmt.Print(\"Car authors: \")\n    for _, a := range carAuthors {\n        fmt.Printf(\"%s \", a.Name)\n    }\n    fmt.Print(\"\\n\")\n\n    fmt.Print(\"Plane authors: \")\n    for _, a := range planeAuthors {\n        fmt.Printf(\"%s \", a.Name)\n    }\n```\nwill result\n```\nCar authors: John Pete \nPlane authors: Pete \n```\n### Comparison operators\n\nBy default package use `=` operator to compare values introduced in `Where` struct, except strings, they are compared by `LIKE` operator. But there is a list of other operators that you can use:\n\n- `Greater` stands for `\u003e`\n- `GreaterOrEqual` stands for `\u003e=`\n- `Less` stands for `\u003c`\n- `LessOrEqual` stands for `\u003c=`\n- `NotEqual` stands for `!=`\n- `BitwiseAND` stands for `value\u0026? \u003e 0`\n- `BitwiseANDStrict` stand for `value\u0026? = 0`\n- `StrictString` - by default string comparison are done using `LIKE` operator, `StrictString` will force using `=`\n \nTo use these operators just wrap value with them\n\n```go\nopts := \u0026ormlite.Options{Where: {\"Age\": GreaterOrEqual(10)}}\n```\n\n### More Examples\n\nSee tests.\n","funding_links":[],"categories":["Database","数据库","数据库  `go语言实现的数据库`","Data Integration Frameworks","Generators","Uncategorized"],"sub_categories":["SQL Query Builders","SQL 查询语句构建库","Advanced Console UIs","SQL查询生成器"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpupizoid%2Formlite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpupizoid%2Formlite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpupizoid%2Formlite/lists"}