Compare commits

...

80 Commits

Author SHA1 Message Date
Garrett Mills ac6fd0ef1d 0.14.14: Misc bugfixes in migrations & AsyncCollection keys
continuous-integration/drone/tag Build is failing Details
continuous-integration/drone/push Build is passing Details
6 months ago
Garrett Mills 9a55623370 Modify isAwareOfContainerLifecycle to include function-type instances
continuous-integration/drone/push Build is passing Details
10 months ago
Garrett Mills 743e81ae94 bump version
11 months ago
Garrett Mills 61c4d86fff Update dependencies with pnpm v8
continuous-integration/drone/promote/production Build is passing Details
continuous-integration/drone Build is passing Details
11 months ago
Garrett Mills 9aa3f56340 Update package versions for node 18
continuous-integration/drone Build is failing Details
11 months ago
Garrett Mills 899c8448fc Create HTTPFilesystem implementation and add support to universalPath helper to automatically use it
continuous-integration/drone Build is failing Details
11 months ago
Garrett Mills 7c9b1ff212 Bump version
continuous-integration/drone Build is passing Details
1 year ago
Garrett Mills cae9a1acbe Drone: only rollout docs on tag
continuous-integration/drone Build is passing Details
1 year ago
Garrett Mills aeb37d711b Drone: add npm build pipeline
continuous-integration/drone Build is passing Details
1 year ago
Garrett Mills d66ae85f54 Drone: fix k8s rollout path
continuous-integration/drone Build is passing Details
1 year ago
Garrett Mills 68b81106e6 Docs: update to latest typedoc (breaks theme)
continuous-integration/drone Build is failing Details
1 year ago
Garrett Mills 5395fb9054 Drone: Add docs build
continuous-integration/drone Build is failing Details
1 year ago
Garrett Mills bbafd54dcc Add docs K8s spec
1 year ago
Garrett Mills 37fcaabdef Fix isHTTPMethod validator
1 year ago
Garrett Mills bbf2807cfa Add `options` to HTTPMethod type
1 year ago
Garrett Mills 2d9f22b895 Fix left() response handling for parameter middleware
1 year ago
Garrett Mills 0484a586bd Update dependencies & fix misc formatting errors
2 years ago
Garrett Mills 52762bd4a1 Experimental SQLite support
2 years ago
Garrett Mills c0595f3ef9 Centralize configure-able factory classes
2 years ago
Garrett Mills 5557aae543 Fix order of dates in redis expiration calculation
2 years ago
Garrett Mills f1791b1d76 Add push$ to Collection; make Container listen for retroactive blueprint changes
2 years ago
Garrett Mills a173393697 Fix property injection prototype hoisting bug
2 years ago
Garrett Mills c966904418 finish TreeModel implementation and updateMany builder method
2 years ago
Garrett Mills f63891ef99 Add TreeModel and HasSubtree implementation
2 years ago
Garrett Mills 3d836afa59 Improve error context for DI
2 years ago
Garrett Mills 9b47d2ac99 Fix async context loss in WebSocketBus
2 years ago
Garrett Mills 085fe04f90 Fix issue where WebSocketBus depends on Session before session factory registered
2 years ago
Garrett Mills e339ec718d Support periodic auth checks in SecurityContext on web socket connections
2 years ago
Garrett Mills efb9726470 Add CacheSession implementation & make WebSocketBus re-load the session when an event is received
2 years ago
Garrett Mills 8a153e3807 Properly unwrap StateEvent state before calling handler
2 years ago
Garrett Mills b8cf8499d2 Update ShellDirective to use ts-node interpreter by default
2 years ago
Garrett Mills 710b6cb535 Add workaround for global registry async context in REPL
2 years ago
Garrett Mills 91d76f44b5 Make global registry better accessible externally
2 years ago
Garrett Mills 8774bd8d34 Fix serial format of StateEvent
2 years ago
Garrett Mills 3712fae979 Better comments
2 years ago
Garrett Mills 4aa33e8dd2 Add SocketRouteBuilder and make Route.socket(...) return it
2 years ago
Garrett Mills ef405093dc Actually export StateEvent...
2 years ago
Garrett Mills fc85c9d2c8 Add StateEvent implementation
2 years ago
Garrett Mills d00e6a02e2 Remove unnecessary container call
2 years ago
Garrett Mills ea81a37315 Bump version
2 years ago
Garrett Mills 33a64b99ff Implement websocket server
2 years ago
Garrett Mills dc663ec8f5 Bump version
2 years ago
Garrett Mills 6476416c67 Add foreground service, some cleanup, and start websocket server
2 years ago
Garrett Mills 4d7769de56 Fix PostgreSQLDialect escaping of single quotes
2 years ago
Garrett Mills 6ca4bc1151 Fix buildMigrationNamespaceResolver to use isValidSuffix(...) instead of hard-coded check
2 years ago
Garrett Mills 7d3fde85eb Modify canonical loading to allow either suffix.js or suffix.ts endings
2 years ago
Garrett Mills 111ce0bcf9 Fix Routing suffix loading
2 years ago
Garrett Mills d3a6a8495c Update application bootstrapping to load .env from repo root
2 years ago
Garrett Mills 2ff9354538 Make file extensions on Canonical resources runtime-agnostic
2 years ago
Garrett Mills cf0ae260dc Fix syntax of postgres update queries
2 years ago
Garrett Mills fe9170282f Add ArrayElement type helper
2 years ago
Garrett Mills 2e43b5bda9 Fix route parameter typing to use SuffixTypeArray
2 years ago
Garrett Mills 705bb20db1 Make Route.clone properly copy parameter definitions
2 years ago
Garrett Mills 1be73dd347 Add docs server Dockerfile
2 years ago
Garrett Mills afbf6e7682 Add force SSL config flag
2 years ago
Garrett Mills 48ce1bfa2f Improve app URL detection
2 years ago
Garrett Mills 1d717e0eb9 bump version
2 years ago
Garrett Mills 90b16eef53 Add better debugging to routing service
2 years ago
Garrett Mills 1399399af9 Improve error handling in OAuth2 token issuer
2 years ago
Garrett Mills 30a23b1659 Temporarily cast migration AsyncCollection to Collection pending #17
2 years ago
Garrett Mills de13030815 Fix AsyncCollection.filter to allow for async filter functions
2 years ago
Garrett Mills fd77ad5cd3 Create migration for oauth2_tokens table
2 years ago
Garrett Mills 814a5763d9 fix formatting
2 years ago
Garrett Mills 9ede67cb12 Make orm token repo use make() to create token model instances
2 years ago
Garrett Mills 8a9264b9de Simplify /oauth2/issue logic
2 years ago
Garrett Mills d210cba236 add oauth2 issue debugging and bump version
2 years ago
Garrett Mills 015d6fd6ae Fix Either<> pass in OAuth2Server; bump version
2 years ago
Garrett Mills ce4133ff8e Fix scope() helper and bump version
2 years ago
Garrett Mills 9d8f43d8fb Bump version
2 years ago
Garrett Mills 940d50b89c Implement /oauth2/token endpoint; token auth middleware
2 years ago
Garrett Mills 36647a013d Add ability to get provider by name from auth service
2 years ago
Garrett Mills 5616b3cc1f Add suggestion for missing schema for validator error
2 years ago
Garrett Mills 2e7c927114 Make route extraction account for extraneous / matches
2 years ago
Garrett Mills 8b2ee1c949 Add debugging for route extraction
2 years ago
Garrett Mills c7557cf5b6 Allow overriding content-type of plaintext response factory
2 years ago
Garrett Mills 771fed8002 Make parameter middleware handler call collection.toArray NON-recursively
2 years ago
Garrett Mills 7914a8f12e Fix incorrect flow-through if-else case
2 years ago
Garrett Mills 5fa4f614e2 Pass request to ParameterMiddleware make call
2 years ago
Garrett Mills ee21811771 Add ability to pass arguments to parameter providing middleware
2 years ago
Garrett Mills 8b9f393405 Add support for parameter middleware classes
2 years ago

@ -1,228 +1,87 @@
---
kind: pipeline
type: docker
type: kubernetes
name: docs
steps:
# ============ BUILD STEPS ===============
- name: build documentation
image: glmdev/node-pnpm:latest
commands:
- pnpm i --silent
- pnpm docs:build
- cd docs && tar czf ../extollo_api_documentation.tar.gz www
# =============== DEPLOY STEPS ===============
- name: copy artifacts to static host
image: appleboy/drone-scp
settings:
host:
from_secret: docs_deploy_host
username:
from_secret: docs_deploy_user
key:
from_secret: docs_deploy_key
port: 22
source: extollo_api_documentation.tar.gz
target: /var/nfs/storage/static/sites/extollo
when:
event: promote
target: docs
- name: deploy artifacts on static host
image: appleboy/drone-ssh
settings:
host:
from_secret: docs_deploy_host
username:
from_secret: docs_deploy_user
key:
from_secret: docs_deploy_key
port: 22
script:
- cd /var/nfs/storage/static/sites/extollo
- rm -rf docs
- tar xzf extollo_api_documentation.tar.gz
- rm -rf extollo_api_documentation.tar.gz
- mv www docs
when:
event: promote
target: docs
# =============== BUILD NOTIFICATIONS ===============
- name: send build success notifications
image: plugins/webhook
settings:
urls:
from_secret: notify_webhook_url
content_type: application/json
template: |
{
"title": "Drone-CI [extollo/docs @ ${DRONE_BUILD_NUMBER}]",
"message": "Build & deploy completed successfully.",
"priority": 4
}
when:
status: success
event:
- promote
metadata:
labels:
pod-security.kubernetes.io/audit: privileged
- name: send build error notifications
image: plugins/webhook
settings:
urls:
from_secret: notify_webhook_url
content_type: application/json
template: |
{
"title": "Drone-CI [extollo/docs @ ${DRONE_BUILD_NUMBER}]",
"message": "Documentation build failed!",
"priority": 6
}
services:
- name: docker daemon
image: docker:dind
privileged: true
environment:
DOCKER_TLS_CERTDIR: ""
when:
status: failure
---
event: tag
kind: pipeline
name: default
type: docker
steps:
- name: post build in progress comment to PR
image: tsakidev/giteacomment:latest
settings:
gitea_token:
from_secret: gitea_token
gitea_base_url: https://code.garrettmills.dev
comment: "Build ${DRONE_BUILD_NUMBER} started."
when:
event: pull_request
- name: typedoc build
image: node:18
commands:
- "node -v"
- "npm add --global pnpm"
- "pnpm --version"
- pnpm i
- pnpm run docs:build
- name: remove lockfile
image: glmdev/node-pnpm:latest
- name: container build
image: docker:latest
privileged: true
commands:
- rm -rf pnpm-lock.yaml
- "while ! docker stats --no-stream; do sleep 1; done"
- docker image build docs -t $DOCKER_REGISTRY/extollo/docs:latest
- docker push $DOCKER_REGISTRY/extollo/docs:latest
environment:
DOCKER_HOST: tcp://localhost:2375
DOCKER_REGISTRY:
from_secret: DOCKER_REGISTRY
when:
event:
exclude: tag
event: tag
status: success
- name: Install dependencies
image: glmdev/node-pnpm:latest
- name: k8s rollout
image: bitnami/kubectl
commands:
- pnpm i
- cd docs/deploy && kubectl apply -f .
- kubectl rollout restart -n extollo deployment/docs
when:
event: tag
status: success
- name: Lint code
image: glmdev/node-pnpm:latest
commands:
- pnpm lint
---
kind: pipeline
type: kubernetes
name: npm
- name: build module
image: glmdev/node-pnpm:latest
steps:
- name: node.js build
image: node:18
commands:
- "npm add --global pnpm"
- pnpm i
- pnpm build
- mkdir artifacts
- tar czf artifacts/extollo-lib.tar.gz lib
- name: create Gitea release
- name: gitea release
image: plugins/gitea-release
settings:
api_key:
from_secret: gitea_token
from_secret: GITEA_TOKEN
base_url: https://code.garrettmills.dev
checksum: md5
title: ${DRONE_TAG}
files: "artifacts/*"
when:
event: tag
status: success
- name: prepare NPM release
image: glmdev/node-pnpm:latest
commands:
- rm -rf artifacts
when:
event: tag
status: success
- name: create NPM release
- name: npm release
image: plugins/npm
settings:
username: extollo_bot
password:
from_secret: npm_password
from_secret: NPM_PASSWORD
email: extollo@garrettmills.dev
when:
event: tag
status: success
- name: send build success notifications
image: plugins/webhook
settings:
urls:
from_secret: notify_webhook_url
content_type: application/json
template: |
{
"title": "Drone-CI [extollo/lib @ ${DRONE_BUILD_NUMBER}]",
"message": "Build completed successfully.",
"priority": 4
}
when:
status: success
event:
exclude:
- pull_request
- tag
- name: send publish success notifications
image: plugins/webhook
settings:
urls:
from_secret: notify_webhook_url
content_type: application/json
template: |
{
"title": "Drone-CI [extollo/lib @ ${DRONE_BUILD_NUMBER}]",
"message": "Successfully published tag ${DRONE_TAG}.",
"priority": 4
}
when:
status: success
event: tag
- name: post build success comment to PR
image: tsakidev/giteacomment:latest
settings:
gitea_token:
from_secret: gitea_token
gitea_base_url: https://code.garrettmills.dev
comment: "Build ${DRONE_BUILD_NUMBER} completed successfully."
when:
status: success
event: pull_request
- name: send build error notifications
image: plugins/webhook
settings:
urls:
from_secret: notify_webhook_url
content_type: application/json
template: |
{
"title": "Drone-CI [extollo/lib @ ${DRONE_BUILD_NUMBER}]",
"message": "Build failed!",
"priority": 6
}
when:
status: failure
event:
exclude:
- pull_request
- name: post build error comment to PR
image: tsakidev/giteacomment:latest
settings:
gitea_token:
from_secret: gitea_token
gitea_base_url: https://code.garrettmills.dev
comment: "Build ${DRONE_BUILD_NUMBER} failed!"
when:
status: failure
event: pull_request

2
.gitignore vendored

@ -1,3 +1,5 @@
.undodir
# ---> JetBrains
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

@ -0,0 +1,4 @@
FROM joseluisq/static-web-server:2
COPY ./www /public

@ -22,7 +22,7 @@ Node.js provides an excellent platform for quickly getting an application up and
## Getting Started
Writing an application with Extollo is very straightforward if you are familiar with Node.js/TypeScript, or similar frameworks like Laravel.
Check out the [Getting Started](https://extollo.garrettmills.dev/pages/Documentation/Getting-Started.html) page site for more information.
Check out the [Getting Started](pages/Getting-Started.html) page site for more information.
## License & Philosophy
The Extollo project is, and will always be, free & libre software. The framework itself is open-source available [here](https://code.garrettmills.dev/Extollo), and is licensed under the terms of the MIT license. See the LICENSE file for more information.

@ -0,0 +1,5 @@
---
apiVersion: v1
kind: Namespace
metadata:
name: extollo

@ -0,0 +1,23 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: docs
namespace: extollo
spec:
selector:
matchLabels:
app: docs
template:
metadata:
name: docs
namespace: extollo
labels:
app: docs
spec:
containers:
- name: docs-www
image: registry.millslan.net/extollo/docs
imagePullPolicy: Always
ports:
- containerPort: 80

@ -0,0 +1,25 @@
---
apiVersion: v1
kind: Service
metadata:
name: docs-service
namespace: extollo
spec:
selector:
app: docs
ports:
- port: 80
targetPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: docs-service-lb
namespace: extollo
spec:
type: LoadBalancer
selector:
app: docs
ports:
- port: 80
targetPort: 80

@ -0,0 +1,13 @@
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: docs-tls
namespace: extollo
spec:
secretName: docs-tls-secret
dnsNames:
- 'extollo.garrettmills.dev'
issuerRef:
name: letsencrypt-ca
kind: ClusterIssuer

@ -0,0 +1,25 @@
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: docs-ingress
namespace: extollo
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: 'false'
spec:
tls:
- hosts:
- extollo.garrettmills.dev
secretName: docs-tls-secret
ingressClassName: nginx
rules:
- host: extollo.garrettmills.dev
http:
paths:
- pathType: Prefix
path: '/'
backend:
service:
name: docs-service
port:
number: 80

@ -1 +0,0 @@
# About the Extollo Project

@ -1,4 +1,3 @@
# Getting Started with Extollo
## Requirements

1608
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -1,6 +1,6 @@
{
"name": "@extollo/lib",
"version": "0.9.23",
"version": "0.14.14",
"description": "The framework library that lifts up your code.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
@ -8,47 +8,48 @@
"lib": "lib"
},
"dependencies": {
"@atao60/fse-cli": "^0.1.6",
"@atao60/fse-cli": "^0.1.7",
"@extollo/ui": "^0.1.0",
"@types/bcrypt": "^5.0.0",
"@types/busboy": "^0.2.3",
"@types/cli-table": "^0.3.0",
"@types/ioredis": "^4.26.6",
"@types/jsonwebtoken": "^8.5.8",
"@types/mime-types": "^2.1.0",
"@types/mkdirp": "^1.0.1",
"@types/busboy": "^0.2.4",
"@types/cli-table": "^0.3.1",
"@types/ioredis": "^4.28.10",
"@types/jsonwebtoken": "^8.5.9",
"@types/mime-types": "^2.1.1",
"@types/mkdirp": "^1.0.2",
"@types/negotiator": "^0.6.1",
"@types/node": "^14.17.4",
"@types/pg": "^8.6.0",
"@types/node": "^14.18.51",
"@types/pg": "^8.10.2",
"@types/pluralize": "^0.0.29",
"@types/pug": "^2.0.4",
"@types/rimraf": "^3.0.0",
"@types/ssh2": "^0.5.46",
"@types/uuid": "^8.3.0",
"bcrypt": "^5.0.1",
"@types/pug": "^2.0.6",
"@types/rimraf": "^3.0.2",
"@types/ssh2": "^0.5.52",
"@types/uuid": "^8.3.4",
"@types/ws": "^8.5.5",
"bcrypt": "^5.1.0",
"busboy": "^0.3.1",
"cli-table": "^0.3.6",
"cli-table": "^0.3.11",
"colors": "^1.4.0",
"dotenv": "^8.2.0",
"ioredis": "^4.27.6",
"dotenv": "^8.6.0",
"ioredis": "^4.28.5",
"jsonwebtoken": "^8.5.1",
"mime-types": "^2.1.31",
"mime-types": "^2.1.35",
"mkdirp": "^1.0.4",
"negotiator": "^0.6.2",
"node-fetch": "^3",
"pg": "^8.6.0",
"negotiator": "^0.6.3",
"node-fetch": "^3.3.1",
"pg": "^8.11.0",
"pluralize": "^8.0.0",
"pug": "^3.0.2",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"ssh2": "^1.1.0",
"ts-node": "^9.1.1",
"typedoc": "^0.20.36",
"typedoc-plugin-pages-fork": "^0.0.1",
"typedoc-plugin-sourcefile-url": "^1.0.6",
"typescript": "^4.2.3",
"sqlite": "^4.2.1",
"sqlite3": "^5.1.6",
"ssh2": "^1.13.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.5",
"uuid": "^8.3.2",
"zod": "^3.11.6"
"ws": "^8.13.0",
"zod": "^3.21.4"
},
"scripts": {
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register 'tests/**/*.ts'",
@ -56,6 +57,7 @@
"app": "tsc && node lib/index.js",
"prepare": "pnpm run build",
"docs:build": "typedoc --options typedoc.json",
"docs:build:docker": "pnpm run docs:build && docker image build docs -t ${DOCKER_REGISTRY}/extollo/docs:latest && docker push ${DOCKER_REGISTRY}/extollo/docs:latest",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint --fix . --ext .ts"
},
@ -71,16 +73,19 @@
"author": "garrettmills <shout@garrettmills.dev>",
"license": "MIT",
"devDependencies": {
"@types/chai": "^4.2.22",
"@types/mocha": "^9.0.0",
"@types/sinon": "^10.0.6",
"@knodes/typedoc-plugin-pages": "^0.23.4",
"@types/chai": "^4.3.5",
"@types/mocha": "^9.1.1",
"@types/sinon": "^10.0.15",
"@types/wtfnode": "^0.7.0",
"@typescript-eslint/eslint-plugin": "^4.26.0",
"@typescript-eslint/parser": "^4.26.0",
"chai": "^4.3.4",
"eslint": "^7.27.0",
"mocha": "^9.1.3",
"@typescript-eslint/eslint-plugin": "^5.59.11",
"@typescript-eslint/parser": "^5.59.11",
"chai": "^4.3.7",
"eslint": "^8.42.0",
"lunr": "^2.3.9",
"mocha": "^9.2.2",
"sinon": "^12.0.1",
"typedoc": "^0.23.28",
"wtfnode": "^0.9.1"
},
"extollo": {

File diff suppressed because it is too large Load Diff

@ -27,6 +27,15 @@ export class Authentication extends Unit {
protected providers: {[name: string]: LoginProvider<LoginProviderConfig>} = {}
getProvider(name: string): LoginProvider<LoginProviderConfig> {
const provider = this.providers[name]
if ( !provider ) {
throw new ErrorWithContext('Invalid auth provider name: ' + name, { name })
}
return provider
}
async up(): Promise<void> {
this.middleware.registerNamespace('@auth', this.getMiddlewareResolver())

@ -80,6 +80,9 @@ export abstract class SecurityContext {
/**
* Assuming a user is still authenticated in the context,
* try to look up and fill in the user.
*
* If there is NO USER to be resumed, then the method should flush
* the user from this context.
*/
abstract resume(): Awaitable<void>

@ -33,7 +33,10 @@ export class SessionSecurityContext extends SecurityContext {
if ( user ) {
this.authenticatedUser = user
await this.bus.push(new UserAuthenticationResumedEvent(user, this))
return
}
}
this.authenticatedUser = undefined
}
}

@ -0,0 +1,44 @@
import {SecurityContext} from './SecurityContext'
import {AuthenticatableRepository} from '../types'
import {Awaitable} from '../../util'
import {Inject} from '../../di'
import {Request} from '../../http/lifecycle/Request'
import {OAuth2Token, TokenRepository} from '../server/types'
import {UserAuthenticationResumedEvent} from '../event/UserAuthenticationResumedEvent'
export class TokenSecurityContext extends SecurityContext {
@Inject()
protected readonly request!: Request
@Inject()
protected readonly tokens!: TokenRepository
constructor(
public readonly repository: AuthenticatableRepository,
) {
super(repository, 'token')
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
persist(): Awaitable<void> {}
async resume(): Promise<void> {
if ( !this.request.hasInstance(OAuth2Token) ) {
return
}
const token: OAuth2Token = this.request.getExistingInstance(OAuth2Token)
if ( !token.userId ) {
return
}
const user = await this.repository.getByIdentifier(token.userId)
if ( user ) {
this.authenticatedUser = user
await this.bus.push(new UserAuthenticationResumedEvent(user, this))
return
}
this.authenticatedUser = undefined
}
}

@ -0,0 +1,27 @@
import {BaseEvent, BaseSerializer, ObjectSerializer} from '../../support/bus'
import {Awaitable} from '../../util'
/** An event raised when a required auth check has failed. */
export class AuthCheckFailed extends BaseEvent {
eventName = '@extollo/lib:AuthCheckFailed'
}
/** Serializes AuthCheckFailed events. */
@ObjectSerializer()
export class AuthCheckFailedSerializer extends BaseSerializer<AuthCheckFailed, { authCheckFailed: true }> {
protected decodeSerial(): Awaitable<AuthCheckFailed> {
return new AuthCheckFailed()
}
protected encodeActual(): Awaitable<{ authCheckFailed: true }> {
return { authCheckFailed: true }
}
protected getName(): string {
return '@extollo/lib:AuthCheckFailedSerializer'
}
matchActual(some: AuthCheckFailed): boolean {
return some instanceof AuthCheckFailed
}
}

@ -2,18 +2,23 @@ export * from './types'
export * from './AuthenticatableAlreadyExistsError'
export * from './NotAuthorizedError'
export * from './Authentication'
export * from './repository/AuthenticatableRepositoryFactory'
export * from './context/SecurityContext'
export * from './context/SessionSecurityContext'
export * from './context/TokenSecurityContext'
export * from './event/AuthenticationEvent'
export * from './event/UserAuthenticatedEvent'
export * from './event/UserAuthenticationResumedEvent'
export * from './event/UserFlushedEvent'
export * from './event/AuthCheckFailed'
export * from './middleware/AuthRequiredMiddleware'
export * from './middleware/GuestRequiredMiddleware'
export * from './middleware/SessionAuthMiddleware'
export * from './middleware/TokenAuthMiddleware'
export * from './middleware/ScopeRequiredMiddleware'
export * from './provider/basic/BasicLoginAttempt'
export * from './provider/basic/BasicLoginProvider'
@ -29,6 +34,8 @@ export * from './repository/orm/ORMUserRepository'
export * from './config'
export * from './webSocketAuthCheck'
export * from './server/types'
export * from './server/models/OAuth2TokenModel'
export * from './server/repositories/ConfigClientRepository'

@ -0,0 +1,33 @@
import {Middleware} from '../../http/routing/Middleware'
import {ResponseObject} from '../../http/routing/Route'
import {OAuth2Token} from '../server/types'
import {HTTPError} from '../../http/HTTPError'
import {HTTPStatus, Pipeline} from '../../util'
import {Request} from '../../http/lifecycle/Request'
import {Constructable, Container} from '../../di'
export class ScopeRequiredMiddleware extends Middleware {
constructor(
protected readonly request: Request,
protected readonly scope: string,
) {
super(request)
}
apply(): ResponseObject {
if ( !this.request.hasInstance(OAuth2Token) ) {
throw new HTTPError(HTTPStatus.UNAUTHORIZED, 'Must specify an OAuth2 token.')
}
const token: OAuth2Token = this.request.getExistingInstance(OAuth2Token)
if ( typeof token.scope !== 'undefined' && token.scope !== this.scope ) {
throw new HTTPError(HTTPStatus.UNAUTHORIZED, 'Insufficient token permissions (requires: ' + this.scope + ')')
}
}
}
export const scope = (name: string): Constructable<ScopeRequiredMiddleware> => {
return new Pipeline<Container, ScopeRequiredMiddleware>(
container => container.make(ScopeRequiredMiddleware, container, name),
)
}

@ -1,10 +1,8 @@
import {Middleware} from '../../http/routing/Middleware'
import {Inject, Injectable, Instantiable} from '../../di'
import {Inject, Injectable} from '../../di'
import {Config} from '../../service/Config'
import {Logging} from '../../service/Logging'
import {AuthenticatableRepository} from '../types'
import {Maybe} from '../../util'
import {AuthenticationConfig, isAuthenticationConfig} from '../config'
import {ResponseObject} from '../../http/routing/Route'
import {SessionSecurityContext} from '../context/SessionSecurityContext'
import {SecurityContext} from '../context/SecurityContext'
@ -23,22 +21,9 @@ export class SessionAuthMiddleware extends Middleware {
async apply(): Promise<ResponseObject> {
this.logging.debug('Applying session auth middleware.')
const context = <SessionSecurityContext> this.make(SessionSecurityContext, this.getRepository())
const repo = <AuthenticatableRepository> this.make(AuthenticatableRepository)
const context = <SessionSecurityContext> this.make(SessionSecurityContext, repo)
this.request.registerSingletonInstance(SecurityContext, context)
await context.resume()
}
/**
* Build the correct AuthenticatableRepository based on the auth config.
* @protected
*/
protected getRepository(): AuthenticatableRepository {
const config: Maybe<AuthenticationConfig> = this.config.get('auth')
if ( !isAuthenticationConfig(config) ) {
throw new TypeError('Invalid authentication config.')
}
const repo: Instantiable<AuthenticatableRepository> = config.storage
return this.make(repo)
}
}

@ -0,0 +1,45 @@
import {Middleware} from '../../http/routing/Middleware'
import {Inject, Injectable} from '../../di'
import {Config} from '../../service/Config'
import {Logging} from '../../service/Logging'
import {AuthenticatableRepository} from '../types'
import {ResponseObject} from '../../http/routing/Route'
import {SecurityContext} from '../context/SecurityContext'
import {TokenSecurityContext} from '../context/TokenSecurityContext'
import {OAuth2Token, oauth2TokenString, TokenRepository} from '../server/types'
/**
* Injects a TokenSecurityContext into the request and attempts to
* resume the user's authentication.
*/
@Injectable()
export class TokenAuthMiddleware extends Middleware {
@Inject()
protected readonly config!: Config
@Inject()
protected readonly logging!: Logging
@Inject()
protected readonly tokens!: TokenRepository
async apply(): Promise<ResponseObject> {
this.logging.debug('Applying token auth middleware.')
let tokenString = this.request.getHeader('Authorization')
if ( Array.isArray(tokenString) ) {
tokenString = tokenString[0]
}
if ( tokenString ) {
const token = await this.tokens.decode(oauth2TokenString(tokenString))
if ( token ) {
this.request.registerSingletonInstance(OAuth2Token, token)
}
}
const repo = <AuthenticatableRepository> this.make(AuthenticatableRepository)
const context = <TokenSecurityContext> this.make(TokenSecurityContext, repo)
this.request.registerSingletonInstance(SecurityContext, context)
await context.resume()
}
}

@ -11,12 +11,7 @@ import {ErrorWithContext, uuid4, fetch} from '../../../util'
export class CoreIDLoginProvider extends OAuth2LoginProvider<OAuth2LoginProviderConfig> {
protected async callback(request: Request): Promise<Authenticatable> {
// Get authentication_code from the request
const code = String(request.input('code') || '')
if ( !code ) {
throw new ErrorWithContext('Unable to authenticate user: missing login code', {
input: request.input(),
})
}
const code = request.safe('code').string()
// Get OAuth2 token from CoreID
const token = await this.getToken(code)
@ -97,5 +92,8 @@ export class CoreIDLoginProvider extends OAuth2LoginProvider<OAuth2LoginProvider
user.email = data.email
user.tagline = data.tagline
user.photoUrl = data.profile_photo
if ( typeof user.save === 'function' ) {
user.save()
}
}
}

@ -0,0 +1,23 @@
import {Instantiable, FactoryProducer} from '../../di'
import {AuthenticatableRepository} from '../types'
import {ORMUserRepository} from './orm/ORMUserRepository'
import {ConfiguredSingletonFactory} from '../../di/factory/ConfiguredSingletonFactory'
/**
* A dependency injection factory that matches the abstract ClientRepository class
* and produces an instance of the configured repository driver implementation.
*/
@FactoryProducer()
export class AuthenticatableRepositoryFactory extends ConfiguredSingletonFactory<AuthenticatableRepository> {
protected getConfigKey(): string {
return 'auth.storage'
}
protected getDefaultImplementation(): Instantiable<AuthenticatableRepository> {
return ORMUserRepository
}
protected getAbstractImplementation(): any {
return AuthenticatableRepository
}
}

@ -1,5 +1,5 @@
import {Controller} from '../../http/Controller'
import {Injectable} from '../../di'
import {Inject, Injectable} from '../../di'
import {ResponseObject, Route} from '../../http/routing/Route'
import {Request} from '../../http/lifecycle/Request'
import {Session} from '../../http/session/Session'
@ -10,6 +10,7 @@ import {
OAuth2Scope,
RedemptionCodeRepository,
ScopeRepository,
TokenRepository,
} from './types'
import {HTTPError} from '../../http/HTTPError'
import {HTTPStatus, Maybe} from '../../util'
@ -17,9 +18,23 @@ import {view} from '../../http/response/ViewResponseFactory'
import {SecurityContext} from '../context/SecurityContext'
import {redirect} from '../../http/response/RedirectResponseFactory'
import {AuthRequiredMiddleware} from '../middleware/AuthRequiredMiddleware'
import {one} from '../../http/response/api'
import {AuthenticatableRepository} from '../types'
import {Logging} from '../../service/Logging'
export enum GrantType {
Client = 'client_credentials',
Password = 'password',
Code = 'authorization_code',
}
export const grantTypes: GrantType[] = [GrantType.Client, GrantType.Code, GrantType.Password]
@Injectable()
export class OAuth2Server extends Controller {
@Inject()
protected readonly logging!: Logging
public static routes(): void {
Route.get('/oauth2/authorize')
.alias('@oauth2:authorize')
@ -33,25 +48,52 @@ export class OAuth2Server extends Controller {
.passingRequest()
.calls<OAuth2Server>(OAuth2Server, x => x.authorizeAndRedirect)
Route.post('/oauth2/redeem')
.alias('@oauth2:authorize:redeem')
Route.post('/oauth2/token')
.alias('@oauth2:token')
.passingRequest()
.calls<OAuth2Server>(OAuth2Server, x => x.redeemToken)
.calls<OAuth2Server>(OAuth2Server, x => x.issue)
}
async redeemToken(request: Request): Promise<ResponseObject> {
const authParts = String(request.getHeader('Authorization')).split(':')
if ( authParts.length !== 2 ) {
throw new HTTPError(HTTPStatus.BAD_REQUEST)
async issue(request: Request): Promise<ResponseObject> {
const grant = request.safe('grant_type').in(grantTypes)
const client = await this.getClientFromRequest(request)
if ( grant === GrantType.Client ) {
return this.issueFromClient(request, client)
} else if ( grant === GrantType.Code ) {
return this.issueFromCode(request, client)
} else if ( grant === GrantType.Password ) {
return this.issueFromCredential(request, client)
}
}
const clientRepo = <ClientRepository> request.make(ClientRepository)
const [clientId, clientSecret] = authParts
const client = await clientRepo.find(clientId)
if ( !client || client.secret !== clientSecret ) {
throw new HTTPError(HTTPStatus.UNAUTHORIZED)
protected async issueFromCredential(request: Request, client: OAuth2Client): Promise<ResponseObject> {
const scope = String(this.request.input('scope') ?? '') || undefined
const username = this.request.safe('username').string()
const password = this.request.safe('password').string()
this.logging.verbose('Attempting password grant token issue...')
this.logging.verbose({
scope,
username,
client,
})
const userRepo = <AuthenticatableRepository> request.make(AuthenticatableRepository)
const user = await userRepo.getByIdentifier(username)
if ( !user || !(await user.validateCredential(password)) ) {
throw new HTTPError(HTTPStatus.BAD_REQUEST)
}
const tokenRepo = <TokenRepository> request.make(TokenRepository)
const token = await tokenRepo.issue(user, client, scope)
return one({
token: await tokenRepo.encode(token),
})
}
protected async issueFromCode(request: Request, client: OAuth2Client): Promise<ResponseObject> {
const scope = String(this.request.input('scope') ?? '') || undefined
const codeRepo = <RedemptionCodeRepository> request.make(RedemptionCodeRepository)
const codeString = request.safe('code').string()
const code = await codeRepo.find(codeString)
@ -59,15 +101,56 @@ export class OAuth2Server extends Controller {
throw new HTTPError(HTTPStatus.BAD_REQUEST)
}
const userRepo = <AuthenticatableRepository> request.make(AuthenticatableRepository)
const user = await userRepo.getByIdentifier(code.userId)
if ( !user ) {
throw new HTTPError(HTTPStatus.BAD_REQUEST)
}
const tokenRepo = <TokenRepository> request.make(TokenRepository)
const token = await tokenRepo.issue(user, client, scope)
return one({
token: await tokenRepo.encode(token),
})
}
protected async issueFromClient(request: Request, client: OAuth2Client): Promise<ResponseObject> {
const scope = String(this.request.input('scope') ?? '') || undefined
const tokenRepo = <TokenRepository> request.make(TokenRepository)
const token = await tokenRepo.issue(undefined, client, scope)
return one({
token: await tokenRepo.encode(token),
})
}
protected async getClientFromRequest(request: Request): Promise<OAuth2Client> {
const authParts = String(request.getHeader('Authorization')).split(':')
if ( authParts.length !== 2 ) {
throw new HTTPError(HTTPStatus.BAD_REQUEST)
}
this.logging.debug('Client auth parts:')
this.logging.debug(authParts)
const clientRepo = <ClientRepository> request.make(ClientRepository)
const [clientId, clientSecret] = authParts
const client = await clientRepo.find(clientId)
this.logging.verbose('Client:')
this.logging.verbose(client)
if ( !client || client.secret !== clientSecret ) {
throw new HTTPError(HTTPStatus.UNAUTHORIZED)
}
return client
}
async authorizeAndRedirect(request: Request): Promise<ResponseObject> {
// Look up the client in the client repo
const session = <Session> request.make(Session)
const clientId = session.safe('oauth2.authorize.clientId').string()
const client = await this.getClient(request, clientId)
const client = await this.getClientFromRequest(request)
const flowType = session.safe('oauth2.authorize.flow').in(client.allowedFlows)
if ( flowType === OAuth2FlowType.code ) {
return this.authorizeCodeFlow(request, client)

@ -14,7 +14,7 @@ export class OAuth2TokenModel extends Model<OAuth2TokenModel> implements OAuth2T
}
@Field(FieldType.varchar, 'user_id')
public userId!: string
public userId?: string
@Field(FieldType.varchar, 'client_id')
public clientId!: string

@ -1,74 +1,23 @@
import {
AbstractFactory,
Container,
DependencyRequirement,
PropertyDependency,
isInstantiable,
DEPENDENCY_KEYS_METADATA_KEY,
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, Instantiable, FactoryProducer,
} from '../../../di'
import {Collection, ErrorWithContext} from '../../../util'
import {Config} from '../../../service/Config'
import {Instantiable, FactoryProducer} from '../../../di'
import {ClientRepository} from '../types'
import {ConfigClientRepository} from './ConfigClientRepository'
import {ConfiguredSingletonFactory} from '../../../di/factory/ConfiguredSingletonFactory'
/**
* A dependency injection factory that matches the abstract ClientRepository class
* and produces an instance of the configured repository driver implementation.
*/
@FactoryProducer()
export class ClientRepositoryFactory extends AbstractFactory<ClientRepository> {
protected get config(): Config {
return Container.getContainer().make<Config>(Config)
export class ClientRepositoryFactory extends ConfiguredSingletonFactory<ClientRepository> {
protected getConfigKey(): string {
return 'oauth2.repository.client'
}
produce(): ClientRepository {
return new (this.getClientRepositoryClass())()
protected getDefaultImplementation(): Instantiable<ClientRepository> {
return ConfigClientRepository
}
match(something: unknown): boolean {
return something === ClientRepository
}
getDependencyKeys(): Collection<DependencyRequirement> {
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getClientRepositoryClass())
if ( meta ) {
return meta
}
return new Collection<DependencyRequirement>()
}
getInjectedProperties(): Collection<PropertyDependency> {
const meta = new Collection<PropertyDependency>()
let currentToken = this.getClientRepositoryClass()
do {
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
if ( loadedMeta ) {
meta.concat(loadedMeta)
}
currentToken = Object.getPrototypeOf(currentToken)
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
return meta
}
/**
* Return the instantiable class of the configured client repository backend.
* @protected
* @return Instantiable<ClientRepository>
*/
protected getClientRepositoryClass(): Instantiable<ClientRepository> {
const ClientRepositoryClass = this.config.get('oauth2.repository.client', ConfigClientRepository)
if ( !isInstantiable(ClientRepositoryClass) || !(ClientRepositoryClass.prototype instanceof ClientRepository) ) {
const e = new ErrorWithContext('Provided client repository class does not extend from @extollo/lib.ClientRepository')
e.context = {
configKey: 'oauth2.repository.client',
class: ClientRepositoryClass.toString(),
}
}
return ClientRepositoryClass
protected getAbstractImplementation(): any {
return ClientRepository
}
}

@ -1,10 +1,11 @@
import {isOAuth2Token, OAuth2Client, OAuth2Token, oauth2TokenString, OAuth2TokenString, TokenRepository} from '../types'
import {Inject, Injectable} from '../../../di'
import {Maybe} from '../../../util'
import {ErrorWithContext, Maybe} from '../../../util'
import {OAuth2TokenModel} from '../models/OAuth2TokenModel'
import {Config} from '../../../service/Config'
import * as jwt from 'jsonwebtoken'
import {Authenticatable} from '../../types'
import {make} from '../../../make'
@Injectable()
export class ORMTokenRepository extends TokenRepository {
@ -20,17 +21,19 @@ export class ORMTokenRepository extends TokenRepository {
}
}
async issue(user: Authenticatable, client: OAuth2Client, scope?: string): Promise<OAuth2Token> {
async issue(user: Authenticatable|undefined, client: OAuth2Client, scope?: string): Promise<OAuth2Token> {
const expiration = this.config.safe('outh2.token.lifetimeSeconds')
.or(60 * 60 * 6)
.integer() * 1000
const token = new OAuth2TokenModel()
const token = make<OAuth2TokenModel>(OAuth2TokenModel)
token.scope = scope
token.userId = String(user.getUniqueIdentifier())
token.clientId = client.id
token.issued = new Date()
token.expires = new Date(Math.floor(Date.now() + expiration))
if ( user ) {
token.userId = String(user.getUniqueIdentifier())
}
await token.save()
return token
@ -40,17 +43,20 @@ export class ORMTokenRepository extends TokenRepository {
const secret = this.config.safe('oauth2.secret').string()
const payload = {
id: token.id,
userId: token.userId,
clientId: token.clientId,
iat: Math.floor(token.issued.valueOf() / 1000),
exp: Math.floor(token.expires.valueOf() / 1000),
...(token.userId ? { userId: token.userId } : {}),
...(token.scope ? { scope: token.scope } : {}),
}
const generated = await new Promise<string>((res, rej) => {
jwt.sign(payload, secret, {}, (err, gen) => {
if (err || err === null || !gen) {
rej(err || new Error('Unable to encode JWT.'))
if (err || !gen) {
rej(err || new ErrorWithContext('Unable to encode JWT.', {
payload,
gen,
}))
} else {
res(gen)
}
@ -74,10 +80,10 @@ export class ORMTokenRepository extends TokenRepository {
const value = {
id: decoded.id,
userId: decoded.userId,
clientId: decoded.clientId,
issued: new Date(decoded.iat * 1000),
expires: new Date(decoded.exp * 1000),
...(decoded.userId ? { userId: decoded.userId } : {}),
...(decoded.scope ? { scope: decoded.scope } : {}),
}

@ -1,74 +1,23 @@
import {
AbstractFactory,
Container,
DependencyRequirement,
PropertyDependency,
isInstantiable,
DEPENDENCY_KEYS_METADATA_KEY,
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, Instantiable, FactoryProducer,
} from '../../../di'
import {Collection, ErrorWithContext} from '../../../util'
import {Config} from '../../../service/Config'
import {Instantiable, FactoryProducer} from '../../../di'
import {RedemptionCodeRepository} from '../types'
import {CacheRedemptionCodeRepository} from './CacheRedemptionCodeRepository'
import {ConfiguredSingletonFactory} from '../../../di/factory/ConfiguredSingletonFactory'
/**
* A dependency injection factory that matches the abstract RedemptionCodeRepository class
* and produces an instance of the configured repository driver implementation.
*/
@FactoryProducer()
export class RedemptionCodeRepositoryFactory extends AbstractFactory<RedemptionCodeRepository> {
protected get config(): Config {
return Container.getContainer().make<Config>(Config)
export class RedemptionCodeRepositoryFactory extends ConfiguredSingletonFactory<RedemptionCodeRepository> {
protected getConfigKey(): string {
return 'oauth2.repository.client'
}
produce(): RedemptionCodeRepository {
return new (this.getRedemptionCodeRepositoryClass())()
protected getDefaultImplementation(): Instantiable<RedemptionCodeRepository> {
return CacheRedemptionCodeRepository
}
match(something: unknown): boolean {
return something === RedemptionCodeRepository
}
getDependencyKeys(): Collection<DependencyRequirement> {
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getRedemptionCodeRepositoryClass())
if ( meta ) {
return meta
}
return new Collection<DependencyRequirement>()
}
getInjectedProperties(): Collection<PropertyDependency> {
const meta = new Collection<PropertyDependency>()
let currentToken = this.getRedemptionCodeRepositoryClass()
do {
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
if ( loadedMeta ) {
meta.concat(loadedMeta)
}
currentToken = Object.getPrototypeOf(currentToken)
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
return meta
}
/**
* Return the instantiable class of the configured client repository backend.
* @protected
* @return Instantiable<RedemptionCodeRepository>
*/
protected getRedemptionCodeRepositoryClass(): Instantiable<RedemptionCodeRepository> {
const RedemptionCodeRepositoryClass = this.config.get('oauth2.repository.client', CacheRedemptionCodeRepository)
if ( !isInstantiable(RedemptionCodeRepositoryClass) || !(RedemptionCodeRepositoryClass.prototype instanceof RedemptionCodeRepository) ) {
const e = new ErrorWithContext('Provided client repository class does not extend from @extollo/lib.RedemptionCodeRepository')
e.context = {
configKey: 'oauth2.repository.client',
class: RedemptionCodeRepositoryClass.toString(),
}
}
return RedemptionCodeRepositoryClass
protected getAbstractImplementation(): any {
return RedemptionCodeRepository
}
}

@ -1,74 +1,23 @@
import {
AbstractFactory,
Container,
DependencyRequirement,
PropertyDependency,
isInstantiable,
DEPENDENCY_KEYS_METADATA_KEY,
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, Instantiable, FactoryProducer,
} from '../../../di'
import {Collection, ErrorWithContext} from '../../../util'
import {Config} from '../../../service/Config'
import {Instantiable, FactoryProducer} from '../../../di'
import {ScopeRepository} from '../types'
import {ConfigScopeRepository} from './ConfigScopeRepository'
import {ConfiguredSingletonFactory} from '../../../di/factory/ConfiguredSingletonFactory'
/**
* A dependency injection factory that matches the abstract ScopeRepository class
* and produces an instance of the configured repository driver implementation.
*/
@FactoryProducer()
export class ScopeRepositoryFactory extends AbstractFactory<ScopeRepository> {
protected get config(): Config {
return Container.getContainer().make<Config>(Config)
export class ScopeRepositoryFactory extends ConfiguredSingletonFactory<ScopeRepository> {
protected getConfigKey(): string {
return 'oauth2.repository.scope'
}
produce(): ScopeRepository {
return new (this.getScopeRepositoryClass())()
protected getDefaultImplementation(): Instantiable<ScopeRepository> {
return ConfigScopeRepository
}
match(something: unknown): boolean {
return something === ScopeRepository
}
getDependencyKeys(): Collection<DependencyRequirement> {
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getScopeRepositoryClass())
if ( meta ) {
return meta
}
return new Collection<DependencyRequirement>()
}
getInjectedProperties(): Collection<PropertyDependency> {
const meta = new Collection<PropertyDependency>()
let currentToken = this.getScopeRepositoryClass()
do {
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
if ( loadedMeta ) {
meta.concat(loadedMeta)
}
currentToken = Object.getPrototypeOf(currentToken)
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
return meta
}
/**
* Return the instantiable class of the configured scope repository backend.
* @protected
* @return Instantiable<ScopeRepository>
*/
protected getScopeRepositoryClass(): Instantiable<ScopeRepository> {
const ScopeRepositoryClass = this.config.get('oauth2.repository.scope', ConfigScopeRepository)
if ( !isInstantiable(ScopeRepositoryClass) || !(ScopeRepositoryClass.prototype instanceof ScopeRepository) ) {
const e = new ErrorWithContext('Provided client repository class does not extend from @extollo/lib.ScopeRepository')
e.context = {
configKey: 'oauth2.repository.client',
class: ScopeRepositoryClass.toString(),
}
}
return ScopeRepositoryClass
protected getAbstractImplementation(): any {
return ScopeRepository
}
}

@ -1,74 +1,23 @@
import {
AbstractFactory,
Container,
DependencyRequirement,
PropertyDependency,
isInstantiable,
DEPENDENCY_KEYS_METADATA_KEY,
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, Instantiable, FactoryProducer,
} from '../../../di'
import {Collection, ErrorWithContext} from '../../../util'
import {Config} from '../../../service/Config'
import {Instantiable, FactoryProducer} from '../../../di'
import {TokenRepository} from '../types'
import {ORMTokenRepository} from './ORMTokenRepository'
import {ConfiguredSingletonFactory} from '../../../di/factory/ConfiguredSingletonFactory'
/**
* A dependency injection factory that matches the abstract TokenRepository class
* and produces an instance of the configured repository driver implementation.
*/
@FactoryProducer()
export class TokenRepositoryFactory extends AbstractFactory<TokenRepository> {
protected get config(): Config {
return Container.getContainer().make<Config>(Config)
export class TokenRepositoryFactory extends ConfiguredSingletonFactory<TokenRepository> {
protected getConfigKey(): string {
return 'oauth2.repository.token'
}
produce(): TokenRepository {
return new (this.getTokenRepositoryClass())()
protected getDefaultImplementation(): Instantiable<TokenRepository> {
return ORMTokenRepository
}
match(something: unknown): boolean {
return something === TokenRepository
}
getDependencyKeys(): Collection<DependencyRequirement> {
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getTokenRepositoryClass())
if ( meta ) {
return meta
}
return new Collection<DependencyRequirement>()
}
getInjectedProperties(): Collection<PropertyDependency> {
const meta = new Collection<PropertyDependency>()
let currentToken = this.getTokenRepositoryClass()
do {
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
if ( loadedMeta ) {
meta.concat(loadedMeta)
}
currentToken = Object.getPrototypeOf(currentToken)
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
return meta
}
/**
* Return the instantiable class of the configured token repository backend.
* @protected
* @return Instantiable<TokenRepository>
*/
protected getTokenRepositoryClass(): Instantiable<TokenRepository> {
const TokenRepositoryClass = this.config.get('oauth2.repository.token', ORMTokenRepository)
if ( !isInstantiable(TokenRepositoryClass) || !(TokenRepositoryClass.prototype instanceof TokenRepository) ) {
const e = new ErrorWithContext('Provided token repository class does not extend from @extollo/lib.TokenRepository')
e.context = {
configKey: 'oauth2.repository.client',
class: TokenRepositoryClass.toString(),
}
}
return TokenRepositoryClass
protected getAbstractImplementation(): any {
return TokenRepository
}
}

@ -83,13 +83,19 @@ export abstract class ScopeRepository {
abstract findByName(name: string): Awaitable<Maybe<OAuth2Scope>>
}
export interface OAuth2Token {
id: string
userId: AuthenticatableIdentifier
clientId: string
issued: Date
expires: Date
scope?: string
export abstract class OAuth2Token {
abstract id: string
/** When undefined, these are client credentials. */
abstract userId?: AuthenticatableIdentifier
abstract clientId: string
abstract issued: Date
abstract expires: Date
abstract scope?: string
}
export type OAuth2TokenString = TypeTag<'@extollo/lib.OAuth2TokenString'> & string
@ -105,7 +111,6 @@ export function isOAuth2Token(what: unknown): what is OAuth2Token {
if (
!hasOwnProperty(what, 'id')
|| !hasOwnProperty(what, 'userId')
|| !hasOwnProperty(what, 'clientId')
|| !hasOwnProperty(what, 'issued')
|| !hasOwnProperty(what, 'expires')
@ -115,7 +120,7 @@ export function isOAuth2Token(what: unknown): what is OAuth2Token {
if (
typeof what.id !== 'string'
|| !(typeof what.userId === 'string' || typeof what.userId === 'number')
|| (hasOwnProperty(what, 'userId') && !(typeof what.userId === 'string' || typeof what.userId === 'number'))
|| typeof what.clientId !== 'string'
|| !(what.issued instanceof Date)
|| !(what.expires instanceof Date)
@ -129,7 +134,7 @@ export function isOAuth2Token(what: unknown): what is OAuth2Token {
export abstract class TokenRepository {
abstract find(id: string): Awaitable<Maybe<OAuth2Token>>
abstract issue(user: Authenticatable, client: OAuth2Client, scope?: string): Awaitable<OAuth2Token>
abstract issue(user: Authenticatable|undefined, client: OAuth2Client, scope?: string): Awaitable<OAuth2Token>
abstract decode(token: OAuth2TokenString): Awaitable<Maybe<OAuth2Token>>

@ -0,0 +1,36 @@
import {Container} from '../di'
import {RequestLocalStorage} from '../http/RequestLocalStorage'
import {Session} from '../http/session/Session'
import {Logging} from '../service/Logging'
import {SecurityContext} from './context/SecurityContext'
import {Bus} from '../support/bus'
import {AuthCheckFailed} from './event/AuthCheckFailed'
/**
* Check if the security context for the current request's web socket is still valid.
* If not, raise an `AuthCheckFailed` event. This is meant to be used as a subscriber
* to `WebSocketHealthCheckEvent` on the request.
*
* @see AuthCheckFailed
*/
export async function webSocketAuthCheck(): Promise<void> {
const request = Container.getContainer()
.make<RequestLocalStorage>(RequestLocalStorage)
.get()
const logging = request.make<Logging>(Logging)
try {
// Try to re-load the session in case we're using the SessionSecurityContext
await request.make<Session>(Session).load()
} catch (e: unknown) {
logging.error(e)
}
const security = request.make<SecurityContext>(SecurityContext)
await security.resume()
if ( !security.hasUser() ) {
await request.make<Bus>(Bus).push(new AuthCheckFailed())
}
}

@ -1,16 +1,26 @@
import {Directive} from '../Directive'
import {Directive, OptionDefinition} from '../Directive'
import * as colors from 'colors/safe'
import * as repl from 'repl'
import {DependencyKey} from '../../di'
// import * as tsNode from 'ts-node'
import {globalRegistry} from '../../util'
/**
* Launch an interactive REPL shell from within the application.
* This is very useful for debugging and testing things during development.
*
* By default, the shell launches a TypeScript interpreter, but you can use
* the `--js` flag to get a JavaScript interpreter.
*
* @example
* ```sh
* pnpm cli -- shell
* pnpm cli -- shell --js
* ```
*/
export class ShellDirective extends Directive {
protected options: any = {
welcome: `powered by Extollo, © ${(new Date()).getFullYear()} Garrett Mills\nAccess your application using the "app" global.`,
prompt: `${colors.blue('(')}extollo${colors.blue(') ➤ ')}`,
welcome: `powered by Extollo, © ${(new Date()).getFullYear()} Garrett Mills\nAccess your application using the "app" global and @extollo/lib using the "lib" global.`,
prompt: `${colors.blue('(')}extollo${colors.blue(') ➤ ')}`,
}
/**
@ -31,17 +41,57 @@ export class ShellDirective extends Directive {
return ''
}
getOptions(): OptionDefinition[] {
return [
'--js | launch in JavaScript mode instead of TypeScript',
]
}
async handle(): Promise<void> {
const state: any = {
globalRegistry,
app: this.app(),
lib: await import('../../index'),
make: (target: DependencyKey, ...parameters: any[]) => this.make(target, ...parameters),
exports: {},
}
await new Promise<void>(res => {
// Currently, there's no way to programmatically access the async context
// of the REPL from this directive w/o requiring the user to perform manual
// actions. So, instead, override the context on the GlobalRegistry to make
// the current one the global default.
globalRegistry.forceContextOverride()
// Create the ts-node compiler service.
// const replService = tsNode.createRepl()
// const service = tsNode.create({...replService.evalAwarePartialHost})
// replService.setService(service)
// We global these values into the REPL's state directly (using the `state` object
// above), but since we're using a separate ts-node interpreter, we need to make it
// aware of the globals using declaration syntax.
// replService.evalCode(`
// declare const lib: typeof import('@extollo/lib');
// declare const app: typeof lib['Application'];
// declare const globalRegistry: typeof lib['globalRegistry'];
// `)
// Print the welome message and start the interpreter
this.nativeOutput(this.options.welcome)
this.repl = repl.start(this.options.prompt)
this.repl = repl.start({
// Causes the REPL to use the ts-node interpreter service:
// eval: !this.option('js', false) ? (...args) => replService.nodeEval(...args) : undefined,
prompt: this.options.prompt,
useGlobal: true,
useColors: true,
terminal: true,
preview: true,
})
// Add our globals into the REPL's context
Object.assign(this.repl.context, state)
// Wait for the REPL to exit
this.repl.on('exit', () => res())
})
}

@ -8,7 +8,15 @@ import {
TypedDependencyKey,
} from './types'
import {AbstractFactory} from './factory/AbstractFactory'
import {Awaitable, collect, Collection, ErrorWithContext, globalRegistry, hasOwnProperty, logIfDebugging} from '../util'
import {
Awaitable,
collect,
Collection,
globalRegistry,
hasOwnProperty,
logIfDebugging, Unsubscribe,
} from '../util'
import {ErrorWithContext, withErrorContext} from '../util/error/ErrorWithContext'
import {Factory} from './factory/Factory'
import {DuplicateFactoryKeyError} from './error/DuplicateFactoryKeyError'
import {ClosureFactory} from './factory/ClosureFactory'
@ -40,7 +48,7 @@ export interface AwareOfContainerLifecycle {
export function isAwareOfContainerLifecycle(what: unknown): what is AwareOfContainerLifecycle {
return Boolean(
typeof what === 'object'
(typeof what === 'object' || typeof what === 'function')
&& what !== null
&& hasOwnProperty(what, 'awareOfContainerLifecycle')
&& what.awareOfContainerLifecycle,
@ -92,6 +100,8 @@ export class Container {
.then(value => listener.callback(value))
})
container.subscribeToBlueprintChanges(ContainerBlueprint.getContainerBlueprint())
return container
}
@ -137,7 +147,7 @@ export class Container {
* Collection of callbacks waiting for a dependency key to be resolved.
* @protected
*/
protected waitingResolveCallbacks: Collection<{ key: DependencyKey, callback: (t: unknown) => unknown }> = new Collection<{key: DependencyKey; callback:(t: unknown) => unknown}>();
protected waitingResolveCallbacks: Collection<{ key: DependencyKey, callback: (t: unknown) => unknown }> = new Collection<{key: DependencyKey; callback:(t: unknown) => unknown}>()
/**
* Collection of created objects that should have lifecycle events called on them, if they still exist.
@ -145,11 +155,33 @@ export class Container {
*/
protected waitingLifecycleCallbacks: Collection<WeakRef<AwareOfContainerLifecycle>> = new Collection()
/**
* Collection of subscriptions to ContainerBlueprint events.
* We keep this around so we can remove the subscriptions when the container is destroyed.
* @protected
*/
protected blueprintSubscribers: Collection<Unsubscribe> = new Collection()
constructor() {
this.registerSingletonInstance<Container>(Container, this)
this.registerSingleton('injector', this)
}
/** Make the container listen to changes in the given blueprint. */
private subscribeToBlueprintChanges(blueprint: ContainerBlueprint): void {
this.blueprintSubscribers.push(
blueprint.resolve$(factory => this.registerFactory(factory())),
)
this.blueprintSubscribers.push(
blueprint.resolveConstructable$(factoryClass => this.registerFactory(this.make(factoryClass))),
)
this.blueprintSubscribers.push(
blueprint.resolveResolutionCallbacks$(listener => this.onResolve(listener.key).then(value => listener.callback(value))),
)
}
/**
* Purge all factories and instances of the given key from this container.
* @param key
@ -376,7 +408,7 @@ export class Container {
/**
* Resolve the dependency key. If a singleton value for that key already exists in this container,
* return that value. Otherwise, use the factory an given parameters to produce and return the value.
* return that value. Otherwise, use the factory and given parameters to produce and return the value.
* @param {DependencyKey} key
* @param {...any} parameters
*/
@ -432,11 +464,16 @@ export class Container {
// Create the dependencies for the factory
const keys = factory.getDependencyKeys().filter(req => this.hasKey(req.key))
const dependencies = keys.map<ResolvedDependency>(req => {
return {
paramIndex: req.paramIndex,
key: req.key,
resolved: this.resolveAndCreate(req.key),
}
return withErrorContext(() => {
return {
paramIndex: req.paramIndex,
key: req.key,
resolved: this.resolveAndCreate(req.key),
}
}, {
producingToken: factory.getTokenName(),
constructorDependency: req,
})
}).sortBy('paramIndex')
// Build the arguments for the factory, using dependencies in the
@ -460,7 +497,12 @@ export class Container {
factory.getInjectedProperties().each(dependency => {
logIfDebugging('extollo.di.injector', 'Resolving injected dependency:', dependency)
if ( dependency.key && inst ) {
(inst as any)[dependency.property] = this.resolveAndCreate(dependency.key)
withErrorContext(() => {
(inst as any)[dependency.property] = this.resolveAndCreate(dependency.key)
}, {
producingToken: factory.getTokenName(),
propertyDependency: dependency,
})
}
})
@ -499,14 +541,22 @@ export class Container {
this.checkForMakeCycles()
try {
if (this.hasKey(target)) {
const realized = this.resolveAndCreate(target, ...parameters)
Container.makeStack.pop()
return realized
} else if (typeof target !== 'string' && isInstantiable(target)) {
const realized = this.produceFactory(new Factory(target), parameters)
Container.makeStack.pop()
return realized
const result = withErrorContext(() => {
if (this.hasKey(target)) {
const realized = this.resolveAndCreate(target, ...parameters)
Container.makeStack?.pop()
return realized
} else if (typeof target !== 'string' && isInstantiable(target)) {
const realized = this.produceFactory(new Factory(target), parameters)
Container.makeStack?.pop()
return realized
}
}, {
makeStack: Container.makeStack.map(x => typeof x === 'string' ? x : (x?.name || 'unknown')).toArray(),
})
if ( result ) {
return result
}
} catch (e: unknown) {
Container.makeStack.pop()
@ -596,6 +646,8 @@ export class Container {
* Perform any cleanup necessary to destroy this container instance.
*/
destroy(): void {
this.blueprintSubscribers.mapCall('unsubscribe')
this.waitingLifecycleCallbacks
.mapCall('deref')
.whereDefined()

@ -3,6 +3,8 @@ import NamedFactory from './factory/NamedFactory'
import {AbstractFactory} from './factory/AbstractFactory'
import {Factory} from './factory/Factory'
import {ClosureFactory} from './factory/ClosureFactory'
import {Collection, collect} from '../util/collection/Collection'
import {Subscription, Unsubscribe} from '../util/support/BehaviorSubject'
/** Simple type alias for a callback to a container's onResolve method. */
export type ContainerResolutionCallback<T> = (() => unknown) | ((t: T) => unknown)
@ -25,11 +27,11 @@ export class ContainerBlueprint {
return this.instance
}
protected factories: (() => AbstractFactory<any>)[] = []
protected factories: Collection<(() => AbstractFactory<any>)> = collect()
protected constructableFactories: StaticClass<AbstractFactory<any>, any>[] = []
protected constructableFactories: Collection<StaticClass<AbstractFactory<any>, any>> = collect()
protected resolutionCallbacks: ({key: TypedDependencyKey<any>, callback: ContainerResolutionCallback<any>})[] = []
protected resolutionCallbacks: Collection<{key: TypedDependencyKey<any>, callback: ContainerResolutionCallback<any>}> = collect()
/**
* Register some factory class with the container. Should take no construction params.
@ -74,7 +76,16 @@ export class ContainerBlueprint {
* Get an array of factory instances in the blueprint.
*/
resolve(): AbstractFactory<any>[] {
return this.factories.map(x => x())
return this.factories.map(x => x()).all()
}
/**
* Subscribe to new factories being registered.
* Used by `Container` implementations to listen for factories being registered after the container is realized.
* @param sub
*/
resolve$(sub: Subscription<() => AbstractFactory<any>>): Unsubscribe {
return this.factories.push$(sub)
}
/**
@ -94,14 +105,32 @@ export class ContainerBlueprint {
* Get an array of static Factory classes that need to be instantiated by
* the container itself.
*/
resolveConstructable(): StaticClass<AbstractFactory<any>, any> {
return [...this.constructableFactories]
resolveConstructable(): StaticClass<AbstractFactory<any>, any>[] {
return this.constructableFactories.all()
}
/**
* Subscribe to new constructable factories being registered.
* Used by `Container` implementations to listen for factories registered after the container is realized.
* @param sub
*/
resolveConstructable$(sub: Subscription<StaticClass<AbstractFactory<any>, any>>): Unsubscribe {
return this.constructableFactories.push$(sub)
}
/**
* Get an array of DependencyKey-callback pairs to register with new containers.
*/
resolveResolutionCallbacks(): ({key: TypedDependencyKey<any>, callback: ContainerResolutionCallback<any>})[] {
return [...this.resolutionCallbacks]
return this.resolutionCallbacks.all()
}
/**
* Subscribe to new resolution callbacks being registered.
* Used by `Container` implementations to listen for callbacks registered after the container is realized.
* @param sub
*/
resolveResolutionCallbacks$(sub: Subscription<{key: TypedDependencyKey<any>, callback: ContainerResolutionCallback<any>}>): Unsubscribe {
return this.resolutionCallbacks.push$(sub)
}
}

@ -0,0 +1,9 @@
import {Instantiable, PropertyDependency} from '../types'
import {Collection, logIfDebugging} from '../../util'
import {propertyInjectionMetadata} from './propertyInjectionMetadata'
export function getPropertyInjectionMetadata(token: Instantiable<any>): Collection<PropertyDependency> {
const loadedMeta = ((token as any)[propertyInjectionMetadata] || new Collection()) as Collection<PropertyDependency>
logIfDebugging('extollo.di.injection', 'getPropertyInjectionMetadata() target:', token, 'loaded:', loadedMeta)
return loadedMeta
}

@ -1,16 +1,17 @@
import 'reflect-metadata'
import {collect, Collection, logIfDebugging} from '../../util'
import {collect, Collection} from '../../util/collection/Collection'
import {logIfDebugging} from '../../util/support/debug'
import {
DEPENDENCY_KEYS_METADATA_KEY,
DEPENDENCY_KEYS_SERVICE_TYPE_KEY,
DependencyKey,
DependencyRequirement,
DEPENDENCY_KEYS_METADATA_KEY,
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY,
isInstantiable,
InjectionType,
DEPENDENCY_KEYS_SERVICE_TYPE_KEY,
isInstantiable,
PropertyDependency,
} from '../types'
import {ContainerBlueprint} from '../ContainerBlueprint'
import {propertyInjectionMetadata} from './propertyInjectionMetadata'
/**
* Get a collection of dependency requirements for the given target object.
@ -66,6 +67,7 @@ export const Injectable = (): ClassDecorator => {
}
}
/**
* Mark the given class property to be injected by the container.
* If a `key` is specified, that DependencyKey will be injected.
@ -76,10 +78,27 @@ export const Injectable = (): ClassDecorator => {
*/
export const Inject = (key?: DependencyKey, { debug = false } = {}): PropertyDecorator => {
return (target, property) => {
let propertyMetadata = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, target?.constructor || target) as Collection<PropertyDependency>
if ( !target?.constructor ) {
logIfDebugging('extollo.di.decoration', '[DEBUG] @Inject(): target has no constructor', target)
throw new Error('Unable to define property injection: target has no constructor. Enable `extollo.di.decoration` logging to debug')
}
const propertyTarget = target.constructor
// let propertyMetadata = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, propertyTarget) as Collection<PropertyDependency>
// Okay, this is a little fucky. We can't use Reflect's metadata capabilities because we need to write the metadata to
// the constructor, not the `target`. Because Reflect is using the prototype to store data, defining a metadata key on the constructor
// will define it for its parent constructors as well.
// So, if you have class A, class B extends A, and class C extends A, the properties for B and C will be defined on A, causing
// BOTH B and C's properties to be injected on any class extending A.
// To get around this, we instead define a custom property on the constructor itself, then use hasOwnProperty to make sure we're not
// getting the one for the parent class via the prototype chain.
let propertyMetadata = Object.prototype.hasOwnProperty.call(propertyTarget, propertyInjectionMetadata) ?
(propertyTarget as any)[propertyInjectionMetadata] as Collection<PropertyDependency> : undefined
if ( !propertyMetadata ) {
propertyMetadata = new Collection<PropertyDependency>()
Reflect.defineMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, propertyMetadata, target?.constructor || target)
;(propertyTarget as any)[propertyInjectionMetadata] = propertyMetadata
}
const type = Reflect.getMetadata('design:type', target, property)
@ -100,11 +119,9 @@ export const Inject = (key?: DependencyKey, { debug = false } = {}): PropertyDec
}
}
if ( debug ) {
logIfDebugging('extollo.di.decoration', '[DEBUG] @Inject() - key:', key, 'property:', property, 'target:', target, 'target constructor:', target?.constructor, 'type:', type)
}
logIfDebugging('extollo.di.decoration', '[DEBUG] @Inject() - key:', key, 'property:', property, 'target:', target, 'target constructor:', target?.constructor, 'type:', type)
Reflect.defineMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, propertyMetadata, target?.constructor || target)
;(propertyTarget as any)[propertyInjectionMetadata] = propertyMetadata
}
}
@ -149,6 +166,7 @@ export const Singleton = (name?: string): ClassDecorator => {
...(name ? { name } : {}),
}
logIfDebugging('extollo.di.singleton', 'Registering singleton target:', target, 'injectionType:', injectionType)
Reflect.defineMetadata(DEPENDENCY_KEYS_SERVICE_TYPE_KEY, injectionType, target)
Injectable()(target)

@ -0,0 +1,2 @@
export const propertyInjectionMetadata = Symbol('@extollo/lib:propertyInjectionMetadata')

@ -1,11 +1,12 @@
import {DependencyKey} from '../types'
import {ErrorWithContext} from '../../util/error/ErrorWithContext'
/**
* Error thrown when a dependency key that has not been registered is passed to a resolver.
* @extends Error
*/
export class InvalidDependencyKeyError extends Error {
constructor(key: DependencyKey) {
super(`No such dependency is registered with this container: ${key}`)
export class InvalidDependencyKeyError extends ErrorWithContext {
constructor(key: DependencyKey, context: {[key: string]: any} = {}) {
super(`No such dependency is registered with this container: ${key}`, context)
}
}

@ -1,5 +1,6 @@
import {DependencyKey, DependencyRequirement, PropertyDependency} from '../types'
import { Collection } from '../../util'
import {DependencyKey, DependencyRequirement, Instantiable, PropertyDependency} from '../types'
import {Collection, logIfDebugging} from '../../util'
import {getPropertyInjectionMetadata} from '../decorator/getPropertyInjectionMetadata'
/**
* Abstract base class for dependency container factories.
@ -41,4 +42,32 @@ export abstract class AbstractFactory<T> {
* @return Collection<PropertyDependency>
*/
abstract getInjectedProperties(): Collection<PropertyDependency>
/** Helper method that returns all `@Inject()`'ed properties for a token and its prototypical ancestors. */
protected getInjectedPropertiesForPrototypeChain(token: Instantiable<any>): Collection<PropertyDependency> {
const meta = new Collection<PropertyDependency>()
do {
const loadedMeta = getPropertyInjectionMetadata(token)
if ( loadedMeta ) {
meta.concat(loadedMeta)
}
token = Object.getPrototypeOf(token)
logIfDebugging('extollo.di.injection', 'next currentToken:', token)
} while (token !== Function.prototype && token !== Object.prototype)
return meta
}
/**
* Get a human-readable name of the token this factory produces.
* This is meant for debugging output only.
*/
public getTokenName(): string {
if ( typeof this.token === 'string' ) {
return this.token
}
return this.token?.name ?? '(unknown token)'
}
}

@ -0,0 +1,84 @@
import {AbstractFactory} from './AbstractFactory'
import {Inject, Injectable} from '../decorator/injection'
import {Logging} from '../../service/Logging'
import {Config} from '../../service/Config'
import {
DEPENDENCY_KEYS_METADATA_KEY,
DependencyRequirement,
Instantiable,
isInstantiable,
PropertyDependency,
} from '../types'
import {Collection, ErrorWithContext, Maybe} from '../../util'
import 'reflect-metadata'
@Injectable()
export abstract class ConfiguredSingletonFactory<T> extends AbstractFactory<T> {
protected static loggedDefaultImplementationWarningOnce = false
@Inject()
protected readonly logging!: Logging
@Inject()
protected readonly config!: Config
constructor() {
super({})
}
protected abstract getConfigKey(): string
protected abstract getDefaultImplementation(): Instantiable<T>
protected abstract getAbstractImplementation(): any
protected getDefaultImplementationWarning(): Maybe<string> {
return undefined
}
produce(dependencies: any[], parameters: any[]): T {
return new (this.getImplementation())(...dependencies, ...parameters)
}
match(something: unknown): boolean {
return something === this.getAbstractImplementation()
}
getDependencyKeys(): Collection<DependencyRequirement> {
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getImplementation())
if ( meta ) {
return meta
}
return new Collection<DependencyRequirement>()
}
getInjectedProperties(): Collection<PropertyDependency> {
return this.getInjectedPropertiesForPrototypeChain(this.getImplementation())
}
protected getImplementation(): Instantiable<T> {
const ctor = this.constructor as typeof ConfiguredSingletonFactory
const ImplementationClass = this.config.get(this.getConfigKey(), this.getDefaultImplementation())
if ( ImplementationClass === this.getDefaultImplementation() ) {
const warning = this.getDefaultImplementationWarning()
if ( warning && !ctor.loggedDefaultImplementationWarningOnce ) {
this.logging.warn(warning)
ctor.loggedDefaultImplementationWarningOnce = true
}
}
if (
!isInstantiable(ImplementationClass)
|| !(ImplementationClass.prototype instanceof this.getAbstractImplementation())
) {
throw new ErrorWithContext('Configured service clas does not properly extend from implementation base class.', {
configKey: this.getConfigKey(),
class: `${ImplementationClass}`,
mustExtendBase: `${this.getAbstractImplementation()}`,
})
}
return ImplementationClass as Instantiable<T>
}
}

@ -1,7 +1,6 @@
import {AbstractFactory} from './AbstractFactory'
import {
DEPENDENCY_KEYS_METADATA_KEY,
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY,
DependencyRequirement,
Instantiable,
PropertyDependency,
@ -53,17 +52,6 @@ export class Factory<T> extends AbstractFactory<T> {
}
getInjectedProperties(): Collection<PropertyDependency> {
const meta = new Collection<PropertyDependency>()
let currentToken = this.token
do {
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
if ( loadedMeta ) {
meta.concat(loadedMeta)
}
currentToken = Object.getPrototypeOf(currentToken)
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
return meta
return this.getInjectedPropertiesForPrototypeChain(this.token)
}
}

@ -7,11 +7,13 @@ export * from './factory/Factory'
export * from './factory/NamedFactory'
export * from './factory/SingletonFactory'
export * from './types'
export * from './ContainerBlueprint'
export * from './decorator/getPropertyInjectionMetadata'
export * from './decorator/injection'
export * from './Container'
export * from './ScopedContainer'
export * from './types'
export * from './decorator/injection'
export * from './InjectionAware'
export * from './constructable'

@ -35,23 +35,13 @@ export interface ModuleRegistrationFluency {
core: () => HTTPKernel,
}
/**
* Error thrown when a kernel module is requested that does not exist w/in the kernel.
* @extends Error
*/
export class KernelModuleNotFoundError extends Error {
constructor(name: string) {
super(`The kernel module ${name} is not registered with the kernel.`)
}
}
/**
* A singleton class that handles requests, applying logic in modular layers.
*/
@Singleton()
export class HTTPKernel extends AppClass {
@Inject()
protected readonly logging!: Logging;
protected readonly logging!: Logging
/**
* Collection of preflight modules to apply.
@ -140,8 +130,6 @@ export class HTTPKernel extends AppClass {
if ( typeof foundIdx !== 'undefined' ) {
this.postflight = this.postflight.put(foundIdx, this.app().make(module))
} else {
throw new KernelModuleNotFoundError(other.name)
}
return this
@ -162,8 +150,6 @@ export class HTTPKernel extends AppClass {
if ( typeof foundIdx !== 'undefined' ) {
this.postflight = this.postflight.put(foundIdx + 1, this.app().make(module))
} else {
throw new KernelModuleNotFoundError(other.name)
}
return this

@ -5,6 +5,7 @@ import {ActivatedRoute} from '../../routing/ActivatedRoute'
import {ResponseObject} from '../../routing/Route'
import {AbstractResolvedRouteHandlerHTTPModule} from './AbstractResolvedRouteHandlerHTTPModule'
import {collect, isLeft, unleft, unright, withErrorContext} from '../../../util'
import {MountWebSocketRouteHTTPModule} from './MountWebSocketRouteHTTPModule'
/**
* HTTP Kernel module that executes the preflight handlers for the route.
@ -13,7 +14,9 @@ import {collect, isLeft, unleft, unright, withErrorContext} from '../../../util'
*/
export class ExecuteResolvedRoutePreflightHTTPModule extends AbstractResolvedRouteHandlerHTTPModule {
public static register(kernel: HTTPKernel): void {
kernel.register(this).after(MountActivatedRouteHTTPModule)
const reg = kernel.register(this)
reg.after(MountWebSocketRouteHTTPModule)
reg.after(MountActivatedRouteHTTPModule)
}
public async apply(request: Request): Promise<Request> {
@ -32,14 +35,16 @@ export class ExecuteResolvedRoutePreflightHTTPModule extends AbstractResolvedRou
}
const parameters = route.parameters
const resolveResult = await collect(parameters)
.asyncMapRight(handler => handler(request))
const resolveResult = (await collect(parameters)
.asyncMapRight(handler => handler(request)))
if ( isLeft(resolveResult) ) {
return unleft(resolveResult)
await this.applyResponseObject(unleft(resolveResult), request)
request.response.blockingWriteback(true)
return request
}
route.resolvedParameters = unright(resolveResult).toArray()
route.resolvedParameters = unright(resolveResult).toArray(false)
}
return request

@ -0,0 +1,35 @@
import {HTTPKernel} from '../HTTPKernel'
import {Request} from '../../lifecycle/Request'
import {ActivatedRoute} from '../../routing/ActivatedRoute'
import {withErrorContext} from '../../../util'
import {AbstractResolvedRouteHandlerHTTPModule} from './AbstractResolvedRouteHandlerHTTPModule'
import {ExecuteResolvedRoutePreflightHTTPModule} from './ExecuteResolvedRoutePreflightHTTPModule'
import {WebSocketBus} from '../../../support/bus'
/**
* HTTP kernel module that runs the web socket handler for the socket connection's route.
*/
export class ExecuteResolvedWebSocketHandlerHTTPModule extends AbstractResolvedRouteHandlerHTTPModule {
public static register(kernel: HTTPKernel): void {
kernel.register(this).after(ExecuteResolvedRoutePreflightHTTPModule)
}
public async apply(request: Request): Promise<Request> {
const route = <ActivatedRoute<unknown, unknown[]>> request.make(ActivatedRoute)
const params = route.resolvedParameters
if ( !params ) {
throw new Error('Attempted to call route handler without resolved parameters.')
}
await withErrorContext(async () => {
const ws = request.make<WebSocketBus>(WebSocketBus)
await route.handler
.tap(handler => handler(ws, ...params))
.apply(request)
}, {
route,
})
return request
}
}

@ -20,7 +20,7 @@ export class InjectSessionHTTPModule extends HTTPKernelModule {
}
public async apply(request: Request): Promise<Request> {
request.registerFactory(new SessionFactory())
request.registerFactory(request.make(SessionFactory))
const session = <Session> request.make(Session)
const id = request.cookies.get('extollo.session')

@ -0,0 +1,96 @@
import {HTTPKernelModule} from '../HTTPKernelModule'
import {HTTPKernel} from '../HTTPKernel'
import {Request} from '../../lifecycle/Request'
import {Inject, Injectable} from '../../../di'
import {Config} from '../../../service/Config'
import {setInterval} from 'timers'
import {Logging} from '../../../service/Logging'
import {WebSocketCloseEvent} from '../../lifecycle/WebSocketCloseEvent'
import Timeout = NodeJS.Timeout;
import {Bus} from '../../../support/bus'
import * as WebSockets from 'ws'
import {Maybe} from '../../../util'
@Injectable()
export class MonitorWebSocketConnectionHTTPModule extends HTTPKernelModule {
@Inject()
protected readonly config!: Config
@Inject()
protected readonly logging!: Logging
public static register(kernel: HTTPKernel): void {
kernel.register(this).core()
}
async apply(request: Request): Promise<Request> {
const ws = request.make<WebSockets.WebSocket>(WebSockets.WebSocket)
// Time to wait between pings
const pollIntervalMs = this.config.safe('server.socket.pollIntervalMs')
.or(30000)
.integer()
// Time to wait for a response
const pollResponseTimeoutMs = this.config.safe('server.socket.pollResponseTimeoutMs')
.or(3000)
.integer()
// Max # of failures before the connection is closed
const maxFailedPolls = this.config.safe('server.socket.maxFailedPolls')
.or(5)
.integer()
let failedPolls = 0
let interval: Maybe<Timeout> = undefined
await new Promise<void>(res => {
let gotResponse = false
// Listen for pong responses
ws.on('pong', () => {
this.logging.verbose('Got pong response from socket.')
gotResponse = true
})
// Listen for close event
ws.on('close', () => {
this.logging.debug('Got close event from socket.')
res()
})
interval = setInterval(async () => {
// Every interval, send a ping request and set a timeout for the response
this.logging.verbose('Sending ping request to socket...')
gotResponse = false
ws.ping()
await new Promise<void>(res2 => setTimeout(res2, pollResponseTimeoutMs))
// If no pong response is received before the timeout occurs, tick the # of failed response
if ( !gotResponse ) {
this.logging.verbose('Socket failed to respond in time.')
failedPolls += 1
} else {
// Otherwise, reset the failure counter
failedPolls = 0
}
// Once the failed responses exceeds the threshold, kill the connection
if ( failedPolls > maxFailedPolls ) {
this.logging.debug('Socket exceeded maximum # of failed pings. Killing.')
res()
}
}, pollIntervalMs)
})
if ( interval ) {
clearInterval(interval)
}
// Tell the server to close the socket connection
const bus = request.make<Bus>(Bus)
await bus.push(new WebSocketCloseEvent())
return request
}
}

@ -0,0 +1,51 @@
import {Injectable, Inject} from '../../../di'
import {HTTPKernelModule} from '../HTTPKernelModule'
import {HTTPKernel} from '../HTTPKernel'
import {Request} from '../../lifecycle/Request'
import {Routing} from '../../../service/Routing'
import {ActivatedRoute} from '../../routing/ActivatedRoute'
import {Logging} from '../../../service/Logging'
import {apiEvent, error} from '../../response/api'
import {Bus, WebSocketBus} from '../../../support/bus'
import {WebSocketCloseEvent} from '../../lifecycle/WebSocketCloseEvent'
/**
* HTTP kernel middleware that tries to find a registered route matching the request's
* path and creates an ActivatedRoute instance from it, limited to websocket handling
* routes.
*/
@Injectable()
export class MountWebSocketRouteHTTPModule extends HTTPKernelModule {
public readonly executeWithBlockingWriteback = true
@Inject()
protected readonly routing!: Routing
@Inject()
protected readonly logging!: Logging
public static register(kernel: HTTPKernel): void {
kernel.register(this).before()
}
public async apply(request: Request): Promise<Request> {
const route = this.routing.match('ws', request.path)
if ( route ) {
this.logging.verbose(`Mounting activated WebSocket route: ${request.path} -> ${route}`)
const activated = <ActivatedRoute<unknown, unknown[]>> request.make(ActivatedRoute, route, request.path)
request.registerSingletonInstance<ActivatedRoute<unknown, unknown[]>>(ActivatedRoute, activated)
} else {
this.logging.debug(`No matching WebSocket route found for: ${request.method} -> ${request.path}`)
// Send an error response on the socket to the client
const ws = request.make<WebSocketBus>(WebSocketBus)
await ws.push(apiEvent(error('Endpoint is not a configured socket listener.')))
// Then, terminate the request & socket connections
await request.make<Bus>(Bus).push(new WebSocketCloseEvent())
request.response.blockingWriteback(true)
}
return request
}
}

@ -12,7 +12,7 @@ export class PoweredByHeaderInjectionHTTPModule extends HTTPKernelModule {
public readonly executeWithBlockingWriteback = true
@Inject()
protected readonly config!: Config;
protected readonly config!: Config
public static register(kernel: HTTPKernel): void {
kernel.register(this).after()

@ -13,14 +13,14 @@ import {ActivatedRoute} from '../routing/ActivatedRoute'
* Enumeration of different HTTP verbs.
* @todo add others?
*/
export type HTTPMethod = 'post' | 'get' | 'patch' | 'put' | 'delete' | 'unknown';
export type HTTPMethod = 'post' | 'get' | 'patch' | 'put' | 'delete' | 'options' | 'unknown'
/**
* Returns true if the given item is a valid HTTP verb.
* @param what
*/
export function isHTTPMethod(what: unknown): what is HTTPMethod {
return ['post', 'get', 'patch', 'put', 'delete'].includes(String(what))
return ['post', 'get', 'patch', 'put', 'delete', 'options'].includes(String(what))
}
/**
@ -36,9 +36,9 @@ export interface HTTPProtocol {
* Interface that describes the origin IP address of a request.
*/
export interface HTTPSourceAddress {
address: string;
family: 'IPv4' | 'IPv6';
port: number;
address: string
family: 'IPv4' | 'IPv6'
port: number
}
/**
@ -55,43 +55,43 @@ export interface DataContainer {
export class Request extends ScopedContainer implements DataContainer {
/** The cookie manager for the request. */
public readonly cookies: HTTPCookieJar;
public readonly cookies: HTTPCookieJar
/** The URL suffix of the request. */
public readonly url: string;
public readonly url: string
/** The fully-qualified URL of the request. */
public readonly fullUrl: string;
public readonly fullUrl: string
/** The HTTP verb of the request. */
public readonly method: HTTPMethod;
public readonly method: HTTPMethod
/** True if the request was made via TLS. */
public readonly secure: boolean;
public readonly secure: boolean
/** The request HTTP protocol version. */
public readonly protocol: HTTPProtocol;
public readonly protocol: HTTPProtocol
/** The URL path, stripped of query params. */
public readonly path: string;
public readonly path: string
/** The raw parsed query data from the request. */
public readonly rawQueryData: {[key: string]: string | string[] | undefined};
public readonly rawQueryData: {[key: string]: string | string[] | undefined}
/** The inferred query data. */
public readonly query: {[key: string]: any};
public readonly query: {[key: string]: any}
/** True if the request was made via XMLHttpRequest. */
public readonly isXHR: boolean;
public readonly isXHR: boolean
/** The origin IP address of the request. */
public readonly address: HTTPSourceAddress;
public readonly address: HTTPSourceAddress
/** The associated response. */
public readonly response: Response;
public readonly response: Response
/** The media types accepted by the client. */
public readonly mediaTypes: string[];
public readonly mediaTypes: string[]
/** Input parsed from the request */
public readonly parsedInput: {[key: string]: any} = {}
@ -107,7 +107,7 @@ export class Request extends ScopedContainer implements DataContainer {
protected clientRequest: IncomingMessage,
/** The native Node.js response. */
protected serverResponse: ServerResponse,
protected serverResponse?: ServerResponse,
) {
super(Container.getContainer())
this.registerSingletonInstance(Request, this)

@ -66,7 +66,7 @@ export class Response {
public readonly request: Request,
/** The native Node.js ServerResponse. */
protected readonly serverResponse: ServerResponse,
protected readonly serverResponse?: ServerResponse,
) { }
protected get logging(): Logging {
@ -109,6 +109,20 @@ export class Response {
return this
}
/**
* Remove a header from the response by name.
* @param name
*/
public unsetHeader(name: string): this {
this.logging.verbose(`Will unset header on response: ${name}`)
if ( this.sentHeaders ) {
throw new HeadersAlreadySentError(this, name)
}
delete this.headers[name]
return this
}
/**
* Bulk set the specified headers in the response.
* @param data
@ -159,6 +173,11 @@ export class Response {
*/
public sendHeaders(): this {
this.logging.verbose(`Sending headers...`)
if ( !this.serverResponse ) {
throw new ErrorWithContext('Unable to send headers: Response has no underlying connection.', {
suggestion: 'This usually means the Request was created by an alternative server, like WebsocketServer. You should use that server to handle the request.',
})
}
const headers = {} as any
const setCookieHeaders = this.cookies.getSetCookieHeaders()
@ -206,8 +225,14 @@ export class Response {
* @param data
*/
public async write(data: string | Buffer | Uint8Array | Readable): Promise<void> {
this.logging.verbose(`Writing headers & data to response... (destroyed? ${this.serverResponse.destroyed})`)
this.logging.verbose(`Writing headers & data to response... (destroyed? ${!this.serverResponse || this.serverResponse.destroyed})`)
return new Promise<void>((res, rej) => {
if ( !this.serverResponse ) {
throw new ErrorWithContext('Unable to write response: Response has no underlying connection.', {
suggestion: 'This usually means the Request was created by an alternative server, like WebsocketServer. You should use that server to handle the request.',
})
}
if ( this.responseEnded || this.serverResponse.destroyed ) {
throw new ErrorWithContext('Tried to write to Response after lifecycle ended.')
}
@ -260,7 +285,7 @@ export class Response {
* or the connection has been destroyed.
*/
public canSend(): boolean {
return !(this.responseEnded || this.serverResponse.destroyed)
return !(this.responseEnded || !this.serverResponse || this.serverResponse.destroyed)
}
/**
@ -272,7 +297,7 @@ export class Response {
}
this.sentHeaders = true
this.serverResponse.end()
this.serverResponse?.end()
return this
}

@ -0,0 +1,11 @@
import {Event} from '../../support/bus'
import {uuid4} from '../../util'
/** Event used to tell the server to close the websocket connection. */
export class WebSocketCloseEvent implements Event {
eventName = '@extollo/lib:WebSocketCloseEvent'
eventUuid = uuid4()
shouldBroadcast = false
}

@ -0,0 +1,11 @@
import {Event} from '../../support/bus'
import {uuid4} from '../../util'
/** Event used to tell the server to close the websocket connection. */
export class WebSocketHealthCheckEvent implements Event {
eventName = '@extollo/lib:WebSocketHealthCheckEvent'
eventUuid = uuid4()
shouldBroadcast = false
}

@ -107,6 +107,12 @@ ${Object.keys(context).map(key => ` - ${key} : ${JSON.stringify(context[key])
protected getSuggestion(): string {
if ( this.thrownError.message.startsWith('No such dependency is registered with this container: class SecurityContext') ) {
return 'It looks like this route relies on the security framework. Is the route you are accessing inside a middleware (e.g. SessionAuthMiddleware)?'
} else if ( this.thrownError.message.startsWith('Unable to resolve schema for validator') ) {
return 'Make sure the directory in which the interface file is located is listed in extollo.cc.zodify in package.json, and that it ends with the proper .type.ts suffix.'
} else if ( this.thrownError instanceof ErrorWithContext ) {
if ( typeof this.thrownError.context.suggestion === 'string' ) {
return this.thrownError.context.suggestion
}
}
return ''

@ -13,6 +13,8 @@ export function plaintext(value: string): StringResponseFactory {
* Response factory that renders a given string as the response in plaintext.
*/
export class StringResponseFactory extends ResponseFactory {
protected targetContentType = 'text/plain'
constructor(
/** The string to write as the body. */
public readonly value: string,
@ -22,8 +24,14 @@ export class StringResponseFactory extends ResponseFactory {
public async write(request: Request): Promise<Request> {
request = await super.write(request)
request.response.setHeader('Content-Type', 'text/plain')
request.response.setHeader('Content-Type', this.targetContentType)
request.response.body = this.value
return request
}
/** Override the content type of the string. */
public contentType(type: string): this {
this.targetContentType = type
return this
}
}

@ -1,7 +1,12 @@
/**
* Base type for an API response format.
*/
import {BaseSerializer, Event, ObjectSerializer} from '../../support/bus'
import {Awaitable, ErrorWithContext, hasOwnProperty, JSONState, uuid4} from '../../util'
export interface APIResponse<T> {
eventName?: string,
eventUuid?: string,
success: boolean,
message?: string,
data?: T,
@ -12,6 +17,61 @@ export interface APIResponse<T> {
}
}
export function isAPIResponse(what: unknown): what is APIResponse<unknown> {
return typeof what === 'object' && what !== null
&& hasOwnProperty(what, 'success')
&& typeof what.success === 'boolean'
&& (!hasOwnProperty(what, 'message') || typeof what.message === 'string')
&& (!hasOwnProperty(what, 'error') || (
typeof what.error === 'object' && what.error !== null
&& hasOwnProperty(what.error, 'name') && typeof what.error.name === 'string'
&& hasOwnProperty(what.error, 'message') && typeof what.error.message === 'string'
&& (!hasOwnProperty(what.error, 'stack') || (
Array.isArray(what.error.stack) && what.error.stack.every(x => typeof x === 'string')
)
)
)
)
}
export function apiEvent<T>(response: APIResponse<T>): APIResponse<T> & Event {
if ( !response.eventName ) {
response.eventName = '@extollo/lib:APIResponse'
}
if ( !response.eventUuid ) {
response.eventUuid = uuid4()
}
return response as APIResponse<T> & Event
}
/**
* Serializer implementation that can encode/decode APIResponse objects.
*/
@ObjectSerializer()
export class APIResponseSerializer extends BaseSerializer<APIResponse<unknown>, JSONState> {
protected decodeSerial(serial: JSONState): Awaitable<APIResponse<unknown>> {
if ( isAPIResponse(serial) ) {
return serial
}
throw new ErrorWithContext('Could not decode API response: object is malformed')
}
protected encodeActual(actual: APIResponse<unknown>): Awaitable<JSONState> {
return actual as unknown as JSONState
}
protected getName(): string {
return '@extollo/lib:APIResponseSerializer'
}
matchActual(some: APIResponse<unknown>): boolean {
return isAPIResponse(some)
}
}
/**
* Formats a mesage as a successful API response.
* @param {string} displayMessage
@ -56,7 +116,7 @@ export function many<T>(records: T[]): APIResponse<{records: T[], total: number}
* @return APIResponse
* @param thrownError
*/
export function error(thrownError: string | Error): APIResponse<void> {
export function error(thrownError: string | Error): APIResponse<undefined> {
if ( typeof thrownError === 'string' ) {
return {
success: false,

@ -2,6 +2,7 @@ import {Request} from '../lifecycle/Request'
import {ResponseObject} from './Route'
import {Container} from '../../di'
import {CanonicalItemClass} from '../../support/CanonicalReceiver'
import {Awaitable, Either, ErrorWithContext, Left, left, Right, right} from '../../util'
/**
* Base class representing a middleware handler that can be applied to routes.
@ -31,3 +32,26 @@ export abstract class Middleware extends CanonicalItemClass {
*/
public abstract apply(): ResponseObject
}
/**
* A type of Middleware that produces a parameter that is passed to later handlers.
* Can be used to do common look-ups, &c before routes.
*/
export abstract class ParameterMiddleware<T, THandlerArgs extends any[] = []> extends Middleware {
/** Look up the value. */
public abstract handle(...options: THandlerArgs): Awaitable<Either<ResponseObject, T>>
apply(): ResponseObject {
throw new ErrorWithContext('Attempted to apply parameter-providing middleware directly. Try using `parameterMiddleware()` instead.')
}
/** Alias for an error response return. */
left(what: T): Left<T> {
return left(what)
}
/** Alias for a good value return. */
right(what: T): Right<T> {
return right(what)
}
}

@ -1,13 +1,16 @@
import {Collection, Either, ErrorWithContext, Maybe, Pipeline, PrefixTypeArray, right} from '../../util'
import {Awaitable, Collection, Either, ErrorWithContext, Maybe, Pipeline, SuffixTypeArray, right} from '../../util'
import {ResponseFactory} from '../response/ResponseFactory'
import {HTTPMethod, Request} from '../lifecycle/Request'
import {constructable, Constructable, Container, Instantiable, isInstantiableOf, TypedDependencyKey} from '../../di'
import {Middleware} from './Middleware'
import {Middleware, ParameterMiddleware} from './Middleware'
import {Valid, Validator, ValidatorFactory} from '../../validation/Validator'
import {validateMiddleware} from '../../validation/middleware'
import {RouteGroup} from './RouteGroup'
import {Config} from '../../service/Config'
import {Application} from '../../lifecycle/Application'
import {Logging} from '../../service/Logging'
import {WebSocketBus} from '../../support/bus/WebSocketBus'
import {SocketRouteBuilder} from './SocketRouteBuilder'
/**
* Type alias for an item that is a valid response object, or lack thereof.
@ -20,7 +23,7 @@ export type ResponseObject = ResponseFactory | string | number | void | any | Pr
*/
export type ResolvedRouteHandler = (request: Request) => ResponseObject
export type ParameterProvidingMiddleware<T> = (request: Request) => Either<ResponseObject, T>
export type ParameterProvidingMiddleware<T> = (request: Request) => Awaitable<Either<ResponseObject, T>>
export interface HandledRoute<TReturn extends ResponseObject, THandlerParams extends unknown[] = []> {
handler: Constructable<(...x: THandlerParams) => TReturn>
@ -135,6 +138,22 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
return new Route(method, endpoint)
}
/**
* Create a new WebSocket route on the given endpoint.
* @param endpoint
*/
public static socket(endpoint: string): SocketRouteBuilder {
const builder = SocketRouteBuilder.get()
;(new Route<Awaitable<void>, [WebSocketBus]>('ws', endpoint))
.passingRequest()
.handledBy(async (ws: WebSocketBus, request: Request) => {
await builder.build(request, ws)
})
return builder
}
/**
* Create a new GET route on the given endpoint.
*/
@ -187,10 +206,14 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
protected displays: Collection<{stage: 'pre'|'post'|'handler', display: string}> = new Collection()
constructor(
protected method: HTTPMethod | HTTPMethod[],
protected method: 'ws' | HTTPMethod | HTTPMethod[],
protected route: string,
) {}
public isForWebSocket(): boolean {
return this.method === 'ws'
}
/**
* Set a programmatic name for this route.
* @param name
@ -211,6 +234,10 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
* Get the string-form methods supported by the route.
*/
public getMethods(): HTTPMethod[] {
if ( this.method === 'ws' ) {
return []
}
if ( !Array.isArray(this.method) ) {
return [this.method]
}
@ -249,10 +276,14 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
* @param method
* @param potential
*/
public match(method: HTTPMethod, potential: string): boolean {
if ( Array.isArray(this.method) && !this.method.includes(method) ) {
public match(method: 'ws' | HTTPMethod, potential: string): boolean {
if ( method === 'ws' && !this.isForWebSocket() ) {
return false
} else if ( !Array.isArray(this.method) && this.method !== method ) {
} else if ( method !== 'ws' && this.isForWebSocket() ) {
return false
} else if ( method !== 'ws' && Array.isArray(this.method) && !this.method.includes(method) ) {
return false
} else if ( method !== 'ws' && !Array.isArray(this.method) && this.method !== method ) {
return false
}
@ -274,8 +305,11 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
* @param potential
*/
public extract(potential: string): {[key: string]: string} | undefined {
const routeParts = (this.route.startsWith('/') ? this.route.substr(1) : this.route).split('/')
const potentialParts = (potential.startsWith('/') ? potential.substr(1) : potential).split('/')
const routeParts = (this.route.startsWith('/') ? this.route.substr(1) : this.route).split('/').filter(Boolean)
const potentialParts = (potential.startsWith('/') ? potential.substr(1) : potential).split('/').filter(Boolean)
Application.getApplication().make<Logging>(Logging)
.trace(`Extracting route - (potential: ${potential}, rP: ${JSON.stringify(routeParts)}, pP: ${JSON.stringify(potentialParts)})`)
const params: any = {}
let wildcardIdx = 0
@ -310,16 +344,23 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
return params
}
public parameterMiddleware<T>(
handler: ParameterProvidingMiddleware<T>,
): Route<TReturn, PrefixTypeArray<T, THandlerParams>> {
const route = new Route<TReturn, PrefixTypeArray<T, THandlerParams>>(
public parameterMiddleware<T, THandlerArgs extends any[] = []>(
handler: ParameterProvidingMiddleware<T> | Instantiable<ParameterMiddleware<T, THandlerArgs>>,
...handlerArgs: THandlerArgs
): Route<TReturn, SuffixTypeArray<THandlerParams, T>> {
const route = new Route<TReturn, SuffixTypeArray<THandlerParams, T>>(
this.method,
this.route,
)
route.copyFrom(this)
route.parameters.push(handler)
if ( handler.prototype instanceof ParameterMiddleware ) {
route.parameters.push(req => req.make<ParameterMiddleware<T, THandlerArgs>>(handler, req).handle(...handlerArgs))
} else {
route.parameters.push(handler as ParameterProvidingMiddleware<T>)
}
return route
}
@ -328,6 +369,7 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
this.postflight = other.postflight.clone()
this.aliases = other.aliases.clone()
this.displays = other.displays.clone()
this.parameters = other.parameters.clone()
}
public calls<TKey>(
@ -361,27 +403,43 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
return this as HandledRoute<TReturn, THandlerParams>
}
public pre(middleware: Instantiable<Middleware>): this {
this.preflight.prepend(request => request.make<Middleware>(middleware, request).apply())
public pre(middleware: Instantiable<Middleware>|Constructable<Middleware>): this {
let name: string
if ( middleware instanceof Pipeline ) {
this.preflight.prepend(request => middleware.apply(request).apply())
name = '(unknown pipeline)'
} else {
this.preflight.prepend(request => request.make<Middleware>(middleware, request).apply())
name = middleware.name
}
this.displays.push({
stage: 'pre',
display: `${middleware.name}`,
display: name,
})
return this
}
public post(middleware: Instantiable<Middleware>): this {
this.postflight.push(request => request.make<Middleware>(middleware, request).apply())
public post(middleware: Instantiable<Middleware>|Constructable<Middleware>): this {
let name: string
if ( middleware instanceof Pipeline ) {
this.postflight.push(request => middleware.apply(request).apply())
name = '(unknown pipeline)'
} else {
this.preflight.push(request => request.make<Middleware>(middleware, request).apply())
name = middleware.name
}
this.displays.push({
stage: 'pre',
display: `${middleware.name}`,
stage: 'post',
display: name,
})
return this
}
public input<T>(validator: ValidatorFactory<T>): Route<TReturn, PrefixTypeArray<Valid<T>, THandlerParams>> {
public input<T>(validator: ValidatorFactory<T>): Route<TReturn, SuffixTypeArray<THandlerParams, Valid<T>>> {
if ( !(validator instanceof Validator) ) {
validator = validator()
}
@ -394,7 +452,7 @@ export class Route<TReturn extends ResponseObject, THandlerParams extends unknow
return this.parameterMiddleware(validateMiddleware(validator))
}
public passingRequest(): Route<TReturn, PrefixTypeArray<Request, THandlerParams>> {
public passingRequest(): Route<TReturn, SuffixTypeArray<THandlerParams, Request>> {
return this.parameterMiddleware(request => right(request))
}

@ -0,0 +1,105 @@
import {StateEvent, WebSocketBus} from '../../support/bus'
import {constructable, Constructable, Instantiable, TypedDependencyKey} from '../../di'
import {Awaitable, Collection, JSONState} from '../../util'
import {Request} from '../lifecycle/Request'
export type SocketEventHandler<TState extends JSONState> = {
eventClass: Instantiable<StateEvent<TState>>,
handler: Constructable<(state: TState) => Awaitable<void>>,
}
/**
* Helper class for building websocket APIs using first-class route syntax.
* This is returned by the `Route.socket(...)` method, so you'll probably want
* to use that.
*
* @see Route#socket
* @example
* ```ts
* Route.socket('/ws/endpoint')
* .connected(MyCtrl, (ctrl: MyCtrl) => ctrl.connect)
* .event(MyEvent, MyCtrl, (ctrl: MyCtrl) => ctrl.myHandler)
* ```
*/
export class SocketRouteBuilder {
public static get(): SocketRouteBuilder {
return new SocketRouteBuilder()
}
/** Handlers that should be registered with any new socket connections. */
protected handlers: Collection<SocketEventHandler<any>> = new Collection()
/** Callback to execute when a new connection is opened. */
protected connectionCallback?: Constructable<(ws: WebSocketBus) => Awaitable<void>>
/**
* Register a callback to execute each time a new socket is opened.
* This can be used to perform basic setup/validation/authentication tasks.
*
* @example
* ```ts
* Route.socket('/ws/endpoint')
* .connected(MyCtrl, (ctrl: MyCtrl) => ctrl.connect)
* ```
*
* @param key
* @param selector
*/
connected<TKey>(
key: TypedDependencyKey<TKey>,
selector: (x: TKey) => (ws: WebSocketBus) => Awaitable<void>,
): this {
this.connectionCallback = constructable<TKey>(key)
.tap(inst => Function.prototype.bind.call(selector(inst), inst as any) as ((ws: WebSocketBus) => Awaitable<void>))
return this
}
/**
* Register a `StateEvent` listener on the socket.
*
* @example
* ```ts
* Route.socket('/ws/endpoint')
* .event(MyEvent, MyCtrl, (ctrl: MyCtrl) => ctrl.myHandler)
* ```
*
* @see StateEvent
* @param eventClass
* @param key
* @param selector
*/
event<TState extends JSONState, TKey>(
eventClass: Instantiable<StateEvent<TState>>,
key: TypedDependencyKey<TKey>,
selector: (x: TKey) => (state: TState) => Awaitable<void>,
): this {
const handler = constructable<TKey>(key)
.tap(inst => Function.prototype.bind.call(selector(inst), inst as any) as ((state: TState) => Awaitable<void>))
this.handlers.push({
eventClass,
handler,
})
return this
}
/**
* Attaches event listeners & initial callback to the given socket. This is used
* by as the handler for new connections.
*
* @see Route#socket
* @param request
* @param ws
*/
async build(request: Request, ws: WebSocketBus): Promise<void> {
await this.handlers.promiseMap(handler => {
ws.subscribe(handler.eventClass, (event: StateEvent<JSONState>) => {
return handler.handler.apply(request)(event.getState())
})
})
if ( this.connectionCallback ) {
await this.connectionCallback.apply(request)(ws)
}
}
}

@ -0,0 +1,119 @@
import {NoSessionKeyError, Session, SessionData, SessionNotLoadedError} from './Session'
import {Inject, Injectable} from '../../di'
import {Cache, Maybe} from '../../util'
import {Config} from '../../service/Config'
/**
* A Session implementation that uses the configured Cache driver for persistence.
*/
@Injectable()
export class CacheSession extends Session {
@Inject()
protected readonly cache!: Cache
@Inject()
protected readonly config!: Config
protected key?: string
protected data?: SessionData
protected dirty = false
forget(key: string): void {
if ( !this.data ) {
throw new SessionNotLoadedError()
}
delete this.data[key]
this.dirty = true
}
get(key: string, fallback?: unknown): any {
if ( !this.data ) {
throw new SessionNotLoadedError()
}
return this.data[key] ?? fallback
}
getData(): SessionData {
if ( !this.data ) {
throw new SessionNotLoadedError()
}
return {...this.data}
}
getKey(): string {
if ( !this.key ) {
throw new NoSessionKeyError()
}
return this.key
}
async load(): Promise<void> {
const json = await this.cache.fetch(this.formatKey())
if ( json ) {
this.data = JSON.parse(json)
} else {
this.data = {}
}
this.dirty = false
}
async persist(): Promise<void> {
if ( !this.dirty ) {
return
}
const json = JSON.stringify(this.data)
await this.cache.put(this.formatKey(), json, this.getExpiration())
this.dirty = false
}
private getExpiration(): Maybe<Date> {
// Get the session expiration. By default, this is 4 hours.
const durationMins = this.config.safe('server.session.durationMins')
.or(4 * 60)
.integer()
if ( durationMins !== 0 ) {
const date = new Date()
date.setMinutes(date.getMinutes() + durationMins)
return date
}
}
private formatKey(): string {
if ( !this.key ) {
throw new NoSessionKeyError()
}
const prefix = this.config.safe('app.name')
.or('Extollo')
.string()
return `${prefix}_session_${this.key}`
}
set(key: string, value: unknown): void {
if ( !this.data ) {
throw new SessionNotLoadedError()
}
this.data[key] = value
this.dirty = true
}
setData(data: SessionData): void {
this.data = {...data}
this.dirty = true
}
setKey(key: string): void {
this.key = key
}
}

@ -1,87 +1,23 @@
import {
AbstractFactory,
Container,
DependencyRequirement,
PropertyDependency,
isInstantiable,
DEPENDENCY_KEYS_METADATA_KEY,
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, Instantiable,
} from '../../di'
import {Collection, ErrorWithContext} from '../../util'
import {Instantiable} from '../../di'
import {Maybe} from '../../util'
import {MemorySession} from './MemorySession'
import {Session} from './Session'
import {Logging} from '../../service/Logging'
import {Config} from '../../service/Config'
import {ConfiguredSingletonFactory} from '../../di/factory/ConfiguredSingletonFactory'
/**
* A dependency injection factory that matches the abstract Session class
* and produces an instance of the configured session driver implementation.
*/
export class SessionFactory extends AbstractFactory<Session> {
protected readonly logging: Logging
protected readonly config: Config
/** True if we have printed the memory session warning at least once. */
private static loggedMemorySessionWarningOnce = false
constructor() {
super({})
this.logging = Container.getContainer().make<Logging>(Logging)
this.config = Container.getContainer().make<Config>(Config)
export class SessionFactory extends ConfiguredSingletonFactory<Session> {
protected getConfigKey(): string {
return 'server.session.driver'
}
produce(): Session {
return new (this.getSessionClass())()
protected getDefaultImplementation(): Instantiable<Session> {
return MemorySession
}
match(something: unknown): boolean {
return something === Session
protected getAbstractImplementation(): any {
return Session
}
getDependencyKeys(): Collection<DependencyRequirement> {
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getSessionClass())
if ( meta ) {
return meta
}
return new Collection<DependencyRequirement>()
}
getInjectedProperties(): Collection<PropertyDependency> {
const meta = new Collection<PropertyDependency>()
let currentToken = this.getSessionClass()
do {
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
if ( loadedMeta ) {
meta.concat(loadedMeta)
}
currentToken = Object.getPrototypeOf(currentToken)
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
return meta
}
/**
* Return the instantiable class of the configured session backend.
* @protected
* @return Instantiable<Session>
*/
protected getSessionClass(): Instantiable<Session> {
const SessionClass = this.config.get('server.session.driver', MemorySession)
if ( SessionClass === MemorySession && !SessionFactory.loggedMemorySessionWarningOnce ) {
this.logging.warn(`You are using the default memory-based session driver. It is recommended you configure a persistent session driver instead.`)
SessionFactory.loggedMemorySessionWarningOnce = true
}
if ( !isInstantiable(SessionClass) || !(SessionClass.prototype instanceof Session) ) {
const e = new ErrorWithContext('Provided session class does not extend from @extollo/lib.Session')
e.context = {
configKey: 'server.session.driver',
class: SessionClass.toString(),
}
}
return SessionClass
protected getDefaultImplementationWarning(): Maybe<string> {
return 'You are using the default memory-based session driver. It is recommended you configure a persistent session driver instead.'
}
}

@ -35,6 +35,8 @@ export * from './http/kernel/HTTPCookieJar'
export * from './http/lifecycle/Request'
export * from './http/lifecycle/Response'
export * from './http/lifecycle/WebSocketCloseEvent'
export * from './http/lifecycle/WebSocketHealthCheckEvent'
export * from './http/RequestLocalStorage'
export * from './make'
@ -53,6 +55,7 @@ export * from './http/response/ViewResponseFactory'
export * from './http/response/FileResponseFactory'
export * from './http/response/RouteResponseFactory'
export * from './http/routing/SocketRouteBuilder'
export * from './http/routing/ActivatedRoute'
export * from './http/routing/Route'
export * from './http/routing/RouteGroup'
@ -63,6 +66,7 @@ export * from './http/kernel/module/ExecuteResolvedRouteHandlerHTTPModule'
export * from './http/session/Session'
export * from './http/session/SessionFactory'
export * from './http/session/MemorySession'
export * from './http/session/CacheSession'
export * from './http/Controller'
@ -80,9 +84,11 @@ export * from './service/Config'
export * from './service/Controllers'
export * from './service/Files'
export * from './service/HTTPServer'
export * from './service/WebsocketServer'
export * from './service/Routing'
export * from './service/Middlewares'
export * from './service/Discovery'
export * from './service/Foreground'
export * from './support/redis/Redis'
export * from './support/cache/MemoryCache'

@ -242,14 +242,22 @@ export class Application extends Container {
* @protected
*/
protected bootstrapEnvironment(): void {
logIfDebugging('extollo.env', `.env path: ${this.basePath.concat('.env').toLocal}`)
const path = this.basePath.concat('.env').toLocal
let path = this.basePath.concat('.env').toLocal
logIfDebugging('extollo.env', `Trying .env path: ${path}`)
if ( fs.existsSync(path) ) {
dotenv.config({
path,
})
return
}
path = this.basePath.concat('../.env').toLocal
logIfDebugging('extollo.env', `Trying .env path: ${path}`)
if ( fs.existsSync(path) ) {
dotenv.config({
path,
})
}
logIfDebugging('extollo.env', process.env)
}
/**

@ -0,0 +1,46 @@
import {DatabaseService, FieldType, Migration, Schema} from '../orm'
import {Inject} from '../di'
export default class CreateOAuth2TokensTableMigration extends Migration {
@Inject()
protected readonly db!: DatabaseService
async up(): Promise<void> {
const db = this.db.get()
const table = await db.schema().table('oauth2_tokens')
table.primaryKey('oauth2_token_id').required()
table.column('user_id')
.type(FieldType.varchar)
.nullable()
table.column('client_id')
.type(FieldType.varchar)
.required()
table.column('issued')
.type(FieldType.timestamp)
.default(db.dialect().currentTimestamp())
.required()
table.column('expires')
.type(FieldType.timestamp)
.required()
table.column('scope')
.type(FieldType.varchar)
.nullable()
await db.schema().commit(table)
}
async down(): Promise<void> {
const schema: Schema = this.db.get().schema()
const table = await schema.table('oauth2_tokens')
table.dropIfExists()
await schema.commit(table)
}
}

@ -9,8 +9,8 @@ import {
SpecifiedField,
} from '../types'
import {Connection} from '../connection/Connection'
import {deepCopy, ErrorWithContext, Maybe} from '../../util'
import {EscapeValue, QuerySafeValue, raw} from '../dialect/SQLDialect'
import {Collectable, deepCopy, ErrorWithContext, Maybe} from '../../util'
import {EscapeValue, QuerySafeValue, raw, ScalarEscapeValue, VectorEscapeValue} from '../dialect/SQLDialect'
import {ResultCollection} from './result/ResultCollection'
import {AbstractResultIterable} from './result/AbstractResultIterable'
import {AppClass} from '../../lifecycle/AppClass'
@ -144,7 +144,7 @@ export abstract class AbstractBuilder<T> extends AppClass {
* @param table
* @param alias
*/
from(table: string, alias?: string): this {
from(table: string|QuerySafeValue, alias?: string): this {
if ( alias ) {
this.source = { table,
alias }
@ -222,17 +222,45 @@ export abstract class AbstractBuilder<T> extends AppClass {
return this
}
/** Apply a WHERE ... IS NULL constraint to the query. */
whereNull(field: string): this {
return this.whereRawValue(field, 'IS', 'NULL')
}
/** Apply a WHERE ... IS NOT NULL constraint to the query. */
whereNotNull(field: string): this {
return this.whereRawValue(field, 'IS NOT', 'NULL')
}
/**
* Apply a new WHERE constraint to the query, without escaping `operand`. Prefer `where()`.
* @param field
* @param operator
* @param operand
*/
whereRaw(field: string, operator: ConstraintOperator, operand: string): this {
whereRawValue(field: string, operator: ConstraintOperator, operand: string): this {
this.createConstraint('AND', field, operator, raw(operand))
return this
}
/**
* Add raw SQL as a constraint to the query.
* @param clause
*/
whereRaw(clause: string|QuerySafeValue): this {
if ( !(clause instanceof QuerySafeValue) ) {
clause = raw(clause)
}
this.constraints.push(raw(clause))
return this
}
/** Apply an impossible constraint to the query, causing it to match 0 rows. */
whereMatchNone(): this {
return this.whereRaw('1=0')
}
/**
* Apply a new WHERE NOT constraint to the query.
* @param field
@ -282,7 +310,7 @@ export abstract class AbstractBuilder<T> extends AppClass {
* @param field
* @param values
*/
whereIn(field: string, values: EscapeValue): this {
whereIn<TConstraint extends ScalarEscapeValue>(field: string, values: VectorEscapeValue<TConstraint>): this {
this.constraints.push({
field,
operator: 'IN',
@ -297,7 +325,7 @@ export abstract class AbstractBuilder<T> extends AppClass {
* @param field
* @param values
*/
whereNotIn(field: string, values: EscapeValue): this {
whereNotIn<TConstraint extends ScalarEscapeValue>(field: string, values: VectorEscapeValue<TConstraint>): this {
this.constraints.push({
field,
operator: 'NOT IN',
@ -312,7 +340,7 @@ export abstract class AbstractBuilder<T> extends AppClass {
* @param field
* @param values
*/
orWhereIn(field: string, values: EscapeValue): this {
orWhereIn<TConstraint extends ScalarEscapeValue>(field: string, values: VectorEscapeValue<TConstraint>): this {
this.constraints.push({
field,
operator: 'IN',
@ -327,7 +355,7 @@ export abstract class AbstractBuilder<T> extends AppClass {
* @param field
* @param values
*/
orWhereNotIn(field: string, values: EscapeValue): this {
orWhereNotIn<TConstraint extends ScalarEscapeValue>(field: string, values: VectorEscapeValue<TConstraint>): this {
this.constraints.push({
field,
operator: 'NOT IN',
@ -500,6 +528,35 @@ export abstract class AbstractBuilder<T> extends AppClass {
return this.registeredConnection.query(query)
}
/**
* Run a batch update on all rows matched by this query, setting the values for discrete
* rows based on some key.
*
* This is a more efficient way of combining discrete update queries.
*
* @example
* ```ts
* query.table('my_table')
* .updateMany('id_col', [
* {id_col: 1, val1_col: 'a'},
* {id_col: 2, val2_col: 'b'},
* ])
* ```
*
* This will set the `val1_col` to `a` for rows where `id_col` is `1` and so on.
*
* @param key
* @param rows
*/
async updateMany(key: string, rows: Collectable<{[key: string]: EscapeValue}>): Promise<QueryResult> {
if ( !this.registeredConnection ) {
throw new ErrorWithContext(`No connection specified to execute update query.`)
}
const query = this.registeredConnection.dialect().renderBatchUpdate(this, key, rows)
return this.registeredConnection.query(query)
}
/**
* Execute a DELETE based on this query.
*
@ -572,6 +629,15 @@ export abstract class AbstractBuilder<T> extends AppClass {
return Boolean(result.rows.first())
}
/** Render the query as a string. */
toString(): string {
if ( !this.registeredConnection ) {
throw new ErrorWithContext('No connection specified to render query.')
}
return this.registeredConnection.dialect().renderSelect(this.finalize())
}
/**
* Set the query manually. Overrides any builder methods.
* @example
@ -587,6 +653,12 @@ export abstract class AbstractBuilder<T> extends AppClass {
return this
}
/** Pass this instance into a callback, then return this instance for chaining. */
tap(callback: (inst: this) => unknown): this {
callback(this)
return this
}
/**
* Adds a constraint to this query. This is used internally by the various `where`, `whereIn`, `orWhereNot`, &c.
* @param preop

@ -1,11 +1,12 @@
import {Awaitable, ErrorWithContext} from '../../util'
import {QueryResult} from '../types'
import {Awaitable, Collection, ErrorWithContext} from '../../util'
import {QueryResult, QueryRow} from '../types'
import {SQLDialect} from '../dialect/SQLDialect'
import {AppClass} from '../../lifecycle/AppClass'
import {Inject, Injectable} from '../../di'
import {QueryExecutedEvent} from './event/QueryExecutedEvent'
import {Schema} from '../schema/Schema'
import {Bus} from '../../support/bus'
import {ModelField} from '../model/Field'
/**
* Error thrown when a connection is used before it is ready.
@ -75,6 +76,17 @@ export abstract class Connection extends AppClass {
*/
public abstract asTransaction<T>(closure: () => Awaitable<T>): Awaitable<T>
/**
* Normalize a query row before it is used by the framework.
* This helps account for differences in return values from the dialects.
* @param row
* @param fields
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public normalizeRow(row: QueryRow, fields: Collection<ModelField>): QueryRow {
return row
}
/**
* Fire a QueryExecutedEvent for the given query string.
* @param query

@ -80,9 +80,21 @@ export class PostgresConnection extends Connection {
}
await this.client.query('BEGIN')
const result = await closure()
await this.client.query('COMMIT')
return result
try {
const result = await closure()
await this.client.query('COMMIT')
return result
} catch (e) {
await this.client.query('ROLLBACK')
if ( e instanceof Error ) {
throw this.app().errorWrapContext(e, {
connection: this.name,
})
}
throw e
}
}
public schema(name?: string): Schema {

@ -0,0 +1,117 @@
import {Connection, ConnectionNotReadyError} from './Connection'
import {Logging} from '../../service/Logging'
import {Inject} from '../../di'
import {open, Database} from 'sqlite'
import {FieldType, QueryResult, QueryRow} from '../types'
import {Schema} from '../schema/Schema'
import {Awaitable, collect, Collection, hasOwnProperty, UniversalPath} from '../../util'
import {SQLDialect} from '../dialect/SQLDialect'
import {SQLiteDialect} from '../dialect/SQLiteDialect'
import {SQLiteSchema} from '../schema/SQLiteSchema'
import * as sqlite3 from 'sqlite3'
import {ModelField} from '../model/Field'
export interface SQLiteConnectionConfig {
filename: string,
}
export class SQLiteConnection extends Connection {
@Inject()
protected readonly logging!: Logging
protected client?: Database
public dialect(): SQLDialect {
return this.make(SQLiteDialect)
}
public async init(): Promise<void> {
if ( this.config?.filename instanceof UniversalPath ) {
this.config.filename = this.config.filename.toLocal
}
this.logging.debug(`Opening SQLite connection ${this.name} (${this.config?.filename})...`)
this.client = await open({
...this.config,
driver: sqlite3.Database,
})
}
public async close(): Promise<void> {
this.logging.debug(`Closing SQLite connection ${this.name}...`)
if ( this.client ) {
await this.client.close()
}
}
public async query(query: string): Promise<QueryResult> {
if ( !this.client ) {
throw new ConnectionNotReadyError(this.name, {
config: this.config,
})
}
this.logging.verbose(`Executing query in connection ${this.name}: \n${query.split('\n').map(x => ' ' + x)
.join('\n')}`)
try {
const result = await this.client.all(query) // FIXME: this probably won't work for non-select statements?
await this.queryExecuted(query)
return {
rows: collect(result),
rowCount: result.length,
}
} catch (e) {
if ( e instanceof Error ) {
throw this.app().errorWrapContext(e, {
query,
connection: this.name,
})
}
throw e
}
}
public async asTransaction<T>(closure: () => Awaitable<T>): Promise<T> {
if ( !this.client ) {
throw new ConnectionNotReadyError(this.name, {
config: this.config,
})
}
// fixme: sqlite doesn't support tx's properly in node atm
await this.client.run('BEGIN')
try {
const result = await closure()
await this.client.run('COMMIT')
return result
} catch (e) {
await this.client.run('ROLLBACK')
if ( e instanceof Error ) {
throw this.app().errorWrapContext(e, {
connection: this.name,
})
}
throw e
}
}
public normalizeRow(row: QueryRow, fields: Collection<ModelField>): QueryRow {
fields.where('type', '=', FieldType.json)
.pluck('databaseKey')
.filter(key => hasOwnProperty(row, key))
.filter(key => typeof row[key] === 'string')
.each(key => row[key] = JSON.parse(row[key]))
return row
}
public schema(): Schema {
return new SQLiteSchema(this)
}
}

@ -1,8 +1,8 @@
import {EscapeValue, QuerySafeValue, raw, SQLDialect} from './SQLDialect'
import {Constraint, inverseFieldType, isConstraintGroup, isConstraintItem, SpecifiedField} from '../types'
import {Constraint, inverseFieldType, isConstraintGroup, isConstraintItem, QuerySource, SpecifiedField} from '../types'
import {AbstractBuilder} from '../builder/AbstractBuilder'
import {ColumnBuilder, ConstraintBuilder, ConstraintType, IndexBuilder, TableBuilder} from '../schema/TableBuilder'
import {ErrorWithContext, Maybe} from '../../util'
import {collect, Collectable, Collection, ErrorWithContext, hasOwnProperty, Maybe} from '../../util'
/**
* An implementation of the SQLDialect specific to PostgreSQL.
@ -14,7 +14,7 @@ export class PostgreSQLDialect extends SQLDialect {
public escape(value: EscapeValue): QuerySafeValue {
if ( value instanceof QuerySafeValue ) {
return value
} else if ( Array.isArray(value) ) {
} else if ( Array.isArray(value) || value instanceof Collection ) {
return new QuerySafeValue(value, `(${value.map(v => this.escape(v)).join(',')})`)
} else if ( String(value).toLowerCase() === 'true' || value === true ) {
return new QuerySafeValue(value, 'TRUE')
@ -34,16 +34,29 @@ export class PostgreSQLDialect extends SQLDialect {
]
return new QuerySafeValue(value, `'${y}-${m}-${d} ${h}:${i}:${s}'`)
} else if ( !isNaN(Number(value)) ) {
return new QuerySafeValue(value, String(Number(value)))
} else if ( value === null || typeof value === 'undefined' ) {
return new QuerySafeValue(value, 'NULL')
} else if ( !isNaN(Number(value)) ) {
return new QuerySafeValue(value, String(Number(value)))
} else {
const escaped = value.replace(/'/g, '\\\'') // .replace(/"/g, '\\"').replace(/`/g, '\\`')
const escaped = value.replace(/'/g, '\'\'') // .replace(/"/g, '\\"').replace(/`/g, '\\`')
return new QuerySafeValue(value, `'${escaped}'`)
}
}
public renderQuerySource(source: QuerySource): string {
if ( source instanceof QuerySafeValue ) {
return String(source)
} else if ( typeof source === 'string' ) {
return source.replace(/"/g, '""')
.split('.')
.map(x => '"' + x + '"')
.join('.')
}
return `${this.renderQuerySource(source.table)} AS "${source.alias.replace(/"/g, '""')}"`
}
public renderCount(query: string): string {
return [
'SELECT COUNT(*) AS "extollo_render_count"',
@ -59,7 +72,7 @@ export class PostgreSQLDialect extends SQLDialect {
'FROM (',
...query.split('\n').map(x => ` ${x}`),
') AS extollo_target_query',
`OFFSET ${start} LIMIT ${(end - start) + 1}`, // FIXME - the +1 is only needed when start === end
`OFFSET ${start} LIMIT ${start === end ? ((end - start) + 1) : (end - start)}`,
].join('\n')
}
@ -111,10 +124,7 @@ export class PostgreSQLDialect extends SQLDialect {
// FIXME error if no source
const source = builder.querySource
if ( source ) {
const tableString = typeof source === 'string' ? source : source.table
const table: string = tableString.split('.').map(x => `"${x}"`)
.join('.')
queryLines.push('FROM ' + (typeof source === 'string' ? table : `${table} "${source.alias}"`))
queryLines.push('FROM ' + this.renderQuerySource(source))
}
// Add constraints
@ -154,6 +164,76 @@ export class PostgreSQLDialect extends SQLDialect {
return queryLines.join('\n')
}
public renderBatchUpdate(builder: AbstractBuilder<any>, primaryKey: string, dataRows: Collectable<{[key: string]: EscapeValue}>): string {
const rows = Collection.normalize(dataRows)
const rawSql = builder.appliedRawSql
if ( rawSql ) {
return rawSql
}
const queryLines: string[] = []
// Add table source
let source = builder.querySource
if ( !source ) {
throw new ErrorWithContext('No table specified for update query')
}
source = (typeof source !== 'string' && !(source instanceof QuerySafeValue)) ? source : {
table: source,
alias: 'extollo_update_source',
}
const sourceAlias = source.alias
const sourceTable = source.table
queryLines.push('UPDATE ' + this.renderQuerySource(source))
queryLines.push('SET')
const updateFields = this.getAllFieldsFromUpdateRows(rows)
const updateTuples = rows.map(row => {
return updateFields.map(field => {
if ( hasOwnProperty(row, field) ) {
return this.escape(row[field])
}
// FIXME: This is fairly inefficient. Probably a better way with a FROM ... SELECT
// return raw(`"${sourceAlias}"."${field}"`)
return raw(`(SELECT "${field}" FROM ${sourceTable} WHERE "${primaryKey}" = ${this.escape(row[primaryKey])})`)
})
})
queryLines.push(updateFields.map(field => ` "${field}" = "extollo_update_tuple"."${field}"`).join(',\n'))
queryLines.push('FROM (VALUES')
queryLines.push(
updateTuples.map(tuple => ` (${tuple.implode(', ')})`).join(',\n'),
)
queryLines.push(`) as extollo_update_tuple(${updateFields.map(x => `"${x}"`).join(', ')})`)
queryLines.push(`WHERE "extollo_update_tuple"."${primaryKey}" = "${sourceAlias}"."${primaryKey}" AND (`)
queryLines.push(this.renderConstraints(builder.appliedConstraints, 2))
queryLines.push(`)`)
return queryLines.join('\n')
}
private getAllFieldsFromUpdateRows(rows: Collection<{[key: string]: EscapeValue}>): Collection<string> {
return rows.reduce((fields: Collection<string>, row) => {
Object.keys(row).forEach(key => {
if ( !fields.includes(key) ) {
fields.push(key)
}
})
return fields
}, collect<string>())
}
// TODO support FROM, RETURNING
public renderUpdate(builder: AbstractBuilder<any>, data: {[key: string]: EscapeValue}): string {
const rawSql = builder.appliedRawSql
@ -166,10 +246,7 @@ export class PostgreSQLDialect extends SQLDialect {
// Add table source
const source = builder.querySource
if ( source ) {
const tableString = typeof source === 'string' ? source : source.table
const table: string = tableString.split('.').map(x => `"${x}"`)
.join('.')
queryLines.push('UPDATE ' + (typeof source === 'string' ? table : `${table} "${source.alias}"`))
queryLines.push('UPDATE ' + this.renderQuerySource(source))
}
queryLines.push(this.renderUpdateSet(data))
@ -181,6 +258,14 @@ export class PostgreSQLDialect extends SQLDialect {
queryLines.push(wheres)
}
const fields = this.renderFields(builder).map(x => ` ${x}`)
.join(',\n')
if ( fields ) {
queryLines.push('RETURNING')
queryLines.push(fields)
}
return queryLines.join('\n')
}
@ -226,10 +311,7 @@ export class PostgreSQLDialect extends SQLDialect {
// Add table source
const source = builder.querySource
if ( source ) {
const tableString = typeof source === 'string' ? source : source.table
const table: string = tableString.split('.').map(x => `"${x}"`)
.join('.')
queryLines.push('INSERT INTO ' + (typeof source === 'string' ? table : `${table} AS "${source.alias}"`)
queryLines.push('INSERT INTO ' + this.renderQuerySource(source)
+ (columns.length ? ` (${columns.map(x => `"${x}"`).join(', ')})` : ''))
}
@ -272,10 +354,7 @@ export class PostgreSQLDialect extends SQLDialect {
// Add table source
const source = builder.querySource
if ( source ) {
const tableString = typeof source === 'string' ? source : source.table
const table: string = tableString.split('.').map(x => `"${x}"`)
.join('.')
queryLines.push('DELETE FROM ' + (typeof source === 'string' ? table : `${table} "${source.alias}"`))
queryLines.push('DELETE FROM ' + this.renderQuerySource(source))
}
// Add constraints
@ -298,8 +377,8 @@ export class PostgreSQLDialect extends SQLDialect {
return queryLines.join('\n')
}
public renderConstraints(allConstraints: Constraint[]): string {
const constraintsToSql = (constraints: Constraint[], level = 1): string => {
public renderConstraints(allConstraints: Constraint[], startingLevel = 1): string {
const constraintsToSql = (constraints: Constraint[], level = startingLevel): string => {
const indent = Array(level * 2).fill(' ')
.join('')
const statements = []
@ -316,6 +395,8 @@ export class PostgreSQLDialect extends SQLDialect {
const field: string = constraint.field.split('.').map(x => `"${x}"`)
.join('.')
statements.push(`${indent}${statements.length < 1 ? '' : constraint.preop + ' '}${field} ${constraint.operator} ${this.escape(constraint.operand).value}`)
} else if ( constraint instanceof QuerySafeValue ) {
statements.push(`${indent}${statements.length < 1 ? '' : 'AND '}${constraint.toString()}`)
}
}
@ -335,7 +416,7 @@ export class PostgreSQLDialect extends SQLDialect {
sets.push(` "${key}" = ${this.escape(data[key])}`)
}
return ['SET', ...sets].join('\n')
return `SET\n${sets.join(',\n')}`
}
public renderCreateTable(builder: TableBuilder): string {
@ -463,7 +544,7 @@ export class PostgreSQLDialect extends SQLDialect {
return parts.join('\n')
}
public renderAlterTable(builder: TableBuilder): string {
public renderAlterTable(builder: TableBuilder): Maybe<string> {
const alters: string[] = []
const columns = builder.getColumns()
@ -547,6 +628,10 @@ export class PostgreSQLDialect extends SQLDialect {
alters.push(` RENAME TO "${builder.getRename()}"`)
}
if ( !alters.length ) {
return undefined
}
return 'ALTER TABLE ' + builder.name + '\n' + alters.join(',\n')
}
@ -580,4 +665,8 @@ export class PostgreSQLDialect extends SQLDialect {
return parts.join('\n')
}
public currentTimestamp(): QuerySafeValue {
return raw('NOW()')
}
}

@ -1,20 +1,23 @@
import {Constraint} from '../types'
import {Constraint, QuerySource} from '../types'
import {AbstractBuilder} from '../builder/AbstractBuilder'
import {AppClass} from '../../lifecycle/AppClass'
import {ColumnBuilder, IndexBuilder, TableBuilder} from '../schema/TableBuilder'
import {Collectable, Collection, Maybe} from '../../util'
/**
* A value which can be escaped to be interpolated into an SQL query.
*/
export type EscapeValue = null | undefined | string | number | boolean | Date | QuerySafeValue | EscapeValue[] // FIXME | Select<any>
/** A scalar value which can be interpolated safely into an SQL query. */
export type ScalarEscapeValue = null | undefined | string | number | boolean | Date | QuerySafeValue;
/**
* Object mapping string field names to EscapeValue items.
*/
/** A list of scalar escape values. */
export type VectorEscapeValue<T extends ScalarEscapeValue> = T[] | Collection<T>
/** All possible escaped query values. */
export type EscapeValue<T extends ScalarEscapeValue = ScalarEscapeValue> = T | VectorEscapeValue<T> // FIXME | Select<any>
/** Object mapping string field names to EscapeValue items. */
export type EscapeValueObject = { [field: string]: EscapeValue }
/**
* A wrapper class whose value is save to inject directly into a query.
* A wrapper class whose value is safe to inject directly into a query.
*/
export class QuerySafeValue {
constructor(
@ -52,6 +55,12 @@ export abstract class SQLDialect extends AppClass {
*/
public abstract escape(value: EscapeValue): QuerySafeValue
/**
* Render a query source object as a qualified table name string ("tablename" as "alias").
* @param source
*/
public abstract renderQuerySource(source: QuerySource): string;
/**
* Render the given query builder as a "SELECT ..." query string.
*
@ -70,6 +79,15 @@ export abstract class SQLDialect extends AppClass {
*/
public abstract renderUpdate(builder: AbstractBuilder<any>, data: {[key: string]: EscapeValue}): string;
/**
* Render the given query builder as an "UPDATE ..." query string, setting column values
* for multiple distinct records based on their primary key.
* @param builder
* @param primaryKey
* @param dataRows
*/
public abstract renderBatchUpdate(builder: AbstractBuilder<any>, primaryKey: string, dataRows: Collectable<{[key: string]: EscapeValue}>): string;
/**
* Render the given query builder as a "DELETE ..." query string.
*
@ -180,7 +198,7 @@ export abstract class SQLDialect extends AppClass {
* Given a table schema-builder, render an `ALTER TABLE...` query.
* @param builder
*/
public abstract renderAlterTable(builder: TableBuilder): string;
public abstract renderAlterTable(builder: TableBuilder): Maybe<string>;
/**
* Given a table schema-builder, render a `DROP TABLE...` query.
@ -249,6 +267,12 @@ export abstract class SQLDialect extends AppClass {
*/
public abstract renderTransaction(queries: string[]): string;
/**
* Get the expression for the current timestamp as an escaped value.
* @example `raw('NOW()')`
*/
public abstract currentTimestamp(): QuerySafeValue;
/**
* Given a table schema-builder, render a series of queries as a transaction
* that apply the given schema to database.
@ -290,7 +314,10 @@ export abstract class SQLDialect extends AppClass {
if ( !builder.isExisting() && builder.isDirty() ) {
parts.push(this.renderCreateTable(builder))
} else if ( builder.isExisting() && builder.isDirty() ) {
parts.push(this.renderAlterTable(builder))
const alterTable = this.renderAlterTable(builder)
if ( alterTable ) {
parts.push(alterTable)
}
}
// Render the various schema queries as a single transaction

@ -0,0 +1,617 @@
import {EscapeValue, QuerySafeValue, raw, SQLDialect} from './SQLDialect'
import {Collection, ErrorWithContext, Maybe} from '../../util'
import {AbstractBuilder} from '../builder/AbstractBuilder'
import {
Constraint,
FieldType,
inverseFieldType,
isConstraintGroup,
isConstraintItem,
QuerySource,
SpecifiedField,
} from '../types'
import {ColumnBuilder, ConstraintBuilder, ConstraintType, IndexBuilder, TableBuilder} from '../schema/TableBuilder'
export class SQLiteDialect extends SQLDialect {
public escape(value: EscapeValue): QuerySafeValue {
if ( value instanceof QuerySafeValue ) {
return value
} else if ( Array.isArray(value) || value instanceof Collection ) {
return new QuerySafeValue(value, `(${value.map(v => this.escape(v)).join(',')})`)
} else if ( String(value).toLowerCase() === 'true' || value === true ) {
return new QuerySafeValue(value, 'TRUE')
} else if ( String(value).toLowerCase() === 'false' || value === false ) {
return new QuerySafeValue(value, 'FALSE')
} else if ( typeof value === 'number' ) {
return new QuerySafeValue(value, `${value}`)
} else if ( value instanceof Date ) {
const pad = (val: number) => val < 10 ? `0${val}` : `${val}`
const [y, m, d, h, i, s] = [
`${value.getFullYear()}`,
`${pad(value.getMonth() + 1)}`,
`${pad(value.getDate())}`,
`${pad(value.getHours())}`,
`${pad(value.getMinutes())}`,
`${pad(value.getSeconds())}`,
]
return new QuerySafeValue(value, `'${y}-${m}-${d} ${h}:${i}:${s}'`)
} else if ( value === null || typeof value === 'undefined' ) {
return new QuerySafeValue(value, 'NULL')
} else if ( !isNaN(Number(value)) ) {
return new QuerySafeValue(value, String(Number(value)))
} else {
const escaped = value.replace(/'/g, '\'\'')
return new QuerySafeValue(value, `'${escaped}'`)
}
}
public renderQuerySource(source: QuerySource): string {
if ( source instanceof QuerySafeValue ) {
return String(source)
} else if ( typeof source === 'string' ) {
return source.replace(/"/g, '""')
.split('.')
.map(x => '"' + x + '"')
.join('.')
}
return `${this.renderQuerySource(source.table)} AS "${source.alias.replace(/"/g, '""')}"`
}
public renderCount(query: string): string {
return [
'SELECT COUNT(*) AS "extollo_render_count"',
'FROM (',
...query.split('\n').map(x => ` ${x}`),
') AS extollo_target_query',
].join('\n')
}
public renderRangedSelect(query: string, start: number, end: number): string {
return [
'SELECT *',
'FROM (',
...query.split('\n').map(x => ` ${x}`),
') AS extollo_target_query',
`LIMIT ${start === end ? ((end - start) + 1) : (end - start)} OFFSET ${start}`,
].join('\n')
}
/** Render the fields from the builder class to PostgreSQL syntax. */
protected renderFields(builder: AbstractBuilder<any>): string[] {
return builder.appliedFields.map((field: SpecifiedField) => {
let columnString: string
if ( typeof field === 'string' ) {
columnString = field.split('.').map(x => `"${x}"`)
.join('.')
} else if ( field instanceof QuerySafeValue ) {
columnString = field.toString()
} else if ( typeof field.field === 'string' ) {
columnString = field.field.split('.').map(x => `"${x}"`)
.join('.')
} else {
columnString = field.field.toString()
}
let aliasString = ''
if ( typeof field !== 'string' && !(field instanceof QuerySafeValue) ) {
aliasString = ` AS "${field.alias}"`
}
return `${columnString}${aliasString}`
})
}
public renderSelect(builder: AbstractBuilder<any>): string {
const rawSql = builder.appliedRawSql
if ( rawSql ) {
return rawSql
}
const indent = (item: string, level = 1) => Array(level + 1).fill('')
.join(' ') + item
const queryLines = [
`SELECT${builder.appliedDistinction ? ' DISTINCT' : ''}`,
]
// Add fields
// FIXME error if no fields
const fields = this.renderFields(builder).map(x => indent(x))
.join(',\n')
queryLines.push(fields)
// Add table source
// FIXME error if no source
const source = builder.querySource
if ( source ) {
queryLines.push('FROM ' + this.renderQuerySource(source))
}
// Add constraints
const wheres = this.renderConstraints(builder.appliedConstraints)
if ( wheres.trim() ) {
queryLines.push('WHERE')
queryLines.push(wheres)
}
// Add group by
if ( builder.appliedGroupings?.length ) {
const grouping = builder.appliedGroupings.map(group => {
return indent(group.split('.').map(x => `"${x}"`)
.join('.'))
}).join(',\n')
queryLines.push('GROUP BY')
queryLines.push(grouping)
}
// Add order by
if ( builder.appliedOrder?.length ) {
const ordering = builder.appliedOrder.map(x => indent(`${x.field.split('.').map(y => '"' + y + '"')
.join('.')} ${x.direction}`)).join(',\n')
queryLines.push('ORDER BY')
queryLines.push(ordering)
}
// Add limit/offset
const pagination = builder.appliedPagination
if ( pagination.take ) {
queryLines.push(`LIMIT ${pagination.take}${pagination.skip ? ' OFFSET ' + pagination.skip : ''}`)
} else if ( pagination.skip ) {
queryLines.push(`LIMIT -1 OFFSET ${pagination.skip}`)
}
return queryLines.join('\n')
}
public renderBatchUpdate(): string {
throw new ErrorWithContext('SQLite dialect does not support batch updates.')
}
// TODO support FROM, RETURNING
public renderUpdate(builder: AbstractBuilder<any>, data: {[key: string]: EscapeValue}): string {
const rawSql = builder.appliedRawSql
if ( rawSql ) {
return rawSql
}
const queryLines: string[] = []
// Add table source
const source = builder.querySource
if ( source ) {
queryLines.push('UPDATE ' + this.renderQuerySource(source))
}
queryLines.push(this.renderUpdateSet(data))
// Add constraints
const wheres = this.renderConstraints(builder.appliedConstraints)
if ( wheres.trim() ) {
queryLines.push('WHERE')
queryLines.push(wheres)
}
const fields = this.renderFields(builder).map(x => ` ${x}`)
.join(',\n')
if ( fields ) {
queryLines.push('RETURNING')
queryLines.push(fields)
}
return queryLines.join('\n')
}
public renderExistential(builder: AbstractBuilder<any>): string {
const rawSql = builder.appliedRawSql
if ( rawSql ) {
return `
SELECT EXISTS(
${rawSql}
)
`
}
const query = builder.clone()
.clearFields()
.field(raw('TRUE'))
.limit(1)
return this.renderSelect(query)
}
// FIXME: subquery support here and with select
public renderInsert(builder: AbstractBuilder<any>, data: {[key: string]: EscapeValue}|{[key: string]: EscapeValue}[] = []): string {
const rawSql = builder.appliedRawSql
if ( rawSql ) {
return rawSql
}
const indent = (item: string, level = 1) => Array(level + 1).fill('')
.join(' ') + item
const queryLines: string[] = []
if ( !Array.isArray(data) ) {
data = [data]
}
if ( data.length < 1 ) {
return ''
}
const columns = Object.keys(data[0])
// Add table source
const source = builder.querySource
if ( source ) {
queryLines.push('INSERT INTO ' + this.renderQuerySource(source)
+ (columns.length ? ` (${columns.map(x => `"${x}"`).join(', ')})` : ''))
}
if ( Array.isArray(data) && !data.length ) {
queryLines.push('DEFAULT VALUES')
} else {
queryLines.push('VALUES')
const valueString = data.map(row => {
const values = columns.map(x => this.escape(row[x]))
return indent(`(${values.join(', ')})`)
})
.join(',\n')
queryLines.push(valueString)
}
// Add return fields
if ( builder.appliedFields?.length ) {
queryLines.push('RETURNING')
const fields = this.renderFields(builder).map(x => indent(x))
.join(',\n')
queryLines.push(fields)
}
return queryLines.join('\n')
}
public renderDelete(builder: AbstractBuilder<any>): string {
const rawSql = builder.appliedRawSql
if ( rawSql ) {
return rawSql
}
const indent = (item: string, level = 1) => Array(level + 1).fill('')
.join(' ') + item
const queryLines: string[] = []
// Add table source
const source = builder.querySource
if ( source ) {
queryLines.push('DELETE FROM ' + this.renderQuerySource(source))
}
// Add constraints
const wheres = this.renderConstraints(builder.appliedConstraints)
if ( wheres.trim() ) {
queryLines.push('WHERE')
queryLines.push(wheres)
}
// Add return fields
if ( builder.appliedFields?.length ) {
queryLines.push('RETURNING')
const fields = this.renderFields(builder).map(x => indent(x))
.join(',\n')
queryLines.push(fields)
}
return queryLines.join('\n')
}
public renderConstraints(allConstraints: Constraint[], startingLevel = 1): string {
const constraintsToSql = (constraints: Constraint[], level = startingLevel): string => {
const indent = Array(level * 2).fill(' ')
.join('')
const statements = []
for ( const constraint of constraints ) {
if ( isConstraintGroup(constraint) ) {
statements.push(`${indent}${statements.length < 1 ? '' : constraint.preop + ' '}(\n${constraintsToSql(constraint.items, level + 1)}\n${indent})`)
} else if ( isConstraintItem(constraint) ) {
if ( Array.isArray(constraint.operand) && !constraint.operand.length ) {
statements.push(`${indent}1 = 0 -- ${constraint.field} ${constraint.operator} empty set`)
continue
}
const field: string = constraint.field.split('.').map(x => `"${x}"`)
.join('.')
statements.push(`${indent}${statements.length < 1 ? '' : constraint.preop + ' '}${field} ${constraint.operator} ${this.escape(constraint.operand).value}`)
} else if ( constraint instanceof QuerySafeValue ) {
statements.push(`${indent}${statements.length < 1 ? '' : 'AND '}${constraint.toString()}`)
}
}
return statements.filter(Boolean).join('\n')
}
return constraintsToSql(allConstraints)
}
public renderUpdateSet(data: {[key: string]: EscapeValue}): string {
const sets = []
for ( const key in data ) {
if ( !Object.prototype.hasOwnProperty.call(data, key) ) {
continue
}
sets.push(` "${key}" = ${this.escape(data[key])}`)
}
return `SET\n${sets.join(',\n')}`
}
public renderCreateTable(builder: TableBuilder): string {
const cols = this.renderTableColumns(builder).map(x => ` ${x}`)
const builderConstraints = builder.getConstraints()
const constraints: string[] = []
for ( const constraintName in builderConstraints ) {
if ( !Object.prototype.hasOwnProperty.call(builderConstraints, constraintName) ) {
continue
}
const constraintBuilder = builderConstraints[constraintName]
const constraintDefinition = this.renderConstraintDefinition(constraintBuilder)
if ( constraintDefinition ) {
constraints.push(` CONSTRAINT ${constraintDefinition}`)
}
}
const parts = [
`CREATE TABLE ${builder.isSkippedIfExisting() ? 'IF NOT EXISTS ' : ''}${builder.name} (`,
[
...cols,
...constraints,
].join(',\n'),
`)`,
]
return parts.join('\n')
}
public renderTableColumns(builder: TableBuilder): string[] {
const defined = builder.getColumns()
const rendered: string[] = []
for ( const columnName in defined ) {
if ( !Object.prototype.hasOwnProperty.call(defined, columnName) ) {
continue
}
const columnBuilder = defined[columnName]
rendered.push(this.renderColumnDefinition(columnBuilder))
}
return rendered
}
/**
* Given a constraint schema-builder, render the constraint definition.
* @param builder
* @protected
*/
protected renderConstraintDefinition(builder: ConstraintBuilder): Maybe<string> {
const constraintType = builder.getType()
if ( constraintType === ConstraintType.Unique ) {
const fields = builder.getFields()
.map(x => `"${x}"`)
.join(',')
return `${builder.name} UNIQUE(${fields})`
} else if ( constraintType === ConstraintType.Check ) {
const expression = builder.getExpression()
if ( !expression ) {
throw new ErrorWithContext('Cannot create check constraint without expression.', {
constraintName: builder.name,
tableName: builder.parent.name,
})
}
return `${builder.name} CHECK(${expression})`
}
}
/**
* Given a column-builder, render the SQL-definition as used in
* CREATE TABLE and ALTER TABLE statements.
* @fixme Type `serial` only exists on CREATE TABLE... queries
* @param builder
* @protected
*/
protected renderColumnDefinition(builder: ColumnBuilder): string {
const type = builder.getType()
if ( !type ) {
throw new ErrorWithContext(`Missing field type for column: ${builder.name}`, {
columnName: builder.name,
columnType: type,
})
}
let render = `${builder.name} ${inverseFieldType(this.mapFieldType(type))}`
if ( builder.getLength() ) {
render += `(${builder.getLength()})`
}
const defaultValue = builder.getDefaultValue()
if ( typeof defaultValue !== 'undefined' ) {
render += ` DEFAULT ${this.escape(defaultValue)}`
}
if ( builder.isPrimary() ) {
render += ` PRIMARY KEY`
}
if ( type === FieldType.serial ) {
render += ` AUTOINCREMENT`
}
if ( builder.isUnique() ) {
render += ` UNIQUE`
}
render += ` ${builder.isNullable() ? 'NULL' : 'NOT NULL'}`
return render
}
private mapFieldType(type: FieldType): FieldType {
if ( type === FieldType.serial ) {
return FieldType.integer
}
return type
}
public renderDropTable(builder: TableBuilder): string {
return `DROP TABLE ${builder.isSkippedIfExisting() ? 'IF EXISTS ' : ''}${builder.name}`
}
public renderCreateIndex(builder: IndexBuilder): string {
const cols = builder.getFields().map(x => `"${x}"`)
const parts = [
`CREATE ${builder.isUnique() ? 'UNIQUE ' : ''}INDEX ${builder.isSkippedIfExisting() ? 'IF NOT EXISTS ' : ''}${builder.name}`,
` ON ${builder.parent.name}`,
` (${cols.join(',')})`,
]
return parts.join('\n')
}
public renderAlterTable(builder: TableBuilder): string {
const alters: string[] = []
const columns = builder.getColumns()
for ( const columnName in columns ) {
if ( !Object.prototype.hasOwnProperty.call(columns, columnName) ) {
continue
}
const columnBuilder = columns[columnName]
if ( !columnBuilder.isExisting() ) {
// The column doesn't exist on the table, but was added to the schema
alters.push(` ADD COLUMN ${this.renderColumnDefinition(columnBuilder)}`)
} else if ( columnBuilder.isDirty() && columnBuilder.originalFromSchema ) {
// The column exists in the table, but was modified in the schema
if ( columnBuilder.isDropping() || columnBuilder.isDroppingIfExists() ) {
alters.push(` DROP COLUMN "${columnBuilder.name}"`)
continue
}
// Change the data type of the column
if ( columnBuilder.getType() !== columnBuilder.originalFromSchema.getType() ) {
const renderedType = `${columnBuilder.getType()}${columnBuilder.getLength() ? `(${columnBuilder.getLength()})` : ''}`
alters.push(` ALTER COLUMN "${columnBuilder.name}" TYPE ${renderedType}`)
}
// Change the default value of the column
if ( columnBuilder.getDefaultValue() !== columnBuilder.originalFromSchema.getDefaultValue() ) {
alters.push(` ALTER COLUMN "${columnBuilder.name}" SET default ${this.escape(columnBuilder.getDefaultValue())}`)
}
// Change the nullable-status of the column
if ( columnBuilder.isNullable() !== columnBuilder.originalFromSchema.isNullable() ) {
if ( columnBuilder.isNullable() ) {
alters.push(` ALTER COLUMN "${columnBuilder.name}" DROP NOT NULL`)
} else {
alters.push(` ALTER COLUMN "${columnBuilder.name}" SET NOT NULL`)
}
}
// Change the name of the column
if ( columnBuilder.getRename() ) {
alters.push(` RENAME COLUMN "${columnBuilder.name}" TO "${columnBuilder.getRename()}"`)
}
}
}
const constraints = builder.getConstraints()
for ( const constraintName in constraints ) {
if ( !Object.prototype.hasOwnProperty.call(constraints, constraintName) ) {
continue
}
const constraintBuilder = constraints[constraintName]
// Drop the constraint if specified
if ( constraintBuilder.isDropping() ) {
alters.push(` DROP CONSTRAINT ${constraintBuilder.name}`)
continue
}
// Drop the constraint with IF EXISTS if specified
if ( constraintBuilder.isDroppingIfExists() ) {
alters.push(` DROP CONSTRAINT IF EXISTS ${constraintBuilder.name}`)
continue
}
// Otherwise, drop and recreate the constraint if it was modified
if ( constraintBuilder.isDirty() ) {
if ( constraintBuilder.isExisting() ) {
alters.push(` DROP CONSTRAINT IF EXISTS ${constraintBuilder.name}`)
}
const constraintDefinition = this.renderConstraintDefinition(constraintBuilder)
if ( constraintDefinition ) {
alters.push(` ADD CONSTRAINT ${constraintDefinition}`)
}
}
}
if ( builder.getRename() ) {
alters.push(` RENAME TO "${builder.getRename()}"`)
}
return 'ALTER TABLE ' + builder.name + '\n' + alters.join(',\n')
}
public renderDropIndex(builder: IndexBuilder): string {
return `DROP INDEX ${builder.isDroppingIfExists() ? 'IF EXISTS ' : ''}${builder.name}`
}
public renderTransaction(queries: string[]): string {
return queries.join(';\n\n') // fixme: sqlite3 in node doesn't properly support transactions
// const parts = [
// 'BEGIN',
// ...queries,
// 'COMMIT;',
// ]
//
// return parts.join(';\n\n')
}
public renderRenameIndex(builder: IndexBuilder): string {
return `ALTER INDEX ${builder.name} RENAME TO ${builder.getRename()}`
}
public renderRecreateIndex(builder: IndexBuilder): string {
return `${this.renderDropIndex(builder)};\n\n${this.renderCreateIndex(builder)}`
}
public renderDropColumn(builder: ColumnBuilder): string {
const parts = [
`ALTER TABLE ${builder.parent.name} ${builder.parent.isSkippedIfExisting() ? 'IF EXISTS ' : ''}`,
` DROP COLUMN ${builder.isSkippedIfExisting() ? 'IF EXISTS ' : ''}${builder.name}`,
]
return parts.join('\n')
}
public currentTimestamp(): QuerySafeValue {
return raw('CURRENT_TIMESTAMP')
}
}

@ -7,11 +7,13 @@ export * from './builder/Builder'
export * from './connection/Connection'
export * from './connection/PostgresConnection'
export * from './connection/SQLiteConnection'
export * from './connection/event/QueryExecutedEvent'
export * from './connection/event/QueryExecutedEventSerializer'
export * from './dialect/SQLDialect'
export * from './dialect/PostgreSQLDialect'
export * from './dialect/SQLiteDialect'
export * from './model/Field'
export * from './model/ModelBuilder'
@ -20,12 +22,15 @@ export * from './model/ModelResultIterable'
export * from './model/events'
export * from './model/Model'
export * from './model/ModelSerializer'
export * from './model/TreeModel'
export * from './model/relation/RelationBuilder'
export * from './model/relation/Relation'
export * from './model/relation/HasOneOrMany'
export * from './model/relation/HasOne'
export * from './model/relation/HasMany'
export * from './model/relation/HasSubtree'
export * from './model/relation/HasTreeParent'
export * from './model/relation/decorators'
export * from './model/scope/Scope'
@ -42,6 +47,7 @@ export * from './types'
export * from './schema/TableBuilder'
export * from './schema/Schema'
export * from './schema/PostgresSchema'
export * from './schema/SQLiteSchema'
export * from './services/Migrations'
export * from './migrations/Migrator'

@ -1,4 +1,4 @@
import {Container, Inject, Injectable} from '../../di'
import {Inject, Injectable} from '../../di'
import {Migrator} from './Migrator'
import {DatabaseService} from '../DatabaseService'
import {FieldType} from '../types'
@ -14,9 +14,6 @@ export class DatabaseMigrator extends Migrator {
@Inject()
protected readonly db!: DatabaseService
@Inject('injector')
protected readonly injector!: Container
/** True if we've initialized the migrator. */
protected initialized = false
@ -140,12 +137,13 @@ export class DatabaseMigrator extends Migrator {
* @protected
*/
protected async filterAppliedMigrations(identifiers: string[]): Promise<string[]> {
const existing = await this.builder()
const existing = (await this.builder()
.connection('default')
.select('identifier')
.from('migrations')
.whereIn('identifier', identifiers)
.get()
.collect())
.pluck<string>('identifier')
return identifiers.filter(id => !existing.includes(id))

@ -1,82 +1,23 @@
import {
AbstractFactory,
DependencyRequirement,
PropertyDependency,
isInstantiable,
DEPENDENCY_KEYS_METADATA_KEY,
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, Instantiable, Injectable, Inject, FactoryProducer,
} from '../../di'
import {Collection, ErrorWithContext} from '../../util'
import {Logging} from '../../service/Logging'
import {Config} from '../../service/Config'
import {Instantiable, FactoryProducer} from '../../di'
import {Migrator} from './Migrator'
import {DatabaseMigrator} from './DatabaseMigrator'
import {ConfiguredSingletonFactory} from '../../di/factory/ConfiguredSingletonFactory'
/**
* A dependency injection factory that matches the abstract Migrator class
* and produces an instance of the configured session driver implementation.
*/
@Injectable()
@FactoryProducer()
export class MigratorFactory extends AbstractFactory<Migrator> {
@Inject()
protected readonly logging!: Logging
@Inject()
protected readonly config!: Config
constructor() {
super({})
}
produce(): Migrator {
return new (this.getMigratorClass())()
export class MigratorFactory extends ConfiguredSingletonFactory<Migrator> {
protected getConfigKey(): string {
return 'database.migrations.driver'
}
match(something: unknown): boolean {
return something === Migrator
}
getDependencyKeys(): Collection<DependencyRequirement> {
const meta = Reflect.getMetadata(DEPENDENCY_KEYS_METADATA_KEY, this.getMigratorClass())
if ( meta ) {
return meta
}
return new Collection<DependencyRequirement>()
protected getDefaultImplementation(): Instantiable<Migrator> {
return DatabaseMigrator
}
getInjectedProperties(): Collection<PropertyDependency> {
const meta = new Collection<PropertyDependency>()
let currentToken = this.getMigratorClass()
do {
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
if ( loadedMeta ) {
meta.concat(loadedMeta)
}
currentToken = Object.getPrototypeOf(currentToken)
} while (Object.getPrototypeOf(currentToken) !== Function.prototype && Object.getPrototypeOf(currentToken) !== Object.prototype)
return meta
}
/**
* Return the instantiable class of the configured migrator backend.
* @protected
* @return Instantiable<Migrator>
*/
protected getMigratorClass(): Instantiable<Migrator> {
const MigratorClass = this.config.get('database.migrations.driver', DatabaseMigrator)
if ( !isInstantiable(MigratorClass) || !(MigratorClass.prototype instanceof Migrator) ) {
const e = new ErrorWithContext('Provided migration driver class does not extend from @extollo/lib.Migrator')
e.context = {
configKey: 'database.migrations.driver',
class: MigratorClass.toString(),
}
}
return MigratorClass
protected getAbstractImplementation(): any {
return Migrator
}
}

@ -1,11 +1,10 @@
import {ModelKey, QueryRow, QuerySource} from '../types'
import {Container, Inject, Instantiable, isInstantiable} from '../../di'
import {Container, Instantiable, isInstantiable} from '../../di'
import {DatabaseService} from '../DatabaseService'
import {ModelBuilder} from './ModelBuilder'
import {getFieldsMeta, ModelField} from './Field'
import {deepCopy, Collection, uuid4, isKeyof, Pipeline} from '../../util'
import {EscapeValueObject} from '../dialect/SQLDialect'
import {Logging} from '../../service/Logging'
import {deepCopy, Collection, uuid4, isKeyof, Pipeline, hasOwnProperty} from '../../util'
import {EscapeValueObject, QuerySafeValue} from '../dialect/SQLDialect'
import {Connection} from '../connection/Connection'
import {ModelRetrievedEvent} from './events/ModelRetrievedEvent'
import {ModelSavingEvent} from './events/ModelSavingEvent'
@ -19,16 +18,13 @@ import {HasOne} from './relation/HasOne'
import {HasMany} from './relation/HasMany'
import {HasOneOrMany} from './relation/HasOneOrMany'
import {Scope, ScopeClosure} from './scope/Scope'
import {LocalBus} from '../../support/bus/LocalBus'
import {LocalBus} from '../../support/bus/LocalBus' // need the specific import to prevent circular dependencies
import {ModelEvent} from './events/ModelEvent'
/**
* Base for classes that are mapped to tables in a database.
*/
export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>> {
@Inject()
protected readonly logging!: Logging
/**
* The name of the connection this model should run through.
* @type string
@ -96,6 +92,13 @@ export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>>
*/
protected originalSourceRow?: QueryRow
/**
* Database fields that should be run on the next save, even if the
* fields are not mapped to members on the model.
* @protected
*/
protected dirtySourceRow?: QueryRow
/**
* Cache of relation instances by property accessor.
* This is used by the `@Relation()` decorator to cache Relation instances.
@ -149,12 +152,13 @@ export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>>
* ```
*/
public static query<T2 extends Model<T2>>(): ModelBuilder<T2> {
const builder = <ModelBuilder<T2>> Container.getContainer().make<ModelBuilder<T2>>(ModelBuilder, this)
const di = Container.getContainer()
const builder = <ModelBuilder<T2>> di.make<ModelBuilder<T2>>(ModelBuilder, this)
const source: QuerySource = this.querySource()
builder.connection(this.getConnection())
if ( typeof source === 'string' ) {
if ( typeof source === 'string' || source instanceof QuerySafeValue ) {
builder.from(source)
} else {
builder.from(source.table, source.alias)
@ -164,36 +168,17 @@ export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>>
builder.field(field.databaseKey)
})
if ( Array.isArray(this.prototype.with) ) {
const inst = di.make<T2>(this)
if ( Array.isArray(inst.with) ) {
// Try to get the eager-loaded relations statically, if possible
for (const relation of this.prototype.with) {
for (const relation of inst.with) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
builder.with(relation)
}
} else if ( this.constructor.length < 1 ) {
// Otherwise, if we can instantiate the model without any arguments,
// do that and get the eager-loaded relations directly.
const inst = Container.getContainer().make<Model<any>>(this)
if ( Array.isArray(inst.with) ) {
for (const relation of inst.with) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
builder.with(relation)
}
}
}
if ( this.prototype.scopes ) {
// Same thing here. Try to get the scopes statically, if possible
builder.withScopes(this.prototype.scopes)
} else if ( this.constructor.length < 1 ) {
// Otherwise, try to instantiate the model if possible and load the scopes that way
const inst = Container.getContainer().make<Model<any>>(this)
builder.withScopes(inst.scopes)
}
inst.applyScopes(builder)
return builder
}
@ -231,6 +216,49 @@ export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>>
}
}
public getColumn(key: string): unknown {
if ( this.dirtySourceRow && hasOwnProperty(this.dirtySourceRow, key) ) {
return this.dirtySourceRow[key]
}
const field = getFieldsMeta(this)
.firstWhere('databaseKey', '=', key)
if ( field ) {
return (this as any)[field.modelKey]
}
return this.originalSourceRow?.[key]
}
/**
* Sets a value in the override row and (if applicable) associated class property
* for the given database column.
*
* @param key
* @param value
*/
public setColumn(key: string, value: unknown): this {
// Set the property on the database result row, if one exists
if ( !this.dirtySourceRow ) {
this.dirtySourceRow = {}
}
this.dirtySourceRow[key] = value
// Set the property on the mapped field on the class, if one exists
const field = getFieldsMeta(this)
.firstWhere('databaseKey', '=', key)
if ( field ) {
this.setFieldFromObject(field.modelKey, field.databaseKey, {
[key]: value,
})
}
return this
}
/**
* Given a row from the database, set the properties on this model that correspond to
* fields on that database.
@ -273,7 +301,7 @@ export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>>
/**
* Get the value of the primary key of this model, if it exists.
*/
public key(): string {
public key(): string|number {
const ctor = this.constructor as typeof Model
const field = getFieldsMeta(this)
@ -336,7 +364,7 @@ export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>>
builder.connection(ModelClass.getConnection())
if ( typeof source === 'string' ) {
if ( typeof source === 'string' || source instanceof QuerySafeValue ) {
builder.from(source)
} else {
builder.from(source.table, source.alias)
@ -350,8 +378,7 @@ export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>>
builder.with(relation)
}
builder.withScopes(this.scopes)
this.applyScopes(builder)
return builder
}
@ -462,6 +489,7 @@ export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>>
* @param modelKey
*/
public static propertyToColumn(modelKey: string): string {
console.log('propertyToColumn', modelKey, getFieldsMeta(this), this) // eslint-disable-line no-console
return getFieldsMeta(this)
.firstWhere('modelKey', '=', modelKey)?.databaseKey || modelKey
}
@ -504,7 +532,10 @@ export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>>
row[field.databaseKey] = (this as any)[field.modelKey]
})
return row
return {
...row,
...(this.dirtySourceRow || {}),
}
}
/**
@ -638,10 +669,12 @@ export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>>
}
const data = result.rows.firstWhere(this.keyName(), '=', this.key())
this.logging.debug({updata: data})
if ( data ) {
await this.assumeFromSource(data)
}
delete this.dirtySourceRow
await this.push(new ModelUpdatedEvent<T>(this as any))
} else if ( !this.exists() ) {
await this.push(new ModelCreatingEvent<T>(this as any))
@ -671,10 +704,12 @@ export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>>
}
const data = result.rows.first()
this.logging.debug({inserta: data})
if ( data ) {
await this.assumeFromSource(data)
}
delete this.dirtySourceRow
await this.push(new ModelCreatedEvent<T>(this as any))
}
@ -825,7 +860,14 @@ export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>>
*/
protected get isDirtyCheck(): (field: ModelField) => boolean {
return (field: ModelField) => {
return !this.originalSourceRow || (this as any)[field.modelKey] !== this.originalSourceRow[field.databaseKey]
return Boolean(
!this.originalSourceRow
|| (this as any)[field.modelKey] !== this.originalSourceRow[field.databaseKey]
|| (
this.dirtySourceRow
&& hasOwnProperty(this.dirtySourceRow, field.databaseKey)
),
)
}
}
@ -850,12 +892,17 @@ export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>>
this.logging.debug(`buildInsertFieldObject populateKeyOnInsert? ${ctor.populateKeyOnInsert}; keyName: ${this.keyName()}`)
return Pipeline.id<Collection<ModelField>>()
const row = Pipeline.id<Collection<ModelField>>()
.unless(ctor.populateKeyOnInsert, fields => {
return fields.where('databaseKey', '!=', this.keyName())
})
.apply(getFieldsMeta(this))
.keyMap('databaseKey', inst => (this as any)[inst.modelKey])
return {
...row,
...(this.dirtySourceRow || {}),
}
}
/**
@ -935,7 +982,7 @@ export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>>
* @param related
* @param relationName
*/
public belongsToOne<T2 extends Model<T2>>(related: Instantiable<T>, relationName: keyof T2): HasOne<T, T2> {
public belongsToOne<T2 extends Model<T2>>(related: Instantiable<T2>, relationName: keyof T2): HasOne<T, T2> {
const relatedInst = this.make(related) as T2
const relation = relatedInst.getRelation(relationName)
@ -1008,13 +1055,13 @@ export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>>
}
if ( typeof relFn === 'function' ) {
const rel = relFn.apply(relFn, this)
const rel = relFn.bind(this)()
if ( rel instanceof Relation ) {
return rel
}
}
throw new TypeError(`Cannot get relation of name: ${name}. Method does not return a Relation.`)
throw new TypeError(`Cannot get relation of name: ${String(name)}. Method does not return a Relation.`)
}
/**
@ -1072,4 +1119,12 @@ export abstract class Model<T extends Model<T>> extends LocalBus<ModelEvent<T>>
protected hasScope(name: string | Instantiable<Scope>): boolean {
return Boolean(this.scopes.firstWhere('accessor', '=', name))
}
/**
* Apply the default scopes to this model to the given query builder.
* @param builder
*/
public applyScopes(builder: ModelBuilder<T>): void {
builder.withScopes(this.scopes)
}
}

@ -78,7 +78,7 @@ export class ModelBuilder<T extends Model<T>> extends AbstractBuilder<T> {
*/
public with(relationName: keyof T): this {
if ( !this.eagerLoadRelations.includes(relationName) ) {
// Try to load the Relation so we fail if the name is invalid
// Try to load the Relation, so we fail if the name is invalid
this.make<T>(this.ModelClass).getRelation(relationName)
this.eagerLoadRelations.push(relationName)
}
@ -86,6 +86,15 @@ export class ModelBuilder<T extends Model<T>> extends AbstractBuilder<T> {
return this
}
/**
* Prevent a relation from being eager-loaded.
* @param relationName
*/
public without(relationName: keyof T): this {
this.eagerLoadRelations = this.eagerLoadRelations.filter(name => name !== relationName)
return this
}
/**
* Remove all global scopes from this query.
*/

@ -2,9 +2,10 @@ import {Model} from './Model'
import {AbstractResultIterable} from '../builder/result/AbstractResultIterable'
import {Connection} from '../connection/Connection'
import {ModelBuilder} from './ModelBuilder'
import {Container, Instantiable} from '../../di'
import {Instantiable} from '../../di'
import {QueryRow} from '../types'
import {collect, Collection} from '../../util'
import {getFieldsMeta} from './Field'
/**
* Implementation of the result iterable that returns query results as instances of the defined model.
@ -60,8 +61,11 @@ export class ModelResultIterable<T extends Model<T>> extends AbstractResultItera
* @protected
*/
protected async inflateRow(row: QueryRow): Promise<T> {
return Container.getContainer().make<T>(this.ModelClass)
.assumeFromSource(row)
const model = this.make<T>(this.ModelClass)
const fields = getFieldsMeta(model)
return model.assumeFromSource(
this.connection.normalizeRow(row, fields),
)
}
/**
@ -70,6 +74,11 @@ export class ModelResultIterable<T extends Model<T>> extends AbstractResultItera
* @protected
*/
protected async processEagerLoads(results: Collection<T>): Promise<void> {
if ( results.isEmpty() ) {
// Nothing to load relations for, so no reason to perform more queries
return
}
const eagers = this.builder.getEagerLoadedRelations()
const model = this.make<T>(this.ModelClass)
@ -78,9 +87,10 @@ export class ModelResultIterable<T extends Model<T>> extends AbstractResultItera
const relation = model.getRelation(name)
const select = relation.buildEagerQuery(this.builder, results)
const resultCount = await select.get().count()
const allRelated = await select.get().collect()
allRelated.each(result => {
const allRelated = resultCount ? await select.get().collect() : collect()
results.each(result => {
const resultRelation = result.getRelation(name as any)
const resultRelated = resultRelation.matchResults(allRelated as any)
resultRelation.setValue(resultRelated as any)

@ -0,0 +1,287 @@
import {Model} from './Model'
import {collect, Collection, ErrorWithContext, Maybe} from '../../util'
import {HasSubtree} from './relation/HasSubtree'
import {Related} from './relation/decorators'
import {HasTreeParent} from './relation/HasTreeParent'
import {ModelBuilder} from './ModelBuilder'
/**
* Model implementation with helpers for querying tree-structured data.
*
* This works by using a modified pre-order traversal to number the tree nodes
* with a left- and right-side numbers. For example:
*
* ```txt
* (1) A (14)
* |
* (2) B (9) (10) C (11) (12) D (14)
* |
* (3) E (6) (7) G (8)
* |
* (4) F (5)
* ```
*
* These numbers are stored, by default, in `left_num` and `right_num` columns.
* The `subtree()` method returns a `HasSubtree` relation which loads the subtree
* of a model and recursively nests the nodes.
*
* You can use the `children()` helper method to get a collection of the immediate
* children of this node, which also have the subtree set.
*
* To query the model without loading the entire subtree, use the `without()`
* method on the `ModelBuilder`. For example:
*
* ```ts
* MyModel.query<MyModel>().without('subtree')
* ```
*/
export abstract class TreeModel<T extends TreeModel<T>> extends Model<T> {
/** The table column where the left tree number is stored. */
public static readonly leftTreeField = 'left_num'
/** The table column where the right tree number is stored. */
public static readonly rightTreeField = 'right_num'
public static readonly parentIdField = 'parent_id'
/** @override to include the tree fields */
public static query<T2 extends Model<T2>>(): ModelBuilder<T2> {
return super.query<T2>()
.fields(this.rightTreeField, this.leftTreeField, this.parentIdField)
}
/**
* @override to eager-load the subtree by default
* @protected
*/
protected with: (keyof T)[] = ['subtree']
protected removedChildren: Collection<TreeModel<T>> = collect()
/** @override to include the tree fields */
public query(): ModelBuilder<T> {
const ctor = this.constructor as typeof TreeModel
return super.query()
.fields(ctor.leftTreeField, ctor.rightTreeField, ctor.parentIdField)
}
/** Get the left tree number for this model. */
public leftTreeNum(): Maybe<number> {
const ctor = this.constructor as typeof TreeModel
return this.getColumn(ctor.leftTreeField) as number
}
/** Get the right tree number for this model. */
public rightTreeNum(): Maybe<number> {
const ctor = this.constructor as typeof TreeModel
return this.getColumn(ctor.rightTreeField) as number
}
/** Get the ID of this node's parent, if one exists. */
public parentId(): Maybe<number> {
const ctor = this.constructor as typeof TreeModel
return this.getColumn(ctor.parentIdField) as number
}
/** Returns true if this node has no children. */
public isLeaf(): boolean {
const left = this.leftTreeNum()
const right = this.rightTreeNum()
return Boolean(left && right && (right - left === 1))
}
/** Returns true if the given `node` exists within the subtree of this node. */
public contains(node: TreeModel<T>): boolean {
const num = node.leftTreeNum()
const left = this.leftTreeNum()
const right = this.rightTreeNum()
return Boolean(num && left && right && (left < num && right > num))
}
/** The subtree nodes of this model, recursively nested. */
@Related()
public subtree(): HasSubtree<T> {
const ctor = this.constructor as typeof TreeModel
return this.make<HasSubtree<T>>(HasSubtree, this, ctor.leftTreeField)
}
/** The parent node of this model, if one exists. */
@Related()
public parentNode(): HasTreeParent<T> {
const ctor = this.constructor as typeof TreeModel
return this.make<HasTreeParent<T>>(HasTreeParent, this, ctor.leftTreeField, ctor.parentIdField)
}
/** Get the immediate children of this model. */
public children(): Collection<T> {
return this.subtree().getValue()
}
/** Get the parent of this model, if one exists. */
public parent(): Maybe<T> {
return this.parentNode().getValue()
}
public root(): Maybe<TreeModel<T>> {
// eslint-disable-next-line @typescript-eslint/no-this-alias
let parent: Maybe<TreeModel<T>> = this
while ( parent?.parent() ) {
parent = parent?.parent()
}
return parent
}
public rootOrFail(): TreeModel<T> {
const root = this.root()
if ( !root ) {
throw new ErrorWithContext('Unable to determine tree root', {
node: this,
})
}
return root
}
/** Get the nearest node in the parental line that is an ancestor of both this node and the `other` node. */
public commonAncestorWith(other: TreeModel<T>): Maybe<TreeModel<T>> {
if ( this.contains(other) ) {
// Handle the special case when this node is an ancestor of the other.
return this
}
if ( other.contains(this) ) {
// Handle the special case when this node is a descendant of the other.
return other
}
// Otherwise, walk up this node's ancestral line and try to find a shared ancestor.
// It's getting too anthropological up in here.
let parent = this.parent()
while ( parent ) {
if ( parent.contains(other) ) {
return parent
}
parent = parent.parent()
}
}
/**
* Update the preorder traversal numbering for this node and its subtree.
* After renumbering, you probably also want to save the subtree nodes to persist
* the tree into the database.
* @example
* ```ts
* await model.renumber().saveSubtree()
* ```
*/
public renumber(): this {
// Assume our leftTreeNum is -correct-.
// Given that, renumber the children recursively, then set our rightTreeNum
const myLeftNum = this.leftTreeNum()
if ( !myLeftNum ) {
return this
}
const ctor = this.constructor as typeof TreeModel
let nextChildLeftNum = myLeftNum + 1
let myRightNum = myLeftNum + 1
this.children()
.each(child => {
child.setColumn(ctor.leftTreeField, nextChildLeftNum)
child.setColumn(ctor.parentIdField, this.key())
child.parentNode().setValue(this as unknown as T)
child.renumber()
myRightNum = (child.rightTreeNum() ?? 0) + 1
nextChildLeftNum = (child.rightTreeNum() ?? 0) + 1
})
this.setColumn(ctor.rightTreeField, myRightNum)
return this
}
/** Get a flat collection of this node & its subtree nodes, in pre-order. */
public flatten(): Collection<TreeModel<T>> {
return this.flattenLevel()
}
/** Recursive helper for `flatten()`. */
private flattenLevel(topLevel = true): Collection<TreeModel<T>> {
const flat = collect<TreeModel<T>>(topLevel ? [this] : [])
this.children().each(child => {
flat.push(child)
flat.concat(child.flattenLevel(false))
})
return flat
}
/** Call the `save()` method on all nodes in this node & its subtree. */
public async saveSubtree(): Promise<void> {
await this.save()
await this.removedChildren.awaitMapCall('saveSubtree')
this.removedChildren = collect()
await this.children()
.map(child => child.saveSubtree())
.awaitAll()
}
/**
* Append the `other` node as a child of this node.
* Returns the common ancestor from which point the tree was renumbered.
* You should save the subtree after this.
* @example
* ```ts
* await nodeA.appendChild(nodeB).saveSubtree()
* ```
* @param other
* @param before - if provided, `other` will be inserted before the `before` child of this node. Otherwise, it will be added as the last child.
*/
public appendChild(other: T, before: Maybe<T> = undefined): TreeModel<T> {
// Determine the index where we should insert the node in our children
const idx = (before ? this.children().search(before) : undefined) ?? this.children().length
// Insert the child at that index
this.subtree().appendSubtree(idx, other)
// If the child has a parent:
const parent = other.parent()
if ( parent ) {
// Remove the child from the parent's children
parent.subtree().removeSubtree(other)
// Get the common ancestor of the child's parent and this node
const ancestor = this.commonAncestorWith(other)
if ( ancestor ) {
// Renumber from that ancestor
return ancestor.renumber()
}
}
// If the child has no parent or a common ancestor could not be found,
// renumber the entire tree since the total number of nodes has changed
return this.rootOrFail().renumber()
}
/**
* Remove this node from the tree structure.
* If the node existed in the tree structure and was removed, this method
* returns the tree node that was renumbered. You should save the subtree after this.
* @example
* ```ts
* await nodeA.removeFromTree().saveSubtree()
* ```
*/
public removeFromTree(): Maybe<TreeModel<T>> {
const parent = this.parent()
if ( parent ) {
const ctor = this.constructor as typeof TreeModel
parent.subtree().removeSubtree(this)
parent.removedChildren.push(this)
this.setColumn(ctor.leftTreeField, null)
this.setColumn(ctor.rightTreeField, null)
this.setColumn(ctor.parentIdField, null)
return this.rootOrFail().renumber()
}
}
}

@ -33,14 +33,24 @@ export abstract class HasOneOrMany<T extends Model<T>, T2 extends Model<T2>, V e
return this.localKeyOverride || this.foreignKey
}
public get foreignColumn(): string {
const ctor = this.related.constructor as typeof Model
return ctor.propertyToColumn(this.foreignKey)
}
public get localColumn(): string {
const ctor = this.related.constructor as typeof Model
return ctor.propertyToColumn(this.localKey)
}
/** Get the fully-qualified name of the foreign key. */
public get qualifiedForeignKey(): string {
return this.related.qualify(this.foreignKey)
return this.related.qualify(this.foreignColumn)
}
/** Get the fully-qualified name of the local key. */
public get qualifiedLocalKey(): string {
return this.related.qualify(this.localKey)
return this.related.qualify(this.localColumn)
}
/** Get the value of the pivot for this relation from the parent model. */
@ -59,7 +69,7 @@ export abstract class HasOneOrMany<T extends Model<T>, T2 extends Model<T2>, V e
public applyScope(where: AbstractBuilder<T2>): void {
where.where(subq => {
subq.where(this.qualifiedForeignKey, '=', this.parentValue)
.whereRaw(this.qualifiedForeignKey, 'IS NOT', 'NULL')
.whereNotNull(this.qualifiedForeignKey)
})
}
@ -70,11 +80,11 @@ export abstract class HasOneOrMany<T extends Model<T>, T2 extends Model<T2>, V e
.all()
return this.related.query()
.whereIn(this.foreignKey, keys)
.whereIn(this.foreignColumn, keys)
}
/** Given a collection of results, filter out those that are relevant to this relation. */
public matchResults(possiblyRelated: Collection<T>): Collection<T> {
return possiblyRelated.where(this.foreignKey as keyof T, '=', this.parentValue)
return possiblyRelated.where(this.foreignColumn as keyof T, '=', this.parentValue)
}
}

@ -0,0 +1,191 @@
import {TreeModel} from '../TreeModel'
import {Relation, RelationNotLoadedError} from './Relation'
import {collect, Collection, Maybe} from '../../../util'
import {RelationBuilder} from './RelationBuilder'
import {raw} from '../../dialect/SQLDialect'
import {AbstractBuilder} from '../../builder/AbstractBuilder'
import {ModelBuilder} from '../ModelBuilder'
import {Inject, Injectable} from '../../../di'
import {Logging} from '../../../service/Logging'
/**
* A relation that recursively loads the subtree of a model using
* modified preorder traversal.
*/
@Injectable()
export class HasSubtree<T extends TreeModel<T>> extends Relation<T, T, Collection<T>> {
@Inject()
protected readonly logging!: Logging
/**
* When the relation is loaded, the immediate children of the node.
* @protected
*/
protected instances: Maybe<Collection<T>>
constructor(
protected readonly model: T,
protected readonly leftTreeField: string,
) {
super(model, model)
}
public flatten(): Collection<T> {
const children = this.getValue()
const subtrees = children.reduce((subtree, child) => subtree.concat(child.subtree().flatten()), collect())
return children.concat(subtrees)
}
/** Manually load the subtree. */
public async load(): Promise<void> {
this.setValue(await this.get())
}
protected get parentValue(): any {
return this.model.key()
}
public query(): RelationBuilder<T> {
return this.builder()
.tap(b => this.model.applyScopes(b))
.select(raw('*'))
.orderByAscending(this.leftTreeField)
}
public applyScope(where: AbstractBuilder<T>): void {
const left = this.model.leftTreeNum()
const right = this.model.rightTreeNum()
if ( !left || !right ) {
where.whereMatchNone()
return
}
where.where(this.leftTreeField, '>', left)
.where(this.leftTreeField, '<', right)
}
public buildEagerQuery(parentQuery: ModelBuilder<T>, result: Collection<T>): ModelBuilder<T> {
const query = this.model.query().without('subtree')
this.logging.debug(`Building eager query for parent: ${parentQuery}`)
this.logging.debug(result)
if ( result.isEmpty() ) {
return query.whereMatchNone()
}
result.each(inst => {
const left = inst.leftTreeNum()
const right = inst.rightTreeNum()
if ( !left || !right ) {
return
}
query.orWhere(where => {
where.where(this.leftTreeField, '>', left)
.where(this.leftTreeField, '<', right)
})
})
this.logging.debug(`Built eager query: ${query}`)
return query
}
public matchResults(possiblyRelated: Collection<T>): Collection<T> {
this.logging.debug('Matching possibly related: ' + possiblyRelated.length)
this.logging.verbose(possiblyRelated)
const modelLeft = this.model.leftTreeNum()
const modelRight = this.model.rightTreeNum()
if ( !modelLeft || !modelRight ) {
return collect()
}
return possiblyRelated.filter(inst => {
const instLeft = inst.leftTreeNum()
return Boolean(instLeft && instLeft > modelLeft && instLeft < modelRight)
})
}
public appendSubtree(idx: number, subtree: T): void {
if ( !this.instances ) {
throw new RelationNotLoadedError()
}
this.instances = this.instances.put(idx, subtree)
}
public removeSubtree(subtree: TreeModel<T>): void {
if ( !this.instances ) {
throw new RelationNotLoadedError()
}
this.instances = this.instances.filter(x => x.isNot(subtree))
}
public setValue(related: Collection<T>): void {
// `related` contains a flat collection of the subtree nodes, ordered by left key ascending
// We will loop through the related nodes and recursively call `setValue` for our immediate
// children to build the tree.
type ReduceState = {
currentChild: T,
currentSubtree: Collection<T>,
}
const children = this.instances = collect()
const firstChild = related.pop()
if ( !firstChild ) {
return
}
const finalState = related.reduce<ReduceState>((state: ReduceState, node: T) => {
if ( state.currentChild.contains(node) ) {
// `node` belongs in the subtree of `currentChild`, not this node
state.currentSubtree.push(node)
return state
}
// We've hit the end of the subtree for `currentChild`, so set the child's
// subtree relation value and move on to the next child.
state.currentChild.subtree().setValue(state.currentSubtree)
children.push(state.currentChild)
return {
currentChild: node,
currentSubtree: collect(),
}
}, {
currentChild: firstChild,
currentSubtree: collect(),
})
// Do this one last time, since the reducer isn't called for the last node in the collection
if ( finalState ) {
finalState.currentChild.subtree().setValue(finalState.currentSubtree)
children.push(finalState.currentChild)
}
// Set the parent relation on the immediate children we identified
children.each(child => child.parentNode().setValue(this.model))
this.instances = children.sortBy(inst => inst.getOriginalValues()?.[this.leftTreeField])
}
public getValue(): Collection<T> {
if ( !this.instances ) {
throw new RelationNotLoadedError()
}
return this.instances
}
public isLoaded(): boolean {
return Boolean(this.instances)
}
public get(): Promise<Collection<T>> {
return this.fetch().collect()
}
}

@ -0,0 +1,75 @@
import {Relation, RelationNotLoadedError} from './Relation'
import {TreeModel} from '../TreeModel'
import {RelationBuilder} from './RelationBuilder'
import {raw} from '../../dialect/SQLDialect'
import {AbstractBuilder} from '../../builder/AbstractBuilder'
import {ModelBuilder} from '../ModelBuilder'
import {Collection, Maybe} from '../../../util'
export class HasTreeParent<T extends TreeModel<T>> extends Relation<T, T, Maybe<T>> {
protected parentInstance?: T
protected loaded = false
protected constructor(
protected model: T,
protected readonly leftTreeField: string,
protected readonly parentIdField: string,
) {
super(model, model)
}
protected get parentValue(): any {
return this.model.key()
}
public query(): RelationBuilder<T> {
return this.builder()
.tap(b => this.model.applyScopes(b))
.select(raw('*'))
.orderByAscending(this.leftTreeField)
}
public applyScope(where: AbstractBuilder<T>): void {
const parentId = this.model.parentId()
if ( !parentId ) {
where.whereMatchNone()
return
}
where.where(this.parentIdField, '=', parentId)
}
public buildEagerQuery(parentQuery: ModelBuilder<T>, result: Collection<T>): ModelBuilder<T> {
const parentIds = result.map(model => model.parentId()).whereDefined()
return this.model.query()
.without('subtree')
.whereIn(this.parentIdField, parentIds)
}
public matchResults(possiblyRelated: Collection<T>): Collection<T> {
return possiblyRelated.filter(related => related.key() === this.model.parentId())
}
public setValue(related: Maybe<T>): void {
this.loaded = true
this.parentInstance = related
}
public getValue(): Maybe<T> {
if ( !this.loaded && this.model.parentId() ) {
throw new RelationNotLoadedError()
}
return this.parentInstance
}
public isLoaded(): boolean {
return this.loaded || !this.model.parentId()
}
public async get(): Promise<Maybe<T>> {
return this.fetch().first()
}
}

@ -59,7 +59,9 @@ export abstract class Relation<T extends Model<T>, T2 extends Model<T2>, V exten
/** Get a collection of the results of this relation. */
public fetch(): ResultCollection<T2> {
return this.query().get()
return this.query()
.where(where => this.applyScope(where))
.get()
}
/** Resolve the result of this relation. */
@ -106,6 +108,9 @@ export abstract class Relation<T extends Model<T>, T2 extends Model<T2>, V exten
/** Get a new builder instance for this relation. */
public builder(): RelationBuilder<T2> {
return this.make(RelationBuilder, this)
const relatedCtor = this.related.constructor as typeof Model
return this.make<RelationBuilder<T2>>(RelationBuilder, this)
.connection(relatedCtor.getConnection())
.from(relatedCtor.tableName())
}
}

@ -0,0 +1,94 @@
import {Schema} from './Schema'
import {SQLiteConnection} from '../connection/SQLiteConnection'
import {Awaitable} from '../../util'
import {TableBuilder} from './TableBuilder'
import {Builder} from '../builder/Builder'
import {raw} from '../dialect/SQLDialect'
export class SQLiteSchema extends Schema {
constructor(
connection: SQLiteConnection,
) {
super(connection)
}
hasColumn(table: string, name: string): Awaitable<boolean> {
return (new Builder()).connection(this.connection)
.select(raw('*'))
.from(`pragma_table_info(${this.connection.dialect().escape(table)})`) // FIXME: probably needs to be raw(...)
.where('name', '=', name)
.exists()
}
hasColumns(table: string, name: string[]): Awaitable<boolean> {
return (new Builder()).connection(this.connection)
.select(raw('*'))
.from(`pragma_table_info(${this.connection.dialect().escape(table)})`) // FIXME: probably needs to be raw(...)
.whereIn('name', name)
.get()
.count()
.then(num => num === name.length)
}
hasTable(name: string): Awaitable<boolean> {
return (new Builder()).connection(this.connection)
.select(raw('*'))
.from('sqlite_master')
.where('type', '=', 'table')
.where('name', '=', name)
.exists()
}
table(table: string): Awaitable<TableBuilder> {
return this.populateTable(new TableBuilder(table))
}
protected async populateTable(table: TableBuilder): Promise<TableBuilder> {
if ( !(await this.hasTable(table.name)) ) {
return table
}
// Load the existing columns
await (new Builder()).connection(this.connection)
.select(raw('*'))
.from(raw(`pragma_table_info(${this.connection.dialect().escape(table.name)})`))
.get()
.each(col => {
table.column(col.name)
.type(col.type)
.pipe(line => {
return line.unless(col.notnull, builder => builder.nullable())
.when(col.dflt_value, builder => builder.default(raw(col.dflt_value)))
.when(col.pk, builder => builder.primary())
})
.flagAsExistingInSchema()
})
// TODO: Load the existing constraints
// TODO: Look up and apply the check constraints
// Look up table indexes
await (new Builder()).connection(this.connection)
.select(raw('*'))
.from(raw(`pragma_index_list(${this.connection.dialect().escape(table.name)})`))
.get()
.each(async idx => {
const indexBuilder = table.index(idx.name)
.pipe(line => line.when(idx.unique, builder => builder.unique()))
const idxColumns = await (new Builder()).connection(this.connection)
.select(raw('*'))
.from(raw(`pragma_index_xinfo(${this.connection.dialect().escape(idx.name)})`))
.whereNotNull('name')
.get()
.pluck('name')
idxColumns.whereDefined()
.each(col => indexBuilder.field(col))
indexBuilder.flagAsExistingInSchema()
})
return table.flagAsExistingInSchema()
}
}

@ -223,7 +223,16 @@ export class ColumnBuilder extends SchemaBuilderBase {
* @param type
*/
public type(type: FieldType): this {
if ( this.targetType === type ) {
if (
this.targetType === type
|| (
this.existsInSchema
&& (
(this.targetType === FieldType.integer && type === FieldType.serial)
|| (this.targetType === FieldType.bigint && type === FieldType.bigserial)
)
)
) {
return this
}

@ -6,6 +6,7 @@ import {Unit} from '../../lifecycle/Unit'
import {Config} from '../../service/Config'
import {Logging} from '../../service/Logging'
import {MigratorFactory} from '../migrations/MigratorFactory'
import {SQLiteConnection} from '../connection/SQLiteConnection'
/**
* Application unit responsible for loading and creating database connections from config.
@ -48,6 +49,8 @@ export class Database extends Unit {
let conn
if ( config?.dialect === 'postgres' ) {
conn = <PostgresConnection> this.app().make(PostgresConnection, key, config)
} else if ( config?.dialect === 'sqlite' ) {
conn = <SQLiteConnection> this.app().make(SQLiteConnection, key, config)
} else {
const e = new ErrorWithContext(`Invalid or missing database dialect: ${config.dialect}. Should be one of: postgres`)
e.context = { connectionName: key }

@ -18,7 +18,7 @@ export class Migrations extends CanonicalInstantiable<Migration> {
protected canonicalItem = 'migration'
protected suffix = '.migration.js'
protected suffix = '.migration'
async up(): Promise<void> {
if ( await this.path.exists() ) {
@ -67,7 +67,7 @@ export class Migrations extends CanonicalInstantiable<Migration> {
const namespace: {[key: string]: Migration} = {}
for await ( const entry of basePath.walk() ) {
if ( !entry.endsWith(this.suffix) ) {
if ( !this.isValidSuffix(entry) ) {
this.logging.debug(`buildMigrationNamespaceResolver - Skipping file with invalid suffix: ${entry}`)
continue
}

@ -17,7 +17,7 @@ export class Models extends CanonicalStatic<Model<any>, Instantiable<Model<any>>
protected canonicalItem = 'model'
protected suffix = '.model.js'
protected suffix = '.model'
public async up(): Promise<void> {
await super.up()

@ -8,18 +8,18 @@ import {ModelBuilder} from '../model/ModelBuilder'
* A model instance which stores records from the ORMCache driver.
*/
export class CacheModel extends Model<CacheModel> {
protected static table = 'caches'; // FIXME allow configuring
protected static table = 'caches' // FIXME allow configuring
protected static key = 'cache_key';
protected static key = 'cache_key'
@Field(FieldType.varchar, 'cache_key')
public cacheKey!: string;
public cacheKey!: string
@Field(FieldType.text, 'cache_value')
public cacheValue!: string;
public cacheValue!: string
@Field(FieldType.timestamp, 'cache_expires')
public cacheExpires?: Date;
public cacheExpires?: Date
public static withCacheKey(key: string): ModelBuilder<CacheModel> {
return this.query<CacheModel>()

@ -35,7 +35,7 @@ export class ORMSession extends Session {
this.data = this.session.json
} else {
this.session = <SessionModel> Container.getContainer().make(SessionModel)
this.session.uuid = this.key
this.session.sessionUuid = this.key
this.data = {} as SessionData
}
}
@ -48,7 +48,7 @@ export class ORMSession extends Session {
throw new SessionNotLoadedError()
}
this.session.uuid = this.key
this.session.sessionUuid = this.key
this.session.json = JSON.stringify(this.data)
await this.session.save()
}

@ -13,8 +13,8 @@ export class SessionModel extends Model<SessionModel> {
protected static populateKeyOnInsert = true
@Field(FieldType.varchar, 'session_uuid')
public uuid!: string;
public sessionUuid!: string
@Field(FieldType.json, 'session_data')
public json!: any;
public json!: any
}

@ -1,5 +1,5 @@
import { Collection } from '../util'
import {EscapeValue, QuerySafeValue} from './dialect/SQLDialect'
import {QuerySafeValue, VectorEscapeValue} from './dialect/SQLDialect'
/**
* A single query row, as an object.
@ -40,7 +40,7 @@ export type ConstraintOperator = '&' | '>' | '>=' | '<' | '<=' | '!=' | '<=>' |
export interface ConstraintItem {
field: string,
operator: ConstraintOperator,
operand: EscapeValue,
operand: VectorEscapeValue<any>,
preop: ConstraintConnectionOperator,
}
@ -77,7 +77,7 @@ export function isConstraintItem(what: unknown): what is ConstraintItem {
/**
* Type alias for something that can be either a single constraint or a group of them.
*/
export type Constraint = ConstraintItem | ConstraintGroup
export type Constraint = ConstraintItem | ConstraintGroup | QuerySafeValue
/**
* Type alias for an item that refers to a field on a table.
@ -87,7 +87,7 @@ export type SpecifiedField = string | QuerySafeValue | { field: string | QuerySa
/**
* Type alias for an item that refers to a table in a database.
*/
export type QuerySource = string | { table: string, alias: string }
export type QuerySource = string | QuerySafeValue | { table: string | QuerySafeValue, alias: string }
/**
* Possible SQL order-by clause directions.

@ -1,5 +1,5 @@
import {Canonical} from './Canonical'
import {Singleton} from '../di'
import {Singleton} from '../di/decorator/injection'
import {Maybe} from '../util'
/**

@ -70,9 +70,13 @@ export abstract class Canonical<T> extends Unit {
/**
* The file suffix of files in the base path that should be loaded.
* This should EXCLUDE `.js` or `.ts`. These file extensions are determined
* by the framework depending on the Node runtime.
* @example `.service`
* @example `.middleware`
* @type string
*/
protected suffix = '.js'
protected suffix = ''
/**
* The singular, programmatic name of one of these canonical items.
@ -113,6 +117,15 @@ export abstract class Canonical<T> extends Unit {
}
}
/**
* Returns true if the given file path has the correct suffix to be loaded by this unit.
*/
public isValidSuffix(path: string): boolean {
const jsSuffix = `${this.suffix}.js`
const tsSuffix = `${this.suffix}.ts`
return path.endsWith(jsSuffix) || path.endsWith(tsSuffix)
}
/**
* Return an array of all loaded canonical names.
*/
@ -247,7 +260,7 @@ export abstract class Canonical<T> extends Unit {
public async up(): Promise<void> {
if ( await this.path.exists() ) {
for await ( const entry of this.path.walk() ) {
if ( !entry.endsWith(this.suffix) ) {
if ( !this.isValidSuffix(entry) ) {
this.logging.debug(`Skipping file with invalid suffix: ${entry}`)
continue
}
@ -292,7 +305,7 @@ export abstract class Canonical<T> extends Unit {
.split('')
.reverse()
.join('')
.substr(this.suffix.length)
.substr(this.suffix.length + 3) // +3 to account for `.js` or `.ts` ending
.split('')
.reverse()
.join('')

@ -1,18 +1,14 @@
import {Singleton, Inject} from '../di'
import {Singleton} from '../di'
import {CanonicalRecursive} from './CanonicalRecursive'
import {Logging} from './Logging'
/**
* Canonical unit that loads configuration files from `app/configs`.
*/
@Singleton()
export class Config extends CanonicalRecursive {
@Inject()
protected readonly logging!: Logging
protected appPath: string[] = ['configs']
protected suffix = '.config.js'
protected suffix = '.config'
protected canonicalItem = 'config'

@ -12,7 +12,7 @@ export class Controllers extends CanonicalStatic<Instantiable<Controller>, Contr
protected canonicalItem = 'controller'
protected suffix = '.controller.js'
protected suffix = '.controller'
public async initCanonicalItem(definition: CanonicalDefinition): Promise<StaticClass<Instantiable<Controller>, Controller>> {
const item = await super.initCanonicalItem(definition)

@ -0,0 +1,24 @@
import {Unit} from '../lifecycle/Unit'
import {Inject} from '../di'
import {Logging} from './Logging'
import * as process from 'process'
export class Foreground extends Unit {
@Inject()
protected readonly logging!: Logging
protected resolver?: () => unknown
public up(): Promise<void> {
return new Promise<void>(res => {
this.resolver = res
this.logging.success(`Application started! Press ^C or send SIGINT to stop.`)
process.stdin.resume()
process.on('SIGINT', res)
})
}
public down(): void {
this.resolver?.()
}
}

@ -1,5 +1,5 @@
import {Inject, Singleton} from '../di'
import {ErrorWithContext, HTTPStatus, withTimeout} from '../util'
import {ErrorWithContext} from '../util'
import {Unit} from '../lifecycle/Unit'
import {createServer, IncomingMessage, RequestListener, Server, ServerResponse} from 'http'
import {Logging} from './Logging'
@ -67,10 +67,9 @@ export class HTTPServer extends Unit {
this.server = createServer(this.handler)
this.server.listen(port, () => {
this.logging.success(`Server listening on port ${port}. Press ^C to stop.`)
this.logging.success(`Server listening on port ${port}.`)
res()
})
process.on('SIGINT', res)
})
}
@ -85,37 +84,19 @@ export class HTTPServer extends Unit {
}
}
public get handler(): RequestListener {
// const timeout = this.config.get('server.timeout', 10000)
// const timeout = 0 // temporarily disable this because it is causing problems
public getServer(): Server {
if ( !this.server ) {
throw new ErrorWithContext('Unable to access server: it has not yet been created')
}
return this.server
}
public get handler(): RequestListener {
return async (request: IncomingMessage, response: ServerResponse) => {
const extolloReq = new Request(request, response)
await this.requestLocalStorage.run(extolloReq, async () => {
/* withTimeout(timeout, extolloReq.response.sent$.toPromise())
.onTime(() => {
this.logging.verbose(`Request lifecycle finished on time. (Path: ${extolloReq.path})`)
})
.late(() => {
if ( !extolloReq.bypassTimeout ) {
this.logging.warn(`Request lifecycle finished late, so an error response was returned! (Path: ${extolloReq.path})`)
}
})
.timeout(() => {
if ( extolloReq.bypassTimeout ) {
this.logging.info(`Request lifecycle has timed out, but bypassRequest was set. (Path: ${extolloReq.path})`)
return
}
this.logging.error(`Request lifecycle has timed out. Will send error response instead. (Path: ${extolloReq.path})`)
extolloReq.response.setStatus(HTTPStatus.REQUEST_TIMEOUT)
extolloReq.response.body = 'Sorry, your request timed out.'
extolloReq.response.send()
})
.run()
.catch(e => this.logging.error(e))*/
this.logging.info(`${extolloReq.method} ${extolloReq.path}`)
try {

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save