Giter Site home page Giter Site logo

orm's Introduction

ORM (WIP)

Getting Started

// CREATE TABLE people (id SERIAL PRIMARY KEY, name TEXT, age INT);
// CREATE TABLE friends (id SERIAL, person_a_id INT REFERENCES people(id), person_b_id INT REFERENCES people(id));
// INSERT INTO people (id, name, age) VALUES (1, 'bob', 24);
// INSERT INTO people (id, name, age) VALUES (2, 'jim', 28);
// INSERT INTO friends (person_a_id, person_b_id) VALUES (1, 2);

package main

import (
	"context"
	"fmt"

	"github.com/jackc/pgx/v5"
	"github.com/linden/orm"
)

type Person struct {
	ID   int    `orm:"id"`
	Name string `orm:"name"`
}

type Friend struct {
	ID      int    `orm:"id"`
	PersonA Person `orm_foreign:"people,id,person_a_id"`
	PersonB Person `orm_foreign:"people,id,person_b_id"`
}

func main() {
	connection, err := pgx.Connect(context.TODO(), "postgres://postgres:postgres@localhost:5432/postgres")

	if err != nil {
		panic(err)
	}

	defer connection.Close(context.TODO())

	var person Person

	err = orm.ScanRow(context.TODO(), connection, &person, "people", "WHERE age = $1", 24)

	if err != nil {
		panic(err)
	}

	fmt.Printf("person: %+v\n", person)

	var people []Person

	err = orm.Scan(context.TODO(), connection, &people, "people")

	if err != nil {
		panic(err)
	}

	fmt.Printf("people: %+v\n", people)

	var friend Friend

	err = orm.ScanRow(context.TODO(), connection, &friend, "friends")

	if err != nil {
		panic(err)
	}

	fmt.Printf("friend: %+v\n", friend)
}

orm's People

Contributors

linden avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.