Skip to content

Commit

Permalink
Database: Added async error handling to all supported databases
Browse files Browse the repository at this point in the history
  • Loading branch information
FunctionPoint committed Jan 11, 2025
1 parent 5060598 commit d95e71a
Show file tree
Hide file tree
Showing 45 changed files with 858 additions and 299 deletions.
14 changes: 7 additions & 7 deletions Compiler/package-lock.json

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

2 changes: 1 addition & 1 deletion Compiler/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"dependencies": {
"@types/node": "^22.10.3",
"@types/node": "^22.10.5",
"source-map": "^0.7.4"
}
}
6 changes: 4 additions & 2 deletions Database/Database.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ For different databases please PostgresDatabase with MariadbDatabase or MysqlDat

Then connect to a table with code ST like:

productTable := database table: 'public."Product"' rowClass: Product.
productTable := database table: 'public."Product"' rowClass: MyProduct.

Note: Tables should exist already, the ORM does not support creating them.
A "database first" approach is taken, to keep the SQL clean and reusable elsewhere.
Expand All @@ -95,7 +95,7 @@ The following code examples implement CRUD operations:

Insert new product, setting its id:

product := Product new name: 'Test'.
product := MyProduct new name: 'Test'.
productTable insert: product then: [ :product | ... ].

### Read (select query)
Expand All @@ -108,6 +108,8 @@ Read single product by Id:

productTable queryId: 1 then: [ :product | ... ].

If the id does not exist, nil passed to the then block.

### Update

Update a product using its id:
Expand Down
16 changes: 8 additions & 8 deletions Examples/Electron/package-lock.json

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

4 changes: 2 additions & 2 deletions Examples/Electron/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"type": "module",
"dependencies": {
"@types/node": "^22.10.3",
"electron": "^33.2.1",
"@types/node": "^22.10.5",
"electron": "^33.3.1",
"node-fetch": "^3.3.2"
}
}
8 changes: 4 additions & 4 deletions Examples/NodeGui/package-lock.json

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

2 changes: 1 addition & 1 deletion Examples/NodeGui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"nodegui"
],
"devDependencies": {
"@types/node": "^22.10.3"
"@types/node": "^22.10.5"
},
"dependencies": {
"@nodegui/nodegui": "^0.70.0"
Expand Down
4 changes: 2 additions & 2 deletions Examples/Shop/Server/.env.example
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copy this file to: .env

# Uncomment the database you want to use and set correct credentials:
SHOP_DATABASE="postgres://postgres:password@localhost:5432/smalljs"
SHOP_DATABASE="../../../Database/SQLite/smalljs.db"
# SHOP_DATABASE="postgres://postgres:password@localhost:5432/smalljs"
# SHOP_DATABASE="mariadb://root:password@localhost:3307/smalljs?connectTimeout=0"
# SHOP_DATABASE="mysql://root:password@localhost:3306/smalljs?connectTimeout=0"
# SHOP_DATABASE="../../../Database/SQLite/smalljs.db"

# Select the client app your want to use:
SHOP_CLIENT="../Client/web"
Expand Down
14 changes: 7 additions & 7 deletions Examples/Shop/Server/package-lock.json

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

2 changes: 1 addition & 1 deletion Examples/Shop/Server/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"dependencies": {
"@types/node": "^22.10.3",
"@types/node": "^22.10.5",
"express": "^4.21.2",
"express-session": "^1.18.1",
"http-terminator": "^3.2.0",
Expand Down
56 changes: 33 additions & 23 deletions Examples/Shop/Server/src/ShopServer.st
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ connectDatabase

database := SqlDatabase newFor: connectionString.
self log: 'Connecting to database type: ', database class name.
database connect: connectionString then: [ self connectTables ].
database connect: connectionString
then: [ self connectTables ]
catch: [ :error | error throw ].
!
connectTables
personTable := database connectTable: 'Person' rowClass: Person.
Expand Down Expand Up @@ -87,17 +89,19 @@ login: request response: response
ifTrue: [ response send: ( result message: 'Login parameter "password" missing' ) ]
ifFalse: [
query := ( personTable quoted: 'name' ), ' = ', ( personTable parameter: 1 ).
personTable query: query with: #( userName ) then: [ :persons |
"Note: Block returns are not allowed in pg callbacks."
persons length < 1
ifTrue: [ response send: ( result message: 'User not found: ', userName ) ]
ifFalse: [
person := persons first.
( person checkPassword: password ) not
ifTrue: [ response send: ( result message: 'Password check failed' ) ]
ifFalse: [
request session set: 'user' to: person.
response send: ( result success: true; message: 'Login succeeded' ) ] ] ] ] ].
personTable query: query with: #( userName )
then: [ :persons |
"Note: Block returns are not allowed in pg callbacks."
persons length < 1
ifTrue: [ response send: ( result message: 'User not found: ', userName ) ]
ifFalse: [
person := persons first.
( person checkPassword: password ) not
ifTrue: [ response send: ( result message: 'Password check failed' ) ]
ifFalse: [
request session set: 'user' to: person.
response send: ( result success: true; message: 'Login succeeded' ) ] ] ]
catch: [ :error | error throw ] ] ].
!
getRequestUser: request response: response
"Returns the logged-in user (Person) from the request or nil if it does not exist.
Expand Down Expand Up @@ -127,8 +131,9 @@ returns:
products: request response: response
( self checkRequestUser: request response: response ) ifFalse: [ ^ nil ].

productTable queryAllThen:
[ :products | response send: products toJsObject ].
productTable
queryAllThen: [ :products | response send: products toJsObject ]
catch: [ :error | error throw ].
!

"Example orders request:
Expand All @@ -145,15 +150,20 @@ orders: request response: response

query := ( orderTable quoted: 'person' ), ' = ', ( orderTable parameter: 1 ).
userId := user id.
orderTable query: query with: #( userId ) then: [ :orders |
query := 'id in ( select ', ( orderTable quoted: 'product' ), ' from ', orderTable quotedName,
' where ', ( orderTable quoted: 'person' ), ' = ', ( orderTable parameter: 1 ), ' )'.
productTable query: query with: #( userId )
then: [ :products |
result := Object new
atProperty: 'orders' put: orders;
atProperty: 'products' put: products.
response send: result toJsObject ] ].
orderTable query: query
with: #( userId )
then: [ :orders |
query := 'id in ( select ', ( orderTable quoted: 'product' ), ' from ', orderTable quotedName,
' where ', ( orderTable quoted: 'person' ), ' = ', ( orderTable parameter: 1 ), ' )'.
productTable query: query
with: #( userId )
then: [ :products |
result := Object new
atProperty: 'orders' put: orders;
atProperty: 'products' put: products.
response send: result toJsObject ]
catch: [ :error | error throw ] ]
catch: [ :error | error throw ].
!
stop
server terminate.
Expand Down
2 changes: 1 addition & 1 deletion Node/.env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copy this file to .env, uncomment the database(s) you want to use and set correct credentials
# See ../Database/Database.md for instructions on installing databases

SMALLJS_SQLITE="../Database/SQLite/smalljs.db"
# SMALLJS_POSTGRES="postgres://postgres:password@localhost:5432/smalljs"
# SMALLJS_MARIADB="mariadb://root:password@localhost:3307/smalljs?connectTimeout=0"
# SMALLJS_MYSQL="mysql://root:password@localhost:3306/smalljs?connectTimeout=0"
# SMALLJS_SQLITE="../Database/SQLite/smalljs.db"
Loading

0 comments on commit d95e71a

Please sign in to comment.