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

Add database and API examples #93

Merged
merged 1 commit into from
Aug 7, 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
83 changes: 83 additions & 0 deletions javascript/simple-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# simple-counter

___

As a default, the counter is started with 0 and ends with 1000. These values can be changed by passing the `start` and `end` parameters.

## Running

> ❗ Remember to [setup transform-hub locally](https://docs.scramjet.org/transform-hub/installation) or use the [platform's environment](https://docs.scramjet.org/platform/get-started/) for the sequence deployment.

Open the terminal and run the following commands:

```bash
# go to 'simple-counter-js' directory
cd javascript/simple-counter-js

# instal dependencies
npm run build

# deploy 'simple-counter-js' Sequence
si seq deploy dist

> 💡**NOTE:** Command `deploy` performs three actions at once: `pack`, `send` and `start` the Sequence. It is the same as if you would run those three commands separately:

```bash
si seq pack . -o simple-counter-js.tar.gz # compress 'simple-counter-js/' directory into file named 'simple-counter-js.tar.gz'

si seq send simple-counter-js.tar.gz # send compressed Sequence to STH, this will output Sequence ID

si seq start - # start the Sequence, this will output Instance ID
```

## Output

```bash
{ x: 1 }
{ x: 2 }
{ x: 3 }
{ x: 4 }
{ x: 5 }
{ x: 6 }
{ x: 7 }
{ x: 8 }
{ x: 9 }
{ x: 10 }
{ x: 11 }
{ x: 12 }
{ x: 13 }
{ x: 14 }
...
```

## Running the same Sequence but with some parameters

```bash
# go to 'simple-counter-js' directory
cd samples/simple-counter-js

# instal dependencies
npm run build

# deploy sequence with arguments
si seq deploy dist --args [100, 200]

## Output

```bash
# the counter will start counting at 100 and finish at 200
{ x: 101 }
{ x: 102 }
{ x: 103 }
{ x: 104 }
{ x: 105 }
{ x: 106 }
...
{ x: 193 }
{ x: 194 }
{ x: 195 }
{ x: 196 }
{ x: 197 }
{ x: 198 }
{ x: 199 }
```
26 changes: 26 additions & 0 deletions javascript/simple-api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const http = require('http');

module.exports = [
{ requires: "inputs", contentType: "application/x-ndjson" },
async function (input) {
let data = null;

const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ greet: 'Hello World', data }));
});

server.listen(9080, () => {
console.log('Server is listening on port 9080');
});

for await (const message of input) {
data = message;
}

await new Promise((res, rej) => {
server.on('close', res);
server.on('error', rej);
});
}
];
26 changes: 26 additions & 0 deletions javascript/simple-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@scramjet/simple-api",
"version": "0.23.0",
"main": "index.js",
"author": "MichalCz",
"license": "GPL-3.0",
"description": "A simple Sequence, that exposes a hello world API with last message on topic.",
"keywords": [
"easy",
"streaming",
"api",
"Data Consumer"
],
"repository": {
"type": "git",
"url": "https://github.com/scramjetorg/platform-samples/tree/main/javascript/simple-api"
},
"scripts": {
"build": "mkdir -p dist/ && cp -rt dist/ *.js package.json && (cd dist && npm i --omit=dev)",
"pack": "si seq pack ./dist/",
"clean": "rm -rf ./dist ./*.tar.gz"
},
"engines": {
"node": ">=16"
}
}
83 changes: 83 additions & 0 deletions javascript/sqlite/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# simple-counter

___

As a default, the counter is started with 0 and ends with 1000. These values can be changed by passing the `start` and `end` parameters.

## Running

> ❗ Remember to [setup transform-hub locally](https://docs.scramjet.org/transform-hub/installation) or use the [platform's environment](https://docs.scramjet.org/platform/get-started/) for the sequence deployment.

Open the terminal and run the following commands:

```bash
# go to 'simple-counter-js' directory
cd javascript/simple-counter-js

# instal dependencies
npm run build

# deploy 'simple-counter-js' Sequence
si seq deploy dist

> 💡**NOTE:** Command `deploy` performs three actions at once: `pack`, `send` and `start` the Sequence. It is the same as if you would run those three commands separately:

```bash
si seq pack . -o simple-counter-js.tar.gz # compress 'simple-counter-js/' directory into file named 'simple-counter-js.tar.gz'

si seq send simple-counter-js.tar.gz # send compressed Sequence to STH, this will output Sequence ID

si seq start - # start the Sequence, this will output Instance ID
```

## Output

```bash
{ x: 1 }
{ x: 2 }
{ x: 3 }
{ x: 4 }
{ x: 5 }
{ x: 6 }
{ x: 7 }
{ x: 8 }
{ x: 9 }
{ x: 10 }
{ x: 11 }
{ x: 12 }
{ x: 13 }
{ x: 14 }
...
```

## Running the same Sequence but with some parameters

```bash
# go to 'simple-counter-js' directory
cd samples/simple-counter-js

# instal dependencies
npm run build

# deploy sequence with arguments
si seq deploy dist --args [100, 200]

## Output

```bash
# the counter will start counting at 100 and finish at 200
{ x: 101 }
{ x: 102 }
{ x: 103 }
{ x: 104 }
{ x: 105 }
{ x: 106 }
...
{ x: 193 }
{ x: 194 }
{ x: 195 }
{ x: 196 }
{ x: 197 }
{ x: 198 }
{ x: 199 }
```
62 changes: 62 additions & 0 deletions javascript/sqlite/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const http = require('http');
const qs = require('querystring');
const sqlite3 = require('sqlite3');
const path = require('path');

// Assuming the database structure is defined in prime.js
const dbPath = path.join(__dirname, 'products.db');; // Replace with the actual path to the database file
module.exports = async function (input) {
const db = new sqlite3.Database(dbPath);

// Function to query the database and extract the product list
async function extractProductList(query) {
return new Promise((res, rej) => {
db.all(query, (err, rows) => {
if (err) {
rej(err);
} else {
res(rows);
}
});
});
}

const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'application/json');

try {
const { category } = qs.parse(req.url.split('?')[1]);

let query;
if (category) {
query = `SELECT sku, msrp, name FROM products WHERE category = '${category}'`;
} else {
query = `SELECT sku, msrp, name FROM products`;
}
extractProductList(query)
.then((rows) => {
res.statusCode = 200;
res.end(JSON.stringify(rows));
})
.catch((error) => {
res.statusCode = 500;
res.end(JSON.stringify({ error: 'Query Error', message: error.message }));
});

} catch (error) {
res.statusCode = 500;
res.end(JSON.stringify({ error: 'Internal Server Error', message: error.message }));
}
});

server.listen(9080, () => {
console.log('Server is listening on port 9080');
});

await new Promise((res, rej) => {
server.on('close', res);
server.on('error', rej);
});

db.close();
};
Loading
Loading