npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@yizhi/postgres

v1.0.4

Published

postgres数据库ORM

Downloads

122

Readme

yizhi-postgres

postgres数据库ORM

安装

npm install @yizhi/postgres

配置

使用database.config函数来为数据库提供配置


import { database } from '@yizhi/postgres';

database.config(()=>({
	host: "127.0.0.1",
	port: 5432,
	user: "postgres",
	password: "your_password",
	database: "your_database",
	max: 100,
}));

实体定义

实体需继承自BasicEntity类,使用@Table装饰器来定义表格,使用@XXXColumn来定义字段,使用@JoinOne来定义一对一和多对一关系,使用@JoinMany来定义一对多关系(多对多关系使用多对一和一对多来实现)。

import { StringColumn, IntColumn } from "@yizhi/database";

@Table("public.pets")
class Pet extends BasicEntity {
	@IntColumn({ primary: true })
	declare id: number;
	
	@StringColumn()
	declare name: string;

	@IntColumn()
	declare userID: number;

	@JoinOne(()=>User, "userID")
	declare user: User;
}

@Table("public.users")
class User extends BasicEntity {
	@IntColumn({ primary: true })
	declare id: number;

	@StringColumn()
	declare name: string;

	@JoinMany(()=>Pet, "userID")
	declare pets: Pet[];
}

可以使用Field函数来定义自定义列类型, 例如:

import { Field } from "@yizhi/database";
/**
 * 定义地址字段
 * @param name 字段名称
 */
export function AddressColumn(name?: string) {
	return Field(name, v => escapeValue(JSON.stringify(v)) + "::jsonb", v => new Address(v), false);
}

数据库操作

使用database.xxx来进行数据库操作,例如:

import { database } from '@yizhi/postgres';
import { User } from './models/User';

//添加数据
await database.insert(User).data({name:"张三"}).returning("id").query();		// [{id:1}]
//添加多条数据
await database.insert(User).data([
	{name:"李四"},
	{name:"王五"}
]).returning("id").query();		// [{id:2},{id:3}]

//修改数据
await database.update(User).set({name:"王麻子"}).where({id:3}).returning("id","name").query();		// [{id:3, name:"王麻子"}]

//删除数据
await database.delete(User).where({id:2}).returning("id").query();		// [{id:2}]

//查询数据
await database.select(User)
	.where({id:{gte:1}})							//查询条件 user.id>=1
	.join("pets", pet=>pet.where({id:{gte:2}}))		//一对多查询 user.pets.id>=2
	.find();		// [{id:1, name:"张三", pets:[{id:2, name:"小狗"}]}, {id:3, name:"王麻子", pets:[{id:3, name:"小猫"}}]

//事务
await database.transaction(async (database)=> {
	//事务内操作
});