Skip to content

Commit

Permalink
Merge pull request #380 from 5app/next
Browse files Browse the repository at this point in the history
feat(release): support for Postgres and MariaDB
  • Loading branch information
MrSwitch authored Aug 30, 2024
2 parents 6231e79 + 3f7fa93 commit f38e5e3
Show file tree
Hide file tree
Showing 48 changed files with 1,840 additions and 1,082 deletions.
15 changes: 11 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ jobs:

integration:
parameters:
mysql_version:
default: '5.6'
db_engine:
default: 'mysql:5.6'
type: string
executor: ubuntu-machine
steps:
- checkout
- node/install-packages
- run:
name: Run integration tests
command: MYSQL_VERSION=<< parameters.mysql_version >> npm run test:integration -- --exit --timeout=4000
command: DB_ENGINE=<< parameters.db_engine >> npm run test:integration -- --exit --timeout=4000

release:
executor:
Expand All @@ -80,7 +80,14 @@ workflows:
- integration:
matrix:
parameters:
mysql_version: ['5.6', '5.7.40', '8.0.23']
db_engine:
[
'mysql:5.6',
'mysql:5.7.40',
'mysql:8.0.23',
'mariadb:11.4',
'postgres:16.3',
]
- release:
context: org-global
requires:
Expand Down
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
echo "Running pre-commit checks... (skip using --no-verify)";
LIST=`git diff-index --name-only --cached HEAD`; if [ "$LIST" ]; then npx prettier --write $LIST; git add $LIST; fi
# LIST=`git diff-index --name-only --cached --diff-filter=CARM HEAD`; if [ "$LIST" ]; then npx prettier --write $LIST; git add $LIST; fi
npm run lint-diff;
npm run spec;
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ coverage

package-lock.json
CHANGELOG.md

.sh
.sql
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
## [0.90.2](https://github.com/5app/dare/compare/v0.90.1...v0.90.2) (2024-08-02)


### Bug Fixes

* **patch:** remove space between alias name and set value, noissue ([8876b85](https://github.com/5app/dare/commit/8876b8572af4557c9237791af4f060ef18d49faa))

## [0.90.1](https://github.com/5app/dare/compare/v0.90.0...v0.90.1) (2024-08-02)


### Bug Fixes

* **ts:** add Engine string type pattern, noissue ([9cf7313](https://github.com/5app/dare/commit/9cf7313f7cb970f8b3f945a07bccb490010397c8))

# [0.90.0](https://github.com/5app/dare/compare/v0.89.0...v0.90.0) (2024-07-05)


### Features

* **mariadb:** add support for MariaDB ([0e9c8b9](https://github.com/5app/dare/commit/0e9c8b9ab69f46f82b8b23864572dfaa74011927))

# [0.89.0](https://github.com/5app/dare/compare/v0.88.3...v0.89.0) (2024-07-05)


### Features

* **postgres:** initial integration test implementation ([#338](https://github.com/5app/dare/issues/338)) ([9e81d0c](https://github.com/5app/dare/commit/9e81d0c25462897646c75e645d438050984646c8)), closes [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339) [#339](https://github.com/5app/dare/issues/339)

## [0.88.3](https://github.com/5app/dare/compare/v0.88.2...v0.88.3) (2024-05-22)


Expand Down
84 changes: 50 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@

Dare is a brave API for generating SQL out of structured Javascript objects.

# Example usage...
## Example usage...

This is a simple setup to get started with, it'll make a basic SELECT query.
This is a simple setup to make a SELECT query

```js
// Require the module
import Dare from 'dare';
import sqlConn from './mySqlConn.js';
import dbconn from './dbConn.js'; // <- your script for executing queries

// Initiate it
const dare = new Dare();
const dare = new Dare({
engine: 'mysql:8.0' // set the engine
});

// Define the handler dare.execute for handing database requests
dare.execute = async ({sql, values}) => {
dare.execute = async (request) => {

// Execute query using prepared statements
return sqlConn.execute(sql, values);
// use request.sql, whilst in postgres use request.text
return dbconn.query(request.sql, request.values);
};

// Make a request
Expand All @@ -40,6 +44,15 @@ console.log(`Hi ${resp.name}');
npm i dare --save
```

# Connect

The setup needs to define a execution handler `dare.execute(SqlRequest) : Promise<Array | Object<{insertId, affectedRows}>`

The integration tests illustrates how a [setup of a dare instance](`./test/integration/helpers/api.js`) connects to different clients...

- **MySQL** (5.6, 5.7, 8.0,...) and **MariaDB** (11) See [connection with `mysql2`](./test/integration/helpers/MySQL.js)
- **Postgres** (16+) See [connection with `pg`](./test/integration/helpers/Postgres.js)

# Methods

## dare.get(table[, fields][, filter][, options])
Expand Down Expand Up @@ -232,7 +245,7 @@ The type of value affects the choice of SQL Condition syntax to use. For example
Prefixing the prop with:
- `%`: creates a `LIKE` comparison
- `%`: creates a `LIKE` comparison (or `ILIKE` in _postgres_)
- `-`: hyhen negates the value
- `~`: creates a range
Expand Down Expand Up @@ -570,38 +583,30 @@ await dare.get({
});
```
As you can see, to apply `options` you have... um _options_.
| option name | type | description |
| --------------- | ----------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `engine` | `string` | Database engine, e.g. `mysql:5.7:40`, `postgres:16.3` |
| `models` | `Object<ModelName, Model>` | An object where the keys are the model names to which models can be referred. |
| `validateInput` | `Function(fieldAttributes, field, value)` | Validate input on _patch_ and _post_ operations |
| `getFieldKey` | `Function(field, schema)` | Override the default function for retrieving schema fields, this is useful if you want to support altenative case (camelCase and/or snakeCase) |
# `options.models`
# Model
The `options.models` object, allows us to apply all our models to Dare. Where the object key is the label for which we'll refer to that model.
Models define many things, the underling db table, the schema and any handler functions which are invoked on any operation (post, patch, del, get, getCount).
See next section **Model** for what a model looks like.
Models are applied to the `options.models` object e.g...
```js
// options Object containing a property called `models`
// Models is a key => model store, where the key is what we'll always refer to as the label for that model.
const options = {
const dare = new Dare({
models: {
// modelA,
// modelB,
// etc...
},
};

// options applied to dare as before.
mymodel : {
/** model properties */
}.
}
});
```
# Model
Perhaps the most important part of the **Dare** library is concept of a **model**.
A **model** defines:
- how data is interlinked, i.e. how one relational data table is joined to another via a key
- mutation handlers, for changing requests. This allows access permissions to be applied, to filter results, to restrict or mutate input data.
E.g. here are available properties which can be defined on a model,
Available properties which can be defined on a model are...
```js
const myModel = {
Expand Down Expand Up @@ -1540,14 +1545,25 @@ await dare.patch({
### DB Engine compatibility
The latest version is designed to work with MySQL 5.7 and 8
This version of Dare is designed to work with:
There is currently backwards compatibility with MySQL 5.6 provided by setting the environment variable `MYSQL_VERSION`
- MySQL (5.6, 5.7 and 8)
- Postgres (16.3)
- MariaDB (11)
Set the property `engine` on the Dare instance
e.g.
```js
process.env.MYSQL_VERSION = '5.6';
const dare = new Dare{{
engine: 'postgres:16.3',
...
}};
// Or, have multiple instances...
let dareWithPostgres = dare.use({
engine: 'postgres:16.3'
});
```
# Caveats
Expand Down
Loading

0 comments on commit f38e5e3

Please sign in to comment.