Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate transaction endpoint #196

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ DB=foobar.db
ACCESS_TOKEN_EXPIRATION_TIME=10H
REFRESH_TOKEN_EXPIRATION_TIME=2D

INITIAL_USER_USERNAME
INITIAL_USER_USERNAME
INITIAL_USER_PASSWORD
TOKEN_SECRET

Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,18 @@ npm run dev # Start the dev server

## Testing

Set the `AUTH` variable to `true` in your `.env` file and use the command below to run the tests
1. Set the `AUTH` variable to true in your `.env` file.
2. Provide a username for the `INITIAL_USER_USERNAME` environment variable. The username should be a valid, meaningful username.
3. Provide a strong password for the `INITIAL_USER_PASSWORD` environment variable. The password should be at least 8 characters long and contain a combination of lowercase letters, uppercase letters, numbers, and special characters, for example: "Str0ng$Pw!".
4. Provider a secret for the `TOKEN_SECRET` environment variable.
5. Use the following command to run the tests:

```
npm run test
```

Make sure to replace the placeholders with the appropriate values for your environment.

## Community

[Join](https://bit.ly/soul-discord) the discussion in our Discord server and help making Soul together.
Expand Down
40 changes: 0 additions & 40 deletions docs/api/root-examples.md
Original file line number Diff line number Diff line change
@@ -1,41 +1 @@
## Root

### 1. Transaction

To start a transaction call `/transaction` endpoint with `POST` method.

```bash
curl --request POST \
--url http://localhost:8000/api/transaction \
--header 'Content-Type: application/json' \
--data '{
"transaction": [
{
"statement": "INSERT INTO Artist (ArtistId, Name) VALUES (:id, :name)",
"values": { "id": 100000, "name": "Glen Hansard" }
},
{
"query": "SELECT * FROM Artist ORDER BY ArtistId DESC LIMIT 1"
}
]
}'
```

Response

```json
{
"data": [
{
"changes": 1,
"lastInsertRowid": 100000
},
[
{
"ArtistId": 100000,
"Name": "Glen Hansard"
}
]
]
}
```
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "soul-cli",
"version": "0.7.9",
"version": "0.8.0",
"description": "A SQLite REST and Realtime server",
"main": "src/server.js",
"bin": {
Expand Down
3 changes: 2 additions & 1 deletion src/constants/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module.exports = {

errorMessage: {
USERNAME_TAKEN_ERROR: 'This username is taken',
WEAK_PASSWORD_ERROR: 'This password is weak, please use another password',
WEAK_PASSWORD_ERROR:
'This password is weak, it should be at least 8 characters long and contain a combination of lowercase letters, uppercase letters, numbers, and special characters',
DEFAULT_ROLE_NOT_CREATED_ERROR:
'Please restart soul so a default role can be created',
INVALID_USERNAME_PASSWORD_ERROR: 'Invalid username or password',
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('Auth Endpoints', () => {

expect(res.status).toEqual(400);
expect(res.body.message).toBe(
'This password is weak, please use another password',
'This password is weak, it should be at least 8 characters long and contain a combination of lowercase letters, uppercase letters, numbers, and special characters',
);

expect(res.body).not.toHaveProperty('password');
Expand Down
78 changes: 3 additions & 75 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const db = require('../db/index');
const version = require('../../package.json').version;

// Root endpoint
const root = async (req, res) => {
/*
/*
#swagger.tags = ['Root']
#swagger.summary = 'Timestamp'
#swagger.description = 'Endpoint to return server timestamp'
#swagger.summary = 'Timestamp'
#swagger.description = 'Endpoint to return server timestamp'
*/

res.json({
Expand All @@ -18,77 +17,6 @@ const root = async (req, res) => {
});
};

// Run any query transactions
// inspired by https://github.com/proofrock/ws4sqlite
// e.g. body:
// "transaction": [
// {
// "statement": "INSERT INTO users (id, firstName, lastName) VALUES (:id, :firstName, :lastName)",
// "values": { "id": 1, "firstName": "John", "lastName": "Doe" }
// },
// {
// "query": "SELECT * FROM users"
// }
// }
//
// response:
// "data": [
// {
// "changes": 1,
// "lastInsertRowid": 1
// },
// [
// {
// "id": 1,
// "createdAt": "2022-10-10 10:55:29",
// "updatedAt": "2022-10-10 10:55:29",
// "firstName": "John",
// "lastName": "Doe"
// }
// ]
// ]
//

const transaction = async (req, res) => {
/*
#swagger.tags = ['Root']
#swagger.summary = 'Transaction'
#swagger.description = 'Endpoint to run any transaction, e.g. [{ "query": "" }, { "statement": "", "values": {} }, { "query": "" }]',
#swagger.parameters['body'] = {
in: 'body',
required: true,
schema: { $ref: "#/definitions/TransactionRequestBody" }
}
*/
const { transaction } = req.body;
const results = [];
try {
db.transaction(() => {
transaction.forEach((query) => {
if (query.statement) {
const { statement, values } = query;
const data = db.prepare(statement).run(values);
results.push(data);
} else if (query.query) {
const { query: queryString } = query;
const data = db.prepare(queryString).all();
results.push(data);
}
});
})();

res.json({
data: results,
});
} catch (error) {
res.status(400).json({
message: error.message,
error: error,
});
}
};

module.exports = {
root,
transaction,
};
27 changes: 0 additions & 27 deletions src/controllers/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,3 @@ describe('Root Endpoints', () => {
expect(res.body.data).toHaveProperty('timestamp');
});
});

describe('Transaction Endpoint', () => {
it('POST /transaction should commit transaction and return an array of changes and lastInsertRowid', async () => {
const res = await requestWithSupertest.post('/api/transaction').send({
transaction: [
{
statement: `CREATE TABLE students (id INTEGER PRIMARY KEY, firstName TEXT, lastName TEXT)`,
values: {},
},
{
statement: `INSERT INTO students (id, firstName, lastName) VALUES (:id, :firstName, :lastName)`,
values: { id: 1, firstName: 'John', lastName: 'Doe' },
},
{
query: `SELECT * FROM students`,
},
],
});

expect(res.status).toEqual(200);
expect(res.type).toEqual(expect.stringContaining('json'));
expect(res.body).toHaveProperty('data');
expect(res.body.data).toEqual(expect.any(Array));
expect(res.body.data[0]).toHaveProperty('changes');
expect(res.body.data[0]).toHaveProperty('lastInsertRowid');
});
});
7 changes: 0 additions & 7 deletions src/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
const express = require('express');

const controllers = require('../controllers/index');
const { validator } = require('../middlewares/validation');
const schema = require('../schemas/index');

const router = express.Router();

router.get('/', controllers.root);
router.post(
'/transaction',
validator(schema.transaction),
controllers.transaction
);

module.exports = router;
3 changes: 2 additions & 1 deletion src/swagger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ const doc = {
},

WeakPasswordErrorResponse: {
message: 'This password is weak, please use another password',
message:
'This password is weak, it should be at least 8 characters long and contain a combination of lowercase letters, uppercase letters, numbers, and special characters',
},

UsernameTakenErrorResponse: {
Expand Down
27 changes: 1 addition & 26 deletions src/swagger/swagger.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"swagger": "2.0",
"info": {
"version": "0.7.2",
"version": "0.8.0",
"title": "Soul API",
"description": "API Documentation for <b>Soul</b>, a SQLite REST and realtime server. "
},
Expand Down Expand Up @@ -54,31 +54,6 @@
}
}
},
"/api/transaction": {
"post": {
"tags": ["Root"],
"summary": "Transaction",
"description": "Endpoint to run any transaction, e.g. [{ \"query\": \"\" }, { \"statement\": \"\", \"values\": {} }, { \"query\": \"\" }]",
"parameters": [
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/TransactionRequestBody"
}
}
],
"responses": {
"200": {
"description": "OK"
},
"400": {
"description": "Bad Request"
}
}
}
},
"/api/tables/": {
"get": {
"tags": ["Tables"],
Expand Down