Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
willgriffin committed Feb 18, 2024
0 parents commit 9c733ac
Show file tree
Hide file tree
Showing 16 changed files with 2,205 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Release
on:
push:
branches:
- master
jobs:
release-please:
runs-on: ubuntu-latest
steps:
- uses: google-github-actions/release-please-action@v4
id: release
with:
release-type: node
# The logic below handles the npm publication:
# - uses: actions/checkout@v4
# # these if statements ensure that a publication only occurs when
# # a new release is created:
# if: ${{ steps.release.outputs.release_created }}
# - uses: actions/setup-node@v4
# with:
# node-version: "20.x"
# registry-url: "https://registry.npmjs.org"
# if: ${{ steps.release.outputs.release_created }}
# - run: npm ci
# if: ${{ steps.release.outputs.release_created }}
# - run: npm publish
# env:
# NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
# if: ${{ steps.release.outputs.release_created }}
37 changes: 37 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Run Unit Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: sqloo
POSTGRES_PASSWORD: sqloo
POSTGRES_DB: sqloo
ports:
- 5432:5432
# Wait for the database to be ready
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: "20.x"
- name: Install pnpm
run: curl -f https://get.pnpm.io/v6.js | node - add --global pnpm
- name: Install dependencies
run: pnpm install
- name: Run tests
env:
PGDATABASE: sqloo
PGUSER: sqloo
PGPASSWORD: sqloo
PGPORT: 5432
run: pnpm test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
.envrc
.env
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tabWidth": 2,
"useTabs": false
}
140 changes: 140 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# sqloo

a simple interface to sqlite and/or postgres for basic tasks

```js
// get a postgres database
const db = await getDatabase({
host: "localhost",
port: 5432,
database: "sqlooooo",
user: "oO",
password: "ackBillD.C@",
});
```

```js
// get a sqlite database
const db = await getDatabase({
file: "::memory::",
});
```

```js
// fetch all the posts
const { oo } = db;
const posts = await oo`
select * from posts
`;
```

```js
// fetch a single post
const { oO } = db;
const post = await oO`
select * from posts where author = ${author} order by created_at desc limit 1
`;
```

```js
// pluck the first value from the first row
const { ox } = db;
const id = await ox`
select id from posts where author = ${author} order by created_at desc limit 1
`;
```

```js
// execute a statement
const { xx } = db;
await xx`
delete from posts where author = ${author}
`;
```

## Functions (db.\*)

### `insert(table, data)`

Inserts data into a table and returns the inserted rows.

- `table` (string): The name of the table.
- `data` (Object | Object[]): The data to insert.
- Returns: `Promise<Object | Object[]>`: The inserted rows, return type will match data.

### `get(table, where)`

Retrieves a row from a table based on a where clause.

- `table` (string): The name of the table.
- `where` (Object): The where clause.
- Returns: `Promise<Object>`: The retrieved row.

### `list(table, where)`

Retrieves multiple rows from a table based on a where clause.

- `table` (string): The name of the table.
- `where` (Object): The where clause.
- Returns: `Promise<Array>`: The retrieved rows.

### `update(table, where, data)`

Updates rows in a table based on a where clause and returns the updated rows.

- `table` (string): The name of the table.
- `where` (Object): The where clause.
- `data` (Object): The data to update.
- Returns: `Promise<Object>`: The updated rows.

### `table(table)`

Returns an object with insert, get, and list methods for a specific table.

- `table` (string): The name of the table.
- Returns: `Object`: An object with insert, get, and list methods.

### `parseTemplate(strings, ...vars)`

Parses a SQL template into a SQL string and an array of values.

- `strings` (Array): The strings in the SQL template.
- `vars` (...any): The variables in the SQL template.
- Returns: `Object`: An object with a sql property and a values property.

### `pluck(strings, ...vars)`

Executes a SQL query and returns the first column of the first row. Handles templates with it's alias "xo"

- `strings` (TemplateStringsArray): The strings in the SQL template.
- `vars` (...any): The variables in the SQL template.
- Returns: `Promise<any>`: The first column of the first row.

### `single(strings, ...vars)`

Executes a SQL query and returns the first row. Handles templates with it's alias "oO"

- `strings` (TemplateStringsArray): The strings in the SQL template.
- `vars` (...any): The variables in the SQL template.
- Returns: `Promise<Object>`: The first row.

### `many(strings, ...vars)`

Executes a SQL query and returns all rows. Handles templates with it's alias "oo"

- `strings` (TemplateStringsArray): The strings in the SQL template.
- `vars` (...any): The variables in the SQL template.
- Returns: `Promise<Array>`: All rows.

### `execute(strings, ...vars)`

Executes a SQL query. Handles templates with it's alias "xx"

- `strings` (TemplateStringsArray): The strings in the SQL template.
- `vars` (...any): The variables in the SQL template.
- Returns: `Promise<pg.QueryResult>`: The result of the query.

## Warning

- dont use variables for anything except values in template queries
- dont accept unsantized user input for table or column names
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "3.1"
services:
postgres:
image: postgres:latest
environment:
POSTGRES_USER: sqloo
POSTGRES_PASSWORD: sqloo
POSTGRES_DB: sqloo
ports:
- "35432:5432"
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "sqloo",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "vitest"
},
"keywords": [],
"author": "[email protected]",
"license": "ISC",
"dependencies": {
"better-sqlite3": "^9.4.0",
"pg": "^8.11.3",
"sqlite-vss": "^0.1.2"
},
"devDependencies": {
"vitest": "^1.2.2"
},
"engines": {
"node": "20.x"
}
}
Loading

0 comments on commit 9c733ac

Please sign in to comment.