You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+10-9Lines changed: 10 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -89,9 +89,10 @@ Now, you've got all that JavaScript goodness flowing, and it hits you -- What?
89
89
90
90
Enter **SupaScript require()**.
91
91
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?
93
94
94
-
## Method 1: load from the web automatically
95
+
###Method 1: load from the web automatically
95
96
This is the easiest **(and preferred)** method.
96
97
97
98
```
@@ -112,13 +113,13 @@ Then just call this function from SQL:
112
113
select test_momentjs();
113
114
```
114
115
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.**
116
117
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.
119
120
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:
122
123
123
124
1. If your requested module is cached, we return it from the cache. Super fast! Woohoo! Otherwise...
124
125
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:
129
130
2. Are you in the database? Load you from the database and cache you for next time!
130
131
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!
131
132
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.
133
134
134
135
## Requirements:
135
136
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
148
149
Example usage:
149
150
```js
150
151
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' ]);
0 commit comments