Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@balena/abstract-sql-to-typescript

Package Overview
Dependencies
Maintainers
0
Versions
235
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@balena/abstract-sql-to-typescript - npm Package Compare versions

Comparing version 5.0.4-build-renovate-major-22-node-eb139de0d737ac67172fc616e5cbf711ee00ce91-1 to 5.1.0-build-joshbwlng-replace-serials-57a92b59b8c98447a4fbb476bbaeb70d8475c77b-1

6

out/package.json
{
"name": "@balena/abstract-sql-to-typescript",
"version": "5.0.4",
"version": "5.1.0",
"description": "A translator for abstract sql into typescript types.",

@@ -25,3 +25,3 @@ "type": "commonjs",

"@balena/odata-to-abstract-sql": "^7.0.0",
"@types/node": "^22.0.0",
"@types/node": "^20.16.5",
"common-tags": "^1.8.2"

@@ -58,4 +58,4 @@ },

"versionist": {
"publishedAt": "2024-12-05T19:44:40.044Z"
"publishedAt": "2024-12-08T07:20:21.684Z"
}
}
import type { AbstractSqlModel } from '@balena/abstract-sql-compiler';
type RequiredModelSubset = Pick<AbstractSqlModel, 'tables' | 'relationships' | 'synonyms'>;
export declare const abstractSqlToTypescriptTypes: (m: RequiredModelSubset) => string;
export type Options = {
convertSerialToInteger?: boolean;
};
export declare const abstractSqlToTypescriptTypes: (m: RequiredModelSubset, opts?: Options) => string;
export {};

@@ -8,2 +8,3 @@ "use strict";

const trimNL = new common_tags_1.TemplateTag((0, common_tags_1.replaceResultTransformer)(/^[\r\n]*|[\r\n]*$/g, ''));
const replaceSerial = (s) => s.replace(/serial$/i, 'Integer');
const modelNameToCamelCaseName = (s) => s

@@ -14,3 +15,3 @@ .split(/[ -]/)

const getReferencedInterface = (modelName, mode) => `${modelNameToCamelCaseName(modelName)}['${mode}']`;
const sqlTypeToTypescriptType = (m, f, mode) => {
const sqlTypeToTypescriptType = (m, f, mode, opts) => {
if (!['ForeignKey', 'ConceptType'].includes(f.dataType) && f.checks) {

@@ -37,6 +38,6 @@ const inChecks = f.checks.find((checkTuple) => checkTuple[0] === 'In');

default:
return `Types['${f.dataType}']['${mode}']`;
return `Types['${opts?.convertSerialToInteger ? replaceSerial(f.dataType) : f.dataType}']['${mode}']`;
}
};
const fieldToInterfaceProps = (key, m, f, mode) => {
const fieldToInterfaceProps = (key, m, f, mode, opts) => {
if (mode === 'Write' && f.computed != null) {

@@ -46,6 +47,6 @@ return;

const nullable = f.required ? '' : ' | null';
return `${(0, odata_to_abstract_sql_1.sqlNameToODataName)(key)}: ${sqlTypeToTypescriptType(m, f, mode)}${nullable};`;
return `${(0, odata_to_abstract_sql_1.sqlNameToODataName)(key)}: ${sqlTypeToTypescriptType(m, f, mode, opts)}${nullable};`;
};
const fieldsToInterfaceProps = (m, fields, mode) => fields
.map((f) => fieldToInterfaceProps(f.fieldName, m, f, mode))
const fieldsToInterfaceProps = (m, fields, mode, opts) => fields
.map((f) => fieldToInterfaceProps(f.fieldName, m, f, mode, opts))
.filter((f) => f != null);

@@ -122,6 +123,6 @@ const getSynonyms = (s, inverseSynonyms) => {

};
const tableToInterface = (m, table) => {
const tableToInterface = (m, table, opts) => {
const writableFields = table.definition != null
? []
: fieldsToInterfaceProps(m, table.fields, 'Write');
: fieldsToInterfaceProps(m, table.fields, 'Write', opts);
const writeType = writableFields.length === 0

@@ -137,3 +138,3 @@ ?

${[
...fieldsToInterfaceProps(m, table.fields, 'Read'),
...fieldsToInterfaceProps(m, table.fields, 'Read', opts),
...relationshipsToInterfaceProps(m, table, 'Read'),

@@ -146,3 +147,3 @@ ].join('\n\t\t')}

};
const abstractSqlToTypescriptTypes = (m) => {
const abstractSqlToTypescriptTypes = (m, opts) => {
return trimNL `

@@ -156,3 +157,3 @@ // These types were generated by @balena/abstract-sql-to-typescript v${package_json_1.version}

const t = m.tables[tableName];
return tableToInterface(m, t);
return tableToInterface(m, t, opts);
})

@@ -159,0 +160,0 @@ .join('\n\n')}

{
"name": "@balena/abstract-sql-to-typescript",
"version": "5.0.4-build-renovate-major-22-node-eb139de0d737ac67172fc616e5cbf711ee00ce91-1",
"version": "5.1.0-build-joshbwlng-replace-serials-57a92b59b8c98447a4fbb476bbaeb70d8475c77b-1",
"description": "A translator for abstract sql into typescript types.",

@@ -25,3 +25,3 @@ "type": "commonjs",

"@balena/odata-to-abstract-sql": "^7.0.0",
"@types/node": "^22.0.0",
"@types/node": "^20.16.5",
"common-tags": "^1.8.2"

@@ -58,4 +58,4 @@ },

"versionist": {
"publishedAt": "2024-12-05T19:44:40.044Z"
"publishedAt": "2024-12-08T07:20:21.684Z"
}
}

@@ -20,2 +20,6 @@ import type {

export type Options = {
convertSerialToInteger?: boolean;
};
const trimNL = new TemplateTag(

@@ -25,2 +29,4 @@ replaceResultTransformer(/^[\r\n]*|[\r\n]*$/g, ''),

const replaceSerial = (s: string): string => s.replace(/serial$/i, 'Integer');
const modelNameToCamelCaseName = (s: string): string =>

@@ -39,2 +45,3 @@ s

mode: Mode,
opts?: Options,
): string => {

@@ -69,3 +76,3 @@ if (!['ForeignKey', 'ConceptType'].includes(f.dataType) && f.checks) {

default:
return `Types['${f.dataType}']['${mode}']`;
return `Types['${opts?.convertSerialToInteger ? replaceSerial(f.dataType) : f.dataType}']['${mode}']`;
}

@@ -79,2 +86,3 @@ };

mode: Mode,
opts?: Options,
): string | undefined => {

@@ -90,2 +98,3 @@ if (mode === 'Write' && f.computed != null) {

mode,
opts,
)}${nullable};`;

@@ -98,5 +107,6 @@ };

mode: Mode,
opts?: Options,
): string[] =>
fields
.map((f) => fieldToInterfaceProps(f.fieldName, m, f, mode))
.map((f) => fieldToInterfaceProps(f.fieldName, m, f, mode, opts))
.filter((f) => f != null);

@@ -218,7 +228,11 @@

const tableToInterface = (m: RequiredModelSubset, table: AbstractSqlTable) => {
const tableToInterface = (
m: RequiredModelSubset,
table: AbstractSqlTable,
opts?: Options,
) => {
const writableFields =
table.definition != null
? []
: fieldsToInterfaceProps(m, table.fields, 'Write');
: fieldsToInterfaceProps(m, table.fields, 'Write', opts);
const writeType =

@@ -235,3 +249,3 @@ writableFields.length === 0

${[
...fieldsToInterfaceProps(m, table.fields, 'Read'),
...fieldsToInterfaceProps(m, table.fields, 'Read', opts),
...relationshipsToInterfaceProps(m, table, 'Read'),

@@ -249,2 +263,3 @@ ].join('\n\t\t')}

m: RequiredModelSubset,
opts?: Options,
): string => {

@@ -259,3 +274,3 @@ return trimNL`

const t = m.tables[tableName];
return tableToInterface(m, t);
return tableToInterface(m, t, opts);
})

@@ -262,0 +277,0 @@ .join('\n\n')}

import type { AbstractSqlModel } from '@balena/abstract-sql-compiler';
import { expect } from 'chai';
import { source } from 'common-tags';
import { abstractSqlToTypescriptTypes } from '../src/generate';
import { abstractSqlToTypescriptTypes, type Options } from '../src/generate';
import { version } from '../package.json';

@@ -11,2 +11,3 @@

expectation: string,
opts?: Options,
) => {

@@ -23,3 +24,3 @@ it(`should generate ${msg}`, () => {

};
const result = abstractSqlToTypescriptTypes(t);
const result = abstractSqlToTypescriptTypes(t, opts);

@@ -100,3 +101,3 @@ expect(result).to.equal(source`

{
dataType: 'Serial',
dataType: 'Big Serial',
fieldName: 'id',

@@ -329,3 +330,3 @@ required: true,

modified_at: Types['Date Time']['Read'];
id: Types['Serial']['Read'];
id: Types['Big Serial']['Read'];
is_referenced_by__test?: Array<Test['Read']>;

@@ -336,3 +337,3 @@ };

modified_at: Types['Date Time']['Write'];
id: Types['Serial']['Write'];
id: Types['Big Serial']['Write'];
};

@@ -394,1 +395,89 @@ }

);
test(
'correct types for a test table with convertSerialToInteger=true',
testTable,
source`
export interface Parent {
Read: {
created_at: Types['Date Time']['Read'];
modified_at: Types['Date Time']['Read'];
id: Types['Integer']['Read'];
};
Write: {
created_at: Types['Date Time']['Write'];
modified_at: Types['Date Time']['Write'];
id: Types['Integer']['Write'];
};
}
export interface Other {
Read: {
created_at: Types['Date Time']['Read'];
modified_at: Types['Date Time']['Read'];
id: Types['Big Integer']['Read'];
is_referenced_by__test?: Array<Test['Read']>;
};
Write: {
created_at: Types['Date Time']['Write'];
modified_at: Types['Date Time']['Write'];
id: Types['Big Integer']['Write'];
};
}
export interface Test {
Read: {
created_at: Types['Date Time']['Read'];
modified_at: Types['Date Time']['Read'];
id: Types['Integer']['Read'];
a_date: Types['Date']['Read'];
a_file: Types['WebResource']['Read'];
parent: { __id: Parent['Read']['id'] } | [Parent['Read']];
references__other: { __id: Other['Read']['id'] } | [Other['Read']];
aliased__tag?: Array<TestTag['Read']>;
references__test__has__tag_key?: Array<TestTag['Read']>;
references__test_tag?: Array<TestTag['Read']>;
test__has__tag_key?: Array<TestTag['Read']>;
test_tag?: Array<TestTag['Read']>;
};
Write: {
created_at: Types['Date Time']['Write'];
modified_at: Types['Date Time']['Write'];
id: Types['Integer']['Write'];
a_date: Types['Date']['Write'];
a_file: Types['WebResource']['Write'];
parent: Parent['Write']['id'];
references__other: Other['Write']['id'];
};
}
export interface TestTag {
Read: {
created_at: Types['Date Time']['Read'];
modified_at: Types['Date Time']['Read'];
test: { __id: Test['Read']['id'] } | [Test['Read']];
tag_key: Types['Short Text']['Read'];
id: Types['Integer']['Read'];
};
Write: {
created_at: Types['Date Time']['Write'];
modified_at: Types['Date Time']['Write'];
test: Test['Write']['id'];
tag_key: Types['Short Text']['Write'];
id: Types['Integer']['Write'];
};
}
export default interface $Model {
parent: Parent;
other: Other;
test: Test;
test__has__tag_key: TestTag;
// Synonyms
test_tag: TestTag;
}
`,
{
convertSerialToInteger: true,
},
);

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc