Skip to content

Commit 6fd8681

Browse files
authored
feat: export query path (#98)
* Write module which exports the .sql queries path * Expose file system helpers too Co-authored-by: Misha Kaletsky <[email protected]>
1 parent 0ab422f commit 6fd8681

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

readme.md

+9
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ psql -c "
5454
psql -f node_modules/plv8-git/queries/create-git-functions.sql
5555
```
5656

57+
Or from javascript:
58+
59+
```js
60+
const sqlClient = getSqlClient()
61+
62+
const sql = require('plv8-git/queries').getGitFunctionsSql()
63+
await sqlClient.runRawSql(sql)
64+
```
65+
5766
Note: for `create extension plv8` to work the plv8.control file must exist on your database system. You can use [the postgres-plv8 docker image](https://github.com/clkao/docker-postgres-plv8/tree/master/11-2) for development (or production, if you really want to deploy a containerised database to production). Amazon RDS instances [have the extension available](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html), as does [Azure Postgres 11](https://docs.microsoft.com/en-us/azure/postgresql/concepts-extensions#postgres-11-extensions).
5867

5968
This will have created three postgres functions: `git_track`, `git_log` and `git_resolve`.

scripts/generate-queries.ts

+23-3
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,29 @@ export const getQuery = (js: string) => {
8383

8484
export const write = (filesystem = fs) => {
8585
const sql = getQuery(filesystem.readFileSync(require.resolve('..')).toString())
86-
const destPath = path.join(__dirname, '../queries/create-git-functions.sql')
87-
filesystem.mkdirSync(path.dirname(destPath), {recursive: true})
88-
filesystem.writeFileSync(destPath, sql, 'utf8')
86+
const queriesDir = path.join(__dirname, '../queries')
87+
88+
filesystem.mkdirSync(queriesDir, {recursive: true})
89+
filesystem.writeFileSync(path.join(queriesDir, 'create-git-functions.sql'), sql, 'utf8')
90+
filesystem.writeFileSync(
91+
path.join(queriesDir, 'index.js'),
92+
`const path = require('path')\n` + //
93+
`const fs = require('fs')\n\n` +
94+
`exports.gitFunctionsPath = path.join(__dirname, 'create-git-functions.sql')\n\n` +
95+
`exports.getGitFunctionsSql = () => fs.readFileSync(exports.createGitFunctionsPath, 'utf8')\n\n` +
96+
`exports.getGitFunctionsSqlAsync = () => fs.promises.readFile(exports.createGitFunctionsPath, 'utf8')\n`,
97+
'utf8',
98+
)
99+
filesystem.writeFileSync(
100+
path.join(queriesDir, 'index.d.ts'),
101+
`/** Path on filesystem to file containing git tracking SQL functions */\n` + //
102+
`export const gitFunctionsPath: string\n\n` +
103+
`/** Synchronously read the file system to return git tracking SQL functions as a string */\n` +
104+
`export const getGitFunctionsSql: () => string\n\n` +
105+
`/** Asynchronously read the file system to return git tracking SQL functions as a string */\n` +
106+
`export const getGitFunctionsSqlAsync: () => Promise<string>\n`,
107+
'utf8',
108+
)
89109
}
90110

91111
if (require.main === module) write()

0 commit comments

Comments
 (0)