Using PostgreSQL as an object database

As we all know, PostgreSQL is the most powerful open source database. Based on the relational type, it adds a lot of object-oriented functions, and the supported data types can be said to be all-encompassing. Extensible to meet user needs.

The array type in PostgreSQL can play a great role. With its support, we can use PostgreSQL as an object database.

For example we have the following object types:

class Book {
	String id;
	String title;
	List<Author> authors;
}

class Author {
	String id;
	String name;
	List<Book> books;
}

We can also create database tables with the same structure:

CREATE TABLE book {
	id character varying(32) NOT NULL PRIMARY KEY,
	title character varying(50) NOT NULL,
	authors character varying[] COMMENT 'ref: author.id',
}

CREATE TABLE author {
	id character varying(32) NOT NULL PRIMARY KEY,
	name character varying(20) NOT NULL,
	books character varying[] COMMENT 'ref: book.id',
}

how about it? Is it exactly the same as the object type!

In this way, we no longer need to do object-relational mapping.

We can save the object structure in memory to the database as it is. At this point, PostgreSQL becomes a true object database! (Don't forget, PostgreSQL tables also support inheritance!)

All data queries and acquisitions are based on the primary key index. Even if the amount of data in your table is large, it will be returned in milliseconds. And lazy loading can also be implemented. Only when you need the specific data of the sub-object, will you go to the database to query and get it, otherwise only returning an ID is enough.

Isn't this way of data storage much better than that of MongoDB!

{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324085209&siteId=291194637