Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
uasan committed Feb 25, 2024
1 parent 84bc895 commit 7d6c59e
Show file tree
Hide file tree
Showing 23 changed files with 100 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"prettier"
],
"rules": {
"prettier/prettier": 2 // Means error
"prettier/prettier": 2,
"@typescript-eslint/no-explicit-any": 1
},
"env": {
"browser": true,
Expand Down
10 changes: 9 additions & 1 deletion src/compiler/entities/migrations/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ export function createTableMigration(model) {

if (table.unique) {
for (const key of Object.keys(table.unique)) {
fields.push(' CONSTRAINT "' + key + '" UNIQUE ("' + key + '")');
const { columns } = table.unique[key];

if (columns) {
fields.push(
' CONSTRAINT "' + key + '" UNIQUE ("' + columns.join('", "') + '")'
);
} else {
fields.push(' CONSTRAINT "' + key + '" UNIQUE ("' + key + '")');
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/entities/models/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export function TableModel(node) {
name: meta.name,
type: getSqlType(meta),
default: meta.defaultValue,
isNotNull: !meta.isNullable,
references: getTableReferences(meta.links),
isNotNull: !meta.isNullable && !meta.isUndefined,
});

model.fields.set(meta.name, [
Expand Down
17 changes: 13 additions & 4 deletions src/compiler/helpers/checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ export const {
ObjectFlagsType,
} = ts.TypeFlags;

const {
Interface,
TypeAlias,
Alias,
ExportValue,
Value: ValueSymbol,
} = ts.SymbolFlags;

export const {
Decorator,
TrueKeyword,
Expand All @@ -40,7 +48,6 @@ const { Readonly: CheckFlagReadonly } = ts.CheckFlags;
const { Readonly: ModifierFlagReadonly } = ts.ModifierFlags;
const { Const } = ts.NodeFlags;
const { SignatureConstruct } = ts.SignatureKind;
const { Interface, TypeAlias, Alias, ExportValue } = ts.SymbolFlags;
const { every, some, getCheckFlags, getDeclarationModifierFlagsFromSymbol } =
ts;

Expand Down Expand Up @@ -132,6 +139,7 @@ export const getSymbolOfNode = node => host.checker.getSymbolAtLocation(node);
export const isTypeSymbol = ({ flags }) =>
(flags & TypeAlias) !== 0 || (flags & Interface) !== 0;

export const isValueSymbol = ({ flags }) => (flags & ValueSymbol) !== 0;
export const isAliasSymbol = symbol => (symbol.flags & Alias) !== 0;
export const isExportSymbol = symbol => (symbol.flags & ExportValue) !== 0;
export const isExportKeyword = ({ kind }) => kind === ExportKeyword;
Expand Down Expand Up @@ -204,10 +212,11 @@ export const getSignaturesConstructOfType = type =>
export const getSignaturesConstructOfSymbol = symbol =>
getSignaturesConstructOfType(getTypeOfSymbol(symbol));

export const hasSignatureConstruct = node =>
getSignaturesConstructOfSymbol(getSymbolOfNode(node)).length > 0;
export const hasSignatureConstruct = symbol =>
getSignaturesConstructOfSymbol(symbol).length > 0;

export const getConstructIdentifier = node =>
node.kind === TypeReference && hasSignatureConstruct(node.typeName)
node.kind === TypeReference &&
hasSignatureConstruct(getSymbolOfNode(node.typeName))
? node.typeName
: null;
23 changes: 18 additions & 5 deletions src/compiler/helpers/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ export const factoryRouteFunction = statements =>
host.factory.createBlock(statements, false)
);

export const updateMethodParameters = (node, parameters) =>
host.factory.updateMethodDeclaration(
node,
node.modifiers,
undefined,
node.name,
undefined,
undefined,
parameters,
undefined,
node.body
);

export const updateMethodStatements = (node, statements) =>
host.factory.updateMethodDeclaration(
node,
Expand All @@ -70,17 +83,17 @@ export const updateMethodStatements = (node, statements) =>
);

export function ensureArgument(node, index = 0) {
const { name } = node.parameters[index];
const parameter = node.parameters[index];

if (name.kind === Identifier) {
if (parameter.name.kind === Identifier) {
return node;
}

const parameters = [...node.parameters];
const arg = host.module.createIdentifier('_');

parameters[index] = host.factory.updateParameterDeclaration(
node.parameters[index],
parameter,
undefined,
undefined,
arg,
Expand All @@ -89,8 +102,8 @@ export function ensureArgument(node, index = 0) {
undefined
);

return updateMethodStatements(node, [
factoryLet(name, arg),
return updateMethodStatements(updateMethodParameters(node, parameters), [
factoryLet(parameter.name, arg),
...node.body.statements,
]);
}
26 changes: 17 additions & 9 deletions src/compiler/helpers/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getTypeOfNode,
getTypeOfTypeNode,
hasNullType,
hasSignatureConstruct,
hasUndefinedType,
} from './checker.js';

Expand Down Expand Up @@ -46,7 +47,9 @@ function makeMetaType(meta, node) {

if (types.has(symbol)) {
types.get(symbol).make(meta, node.typeArguments);
} else if (meta.isBinary === false) {
} else if (hasSignatureConstruct(symbol)) {
meta.isConstruct = true;
} else {
const typeNode = symbol.declarations?.[0]?.type;

if (typeNode) {
Expand Down Expand Up @@ -95,13 +98,16 @@ export class MetaType {

isNullable = false;
isOptional = false;
isUndefined = false;
isTypeTrusted = false;

isBlob = false;
isFile = false;
isUUID = false;
isBinary = false;
isStream = false;
isDateLike = false;
isConstruct = false;
isBufferStream = false;

isRefType = false;
Expand All @@ -118,14 +124,7 @@ export class MetaType {
this.node = node;
this.type = type;

if (DateLike.isAssignable(type)) {
this.isDateLike = true;
this.sqlType = DateLike.sqlType;
} else if (BinaryData.isAssignable(type)) {
this.isBinary = true;
this.sqlType = BinaryData.sqlType;
}

//console.log(host.checker.typeToString(type), DateLike.isAssignable(type));
makeMetaType(this, node);
}

Expand All @@ -145,6 +144,7 @@ export class MetaType {

if (hasUndefinedType(self.type)) {
self.isOptional = true;
self.isUndefined = true;
self.type = getNonUndefinedType(self.type);
}

Expand All @@ -153,6 +153,14 @@ export class MetaType {
self.defaultValue = node.initializer;
}

if (DateLike.isAssignable(self.type)) {
self.isDateLike = true;
self.sqlType = DateLike.sqlType;
} else if (BinaryData.isAssignable(self.type)) {
self.isBinary = true;
self.sqlType = BinaryData.sqlType;
}

return self;
}
}
15 changes: 9 additions & 6 deletions src/compiler/helpers/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function makeValidatorsByType(ast, meta, type = meta.type) {
}

function makeValidatorsByMeta(ast, meta) {
if (meta.isBinary) {
if (meta.isConstruct) {
return ast;
}

Expand Down Expand Up @@ -167,17 +167,20 @@ export function makePayloadValidator(node, type) {
let initAst = host.factory.createIdentifier('_');
let ast = makeValidatorsBySymbols(initAst, metaType.props);

if (initAst !== ast) {
addTransformer(node, node => {
node = ensureArgument(node);
addTransformer(node, node => {
node = ensureArgument(node);

if (initAst !== ast) {
Object.assign(initAst, internals.newValidator(node.parameters[0].name));

return updateMethodStatements(node, [
factoryStatement(factoryCallMethod(ast, 'validate')),
...node.body.statements,
]);
});
}
} else {
return node;
}
});
}

return metaType;
Expand Down
1 change: 1 addition & 0 deletions src/compiler/makers/types/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class Validator {

static make(meta, args) {
meta.sqlType ||= this.sqlType;

meta.validators.add(new this().setProps(meta, args?.[0]));
}

Expand Down
1 change: 1 addition & 0 deletions src/compiler/makers/types/validators/Blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export class Blob extends Validator {

static make(meta) {
meta.isBlob = true;
meta.isConstruct = true;
meta.isBufferStream = true;
}
}
4 changes: 2 additions & 2 deletions src/compiler/makers/types/validators/DateLike.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Validator } from '../Validator.js';
export class DateLike extends Validator {
static sqlType = 'timestamptz';

static make() {
//
static make(meta) {
meta.isConstruct = true;
}
}
2 changes: 2 additions & 0 deletions src/compiler/makers/types/validators/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export class File extends Validator {

static make(meta) {
meta.isFile = true;
meta.isConstruct = true;
meta.isTypeTrusted = true;
meta.isBufferStream = true;
}
}
3 changes: 2 additions & 1 deletion src/compiler/makers/types/validators/Int16.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Int } from './Int.js';

export class Int16 extends Int {
static sqlType = 'smallint';

byteLength = 2;
sqlType = 'smallint';
numberType = 'Int16';
}
3 changes: 2 additions & 1 deletion src/compiler/makers/types/validators/Int32.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Int } from './Int.js';

export class Int32 extends Int {
static sqlType = 'int';

byteLength = 4;
sqlType = 'int';
numberType = 'Int32';
}
3 changes: 2 additions & 1 deletion src/compiler/makers/types/validators/Int8.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Int } from './Int.js';

export class Int8 extends Int {
static sqlType = 'smallint';

byteLength = 1;
sqlType = 'smallint';
numberType = 'Int8';
}
4 changes: 3 additions & 1 deletion src/compiler/makers/types/validators/TypedArray.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Validator } from '../Validator.js';

export class TypedArray extends Validator {
static make(meta) { }
static make(meta) {
meta.isConstruct = true;
}
}
3 changes: 2 additions & 1 deletion src/compiler/makers/types/validators/Uint16.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Int } from './Int.js';

export class Uint16 extends Int {
static sqlType = 'smallint';

byteLength = 2;
sqlType = 'smallint';
numberType = 'Uint16';
}
3 changes: 2 additions & 1 deletion src/compiler/makers/types/validators/Uint32.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Int } from './Int.js';

export class Uint32 extends Int {
static sqlType = 'int';

byteLength = 4;
sqlType = 'int';
numberType = 'Uint32';
}
3 changes: 2 additions & 1 deletion src/compiler/makers/types/validators/Uint8.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Int } from './Int.js';

export class Uint8 extends Int {
static sqlType = 'smallint';

byteLength = 1;
sqlType = 'smallint';
numberType = 'Uint8';
}
1 change: 1 addition & 0 deletions src/compiler/makers/types/validators/Uint8Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class Uint8Array extends Validator {
static make(meta, args) {
const self = new this().setProps(meta, args?.[0]);

meta.isConstruct = true;
meta.sqlType = self.sqlType;

if (self.props.has('length')) {
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/makers/types/validators/int.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Validator } from '../Validator.js';

export class Int extends Validator {
static sqlType = 'int';

byteLength = 4;
sqlType = 'int';
numberType = 'Int32';

make(ast, method = 'isInt') {
super.make(ast, method);

this.meta.sqlType = this.sqlType;
//this.meta.sqlType = this.sqlType;
this.meta.bytelength = this.bytelength;
this.meta.numberType = this.numberType;

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/makers/types/validators/tuple.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Validator } from '../validator.js';
import { Validator } from '../Validator.js';

export class Tuple extends Validator {
make(ast) {
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/types/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type BigIntSerial = bigint;

export type Int<T extends NumberOptions = undefined> = number;
export type Int8<T extends NumberOptions = undefined> = number;
export type Int15<T extends NumberOptions = undefined> = number;
export type Int16<T extends NumberOptions = undefined> = number;
export type Int32<T extends NumberOptions = undefined> = number;
export type Uint<T extends NumberOptions = undefined> = number;
export type Uint8<T extends NumberOptions = undefined> = number;
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/types/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ export class File {

return path;
}

[Symbol.dispose]() {
console.log('File dispose');
}
}

0 comments on commit 7d6c59e

Please sign in to comment.