Skip to content

Commit a1021ae

Browse files
committed
Database Setup in readme
1 parent d271f64 commit a1021ae

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,47 @@ Then:
145145
require "lustra"
146146
```
147147

148+
### Database Setup
149+
150+
Initialize your database connection:
151+
152+
```crystal
153+
Lustra::SQL.init("postgres://user:password@localhost/database_name")
154+
```
155+
156+
For multiple database connections:
157+
158+
```crystal
159+
Lustra::SQL.init("readonly", "postgres://user:password@localhost/readonly_db")
160+
Lustra::SQL.init("primary", "postgres://user:password@localhost/primary_db")
161+
```
162+
163+
**Using specific connections:**
164+
165+
At the model level:
166+
167+
```crystal
168+
class ReadOnlyModel
169+
include Lustra::Model
170+
171+
self.connection = "readonly" # All queries use readonly connection
172+
173+
column id : Int64, primary: true
174+
column data : String
175+
end
176+
```
177+
178+
At the query level:
179+
180+
```crystal
181+
# For model queries
182+
User.query.use_connection("readonly").where { active == true }
183+
184+
# For raw SQL queries
185+
Lustra::SQL.execute("readonly", "SELECT * FROM users")
186+
Lustra::SQL.select.from("users").where { active == true }.use_connection("readonly").to_a
187+
```
188+
148189
### Model definition
149190

150191
Lustra offers some mixins, just include them in your classes:

0 commit comments

Comments
 (0)