Skip to content

Commit b2c29bd

Browse files
authored
Update README.md
1 parent 7c79647 commit b2c29bd

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ Now, you've got all that JavaScript goodness flowing, and it hits you -- What?
8989

9090
Enter **SupaScript require()**.
9191

92-
Load the extension or run the SQL code, and now you can use `require()`. Since you don't have access to a file system, though, you can't use npm install. So we need to have a way to load those neato node_modules. How do we do it?
92+
## Using require()
93+
If you've used NodeJS before, you're in love with require(). Since you don't have access to a file system, though, you can't use npm install. So we need to have a way to load those neato node_modules. How do we do it?
9394

94-
## Method 1: load from the web automatically
95+
### Method 1: load from the web automatically
9596
This is the easiest **(and preferred)** method.
9697

9798
```
@@ -112,13 +113,13 @@ Then just call this function from SQL:
112113
select test_momentjs();
113114
```
114115

115-
Where do I find the url? Hunt around on the library documentation page to find a CDN version of the library or look for documentation that shows how to load the library in HTML with a <SCRIPT> command.
116+
Where do I find the url for the function or library I want to use? Hunt around on the library documentation page to find a CDN version of the library or look for documentation that shows how to load the library in HTML with a <SCRIPT> command. Basically, the url you use **must point to plain javascript code that can be executed inside a block of JavaScript.**
116117

117-
## Method 2: manually load the library into your plv8_js_modules table
118-
This isn't the ideal method, but you can do this on your own if you want. Basically you load the source code for the module into the table. But you need to deal with escaping the single-quotes and all that fun stuff. Try Method 1 first, there's really no downside as long as you choose a compatible library and you can access it from the internet the first time you use it. See below for details on how all this works.
118+
### Method 2: manually load the library into your plv8_js_modules table
119+
This isn't the ideal method, but you can do this on your own if you want. Basically you load the source code for the module into the `plv8_js_modules` table. But you need to deal with escaping the single-quotes and all that fun stuff. Try Method 1 first, there's really no downside as long as you choose a compatible library and you can access it from the internet the first time you use it. See below for details on how all this works.
119120

120-
## How it works
121-
The first time you call require(url) the following stuff happens:
121+
### How require() works
122+
The first time you call `require(url)` the following stuff happens:
122123

123124
1. If your requested module is cached, we return it from the cache. Super fast! Woohoo! Otherwise...
124125
2. We check to see if the url (or module name if you loaded it manually) exists in the `plv8_js_modules` table. If it does, we load the source for the module from the database and then `eval()` it. Yes, we're using `eval()`, and that's how this is all possible. We know about the security vulnerabilities with `eval()` but in this case, it's a necessary evil. If you've got a better way, hit me up on GitHub.
@@ -129,7 +130,7 @@ So it goes:
129130
2. Are you in the database? Load you from the database and cache you for next time!
130131
3. First time being called, ever? We'll load you over http, write you to the database, and you're all set and loaded for next time!
131132

132-
If you call `require(url, true)` that "true" parameter means "autoload this module" so that it gets loaded into the cache when PLV8 starts up. Only do this with modules you need to have ready to go immediately. False essentially lazy-loads this module the first time it's called after startup.
133+
If you call `require(url, true)` that "true" parameter means **"autoload this module"** so that it gets loaded into the cache when PLV8 starts up. Only do this with modules you need to have ready to go immediately. False essentially lazy-loads this module the first time it's called after startup.
133134

134135
## Requirements:
135136
1. Supabase database (or any PostgreSQL database, probably, as long as it's a current-enough version).
@@ -148,7 +149,7 @@ This maps directly to plv8.execute() -- SEE: [plv8 documentation here](https://p
148149
Example usage:
149150
```js
150151
var json_result = sql('SELECT * FROM tbl');
151-
var num_affected = sql('DELETE FROM tbl WHERE price > $1', [ 1000 ]);
152+
var num_affected = sql('DELETE FROM tbl WHERE price > $1 and type = $2', [ 1000, 'active' ]);
152153
```
153154

154155
### exec(function_name, arguments)

0 commit comments

Comments
 (0)