typescript-sql-tagged-template-plugin

TypeScript language service plugin that adds type checking for sql tagged templates


Keywords
TypeScript, sql, templates
License
MIT
Install
npm install typescript-sql-tagged-template-plugin@0.0.12

Documentation

TypeScript SQL tagged template plugin

TypeScript server plugin that adds type checking for SQL queries tagged with an sql function.

Features

  • Syntax errors for SQL statements
  • Type checking for expressions in SQL statements

Usage

This plugin can provides SQL syntax errors and type checking in TypeScript files within any editor that uses TypeScript to power their language features. This includes VS Code and any other editor using supporting TypeScript language server plugins.

Other editors

First install the plugin in your project:

npm install --save-dev typescript-sql-tagged-template-plugin

Then add a plugins section to your tsconfig.json.

{
	"compilerOptions": {
		"plugins": [
			{
				"name": "typescript-sql-tagged-template-plugin"
			}
		]
	}
}

Then restart the TS language server.

Configuration

Otherwise you can configure the behavior of this plugin in the plugins section of in your tsconfig.

Database schema

In order to do type checking for parameters in SQL statements, the plugin needs to know about your database schema. You can generate a JSON file with your DB schema using the script scripts/schema/index.js. If you have a different DB type conversion, modify the file afterwards.

Then configure the path to the file using the schemaFile setting:

{
	"compilerOptions": {
		"plugins": [
			{
				"name": "typescript-sql-tagged-template-plugin",
				"schemaFile": "./path/to/database-schema.json"
			}
		]
	}
}

Default schema name

For queries not specifying any schema name (e.g. SELECT * FROM users instead of SELECT * FROM myschema.users), the plugin uses this as the default schema. The value defaults to public, which is the Postgres default.

{
	"compilerOptions": {
		"plugins": [
			{
				"name": "typescript-sql-tagged-template-plugin",
				"defaultSchemaName": "public"
			}
		]
	}
}