@cwqt/refract
Advanced tools
Comparing version 1.0.12 to 1.1.0
@@ -59,16 +59,23 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
const relationship = (column) => { | ||
if (column.type == 'ManyToOne' || column.type == 'OneToOne') { | ||
const isNullable = column.modifiers.find(({ type }) => type == 'nullable'); | ||
const [model, fields, references, ...modifiers] = column.modifiers; | ||
if (column.type == 'OneToOne') { | ||
if (fields?.type !== 'fields' || references?.type !== 'references') { | ||
return `\t${column.name} ${(0, utils_1.isString)(model.value) ? model.value : model.value.name}${isNullable ? '?' : ''}`; | ||
} | ||
} | ||
return `\t${column.name} ${(0, utils_1.isString)(model.value) ? model.value : model.value.name}${isNullable ? '?' : ''} @relation(fields: [${fields.value.join(', ')}], references: [${references.value.join(', ')}])`.trimEnd(); | ||
if (column.type == 'OneToOne' || column.type == 'ManyToOne') { | ||
const modifiers = column.modifiers; | ||
const isNullable = modifiers.find(({ type }) => type === 'nullable'); | ||
const [model, ...restModifiers] = modifiers.filter(({ type }) => type !== 'nullable'); | ||
const relationModifier = restModifiers.length | ||
? `@relation(${restModifiers | ||
.sort(({ type }) => (type === 'name' ? -1 : 0)) | ||
.map(({ type, value }) => type === 'name' | ||
? `"${value}"` | ||
: `${type}: ${(0, utils_1.isArray)(value) ? `[${value.join(', ')}]` : value}`) | ||
.join(', ')})` | ||
: ''; | ||
return `\t${column.name} ${(0, utils_1.isString)(model.value) ? model.value : model.value.name}${isNullable ? '?' : ''} ${relationModifier}`.trimEnd(); | ||
} | ||
if (column.type == 'OneToMany') { | ||
const [model, ...modifiers] = column.modifiers; | ||
return `\t${column.name} ${(0, utils_1.isString)(model.value) ? model.value : model.value.name}[]`; | ||
const [model, relationName] = column.modifiers; | ||
const relationModifier = relationName | ||
? `@relation("${relationName.value}")` | ||
: ''; | ||
return `\t${column.name} ${(0, utils_1.isString)(model.value) ? model.value : model.value.name}[] ${relationModifier}`.trimEnd(); | ||
} | ||
}; |
@@ -34,2 +34,3 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
const align_1 = require("./align"); | ||
const validate_1 = require("./validate"); | ||
exports.default = (config) => { | ||
@@ -48,3 +49,5 @@ const start = perf_hooks_1.performance.now(); | ||
group((0, block_1.header)('enums'), enums.map(e => (0, block_1.block)(`enum ${e.name}`, e.columns.map(e => (0, column_1.column)(e)).join('\n')))), | ||
group((0, block_1.header)('models'), models.map(model => (0, block_1.block)(`model ${model.name}`, (0, align_1.alignFields)(model.columns.map(column_1.column).join('\n'))))), | ||
group((0, block_1.header)('models'), models | ||
.map(validate_1.validateModel) | ||
.map(model => (0, block_1.block)(`model ${model.name}`, (0, align_1.alignFields)(model.columns.map(column_1.column).join('\n'))))), | ||
] | ||
@@ -51,0 +54,0 @@ .filter(utils_1.nonNullable) |
import { Modifier } from '../types/modifiers'; | ||
export declare const modifier: <T extends "BigInt" | "Int" | "Float" | "Bytes" | "Decimal" | "String" | "Boolean" | "DateTime" | "Json" | keyof { | ||
export declare const modifier: <T extends "BigInt" | "Int" | "Float" | "Bytes" | "Decimal" | "String" | "Boolean" | "DateTime" | "Json" | "OneToMany" | "OneToOne" | "ManyToOne" | keyof { | ||
Enum: { | ||
@@ -13,19 +13,2 @@ id?: true; | ||
}; | ||
} | keyof { | ||
OneToMany: { | ||
model: string | import("../types/blocks").Model; | ||
nullable?: true; | ||
}; | ||
OneToOne: { | ||
model: string | import("../types/blocks").Model; | ||
nullable?: true; | ||
fields: string[]; | ||
references: string[]; | ||
}; | ||
ManyToOne: { | ||
nullable?: true; | ||
model: string | import("../types/blocks").Model; | ||
fields: string[]; | ||
references: string[]; | ||
}; | ||
} | "Raw">(type: T, modifier: Modifier<T, keyof import("../types/types").TypeData[T]>) => string; |
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.transform = exports.kv = void 0; | ||
const utils_1 = require("../types/utils"); | ||
const kv = (properties) => { | ||
return Object.entries(properties) | ||
return (0, utils_1.entries)(properties) | ||
.map(([key, value]) => `\t${key} = ${(0, exports.transform)(value)}`) | ||
@@ -6,0 +7,0 @@ .join('\n'); |
export * from './public/modifiers'; | ||
export * from './public/fields'; | ||
export * from './public/model'; | ||
export * from './public/mixin'; | ||
export * from './public/fields/scalars'; | ||
export * from './public/fields/enums'; | ||
export * from './public/fields/relations'; | ||
export * as Types from './types'; | ||
@@ -6,0 +8,0 @@ import * as Types from './types'; |
@@ -33,5 +33,7 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
__exportStar(require("./public/modifiers"), exports); | ||
__exportStar(require("./public/fields"), exports); | ||
__exportStar(require("./public/model"), exports); | ||
__exportStar(require("./public/mixin"), exports); | ||
__exportStar(require("./public/fields/scalars"), exports); | ||
__exportStar(require("./public/fields/enums"), exports); | ||
__exportStar(require("./public/fields/relations"), exports); | ||
exports.Types = __importStar(require("./types")); | ||
@@ -38,0 +40,0 @@ const promises_1 = require("fs/promises"); |
@@ -6,6 +6,3 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Field = (name, type) => { | ||
columns.push({ | ||
name, | ||
...type, | ||
}); | ||
columns.push({ name, ...type }); | ||
return { Field, columns }; | ||
@@ -12,0 +9,0 @@ }; |
@@ -9,3 +9,3 @@ import * as Types from '../types'; | ||
export declare const Model: (name: string) => Model; | ||
export declare class CallableModel implements Types.Blocks.Model, Model { | ||
export declare class $Model implements Types.Blocks.Model, Model { | ||
name: string; | ||
@@ -12,0 +12,0 @@ type: 'model'; |
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CallableModel = exports.Model = void 0; | ||
const Model = (name) => new CallableModel(name); | ||
exports.$Model = exports.Model = void 0; | ||
const Model = (name) => new $Model(name); | ||
exports.Model = Model; | ||
class CallableModel { | ||
class $Model { | ||
name; | ||
@@ -27,4 +27,2 @@ type = 'model'; | ||
Relation(name, type) { | ||
if (type.type == 'ManyToOne') { | ||
} | ||
this.columns.push({ name, ...type }); | ||
@@ -38,2 +36,2 @@ return this; | ||
} | ||
exports.CallableModel = CallableModel; | ||
exports.$Model = $Model; |
@@ -17,2 +17,3 @@ import { Column } from './columns'; | ||
export declare type Any = Scalar | Relation | Enum | 'Raw'; | ||
export declare type ReferentialAction = 'Cascade' | 'Restrict' | 'NoAction' | 'SetNull' | 'SetDefault'; | ||
declare type TopColumn = { | ||
@@ -19,0 +20,0 @@ name: string; |
import { JsonValue } from '../codegen/lib/json'; | ||
import { Model } from './blocks'; | ||
import { ReferentialAction } from './fields'; | ||
declare type Append<T, K> = { | ||
@@ -63,20 +64,22 @@ [index in keyof T]: T[index] & K; | ||
}; | ||
declare type Relations = { | ||
OneToMany: { | ||
model: Model | string; | ||
nullable?: true; | ||
}; | ||
declare type Relations = Append<{ | ||
OneToMany: {}; | ||
OneToOne: { | ||
model: Model | string; | ||
fields?: string[]; | ||
references?: string[]; | ||
onUpdate?: ReferentialAction; | ||
onDelete?: ReferentialAction; | ||
nullable?: true; | ||
fields: string[]; | ||
references: string[]; | ||
}; | ||
ManyToOne: { | ||
nullable?: true; | ||
model: Model | string; | ||
fields: string[]; | ||
references: string[]; | ||
onUpdate?: ReferentialAction; | ||
onDelete?: ReferentialAction; | ||
nullable?: true; | ||
}; | ||
}; | ||
}, { | ||
name?: string; | ||
model: Model; | ||
}>; | ||
export declare type TypeData = Scalars & Enums & Relations & { | ||
@@ -83,0 +86,0 @@ Raw: { |
export declare const isDate: (v: any) => v is Date; | ||
export declare const isFn: <F extends Function>(v: any) => v is F; | ||
export declare const isString: (v: any) => v is string; | ||
export declare const isArray: (v: any) => v is any[]; | ||
declare type Entries<T> = { | ||
@@ -5,0 +6,0 @@ [K in keyof T]: [K, T[K]]; |
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.shift = exports.nonNullable = exports.del = exports.entries = exports.isString = exports.isFn = exports.isDate = void 0; | ||
exports.shift = exports.nonNullable = exports.del = exports.entries = exports.isArray = exports.isString = exports.isFn = exports.isDate = void 0; | ||
const isDate = (v) => v instanceof Date && !isNaN(v); | ||
@@ -9,2 +9,4 @@ exports.isDate = isDate; | ||
exports.isString = isString; | ||
const isArray = (v) => Array.isArray(v); | ||
exports.isArray = isArray; | ||
const entries = (obj) => Object.entries(obj); | ||
@@ -11,0 +13,0 @@ exports.entries = entries; |
{ | ||
"name": "@cwqt/refract", | ||
"version": "1.0.12", | ||
"version": "1.1.0", | ||
"description": "Generate Prisma from TypeScript", | ||
@@ -5,0 +5,0 @@ "author": "cwqt", |
87253
57
1203