-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc81913
commit e7af53a
Showing
4 changed files
with
78 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
# Table of contents | ||
|
||
* [Home](README.md) | ||
* [Overview](README.md) | ||
|
||
## Getting Started | ||
|
||
* [Preamble](getting-started/preamble.md) | ||
* [Connect to a Database](getting-started/connect-to-a-database.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Connect to a Database | ||
|
||
To start simple, create a PostgreSQL database and several tables there. You can also use you existing database. | ||
|
||
```sql | ||
$ export PGUSER=postgres | ||
$ export PGPASSWORD=postgres | ||
$ createdb mytest | ||
|
||
$ psql mytest | ||
% CREATE TABLE topic( | ||
id bigserial PRIMARY KEY, | ||
creator_id bigint NOT NULL, | ||
subject text NOT NULL | ||
); | ||
% CREATE TABLE comment( | ||
id bigserial PRIMARY KEY, | ||
topic_id bigint REFERENCES topic, | ||
creator_id bigint NOT NULL, | ||
message text NOT NULL | ||
); | ||
``` | ||
|
||
To access that database, you need to create an instance of Cluster: | ||
|
||
{% code title="core/ent.ts" fullWidth="false" %} | ||
```typescript | ||
import { Cluster } from "ent-framework"; | ||
import { PgClientPool } from "ent-framework/pg"; | ||
import type { PoolConfig } from "pg"; | ||
|
||
export const cluster = new Cluster({ | ||
islands: () => [{ | ||
no: 0, | ||
nodes: [{ | ||
host: "localhost", | ||
port: 5432, | ||
user: "postgres", | ||
password: "postgres", | ||
database: "mytest", | ||
min: 5, | ||
max: 20, | ||
}] | ||
}], | ||
createClient: (node: PoolConfig) => new PgClientPool({ config: node }), | ||
loggers: { | ||
clientQueryLogger: (props) => console.debug(props), | ||
swallowedErrorLogger: (props) => console.log(props), | ||
}, | ||
}); | ||
|
||
// Pre-open min number of DB connections. | ||
setTimeout(() => cluster.prewarm(), 100); | ||
``` | ||
{% endcode %} | ||
|
||
Terminology: | ||
|
||
1. **Cluster** consists of **Islands**. Each Island has a number. | ||
2. Island consists of master + replica **nodes** (in the above example, we only define one master node and no replicas).  | ||
3. Island also hosts **Microshards** (in the example above, we will have no microshards, aka just 1 microshard). | ||
|
||
Notice that we define the map of the cluster using a callback. Ent Framework will call it from time to time to refresh the view of the cluster, so in this callback, you can read the data from some configuration database. This is called "dynamic real-time reconfiguration". | ||
|
||
[PgClientPool](../../docs/classes/PgClientPool.md) class accepts several options, one of them is the standard [node-postgtes PoolConfig](https://node-postgres.com/apis/pool) interface. For simplicity, when we define a cluster shape in `islands`, we can return a list of such configs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Preamble | ||
|
||
Below, we'll show some Ent Framework usage examples. | ||
|
||
We will progress from the simplest code snippets to more and more advanced topics, like custom ID schemas, privacy rules, master-replica and failover, microsharding, cross-shard foreign keys etc. |