Skip to content

Commit

Permalink
Merge pull request #55 from RV-Argonaut/54-bundle-id
Browse files Browse the repository at this point in the history
Introduce a "bundle id"
  • Loading branch information
divyenduz authored Jul 30, 2024
2 parents 27f5f2d + 55f258b commit fc87cf5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 39 deletions.
13 changes: 6 additions & 7 deletions src/impl/PLV8ifyCLI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('PLV8ifyCLI tests', () => {
name: 'test',
isExported: true,
parameters: [],
comments: [],
returnType: 'void',
jsdocTags: []
},
Expand All @@ -32,7 +31,6 @@ describe('PLV8ifyCLI tests', () => {
name: 'test',
isExported: true,
parameters: [],
comments: [],
returnType: 'void',
jsdocTags: [],
},
Expand All @@ -57,9 +55,10 @@ describe('PLV8ifyCLI tests', () => {
{ name: 'NEW', type: 'testRow' },
{ name: 'OLD', type: 'testRow' },
],
comments: ['//@plv8ify-trigger'],
returnType: 'object',
jsdocTags: []
jsdocTags: [
{ name: 'plv8ify_trigger', commentText: '' },
]
},
scopePrefix: 'plv8ify_',
mode: 'inline',
Expand Down Expand Up @@ -89,9 +88,10 @@ function test(NEW, OLD) {
name: 'test',
isExported: true,
parameters: [],
comments: ['//@plv8ify-schema-name testschema'],
returnType: 'string',
jsdocTags: []
jsdocTags: [
{ name: 'plv8ify_schema_name', commentText: 'testschema' },
]
},
scopePrefix: 'plv8ify_',
mode: 'inline',
Expand All @@ -116,7 +116,6 @@ function test() {
name: 'test',
isExported: true,
parameters: [{ name: 'test', type: 'test_type[]' }],
comments: [],
returnType: 'object',
jsdocTags: []
},
Expand Down
28 changes: 4 additions & 24 deletions src/impl/PLV8ifyCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type FnSqlConfig = {
export class PLV8ifyCLI implements PLV8ify {
private _bundler: Bundler
private _tsCompiler: TSCompiler
private bundleId = Date.now()

private _typeMap: Record<string, string> = {
number: 'float8',
Expand Down Expand Up @@ -166,7 +167,6 @@ export class PLV8ifyCLI implements PLV8ify {
// -- PLV8 + Server
const virtualInitFn: TSFunction = {
name: '_init',
comments: [],
isExported: false,
parameters: [],
returnType: 'void',
Expand All @@ -180,7 +180,7 @@ export class PLV8ifyCLI implements PLV8ify {
}

// set a global symbol so that we can check if the init function has been called
bundledJs += `globalThis[Symbol.for('${scopePrefix}_initialized')] = true;\n`
bundledJs += `globalThis[Symbol.for('${scopePrefix}_initialized')] = ${this.bundleId};\n`
}

const initFunction = this.getPLV8SQLFunction({
Expand Down Expand Up @@ -208,7 +208,6 @@ export class PLV8ifyCLI implements PLV8ify {
const startFunctionName = 'start'
const virtualStartFn: TSFunction = {
name: startFunctionName,
comments: [],
isExported: false,
parameters: [],
returnType: 'void',
Expand All @@ -230,7 +229,7 @@ export class PLV8ifyCLI implements PLV8ify {
}

/**
* handles all the processing for jsdoc / magic comments
* handles all the processing for jsdoc
*/
private getFnSqlConfig (fn: TSFunction): FnSqlConfig {
const config: FnSqlConfig = {
Expand All @@ -247,25 +246,6 @@ export class PLV8ifyCLI implements PLV8ify {
config.paramTypeMapping[param.name] = this.getTypeFromMap(param.type) || null
}

// process magic comments (legacy format)
for (const comment of fn.comments) {
const volatilityMatch = comment.match(/^\/\/@plv8ify-volatility-(STABLE|IMMUTABLE|VOLATILE)/umi)
if (volatilityMatch) config.volatility = volatilityMatch[1] as Volatility

const schemaMatch = comment.match(/^\/\/@plv8ify-schema-name (.+)/umi)
if (schemaMatch) config.customSchema = schemaMatch[1]

for (const param of fn.parameters) {
const paramMatch = comment.match(/^\/\/@plv8ify-param (.+) ([\s\S]+)/umi)
if (paramMatch && paramMatch[1] === param.name) config.paramTypeMapping[param.name] = paramMatch[2]
}

const returnMatch = comment.match(/^\/\/@plv8ify-return ([\s\S]+)/umi)
if (returnMatch) config.sqlReturnType = returnMatch[1]

if (comment.match(/^\/\/@plv8ify-trigger/umi)) config.trigger = true
}

// process jsdoc tags
for (const tag of fn.jsdocTags) {
if (tag.name === 'plv8ify_volatility' && [ 'STABLE', 'IMMUTABLE', 'VOLATILE' ].includes(tag.commentText.toUpperCase())) {
Expand Down Expand Up @@ -328,7 +308,7 @@ export class PLV8ifyCLI implements PLV8ify {
.with(
'bundle',
() =>
`if (!globalThis[Symbol.for('${scopePrefix}_initialized')]) plv8.execute('SELECT ${scopePrefix}_init();');`
`if (globalThis[Symbol.for('${scopePrefix}_initialized')] !== ${this.bundleId}) plv8.execute('SELECT ${scopePrefix}_init();');`
)
.otherwise(() => ''),
match(sqlReturnType.toLowerCase())
Expand Down
8 changes: 1 addition & 7 deletions src/impl/TsMorph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,18 @@ export class TsMorph implements TSCompiler {
})
}

private getFunctionComments(fn: FunctionDeclaration) {
const comments = fn.getLeadingCommentRanges().map((cr) => cr.getText())
return comments
}

private getFunctionJsdocTags(fn: FunctionDeclaration): TSFunction['jsdocTags'] {
const jsdocTags = fn.getJsDocs().flatMap((jsdoc) => jsdoc.getTags())
return jsdocTags.map(tag => ({ name: tag.getTagName(), commentText: tag.getCommentText() || '' }))
}

getFunctions() {
getFunctions(): TSFunction[] {
const fns = this.sourceFile.getFunctions()
return fns.map((fn) => {
return {
name: fn.getName(),
isExported: fn.isExported(),
parameters: this.getFunctionParameters(fn),
comments: this.getFunctionComments(fn),
returnType: this.getFunctionReturnType(fn),
jsdocTags: this.getFunctionJsdocTags(fn),
}
Expand Down
1 change: 0 additions & 1 deletion src/interfaces/TSCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export interface TSFunction {
name: string
isExported: boolean
parameters: TSFunctionParameter[]
comments: string[]
returnType: string
jsdocTags: { name: string, commentText: string }[]
}
Expand Down

0 comments on commit fc87cf5

Please sign in to comment.