https://github.com/monade/any_query
An ORM for any data source (SQL, CSV, TSV, REST API)
https://github.com/monade/any_query
Last synced: 10 days ago
JSON representation
An ORM for any data source (SQL, CSV, TSV, REST API)
- Host: GitHub
- URL: https://github.com/monade/any_query
- Owner: monade
- License: mit
- Created: 2023-05-25T09:50:03.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-09-29T09:46:26.000Z (over 2 years ago)
- Last Synced: 2025-04-25T13:04:13.251Z (about 1 year ago)
- Language: Ruby
- Size: 27.3 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://badge.fury.io/rb/any_query)
# any_query
`any_query` is a versatile ORM designed to interface with various data sources including SQL, CSV, TSV, Fixed Length Text Format, and REST APIs. With any_query, you can write SQL-like queries and receive results as ActiveRecord-like objects. It supports operations like `select`, `limit`, and even `joins` across different data sources.
## Features
* Unified Query Language: Write SQL-like queries for any supported data source.
* ActiveRecord-like Objects: Get results in a familiar format.
* Cross-data Source Joins: Combine data from different sources seamlessly.
## Installation
To install `any_query`, add the gem to your Gemfile:
```ruby
gem 'any_query'
```
Then run:
```bash
bundle install
```
## Usage
### Setting up Models
To use `any_query`, include it in your model and set up the necessary adapters:
```ruby
class Invoice
include AnyQuery
adapter :http do
url 'https://your-api.dev'
primary_key :id
endpoint :list, :get, '/v2/invoices',
pagination: { type: :page, params: { per: 'pageSize', number: 'skippages' } },
wrapper: :collection,
default_params: {
query: { pageSize: 1000 },
headers: {
'Content-Type': 'application/json'
}
}
endpoint :show, :get, '/v2/invoices{id}',
default_params: {
headers: {
'Content-Type': 'application/json'
}
}
end
end
class Customer
include AnyQuery
adapter :http do
url 'https://your-api.dev'
primary_key :id
endpoint :list, :get, '/customers/',
pagination: { type: :page, params: { per: 'pageSize', cursor: 'cursor', number: 'skippages' } },
wrapper: :collection,
default_params: {
query: { pageSize: 1000 },
headers: {
'Content-Type': 'application/json'
}
}
endpoint :show, :get, '/customers/{id}',
default_params: {
headers: {
'Content-Type': 'application/json'
}
}
end
end
```
### Writing Queries
With your models set up, you can now write queries:
```ruby
Invoice
.joins(Customer, :customerNumber, %i[customer customerNumber], into: :customer, strategy: :full_scan)
.select(
:number, :date, %i[customer name], %i[customer customerNumber], :netAmount
# :dueDate
).to_a
```
### Standalone Mode
You can also use `any_query` without models by creating a client directly:
```ruby
sql_client = AnyQuery::Client.new(
adapter: :sql,
params: {
url: 'psql://user:password@localhost:5432/dbname',
primary_key: :id,
table: 'users'
}
)
csv_client = AnyQuery::Client.new(
adapter: :csv,
params: {
url: 'path/to/file.csv',
primary_key: :id,
fields: {
id: { type: :integer },
user_id: { type: :integer },
title: { type: :string },
body: { type: :string },
status: { type: :integer },
created_at: { type: :datetime, format: '%Y-%m-%d %H:%M:%S' }
}
}
)
# Querying
csv_client
.joins(sql_client, :user_id, :id, into: :user, strategy: :full_scan)
.select(:number, :date, :customerNumber, :netAmount)
.to_a
```
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
About Monade
----------------

any_query is maintained by [mònade srl](https://monade.io/en/home-en/).
We <3 open source software. [Contact us](https://monade.io/en/contact-us/) for your next project!