Skip to content

Commit f927031

Browse files
committed
update
Signed-off-by: George Lemon <georgelemon@protonmail.com>
1 parent d693ae3 commit f927031

1 file changed

Lines changed: 57 additions & 22 deletions

File tree

src/greskewel.nim

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ type
2727
# https://github.com/fergusstrange/embedded-postgres/blob/master/config.go#L11
2828

2929
PostgresBoxConfig* = ref object
30-
version: PostgresVersion
30+
version: PostgresVersion = PostgresVersion.v16
3131
# The version of Postgres to use. You can specify a specific
3232
# version like "16.4.0" or use a predefined version
3333
# from the PostgresVersion enum.
3434
port*: Port = Port(5432)
3535
# Default port for Postgres is 5432, but you
3636
# can specify a different one if needed.
37-
database: string
37+
database: string = "postgres"
3838
# Name of the default database to create when Postgres starts.
39-
username: string
39+
username: string = "postgres"
4040
# Username for the default database. Default is "postgres".
41-
password: string
41+
password: string = "postgres"
4242
# Password for the default database user. Default is "postgres".
4343
basePath*: string
4444
# Base path for all Postgres-related files. This is the root
@@ -61,11 +61,11 @@ type
6161
startParameters: Table[string, string]
6262
# Additional parameters to pass when starting Postgres,
6363
# such as shared_buffers, max_connections, etc.
64-
binaryRepositoryURL: string
64+
binaryRepositoryURL: string = "https://repo1.maven.org/maven2/"
6565
# URL to download Postgres binaries. Default is the Maven Central repository.
6666
startTimeout: Duration
6767
# Timeout for starting the Postgres server. Default is 30 seconds.
68-
logger: string # todo
68+
# logger: string # todo
6969

7070
EmbeddedPostgres* = object
7171
config: PostgresBoxConfig
@@ -75,25 +75,14 @@ type
7575

7676
var greskewChan = newChan[string]()
7777

78-
proc getDefaultConfig*(version: PostgresVersion): PostgresBoxConfig =
79-
## Returns a default configuration for the specified Postgres version.
80-
result = PostgresBoxConfig(
81-
version: PostgresVersion.v16,
82-
database: "postgres",
83-
username: "postgres",
84-
password: "postgres",
85-
locale: "en_US.UTF-8",
86-
startParameters: initTable[string, string](),
87-
dataPath: "data",
88-
binaryRepositoryURL: "https://repo1.maven.org/maven2/",
89-
startTimeout: initDuration(seconds = 30)
90-
)
91-
92-
proc initGreskewel*(config: PostgresBoxConfig = nil): EmbeddedPostgres =
78+
proc initGreskewel*(config: PostgresBoxConfig = nil,
79+
version: PostgresVersion.v16): EmbeddedPostgres =
80+
## Initializes the embedded Postgres server with the specified configuration.
81+
## If no configuration is provided, it uses the default configuration for the specified version.
9382
result = EmbeddedPostgres(
9483
config:
9584
if config != nil: config
96-
else: getDefaultConfig(PostgresVersion.v16),
85+
else: PostgresBoxConfig(version: version)
9786
)
9887
if result.config.binaryRepositoryURL.len == 0:
9988
result.config.binaryRepositoryURL = "https://repo1.maven.org/maven2/"
@@ -242,3 +231,49 @@ proc getVersion*(ep: EmbeddedPostgres): PostgresVersion =
242231
proc getConfig*(ep: EmbeddedPostgres): PostgresBoxConfig =
243232
## Get the configuration of the embedded Postgres server
244233
result = ep.config
234+
235+
236+
# {.passL:"./bin/darwin/lib/libpq.dylib".}
237+
238+
# include pkg/db_connector/db_postgres
239+
# let db = open("localhost", "georgelemon", "postgres", "postgres")
240+
241+
# db.exec(sql("""CREATE TABLE myTable (id integer, name varchar(50) not null)"""))
242+
# db.exec(sql"""INSERT INTO myTable (id, name) VALUES (1, 'Alice')""")
243+
244+
# for row in db.getAllRows(sql"""SELECT * FROM myTable"""):
245+
# echo "Row: ", row
246+
247+
when isMainModule:
248+
# Initialize the embedded Postgres server with the default configuration.
249+
var greskew = initGreskewel(
250+
PostgresBoxConfig(
251+
basePath: getCurrentDir() / "greskewelbox",
252+
)
253+
)
254+
255+
# Download the Postgres binaries for the specified version
256+
# and store them in the configured path.
257+
greskew.downloadBinaries()
258+
259+
# Initialize the Postgres server
260+
# This will set up the data directory and prepare
261+
# the server for starting.
262+
greskew.init()
263+
264+
# Start the Postgres server
265+
# This proc will run in a separate thread
266+
# to avoid blocking the main thread.
267+
greskew.start()
268+
269+
270+
import pkg/db_connector/db_postgres
271+
let db = open("localhost", "postgres", "postgres", "postgres")
272+
db.exec(sql"""CREATE TABLE myTable (id integer, name varchar(50) not null)""")
273+
db.exec(sql"""INSERT INTO myTable (id, name) VALUES (1, 'Alice')""")
274+
for row in db.getAllRows(sql"""SELECT * FROM myTable"""):
275+
echo "Row: ", row
276+
277+
greskew.stop()
278+
279+
sleep(2000) # wait a bit for the server to stop before exiting

0 commit comments

Comments
 (0)