update oxmysql js usage

and somewhat clean up transaction docs
This commit is contained in:
Linden committed 2022-07-12 15:21:32 +10:00
1 parent 1363afcd2a
commit 3d627ba762
8 files changed
+177 -80

No files matched your search

+41 -5
View File
@@ -1,5 +1,46 @@
# Usage # Usage
## lib/MySQL
Resources can import oxmysql methods by including our library, granting some type-checking and minor performance improvements over raw export calls.
### Lua
Modify `fxmanifest.lua` for your resource, and add the following above any other script files.
```lua
server_script '@oxmysql/lib/MySQL.lua'
```
If you are using VSCode with [sumneko Lua](https://marketplace.visualstudio.com/items?itemName=sumneko.lua) you can get access to some basic types and intellisense.
```json
"Lua.workspace.library": [
"C:\\pathtoserver\\resources\\oxmysql\\lib\\define.lua",
]
```
### JavaScript
Use your favourite package manager to install our [node package](https://www.npmjs.com/package/@overextended/oxmysql).
```yaml
# With pnpm
pnpm add @overextended/oxmysql
# With Yarn
yarn add @overextended/oxmysql
# With npm
npm install @overextended/oxmysql
```
Import the oxmysql object into your resource.
```js
import { oxmysql as MySQL } from '@overextended/oxmysql';
```
## Placeholders ## Placeholders
Safely format queries by using placeholders in the query, and sending arguments in an array or object. Safely format queries by using placeholders in the query, and sending arguments in an array or object.
@@ -10,11 +51,6 @@ MySQL.query('SELECT * FROM users WHERE identifier = ?', { 'license:abcdefg' })
MySQL.query('SELECT * FROM users WHERE identifier = :identifier', { identifier = 'license:abcdefg' }) MySQL.query('SELECT * FROM users WHERE identifier = :identifier', { identifier = 'license:abcdefg' })
``` ```
```js
exports.oxmysql.query('SELECT * FROM users WHERE identifier = ?', { 'license:abcdefg' });
exports.oxmysql.query('SELECT * FROM users WHERE identifier = :identifier', { identifier: 'license:abcdefg' });
```
## Upsert ## Upsert
When uncertain if a row should be inserted into the database, or an existing row should be updated, queries should check for duplicate keys. When uncertain if a row should be inserted into the database, or an existing row should be updated, queries should check for duplicate keys.
+6 -2
View File
@@ -27,13 +27,17 @@ print(id)
### Callback ### Callback
```js ```js
exports.oxmysql.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?)', [playerIdentifier, firstName, lastName], (id) => { // alias: exports.oxmysql.insert
MySQL.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?)', [playerIdentifier, firstName, lastName], (id) => {
console.log(id) console.log(id)
}) })
``` ```
### Promise ### Promise
```js ```js
const id = await exports.oxmysql.insert_async('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?)', [playerIdentifier, firstName, lastName]) // alias: exports.oxmysql.insert_async
const id = await MySQL.insert('INSERT INTO users (identifier, firstname, lastname) VALUES (?, ?, ?)', [playerIdentifier, firstName, lastName])
console.log(id) console.log(id)
``` ```
+5 -2
View File
@@ -38,7 +38,9 @@ end
### Callback ### Callback
```js ```js
exports.oxmysql.prepare('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], (result) => { // alias: exports.oxmysql.prepare
MySQL.insert('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], (result) => {
if (result) { if (result) {
console.log(v.identifier, v.firstname, v.lastname) console.log(v.identifier, v.firstname, v.lastname)
} }
@@ -47,8 +49,9 @@ exports.oxmysql.prepare('SELECT * FROM users WHERE identifier = ?', [playerIdent
### Promise ### Promise
```js ```js
// alias: exports.oxmysql.prepare_async
const result = await exports.oxmysql.prepare_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) const result = await MySQL.insert('SELECT * FROM users WHERE identifier = ?', [playerIdentifier])
if (result) { if (result) {
console.log(v.identifier, v.firstname, v.lastname) console.log(v.identifier, v.firstname, v.lastname)
} }
+6 -2
View File
@@ -36,7 +36,9 @@ end
### Callback ### Callback
```js ```js
exports.oxmysql.query('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], (result) => { // alias: exports.oxmysql.query
MySQL.query('SELECT * FROM users WHERE identifier = ?', [playerIdentifier], (result) => {
if (result) { if (result) {
result.forEach((row) => { result.forEach((row) => {
console.log(row.identifier, row.firstname, row.lastname) console.log(row.identifier, row.firstname, row.lastname)
@@ -46,7 +48,9 @@ exports.oxmysql.query('SELECT * FROM users WHERE identifier = ?', [playerIdentif
``` ```
### Promise ### Promise
```js ```js
const result = await exports.oxmysql.query_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) // alias: exports.oxmysql.query_async
const result = await MySQL.query('SELECT * FROM users WHERE identifier = ?', [playerIdentifier])
if (result) { if (result) {
result.forEach((row) => { result.forEach((row) => {
console.log(row.identifier, row.firstname, row.lastname) console.log(row.identifier, row.firstname, row.lastname)
+6 -2
View File
@@ -26,13 +26,17 @@ print(firstname)
### Callback ### Callback
```js ```js
exports.oxmysql.scalar('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier], (firstname) => { // alias: exports.oxmysql.scalar
MySQL.scalar('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier], (firstname) => {
console.log(firstname) console.log(firstname)
}) })
``` ```
### Promise ### Promise
```js ```js
const firstname = await exports.oxmysql.scalar_async('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier]) // alias: exports.oxmysql.scalar_async
const firstname = await MySQL.scalar('SELECT firstname FROM users WHERE identifier = ?', [playerIdentifier])
console.log(firstname) console.log(firstname)
``` ```
+16 -12
View File
@@ -8,9 +8,9 @@ Returns the columns for a single row.
-- alias: exports.oxmysql:single -- alias: exports.oxmysql:single
-- alias: MySQL.Async.fetchSingle -- alias: MySQL.Async.fetchSingle
MySQL.single('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(result) MySQL.single('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}, function(row)
if result then if row then
print(result.identifier, result.firstname, result.lastname) print(row.identifier, row.firstname, row.lastname)
end end
end) end)
``` ```
@@ -20,9 +20,9 @@ end)
-- alias: exports.oxmysql:single_async -- alias: exports.oxmysql:single_async
-- alias: MySQL.Sync.fetchSingle -- alias: MySQL.Sync.fetchSingle
local result = MySQL.single.await('SELECT * FROM users WHERE identifier = ?', {playerIdentifier}) local row = MySQL.single.await('SELECT * FROM users WHERE identifier = ?', {playerIdentifier})
if result then if row then
print(result.identifier, result.firstname, result.lastname) print(row.identifier, row.firstname, row.lastname)
end end
``` ```
@@ -30,17 +30,21 @@ end
### Callback ### Callback
```js ```js
exports.oxmysql.single('SELECT * FROM users WHERE identifier = ?', [playerIdentifier],(result) => { // alias: exports.oxmysql.single
if (result) {
console.log(result.identifier, result.firstname, result.lastname) MySQL.single('SELECT * FROM users WHERE identifier = ?', [playerIdentifier],(row) => {
if (row) {
console.log(row.identifier, row.firstname, row.lastname)
} }
}) })
``` ```
### Promise ### Promise
```js ```js
const result = await exports.oxmysql.single_async('SELECT * FROM users WHERE identifier = ?', [playerIdentifier]) // alias: exports.oxmysql.single_async
if (result) {
console.log(result.identifier, result.firstname, result.lastname) const row = await MySQL.single('SELECT * FROM users WHERE identifier = ?', [playerIdentifier])
if (row) {
console.log(row.identifier, row.firstname, row.lastname)
} }
``` ```
+91 -53
View File
@@ -1,57 +1,77 @@
# Transaction # Transaction
## Transactions
A transaction executes multiple queries and commits them only if all succeed. If one fails, none of the queries are committed. The return value is a `boolean`, which is the result of the transaction. A transaction executes multiple queries and commits them only if all succeed. If one fails, none of the queries are committed. The return value is a `boolean`, which is the result of the transaction.
### Specific Format ## Specific Format
When using the `Specific` format you must pass one parameter to the oxmysql export. In this case, the `queries` parameter contains your queries and the SQL variables unique to each query. When using the `Specific` format you must pass one parameter to the oxmysql export. In this case, the `queries` parameter contains your queries and the SQL variables unique to each query.
This is useful for transactions where the queries do not share many SQL variables. This is useful for transactions where the queries do not share many SQL variables.
### Usage ### Lua
#### Lua
```lua ```lua
local queries = { local queries = {
{ query = 'INSERT INTO `test` (id) VALUES (:someid)', values = { ['someid'] = 1 } }, { query = 'INSERT INTO `test` (id) VALUES (:someid)', values = { ['someid'] = 1 } },
{ query = 'INSERT INTO `test` (id) VALUES (:someid)', values = { ['someid'] = 2 } } { query = 'INSERT INTO `test` (id) VALUES (:someid)', values = { ['someid'] = 2 } }
} -- NOTE, the 'values' tables can be named 'parameters' here for MySQL-Async compatibility. } -- NOTE, the 'values' tables can be named 'parameters' here for MySQL-Async compatibility.
```
-- Async #### Callback
```lua
-- alias: exports.oxmysql:transaction -- alias: exports.oxmysql:transaction
-- alias: MySQL.Async.transaction -- alias: MySQL.Async.transaction
MySQL.transaction(queries, function(result)
print(result)
end)
-- Sync MySQL.transaction(queries, function(success)
print(success)
end)
```
#### Promise
```lua
-- alias: exports.oxmysql:transaction_async -- alias: exports.oxmysql:transaction_async
-- alias: MySQL.Sync.transaction -- alias: MySQL.Sync.transaction
local result = MySQL.transaction.await(queries) local success = MySQL.transaction.await(queries)
print(result) print(success)
``` ```
#### JavaScript ### JavaScript
```js ```js
const queries = [ const queries = [
{ query: 'INSERT INTO `test` (id) VALUES (:someid)', values: { someid: 1 } }, { query: 'INSERT INTO `test` (id) VALUES (:someid)', values: { someid: 1 } },
{ query: 'INSERT INTO `test` (id) VALUES (:someid)', values: { someid: 2 } } { query: 'INSERT INTO `test` (id) VALUES (:someid)', values: { someid: 2 } },
] // NOTE, the 'values' objects can be named 'parameters' here for MySQL-Async compatibility. ]; // NOTE, the 'values' objects can be named 'parameters' here for MySQL-Async compatibility.
// Async
exports.oxmysql.transaction(queries, (result) => {
console.log(result)
})
// Sync
const result = await exports.oxmysql.transactionSync(queries)
console.log(result)
``` ```
### Shared Format #### Callback
```js
// alias: exports.oxmysql.transaction
MySQL.transaction(queries, (success) => {
console.log(success);
});
```
#### Promise
```js
// alias: exports.oxmysql.transaction_async
const success = await MySQL.transaction(queries);
console.log(success);
```
## Shared Format
When using the `Shared` format you must pass two parameters to the oxmysql export. The `queries` and the `parameters` those queries will use. When using the `Shared` format you must pass two parameters to the oxmysql export. The `queries` and the `parameters` those queries will use.
This is useful if your queries use the same SQL variables. This is useful if your queries use the same SQL variables.
#### Lua ### Lua
```lua ```lua
local queries = { local queries = {
'INSERT INTO `test` (id, name) VALUES (:someid, :somename)', 'INSERT INTO `test` (id, name) VALUES (:someid, :somename)',
@@ -59,47 +79,65 @@ local queries = {
} }
local parameters = { ['someid'] = 2, ['somename'] = 'John Doe', ['newname'] = 'John Notdoe' } local parameters = { ['someid'] = 2, ['somename'] = 'John Doe', ['newname'] = 'John Notdoe' }
-- Async
-- alias: exports.oxmysql:transaction
-- alias: MySQL.Async.transaction
MySQL.transaction(queries, parameters, function(result)
print(result)
end)
-- Sync
-- alias: exports.oxmysql:transaction_async
-- alias: MySQL.Sync.transaction
local result = MySQL.transaction.await(queries, parameters)
print(result)
``` ```
#### JavaScript #### Callback
```lua
-- alias: exports.oxmysql:transaction
-- alias: MySQL.Async.transaction
MySQL.transaction(queries, parameters, function(success)
print(success)
end)
```
#### Promise
```lua
-- alias: exports.oxmysql:transaction_async
-- alias: MySQL.Sync.transaction
local success = MySQL.transaction.await(queries, parameters)
print(success)
```
### JavaScript
```js ```js
const queries = [ const queries = [
'INSERT INTO `test` (id, name) VALUES (:someid, :somename)', 'INSERT INTO `test` (id, name) VALUES (:someid, :somename)',
'SET `name` = :newname IN `test` WHERE `id` = :someid' 'SET `name` = :newname IN `test` WHERE `id` = :someid',
] ];
const parameters = { someid: 2, somename: 'John Doe', newname: 'John Notdoe' } const parameters = { someid: 2, somename: 'John Doe', newname: 'John Notdoe' };
```
// Async #### Callback
exports.oxmysql.transaction(queries, parameters, (result) => {
console.log(result)
})
// Sync ```js
const result = await exports.oxmysql.transactionSync(queries, parameters) // alias: exports.oxmysql.transaction
console.log(result)
MySQL.transaction(queries, parameters, (success) => {
console.log(success);
});
```
#### Promise
```js
// exports.oxmysql.transaction_async
const success = await MySQL.transaction(queries, parameters);
console.log(success);
``` ```
## Transaction Isolation Level ## Transaction Isolation Level
This can be set through the convar `mysql_transaction_isolation_level` which should be an integer ranging from `1-4`. The default convar value set by oxmysql is `2`. This can be set through the convar `mysql_transaction_isolation_level` which should be an integer ranging from `1-4`. The default convar value set by oxmysql is `2`.
| Convar Value | Result | | Convar Value | Result |
| -------------- | ------------------ | | ------------ | ---------------- |
| 1 | Repeatable Read | | 1 | Repeatable Read |
| 2 | Read Committed | | 2 | Read Committed |
| 3 | Read Uncommitted | | 3 | Read Uncommitted |
+6 -2
View File
@@ -30,7 +30,9 @@ end
### Callback ### Callback
```js ```js
exports.oxmysql.update('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier], (affectedRows) => { // alias: exports.oxmysql.update
MySQL.update('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier], (affectedRows) => {
if (affectedRows) { if (affectedRows) {
console.log(affectedRows) console.log(affectedRows)
} }
@@ -39,7 +41,9 @@ exports.oxmysql.update('UPDATE users SET firstname = ? WHERE identifier = ? ', [
### Promise ### Promise
```js ```js
const id = exports.oxmysql.update_async('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier]) // alias: exports.oxmysql.update_async
const id = MySQL.update('UPDATE users SET firstname = ? WHERE identifier = ? ', [newName, playerIdentifier])
if (affectedRows) { if (affectedRows) {
console.log(affectedRows) console.log(affectedRows)
} }