Skip to content

Commit

Permalink
First Platformatic Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Pragades-Rajagopal committed Jan 22, 2024
1 parent 984b50c commit 73af1fc
Show file tree
Hide file tree
Showing 14 changed files with 263 additions and 58 deletions.
11 changes: 4 additions & 7 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
DATABASE_URL=sqlite://./db.sqlite
PLT_SERVER_HOSTNAME=127.0.0.1
PORT=3042
PORT=3001
PLT_SERVER_LOGGER_LEVEL=info
DATABASE_URL=sqlite://./db.sqlite
MAIL_USER=[email protected]
MAIL_PASS=emailpassword
MAIL_SERVICE=SMTP_email_service
TUTOR_MAIL=[email protected]
PLT_ACCESS_TOKEN=<hex_token>


59 changes: 59 additions & 0 deletions .github/workflows/platformatic-dynamic-workspace-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Deploy Platformatic application to the cloud
on:
pull_request:
paths-ignore:
- 'docs/**'
- '**.md'
workflow_dispatch:
inputs:
label:
description: "Preview Label"
required: true
default: ""

jobs:
build_and_deploy:
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-latest

env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
PLT_SERVER_LOGGER_LEVEL: info
PORT: 3001
PLT_SERVER_HOSTNAME: 127.0.0.1
DEPLOY_SERVICE_HOST: https://deploy.platformatic.cloud


steps:
- name: Checkout application project repository
uses: actions/checkout@v3
- name: npm install --omit=dev
run: npm install --omit=dev
- name: Deploy project
id: deploy-project
uses: platformatic/onestep@latest
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
platformatic_workspace_id: ${{ secrets.PLATFORMATIC_DYNAMIC_WORKSPACE_ID }}
platformatic_workspace_key: ${{ secrets.PLATFORMATIC_DYNAMIC_WORKSPACE_API_KEY }}
platformatic_config_path: ./platformatic.db.json
label: ${{ github.event.inputs.label }}
outputs:
deployment_id: ${{ steps.deploy-project.outputs.deployment_id }}
calculate_risk:
permissions:
contents: read
pull-requests: write
needs: build_and_deploy
runs-on: ubuntu-latest
steps:
- name: Calculate risk
uses: platformatic/onestep/actions/calculate-risk@latest
if: github.event_name == 'pull_request'
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
platformatic_workspace_id: ${{ secrets.PLATFORMATIC_DYNAMIC_WORKSPACE_ID }}
platformatic_workspace_key: ${{ secrets.PLATFORMATIC_DYNAMIC_WORKSPACE_API_KEY }}
platformatic_deployment_id: ${{ needs.build_and_deploy.outputs.deployment_id }}
37 changes: 37 additions & 0 deletions .github/workflows/platformatic-static-workspace-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Deploy Platformatic application to the cloud
on:
push:
branches:
- main
paths-ignore:
- 'docs/**'
- '**.md'
workflow_dispatch:
inputs:

jobs:
build_and_deploy:
permissions:
contents: read
runs-on: ubuntu-latest

env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
PLT_SERVER_LOGGER_LEVEL: info
PORT: 3001
PLT_SERVER_HOSTNAME: 127.0.0.1
DEPLOY_SERVICE_HOST: https://deploy.platformatic.cloud


steps:
- name: Checkout application project repository
uses: actions/checkout@v3
- name: npm install --omit=dev
run: npm install --omit=dev
- name: Deploy project
uses: platformatic/onestep@latest
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
platformatic_workspace_id: ${{ secrets.PLATFORMATIC_STATIC_WORKSPACE_ID }}
platformatic_workspace_key: ${{ secrets.PLATFORMATIC_STATIC_WORKSPACE_API_KEY }}
platformatic_config_path: ./platformatic.db.json
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,3 @@ tags

# clinicjs
.clinic/

seq.drawio
16 changes: 4 additions & 12 deletions migrations/001.do.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
CREATE TABLE IF NOT EXISTS users (

-- Add SQL in this file to create the database tables for your API
CREATE TABLE IF NOT EXISTS movies (
id INTEGER PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
mobile_no INTEGER NOT NULL UNIQUE,
is_email_verified INTEGER CHECK( is_email_verified IN (0, 1) ) NOT NULL DEFAULT 0,
is_mobile_verified INTEGER CHECK( is_mobile_verified IN (0, 1) ) NOT NULL DEFAULT 0,
_type TEXT CHECK( _type IN ('student', 'tutor') ) NOT NULL,
_status INTEGER CHECK( _status IN (0, 1) ) NOT NULL DEFAULT 1,
_created_on DATETIME NOT NULL,
_modified_on DATETIME NOT NULL
title TEXT NOT NULL
);
4 changes: 3 additions & 1 deletion migrations/001.undo.sql
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
DROP TABLE users;

-- Add SQL in this file to drop the database tables
DROP TABLE movies;
19 changes: 5 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
{
"scripts": {
"start": "platformatic start",
"migrate": "platformatic db migrations apply"
"test": "node --test test/**"
},
"devDependencies": {
"fastify": "^4.21.0"
"fastify": "^4.24.3"
},
"dependencies": {
"@platformatic/db": "^0.38.1",
"@platformatic/sql-graphql": "^0.38.1",
"@platformatic/sql-mapper": "^0.38.1",
"bcrypt": "^5.1.1",
"dotenv": "^16.3.1",
"handlebars": "^4.7.8",
"jsonwebtoken": "^9.0.2",
"moment": "^2.29.4",
"nodemailer": "^6.9.7",
"platformatic": "^0.38.1"
"platformatic": "^1.10.0"
},
"engines": {
"node": "^18.8.0"
"node": "^18.8.0 || >=20.6.0"
}
}
}
41 changes: 19 additions & 22 deletions platformatic.db.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
{
"$schema": "https://platformatic.dev/schemas/v0.38.1/db",
"server": {
"hostname": "{PLT_SERVER_HOSTNAME}",
"port": "{PORT}",
"logger": {
"level": "{PLT_SERVER_LOGGER_LEVEL}"
}
},
"$schema": "https://platformatic.dev/schemas/v1.10.0/db",
"db": {
"connectionString": "{DATABASE_URL}",
"graphql": true,
"openapi": {
"prefix": "api",
"ignore": {
"otp": true,
"userLogin": true,
"students": true,
"courses": false,
"tutors": true,
"tutorRequests": true,
"feeds": true
}
}
"openapi": true,
"schemalock": true
},
"watch": {
"ignore": [
"*.sqlite",
"*.sqlite-journal"
]
},
"server": {
"hostname": "{PLT_SERVER_HOSTNAME}",
"port": "{PORT}",
"logger": {
"level": "{PLT_SERVER_LOGGER_LEVEL}"
}
},
"migrations": {
"dir": "migrations"
"dir": "migrations",
"autoApply": true
},
"plugins": {
"paths": [
"./plugins"
{
"path": "./plugins",
"encapsulate": false
},
{
"path": "./routes"
}
]
},
"types": {
Expand Down
6 changes: 6 additions & 0 deletions plugins/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// <reference path="../global.d.ts" />
'use strict'
/** @param {import('fastify').FastifyInstance} fastify */
module.exports = async function (fastify, opts) {
fastify.decorate('example', 'foobar')
}
8 changes: 8 additions & 0 deletions routes/root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path="../global.d.ts" />
'use strict'
/** @param {import('fastify').FastifyInstance} fastify */
module.exports = async function (fastify, opts) {
fastify.get('/example', async (request, reply) => {
return { hello: fastify.example }
})
}
40 changes: 40 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

const { join } = require('node:path')
const { readFile } = require('node:fs/promises')
const { buildServer } = require('@platformatic/db')

const os = require('node:os')
const path = require('node:path')
const fs = require('node:fs/promises')

let counter = 0


async function getServer (t) {

const dbPath = join(os.tmpdir(), 'db-' + process.pid + '-' + counter++ + '.sqlite')
const connectionString = 'sqlite://' + dbPath

const config = JSON.parse(await readFile(join(__dirname, '..', 'platformatic.db.json'), 'utf8'))
// Add your config customizations here. For example you want to set
// all things that are set in the config file to read from an env variable
config.server.logger.level = 'warn'
config.watch = false

config.migrations.autoApply = true
config.types.autogenerate = false
config.db.connectionString = connectionString

// Add your config customizations here
const server = await buildServer(config)
t.after(() => server.close())

t.after(async () => {
await fs.unlink(dbPath)
})

return server
}

module.exports.getServer = getServer
11 changes: 11 additions & 0 deletions test/plugins/example.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

const test = require('node:test')
const assert = require('node:assert')
const { getServer } = require('../helper')

test('example decorator', async (t) => {
const server = await getServer(t)

assert.strictEqual(server.example, 'foobar')
})
49 changes: 49 additions & 0 deletions test/routes/movies.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict'

const test = require('node:test')
const assert = require('node:assert')
const { getServer } = require('../helper')

test('movies', async (t) => {
const server = await getServer(t)

{
const res = await server.inject({
method: 'GET',
url: '/movies'
})

assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(res.json(), [])
}

let id
{
const res = await server.inject({
method: 'POST',
url: '/movies',
body: {
title: 'The Matrix'
}
})

assert.strictEqual(res.statusCode, 200)
const body = res.json()
assert.strictEqual(body.title, 'The Matrix')
assert.strictEqual(body.id !== undefined, true)
id = body.id
}

{
const res = await server.inject({
method: 'GET',
url: '/movies'
})

assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(res.json(), [{
id,
title: 'The Matrix'
}])
}
})
18 changes: 18 additions & 0 deletions test/routes/root.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

const test = require('node:test')
const assert = require('node:assert')
const { getServer } = require('../helper')

test('example', async (t) => {
const server = await getServer(t)
const res = await server.inject({
method: 'GET',
url: '/example'
})

assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(res.json(), {
hello: 'foobar'
})
})

0 comments on commit 73af1fc

Please sign in to comment.