|
4 | 4 |
|
5 | 5 | import boto3
|
6 | 6 | import redis
|
| 7 | +import sqlalchemy |
7 | 8 | from dagster import Field, Int, String, resource
|
8 | 9 |
|
9 | 10 |
|
| 11 | +class Postgres: |
| 12 | + def __init__(self, host: str, user: str, password: str, database: str): |
| 13 | + self.host = host |
| 14 | + self.user = user |
| 15 | + self.password = password |
| 16 | + self.database = database |
| 17 | + self._engine = sqlalchemy.create_engine(self.uri) |
| 18 | + |
| 19 | + @property |
| 20 | + def uri(self): |
| 21 | + return f"postgresql://{self.user}:{self.password}@{self.host}/{self.database}" |
| 22 | + |
| 23 | + def execute_query(self, query: str): |
| 24 | + self._engine.execute(query) |
| 25 | + |
| 26 | + |
10 | 27 | class S3:
|
11 | 28 | def __init__(self, bucket: str, access_key: str, secret_key: str, endpoint_url: str = None):
|
12 | 29 | self.bucket = bucket
|
@@ -39,6 +56,25 @@ def put_data(self, name: str, value: str):
|
39 | 56 | self.client.set(name, value)
|
40 | 57 |
|
41 | 58 |
|
| 59 | +@resource( |
| 60 | + config_schema={ |
| 61 | + "host": Field(String), |
| 62 | + "user": Field(String), |
| 63 | + "password": Field(String), |
| 64 | + "database": Field(String), |
| 65 | + }, |
| 66 | + description="A resource that can run Postgres", |
| 67 | +) |
| 68 | +def postgres_resource(context) -> Postgres: |
| 69 | + """This resource defines a Postgres client""" |
| 70 | + return Postgres( |
| 71 | + host=context.resource_config["host"], |
| 72 | + user=context.resource_config["user"], |
| 73 | + password=context.resource_config["password"], |
| 74 | + database=context.resource_config["database"], |
| 75 | + ) |
| 76 | + |
| 77 | + |
42 | 78 | @resource
|
43 | 79 | def mock_s3_resource(context):
|
44 | 80 | stocks = [
|
|
0 commit comments