Comparing version 0.0.69 to 0.0.70
@@ -51,3 +51,3 @@ "use strict"; | ||
const tblName = tbl.jsName; | ||
const tblType = tbl.jsName.replace(/__/g, '.'); | ||
const tblType = tbl.jsName.replace(/_/g, '.'); | ||
const query = (Q) => `async (args${Cst.argOpts.includes(Q) ? '?' : ''}: ${tblType}.${Cst.aggregates.includes(Q) ? `$.${Q}` : Q === 'delete' ? 'del' : Q}): Promise<{data?: ${Cst.aggregates.includes(Q) | ||
@@ -117,3 +117,2 @@ ? 'number' | ||
check: {}, | ||
optional: {}, | ||
scalar: {}, | ||
@@ -212,4 +211,2 @@ onTime: {}, | ||
}); | ||
if ('required' !== col.optional) | ||
typeDefine.optional[tbl.jsName].push(colName); | ||
switch (col.type) { | ||
@@ -236,2 +233,8 @@ case 'Date': | ||
} | ||
if ('required' !== col.optional && | ||
col.props.default === undefined && | ||
!col.props.isId && | ||
!col.props.createdAt && | ||
!col.props.updatedAt) | ||
colDefine += ' | null'; | ||
typeDefine.scalar[tbl.jsName].push(colDefine); | ||
@@ -245,2 +248,6 @@ typeDefine.table[tbl.jsName].push(colDefine); | ||
const generateTsType = (tables) => { | ||
function titleCase(str) { | ||
const newStr = str.slice(0, 1).toUpperCase() + str.slice(1); | ||
return newStr; | ||
} | ||
const TblType = (columns, uniques) => { | ||
@@ -255,59 +262,49 @@ const query = (Q) => { | ||
.join('\n'); | ||
case 'orderBy': | ||
return fields | ||
.filter((v) => !v.isRelation) | ||
.map((v) => `${v.fieldName}?: $Order`) | ||
.join('\n'); | ||
case 'where': | ||
return fields | ||
.filter((v) => v.fieldType !== 'object') | ||
.map((v) => `${v.fieldName}?: ${v.isRelation | ||
? v.isArray | ||
? `{ | ||
every?: ${v.fieldType}.where | ||
some?: ${v.fieldType}.where | ||
none?: ${v.fieldType}.where | ||
every?: Omit<${v.fieldType}.where, ${v.OmitSelectKeys}> | ||
some?: Omit<${v.fieldType}.where, ${v.OmitSelectKeys}> | ||
none?: Omit<${v.fieldType}.where, ${v.OmitSelectKeys}> | ||
}` | ||
: `${v.fieldType}.where` | ||
: `Omit<${v.fieldType}.where, ${v.OmitSelectKeys}>` | ||
: `${v.isEnum | ||
? `$enumFilter<${v.fieldType}>` | ||
: `$${v.fieldType}Filter`} | ${v.fieldType}`}`) | ||
? `$EnumFilter<${v.fieldType}, ${v.isNull}>` | ||
: `$${titleCase(v.fieldType)}Filter<${v.isNull}>`} | ${v.fieldType} ${v.isNull ? ' | null' : ''}`}`) | ||
.join('\n'); | ||
case 'select': | ||
return ` | ||
'*': boolean | ||
'*'?: boolean | ||
${fields | ||
.map((v) => `${v.fieldName}?: ${v.isRelation | ||
? v.isArray | ||
? `${v.fieldType}.findMany | ${v.fieldType}.select` | ||
: `Omit<${v.fieldType}.select, ${v.OmitSelectKeys}>` | ||
? `Omit<${v.fieldType}.select, ${v.OmitSelectKeys}> | { | ||
where?: ${v.fieldType}.where | ||
orderBy?: ${v.fieldType}.orderBy | ||
limit?: number | ||
offset?: number | ||
distinct?: '*' | $Enumerable<scalar> | ||
select?: Omit<${v.fieldType}.select, ${v.OmitSelectKeys}> | ||
}` | ||
: `Omit<${v.fieldType}.select, ${v.OmitSelectKeys}> | {select?: Omit<${v.fieldType}.select, ${v.OmitSelectKeys}>}` | ||
: 'boolean'}`) | ||
.join('\n')} | ||
`; | ||
case 'findOne': | ||
case 'insertInput': | ||
return ` | ||
where: uniqueWhere | ||
select?: {[P in keyof select]?: select[P]} | ||
sql?: boolean | ||
`; | ||
case 'findFirst': | ||
return ` | ||
where?: where | ||
orderBy?: {[P in keyof scalar]?: $Order} | ||
select?: {[P in keyof select]?: select[P]} | ||
sql?: boolean | ||
`; | ||
case 'findMany': | ||
return ` | ||
where?: where | ||
orderBy?: {[P in keyof scalar]?: $Order} | ||
limit?: number | ||
offset?: number | ||
select?: {[P in keyof select]?: select[P]} | ||
sql?: boolean | ||
`; | ||
case 'insert': | ||
return ` | ||
data: $Enumerable<{ | ||
${fields | ||
.filter((v) => !v.isAuto && !v.isForeign) | ||
.map((v) => `${v.fieldName}${v.required}: ${v.isRelation | ||
? `{ | ||
insert?: ${v.isArray | ||
? `$Enumerable<Omit<${v.fieldType}.insert['data'], ${v.relationKeys}>>` | ||
: `Omit<${v.fieldType}.insert['data'], ${v.relationKeys}>`} | ||
? `$Enumerable<Omit<${v.fieldType}.insertInput, ${v.relationKeys}>>` | ||
: `Omit<${v.fieldType}.insertInput, ${v.relationKeys}>`} | ||
connect?: ${v.isArray | ||
@@ -319,59 +316,89 @@ ? `$Enumerable<${v.fieldType}.uniqueWhere>` | ||
.join('\n')} | ||
}> | ||
select?: {[P in keyof select]?: select[P]} | ||
sql?: boolean | ||
`; | ||
case 'update': | ||
`; | ||
case 'updateInput': | ||
return ` | ||
where: uniqueWhere | ||
data: { | ||
${fields | ||
${fields | ||
.filter((v) => !v.isAuto && !v.isForeign) | ||
.map((v) => `${v.fieldName}?: ${v.isRelation | ||
? `{ | ||
insert?: ${v.isArray | ||
? `$Enumerable<Omit<${v.fieldType}.insert['data'], ${v.relationKeys}>>` | ||
: `Omit<${v.fieldType}.insert['data'], ${v.relationKeys}>`} | ||
connect?: ${v.isArray | ||
insert?: ${v.isArray | ||
? `$Enumerable<Omit<${v.fieldType}.insertInput, ${v.relationKeys}>>` | ||
: `Omit<${v.fieldType}.insertInput, ${v.relationKeys}>`} | ||
connect?: ${v.isArray | ||
? `$Enumerable<${v.fieldType}.uniqueWhere>` | ||
: `${v.fieldType}.uniqueWhere`} | ||
disconnect?: ${v.isArray | ||
disconnect?: ${v.isArray | ||
? `$Enumerable<${v.fieldType}.uniqueWhere>` | ||
: `${v.fieldType}.uniqueWhere`} | ||
delete?: ${v.isArray | ||
delete?: ${v.isArray | ||
? `$Enumerable<${v.fieldType}.uniqueWhere>` | ||
: `${v.fieldType}.uniqueWhere`} | ||
upsert?: ${v.isArray | ||
upsert?: ${v.isArray | ||
? `$Enumerable<{ | ||
where: uniqueWhere | ||
insert: Omit<${v.fieldType}.insert['data'], ${v.relationKeys}> | ||
update: Omit<${v.fieldType}.update['data'], ${v.relationKeys}> | ||
}>` | ||
where: uniqueWhere | ||
insert: Omit<${v.fieldType}.insertInput, ${v.relationKeys}> | ||
update: Omit<${v.fieldType}.updateInput, ${v.relationKeys}> | ||
}>` | ||
: `{ | ||
where: uniqueWhere | ||
insert: Omit<${v.fieldType}.insert['data'], ${v.relationKeys}> | ||
update: Omit<${v.fieldType}.update['data'], ${v.relationKeys}> | ||
}`} | ||
update?: ${v.isArray | ||
where: uniqueWhere | ||
insert: Omit<${v.fieldType}.insertInput, ${v.relationKeys}> | ||
update: Omit<${v.fieldType}.updateInput, ${v.relationKeys}> | ||
}`} | ||
update?: ${v.isArray | ||
? `$Enumerable<{ | ||
where: ${v.fieldType}.uniqueWhere | ||
data: Omit<${v.fieldType}.update['data'], ${v.relationKeys}> | ||
}>` | ||
where: ${v.fieldType}.uniqueWhere | ||
data: Omit<${v.fieldType}.updateInput, ${v.relationKeys}> | ||
}>` | ||
: `{ | ||
where: ${v.fieldType}.uniqueWhere | ||
data: Omit<${v.fieldType}.update['data'], ${v.relationKeys}> | ||
}`} | ||
${v.isArray | ||
where: ${v.fieldType}.uniqueWhere | ||
data: Omit<${v.fieldType}.updateInput, ${v.relationKeys}> | ||
}`} | ||
${v.isArray | ||
? ` | ||
set?: ${v.fieldType}.uniqueWhere | ||
updateMany?: ${v.fieldType}.updateMany | ||
deleteMany?: ${v.fieldType}.where | ||
` | ||
set?: ${v.fieldType}.uniqueWhere | ||
updateMany?: ${v.fieldType}.updateMany | ||
deleteMany?: ${v.fieldType}.where | ||
` | ||
: ''} | ||
}` | ||
}` | ||
: v.fieldType}`) | ||
.join('\n')} | ||
} | ||
`; | ||
case 'findOne': | ||
return ` | ||
where: uniqueWhere | ||
select?: {[P in keyof select]?: select[P]} | ||
sql?: boolean | ||
`; | ||
case 'findFirst': | ||
return ` | ||
where?: where | ||
orderBy?: orderBy | ||
select?: {[P in keyof select]?: select[P]} | ||
distinct?: '*' | $Enumerable<scalar> | ||
sql?: boolean | ||
`; | ||
case 'findMany': | ||
return ` | ||
where?: where | ||
orderBy?: orderBy | ||
limit?: number | ||
offset?: number | ||
select?: {[P in keyof select]?: select[P]} | ||
distinct?: '*' | $Enumerable<scalar> | ||
sql?: boolean | ||
`; | ||
case 'insert': | ||
return ` | ||
data: $Enumerable<insertInput> | ||
select?: {[P in keyof select]?: select[P]} | ||
sql?: boolean | ||
`; | ||
case 'update': | ||
return ` | ||
where: uniqueWhere | ||
data: updateInput | ||
select?: {[P in keyof select]?: select[P]} | ||
sql?: boolean | ||
`; | ||
case 'updateMany': | ||
@@ -382,3 +409,3 @@ return ` | ||
${fields | ||
.filter((v) => !v.isRelation) | ||
.filter((v) => !v.isRelation && !v.isAuto) | ||
.map((v) => `${v.fieldName}?: ${v.fieldType}`) | ||
@@ -392,4 +419,4 @@ .join('\n')} | ||
where: uniqueWhere | ||
insert: insert['data'] | ||
update: update['data'] | ||
insert: insertInput | ||
update: updateInput | ||
select?: {[P in keyof select]?: select[P]} | ||
@@ -424,7 +451,8 @@ sql?: boolean | ||
where?: where | ||
select: ${fields | ||
.filter((v) => v.fieldType === 'number').length ? fields | ||
.filter((v) => v.fieldType === 'number') | ||
.map((v) => `'${v.fieldName}'`) | ||
.join('|') : 'never'} | ||
select: ${fields.filter((v) => v.fieldType === 'number').length | ||
? fields | ||
.filter((v) => v.fieldType === 'number') | ||
.map((v) => `'${v.fieldName}'`) | ||
.join('|') | ||
: 'never'} | ||
sql?: boolean | ||
@@ -447,3 +475,3 @@ `; | ||
.join(' | '); | ||
const queries = ['scalar', 'where', 'select', ...Cst.queries] | ||
const queries = ['where', 'scalar', 'orderBy', 'select', 'insertInput', 'updateInput', ...Cst.queries] | ||
.map((v) => `export ${v === '$' ? 'namespace' : 'type'} ${v === 'delete' ? 'del' : v} ${v === '$' ? '' : '='} { | ||
@@ -463,2 +491,3 @@ ${v === '$' ? aggr() : query(v)} | ||
const columns = {}; | ||
const foreign = []; | ||
for (const k in tbl.columns) { | ||
@@ -472,5 +501,10 @@ const col = tbl.columns[k]; | ||
isEnum: col.type === 'enum' ? true : false, | ||
fieldType: typeTbl.endsWith(']') && col.type !== 'enum' ? typeTbl.slice(0, -2) : typeTbl, | ||
fieldType: typeTbl.endsWith(']') && col.type !== 'enum' | ||
? typeTbl.slice(0, -2) | ||
: typeTbl, | ||
required: col.optional === 'required' ? '' : '?', | ||
isRelation: false, | ||
isAuto: false, | ||
isNull: false, | ||
isForeign: foreign.includes(col.jsName) ? true : false, | ||
isArray: col.optional === 'array' ? '[]' : '', | ||
@@ -480,2 +514,15 @@ relationKeys: 'never', | ||
}; | ||
if (col.props.createdAt || col.props.updatedAt) { | ||
columns[col.jsName].isAuto = true; | ||
} | ||
if (col.props.isId && !['string', 'int'].includes(col.props.idType)) { | ||
columns[col.jsName].isAuto = true; | ||
} | ||
if (!col.props.createdAt && | ||
!col.props.updatedAt && | ||
!col.props.isId && | ||
col.optional !== 'required' && | ||
col.props.default === undefined) { | ||
columns[col.jsName].isNull = true; | ||
} | ||
if (relColOpt) { | ||
@@ -486,9 +533,31 @@ columns[col.jsName].isRelation = true; | ||
relColOpt.endsWith(']') || col.optional === 'array' ? '[]' : ''; | ||
columns[col.jsName].required = relColOpt.endsWith(']') || col.optional === 'array' ? '?' : ''; | ||
columns[col.jsName].required = | ||
relColOpt.endsWith(']') || col.optional !== 'required' ? '?' : ''; | ||
columns[col.jsName].relationKeys = `'${relColOpt.match(/^\w+/)[0]}'`; | ||
columns[col.jsName].OmitSelectKeys = `'${relColOpt.match(/^\w+/)[0]}'`; | ||
if (col.props.foreign) { | ||
columns[col.jsName].relationKeys += | ||
'|' + col.props.foreign.keys.map((v) => `'${v}'`).join(' | '); | ||
if (col.props.foreign.keys) { | ||
for (const k of col.props.foreign.keys) { | ||
if (columns[k]) { | ||
columns[k].isForeign = true; | ||
} | ||
foreign.push(k); | ||
} | ||
} | ||
if (columns[col.jsName].fieldType | ||
.split('.') | ||
.reduce((_, v) => (_.columns ? _.columns[v] : _[v]), tables)) { | ||
const relationTbl = columns[col.jsName].fieldType | ||
.split('.') | ||
.reduce((_, v) => (_.columns ? _.columns[v] : _[v]), tables); | ||
if (relationTbl) { | ||
const relationCol = relationTbl.columns[`${relColOpt.match(/^\w+/)[0]}`]; | ||
if (relationCol && relationCol.optional !== 'array') { | ||
columns[col.jsName].OmitSelectKeys = `'${relColOpt.match(/^\w+/)[0]}'`; | ||
} | ||
} | ||
} | ||
} | ||
else if (!relColOpt.endsWith(']')) { | ||
columns[col.jsName].OmitSelectKeys = `'${relColOpt.match(/^\w+/)[0]}'`; | ||
} | ||
} | ||
@@ -518,2 +587,5 @@ } | ||
require(`${templatePath}/typedefine`); | ||
serverApi += | ||
EnumType(ast.enums) + | ||
require(`${templatePath}/typedefine`); | ||
let dbType = { | ||
@@ -524,3 +596,2 @@ TB: [], | ||
CU: [], | ||
NL: [], | ||
anno: {}, | ||
@@ -541,3 +612,3 @@ }; | ||
dbType.UN.push(...tmp); | ||
for (const v2 of ['foreign', 'onTime', 'optional']) { | ||
for (const v2 of ['foreign', 'onTime']) { | ||
tmp.length = 0; | ||
@@ -548,11 +619,6 @@ for (const k3 in orm.typeDefine[v2]) { | ||
} | ||
dbType[{ foreign: 'FK', onTime: 'CU', optional: 'NL' }[v2]].push(...tmp); | ||
dbType[{ foreign: 'FK', onTime: 'CU' }[v2]].push(...tmp); | ||
} | ||
Object.assign(dbType.anno, orm.Att); | ||
} | ||
serverApi += clientApi += | ||
'\n\nexport ' + | ||
['TB', 'UN', 'FK', 'CU', 'NL'] | ||
.map((v) => `type $${v} = {\n${dbType[v].join('\n')}}`) | ||
.join('\n\n'); | ||
serverApi += `\n\n${fs_1.default.readFileSync(path_1.default.join(__dirname, `${templatePath}/annotation`), 'utf-8')} = ${JSON.stringify(dbType.anno, null, 2)}`; | ||
@@ -568,6 +634,6 @@ serverApi += EnumConst(ast.enums); | ||
const tsType = namespaceType(db, k); | ||
serverApi += classServer(db, k); | ||
clientApi += classClient(db, k); | ||
serverApi += tsType; | ||
clientApi += tsType; | ||
serverApi += classServer(db, k); | ||
clientApi += classClient(db, k); | ||
} | ||
@@ -574,0 +640,0 @@ serverApi += (0, templates_1.apiBridge)(Object.keys(ast.dbs)); |
@@ -34,3 +34,2 @@ "use strict"; | ||
const templates_1 = require("./templates"); | ||
const child_process_1 = require("child_process"); | ||
BigInt.prototype['toJSON'] = function () { | ||
@@ -154,4 +153,7 @@ return this.toString(); | ||
function MapTblName(table, column, table2, column2) { | ||
const sorted = [`${table}_${column}`, `${table2}_${column2}`].sort(); | ||
return `_${sorted[0]}_$_${sorted[1]}`; | ||
const sorted = [ | ||
`${table}${column[0].toUpperCase()}${column.slice(1)}`, | ||
`${table2}${column2[0].toUpperCase()}${column2.slice(1)}`, | ||
].sort(); | ||
return `_${sorted[0]}_${sorted[1]}`; | ||
} | ||
@@ -234,8 +236,2 @@ exports.MapTblName = MapTblName; | ||
fs_1.default.writeFileSync(path_1.default.join(acaRoot, Cst.AcaConfig), JSON.stringify(config, null, 2), 'utf-8'); | ||
if (kind === 'client' && | ||
!fs_1.default.existsSync(path_1.default.join(acaRoot, appName, 'node_modules', 'axios'))) { | ||
console.log(`Installing axios...`); | ||
process.chdir(path_1.default.join(acaRoot, appName)); | ||
(0, child_process_1.execSync)(`npm install axios`); | ||
} | ||
console.log(`\nRun the following command to start development:`); | ||
@@ -242,0 +238,0 @@ console.log(`$ cd ${acaDir === '..' ? '../' : ''}${appName}`); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.argOpts = exports.queries = exports.aggregates = exports.ClientApiIndex = exports.ClientApi = exports.ServerIndex = exports.ServerServe = exports.ServerTsconfig = exports.ServerPackage = exports.ServerRPCIndex = exports.ServerRPCDir = exports.ApiIndex = exports.DefaultClientApiDir = exports.DefaultServerApiDir = exports.DefaultTsDir = exports.DefaultClientName = exports.DefaultServerName = exports.AcaOrmPropetty = exports.AcaMiscRemark = exports.AcaMiscRecordsDir = exports.AcaConfigSchema = exports.AcaMiscDir = exports.AcaTsconfig = exports.AcaExample = exports.AcaConfig = exports.AcaDir = exports.Pkg = exports.FrameworkClient = exports.ServerApiExport = exports.ClientApiExport = exports.Delimiter = void 0; | ||
exports.Delimiter = `__`; | ||
exports.ClientApiExport = ['$RPC', '$Enum', '$TB', '$TbOper']; | ||
exports.Delimiter = `_`; | ||
exports.ClientApiExport = ['$RPC', '$Enum']; | ||
exports.ServerApiExport = [...exports.ClientApiExport, '$ApiBridge']; | ||
@@ -7,0 +7,0 @@ exports.FrameworkClient = ['vue', 'react', 'gatsby']; |
@@ -16,2 +16,5 @@ "use strict"; | ||
const foreigns = []; | ||
if (tbls[jsName].dbName.length > sqlDiff.keyword.maxTblLen) { | ||
throw new Error(`table ${tbls[jsName].dbName} length more than the ${sqlDiff.keyword.maxTblLen}`); | ||
} | ||
for (const k in tbls[jsName].columns) { | ||
@@ -262,2 +265,5 @@ const colObj = tbls[jsName].columns[k]; | ||
const splits = colObj.type.match(/[\w\.]+/)[0].split('.'); | ||
if (colName.length > sqlDiff.keyword.maxColLen) { | ||
throw new Error(`column ${colName} length more than the ${sqlDiff.keyword.maxColLen}`); | ||
} | ||
if (splits.length === 1) { | ||
@@ -332,2 +338,5 @@ const dbType = ` ${(typ.dbType[props.dbType] || props.dbType).toUpperCase()}`; | ||
const sqlDiff = (0, sql_diff_1.default)(config.connectOption.driver); | ||
if (mapName.length > sqlDiff.keyword.maxTblLen) { | ||
throw new Error(`table ${mapName} length more than the ${sqlDiff.keyword.maxTblLen}`); | ||
} | ||
const typ = sqlDiff.keyword; | ||
@@ -334,0 +343,0 @@ const qPrefix = typ.quote.prefix; |
@@ -18,2 +18,4 @@ "use strict"; | ||
quote: { prefix: '"', name: '"', value: "'" }, | ||
maxTblLen: 64, | ||
maxColLen: 64, | ||
idDefaultType: 'cuid', | ||
@@ -92,4 +94,6 @@ idType: { | ||
fullName: 'mssql', | ||
npm: `"tedious": "",`, | ||
npm: `"mssql": "",`, | ||
quote: { prefix: '"', name: '"', value: "'" }, | ||
maxTblLen: 128, | ||
maxColLen: 128, | ||
idDefaultType: 'cuid', | ||
@@ -142,3 +146,3 @@ idType: { | ||
let Db, db; | ||
Db = require(path_1.default.join(resolveAcaDir, app, 'node_modules/tedious')); | ||
Db = require(path_1.default.join(resolveAcaDir, app, 'node_modules/mssql')); | ||
const opts = { | ||
@@ -169,2 +173,4 @@ ...options, | ||
quote: { prefix: '`', name: '`', value: "'" }, | ||
maxTblLen: 64, | ||
maxColLen: 64, | ||
idDefaultType: 'cuid', | ||
@@ -238,2 +244,4 @@ idType: { | ||
quote: { prefix: '`', name: '`', value: "'" }, | ||
maxTblLen: 64, | ||
maxColLen: 64, | ||
idDefaultType: 'cuid', | ||
@@ -311,2 +319,4 @@ idType: { | ||
quote: { prefix: '"', name: '"', value: "'" }, | ||
maxTblLen: 128, | ||
maxColLen: 128, | ||
idDefaultType: 'cuid', | ||
@@ -313,0 +323,0 @@ idType: { |
@@ -45,3 +45,3 @@ "use strict"; | ||
async transaction() { | ||
const trx = await this.knex.transaction() | ||
const trx:any = await this.knex.transaction() | ||
trx['$Driver'] = this.$Driver | ||
@@ -101,3 +101,3 @@ | ||
const tableQuery = (query, tblName) => `{ | ||
const trx = await this.knex.transaction() | ||
const trx:any = await this.knex.transaction() | ||
trx['$Driver'] = this.$Driver | ||
@@ -108,3 +108,3 @@ try { | ||
return rtn | ||
} catch (err) { | ||
} catch (err:any) { | ||
await trx.rollback() | ||
@@ -127,3 +127,3 @@ return { error: err.toString() } | ||
raw: async (args: string): Promise<{ data?: unknown; error?: string }> => { | ||
const trx = await this.knex.transaction() | ||
const trx:any = await this.knex.transaction() | ||
trx['$Driver'] = this.$Driver | ||
@@ -140,3 +140,3 @@ try { | ||
return { data } | ||
} catch (e) { | ||
} catch (e:any) { | ||
await trx.rollback() | ||
@@ -154,3 +154,3 @@ return { error: e.toString() } | ||
private $Driver = '${sqlDiff.keyword.fullName}' | ||
private knex: Knex < any, unknown[] > | ||
private knex: Knex | ||
constructor() { | ||
@@ -196,6 +196,6 @@ let connection = process.env['${config.connectOption.envConnect || ''}'] || ${JSON.stringify(config.connectOption.connect, null, 2)} | ||
return await reqBody.method | ||
.reduce((_, v) => _[v], { ${dbs.toString()} }[reqBody.dbVar]) | ||
.reduce((_:any, v: any) => _[v], { ${dbs.toString()} }[reqBody.dbVar]) | ||
[reqBody.query](reqBody.args) | ||
case 'raw': | ||
return await { ${dbs.toString()} }[reqBody.dbVar].$.raw(reqBody.args) | ||
return await { ${dbs.toString()} }[reqBody.dbVar]?.$.raw(reqBody.args) | ||
} | ||
@@ -237,3 +237,14 @@ } | ||
const apiStr = (arr) => arr | ||
.map((v) => `$.${v} = new $Request(fetch, url) | ||
.map((v) => ` | ||
$.${v} = new $Request(url) | ||
$.${v}.interceptors.request((init:RequestInit) => { | ||
// 请根据需求编写请求拦截代码 | ||
console.log(init) | ||
return init | ||
}) | ||
$.${v}.interceptors.response((rtn:any) => { | ||
// 请根据需求编写响应拦截代码 | ||
console.log(rtn) | ||
return rtn | ||
}) | ||
`) | ||
@@ -243,3 +254,2 @@ .join('\n\n'); | ||
// This file is generated only once. Deleting this file will regenerate it | ||
import fetch from 'axios' | ||
import { $, $Request } from './aca' | ||
@@ -254,3 +264,3 @@ | ||
${apiStr(RPCs.map((v) => `$RPC.${v}`))} | ||
export type { $Enum, $TbOper, $TB } from './aca' | ||
export type { $Enum } from './aca' | ||
export { $RPC, ${dbs.join(', ')} } from './aca' | ||
@@ -278,2 +288,6 @@ `; | ||
}, | ||
interceptors: { | ||
request: (callback: (init: RequestInit) => RequestInit) => {}, | ||
response: (callback: any) => {}, | ||
} | ||
}`; |
@@ -7,3 +7,3 @@ "use strict"; | ||
import './App.css' | ||
import { Blog, $RPC, $Enum, $TB, $TbOper } from './aca.client' | ||
import { Blog, $RPC } from './aca.client' | ||
@@ -10,0 +10,0 @@ function App() { |
{ | ||
"name": "aca.ts", | ||
"version": "0.0.69", | ||
"version": "0.0.70", | ||
"description": "Aca.ts is a CLI based on Typescript and Knex.js. It can automatically sync database schema, automatically generate APIs for accessing the database, automatically create frontend APIs through backend functions (like RPC).", | ||
@@ -8,15 +8,6 @@ "keywords": [ | ||
"ts", | ||
"next.js", | ||
"nuxt.js", | ||
"graphql", | ||
"apollo", | ||
"orm", | ||
"database", | ||
"fullstack", | ||
"framework", | ||
"orm", | ||
"serverless", | ||
"prisma", | ||
"typeorm", | ||
"sequelize", | ||
"pg" | ||
"serverless" | ||
], | ||
@@ -23,0 +14,0 @@ "author": { |
@@ -1,70 +0,2 @@ | ||
const Insert = (Tb, X) => `{[K in keyof Omit<$TB[${Tb}], ${X} | $Auto<${Tb}>>]: Omit<$TB[${Tb}], ${X} | $Auto<${Tb}>>[K]}` | ||
const Update = (Tb, X, S) => ` | ||
{ | ||
[K in Exclude< | ||
keyof $TB[${Tb}], | ||
(${S} extends 'M2M' ? never : ${X}) | $Auto<${Tb}> | ||
>]?: NonNullable<$TB[${Tb}][K]> extends $Relation<infer R, infer R2, infer R3> | ||
? NonNullable<$TB[${Tb}][K]> extends $Relation<never, never, never> | ||
? $TB[${Tb}][K] | ||
: 'toOne' extends R3 | ||
? Omit< | ||
$UpdateToOne<R, R2, R3>, | ||
undefined extends $TB[${Tb}][K] | ||
? never | ||
: | ||
| 'upsert' | ||
| ('toOne' extends ${S} | ||
? undefined extends $TB[R][R2] | ||
? 'disconnect' | ||
: never | ||
: never) | ||
> | ||
: $UpdateToMany<R, R2, R3> | ||
: number extends $TB[${Tb}][K] | ||
? | ||
| $TB[${Tb}][K] | ||
| { | ||
increment?: number | ||
decrement?: number | ||
} | ||
: $TB[${Tb}][K] | ||
} | ||
` | ||
const UpdateMany = (Tb) => ` | ||
{ | ||
[K in keyof Pick< | ||
$TB[${Tb}], | ||
Exclude<$ScalarColumns<${Tb}>, $Auto<${Tb}>> | ||
>]?: number extends Pick<$TB[${Tb}], $ScalarColumns<${Tb}>>[K] | ||
? | ||
| Pick<$TB[${Tb}], $ScalarColumns<${Tb}>>[K] | ||
| { | ||
increment?: number | ||
decrement?: number | ||
} | ||
: Pick<$TB[${Tb}], $ScalarColumns<${Tb}>>[K] | ||
} | ||
` | ||
const Select = (Tb, X, S) => ` | ||
| { | ||
'*'?: true | ||
} & { | ||
[K in keyof Omit<$TB[${Tb}], ${S} extends 'M2M' ? never : ${X}>]?: NonNullable< | ||
$TB[${Tb}][K] | ||
> extends $Relation<infer R, infer R2, infer R3> | ||
? NonNullable<$TB[${Tb}][K]> extends $Relation<never, never, never> | ||
? true | ||
: | ||
| $Select<R, R2, R3> | ||
| ('toOne' extends R3 | ||
? { | ||
select?: $Select<R, R2, R3> | ||
} | ||
: $FindMany<R, R2, R3>) | ||
: true | ||
} | ||
` | ||
module.exports = `\n | ||
@@ -79,6 +11,2 @@ type $EnumKeys = keyof $Enum | ||
type $ToManyFilter = 'every' | 'some' | 'none' | ||
type $RelationSign = 'toOne' | 'toMany' | 'M2M' | ||
const $FindQuery = { | ||
@@ -114,314 +42,3 @@ findOne: true, | ||
// omit key's never type | ||
type $OmitNever<T> = Pick< | ||
T, | ||
{ | ||
[K in keyof Required<T>]: Required<T>[K] extends never ? never : K | ||
}[keyof T] | ||
> | ||
type $Auto<Tb extends keyof $TB> = $FK[Tb] | $CU[Tb] | ||
type $Relation< | ||
Tb extends keyof $TB, | ||
X extends Exclude<keyof $TB[Tb], $ScalarColumns<Tb>>, | ||
S extends $RelationSign | ||
> = | ||
| { | ||
insert: S extends 'toOne' | ||
? ${Insert('Tb', 'X')} | ||
: | ||
| ${Insert('Tb', "(S extends 'M2M' ? never : X)")} | ||
| ${Insert('Tb', "(S extends 'M2M' ? never : X)")}[] | ||
} | ||
| { | ||
connect: S extends 'toOne' | ||
? undefined extends $TB[Tb][X] | ||
? $UN[Tb] | ||
: never | ||
: $UN[Tb] | $UN[Tb][] | ||
} | ||
type $ScalarColumns<Tb extends keyof $TB> = NonNullable< | ||
{ | ||
[K in keyof $TB[Tb]]: NonNullable<$TB[Tb][K]> extends $Relation< | ||
any, | ||
any, | ||
any | ||
> | ||
? never | ||
: K | ||
}[keyof $TB[Tb]] | ||
> | ||
type $SimpleScalarColumns<Tb extends keyof $TB> = { | ||
[K in $ScalarColumns<Tb>]: NonNullable<$TB[Tb][K]> extends | ||
| boolean | ||
| number | ||
| bigint | ||
| Date | ||
| string | ||
? K | ||
: never | ||
}[$ScalarColumns<Tb>] | ||
type $ArrayScalarColumns<Tb extends keyof $TB> = { | ||
[K in $ScalarColumns<Tb>]: NonNullable<$TB[Tb][K]> extends any[] ? K : never | ||
}[$ScalarColumns<Tb>] | ||
type $JsonScalarColumns<Tb extends keyof $TB> = { | ||
[K in $ScalarColumns<Tb>]: object extends $TB[Tb][K] ? K : never | ||
}[$ScalarColumns<Tb>] | ||
type $RelationColumns< | ||
Tb extends keyof $TB, | ||
X extends Exclude<keyof $TB[Tb], $ScalarColumns<Tb>>, | ||
S extends $RelationSign | ||
> = Exclude<keyof $TB[Tb], $ScalarColumns<Tb> | ('M2M' extends S ? never : X)> | ||
type $SimpleScalarAllFilter<T> = { | ||
eq?: NonNullable<T> | (undefined extends T ? null : never) | ||
lt?: NonNullable<T> extends boolean | $EnumKeys ? never : NonNullable<T> | ||
gt?: NonNullable<T> extends boolean | $EnumKeys ? never : NonNullable<T> | ||
lte?: NonNullable<T> extends boolean | $EnumKeys ? never : NonNullable<T> | ||
gte?: NonNullable<T> extends boolean | $EnumKeys ? never : NonNullable<T> | ||
like?: NonNullable<T> extends string ? NonNullable<T> : never | ||
in?: | ||
| (NonNullable<T> extends $EnumKeys ? Array<NonNullable<T>> : never) | ||
| (NonNullable<T> extends boolean ? never : Array<NonNullable<T>>) | ||
between?: NonNullable<T> extends boolean | $EnumKeys | ||
? never | ||
: [NonNullable<T>, NonNullable<T>?] | ||
contains?: NonNullable<T> extends string ? NonNullable<T> : never | ||
startsWith?: NonNullable<T> extends string ? NonNullable<T> : never | ||
endsWith?: NonNullable<T> extends string ? NonNullable<T> : never | ||
} | ||
// applicable field of not | ||
type $SimpleScalarNotFilter<T> = { | ||
not?: | ||
| $SimpleScalarAllFilter<T>['eq'] | ||
| Pick< | ||
$SimpleScalarAllFilter<T>, | ||
| 'eq' | ||
| (boolean extends T | ||
? never | ||
: | ||
| 'in' | ||
| (T extends $EnumKeys | ||
? never | ||
: | ||
| 'lt' | ||
| 'gt' | ||
| 'lte' | ||
| 'gte' | ||
| (string extends T ? 'like' : 'between'))) | ||
> | ||
} | ||
type $SimpleScalarFilter<T> = $SimpleScalarNotFilter<T> & | ||
$OmitNever<$SimpleScalarAllFilter<T>> | ||
// Used in relationship | ||
type $Where< | ||
Tb extends keyof $TB, | ||
X extends keyof $TB[Tb], | ||
S extends $RelationSign | ||
> = { | ||
[K in Exclude<keyof $TB[Tb], S extends 'M2M' ? never : X>]?: | ||
| ($TB[Tb][K] extends $NL[Tb] ? null : never) | ||
| (K extends $SimpleScalarColumns<Tb> | ||
? $TB[Tb][K] | $SimpleScalarFilter<$TB[Tb][K]> | ||
: NonNullable<$TB[Tb][K]> extends $Relation<infer R, infer R2, infer R3> | ||
? 'toOne' extends R3 | ||
? $Where<R, R2, R3> | ||
: { | ||
[K2 in $ToManyFilter]?: $Where<R, R2, R3> | ||
} | ||
: never) | ||
} & { | ||
[L in $WhereLogic]?: | ||
| $Where<Tb, X, S>[] | ||
| (L extends 'AND' | 'NOT' ? $Where<Tb, X, S> : never) | ||
} | ||
// Used in relationship | ||
type $Select< | ||
Tb extends keyof $TB, | ||
X extends keyof $TB[Tb], | ||
S extends $RelationSign | ||
> = | ||
${Select('Tb', 'X', 'S')} | ||
type $FindMany< | ||
Tb extends keyof $TB, | ||
X extends keyof $TB[Tb], | ||
S extends $RelationSign | ||
> = { | ||
where?: $Where<Tb, X, S> | ||
distinct?: '*' | $Enumerable<$ScalarColumns<Tb>> | ||
orderBy?: $Enumerable<{ [K in $ScalarColumns<Tb>]?: $Order }> | ||
limit?: number | ||
offset?: number | ||
select?: ${Select('Tb', 'X', 'S')} | ||
} | ||
type $NumberUpdateOperator = { | ||
increment?: number | ||
decrement?: number | ||
} | ||
type $UpdateToOne< | ||
RTb extends keyof $TB, | ||
RX extends keyof $TB[RTb], | ||
RS extends $RelationSign | ||
> = { | ||
insert?: ${Insert('RTb', 'RX')} | ||
update?: Partial<Omit<$TB[RTb], RX | $Auto<RTb>>> | ||
upsert?: { | ||
insert: ${Insert('RTb', 'RX')} | ||
update?: ${Update('RTb', 'RX', 'RS')} | ||
} | ||
delete?: true | ||
connect?: $UN[RTb] | ||
disconnect?: true | ||
} | ||
type $UpdateToMany< | ||
RTb extends keyof $TB, | ||
RX extends keyof $TB[RTb], | ||
RS extends $RelationSign | ||
> = { | ||
set?: $Enumerable< | ||
| Omit<$TB[RTb], RX | $Auto<RTb>> | ||
| (undefined extends $TB[RTb][RX] ? $UN[RTb] : never) | ||
> | ||
insert?: $Enumerable<${Insert('RTb', 'RX')}> | ||
upsert?: $Enumerable<{ | ||
where: $UN[RTb] | ||
insert: ${Insert('RTb', 'RX')} | ||
update?: ${Update('RTb', 'RX', 'RS')} | ||
}> | ||
update?: $Enumerable<{ | ||
where: $UN[RTb] | ||
data: ${Update('RTb', 'RX', 'RS')} | ||
}> | ||
updateMany?: $UpdateMany<RTb, RX, RS> | ||
delete?: $UN[RTb] | $UN[RTb][] | ||
deleteMany?: $Where<RTb, RX, RS> | ||
connect?: $UN[RTb] | $UN[RTb][] | ||
disconnect?: $UN[RTb] | $UN[RTb][] | ||
} | ||
type $UpdateMany< | ||
Tb extends keyof $TB, | ||
X extends keyof $TB[Tb], | ||
S extends $RelationSign | ||
> = { | ||
where?: $Where<Tb, X, S> | ||
data: ${UpdateMany('Tb')} | ||
} | ||
export type $TbOper<Tb extends keyof $TB> = { | ||
unique: $UN[Tb] | ||
where: $Where<Tb, never, never> | ||
insert: ${Insert('Tb', 'never')} | ||
update: ${Update('Tb', 'never', 'never')} | ||
updateMany: ${UpdateMany('Tb')} | ||
select: ${Select('Tb', 'never', 'never')} | ||
selectScalar: Pick<$TbOper<Tb>['select'], $ScalarColumns<Tb>> | ||
} | ||
type $TableQuery<Tb extends keyof $TB> = { | ||
findOne: { | ||
where: $TbOper<Tb>['unique'] | ||
select?: $TbOper<Tb>['select'] | ||
sql?: true | ||
} | ||
findFirst?: { | ||
where?: $TbOper<Tb>['where'] | ||
select?: $TbOper<Tb>['select'] | ||
sql?: true | ||
orderBy?: $Enumerable<{ [K in $ScalarColumns<Tb>]?: $Order }> | ||
} | ||
findMany?: { | ||
where?: $TbOper<Tb>['where'] | ||
select?: $TbOper<Tb>['select'] | ||
distinct?: '*' | $Enumerable<$ScalarColumns<Tb>> | ||
limit?: number | ||
offset?: number | ||
sql?: boolean | ||
orderBy?: $Enumerable<{ [K in $ScalarColumns<Tb>]?: $Order }> | ||
} | ||
insert: { | ||
data: $TbOper<Tb>['insert'] | $TbOper<Tb>['insert'][] | ||
select?: $TbOper<Tb>['select'] | ||
sql?: true | ||
} | ||
upsert: { | ||
where: $UN[Tb] | ||
insert: $TbOper<Tb>['insert'] | ||
update?: $TbOper<Tb>['update'] | ||
select?: $TbOper<Tb>['select'] | ||
sql?: true | ||
} | ||
update: { | ||
where: $UN[Tb] | ||
data: $TbOper<Tb>['update'] | ||
select?: $TbOper<Tb>['select'] | ||
sql?: true | ||
} | ||
updateMany?: { | ||
where?: $TbOper<Tb>['where'] | ||
data: $TbOper<Tb>['updateMany'] | ||
select?: $TbOper<Tb>['selectScalar'] | ||
sql?: true | ||
} | ||
delete: { | ||
where: $UN[Tb] | ||
select?: $TbOper<Tb>['selectScalar'] | ||
sql?: true | ||
} | ||
deleteMany?: { | ||
where: $TbOper<Tb>['where'] | ||
select?: $TbOper<Tb>['selectScalar'] | ||
sql?: true | ||
} | ||
groupBy?: { | ||
by: $ScalarColumns<Tb> | ||
aggregate?: { | ||
name: string | ||
column: $ScalarColumns<Tb> | ||
function: $AggregateQuery | ||
}[] | ||
where?: $TbOper<Tb>['where'] | ||
having?: $TbOper<Tb>['where'] | ||
} | ||
join?: { | ||
table: keyof $TB | ||
method: 'left' | 'right' | 'inner' | 'outer' | 'cross' | ||
on: { | ||
columns: $ScalarColumns<Tb> | ||
tableColumns: $ScalarColumns<Tb> | ||
} | ||
select: $TbOper<Tb>['select'] | ||
tableSelect: $TbOper<Tb>['where'] | ||
} | ||
aggregate?: { | ||
select: { | ||
[K in keyof Required<$TB[Tb]>]?: number extends $TB[Tb][K] ? K : never | ||
}[keyof $TB[Tb]] | ||
where?: $TbOper<Tb>['where'] | ||
sql?: true | ||
} | ||
} | ||
type $TableMutationType< | ||
Tb extends keyof $TB, | ||
query extends $MutationQuery | ||
> = Omit<$TableQuery<Tb>[query], 'select'> & { | ||
select?: $TbOper<Tb>['selectScalar'] | ||
} | ||
export type $ApiBridge = | ||
@@ -446,25 +63,45 @@ | { | ||
type $stringFilter = { | ||
eq?: string | null | ||
in?: $Enumerable<string> | null | ||
notIn?: $Enumerable<string> | null | ||
type $StringFilter<TNull = true> = { | ||
eq?: TNull extends true ? string | null : string | ||
in?: TNull extends true ? Array<string> | null : Array<string> | ||
notIn?: TNull extends true ? Array<string> | null : Array<string> | ||
not?: TNull extends true | ||
? $StringFilter<TNull> | string | null | ||
: $StringFilter<TNull> | string | ||
contains?: string | ||
startsWith?: string | ||
endsWith?: string | ||
not?: $stringFilter | string | null | ||
like?: string | ||
between?: $Enumerable<string> | ||
between?: Array<string> | ||
lt?: string | ||
lte?: string | ||
gt?: string | ||
gte?: string | ||
} | ||
type $enumFilter<T> = { | ||
eq?: T | ||
in?: $Enumerable<T> | ||
notIn?: $Enumerable<T> | ||
not?: $enumFilter<T> | T | ||
type $EnumFilter<T, TNull = true> = { | ||
eq?: TNull extends true ? T | null : T | ||
in?: TNull extends true ? Array<T> | null : Array<T> | ||
notIn?: TNull extends true ? Array<T> | null : Array<T> | ||
not?: TNull extends true | ||
? $EnumFilter<T, TNull> | T | null | ||
: $EnumFilter<T, TNull> | T | ||
contains?: string | ||
startsWith?: string | ||
endsWith?: string | ||
like?: string | ||
between?: Array<string> | ||
lt?: string | ||
lte?: string | ||
gt?: string | ||
gte?: string | ||
} | ||
type $numberFilter = { | ||
eq?: number | null | ||
in?: $Enumerable<number> | null | ||
notIn?: $Enumerable<number> | null | ||
type $NumberFilter<TNull = true> = { | ||
eq?: TNull extends true ? number | null : number | ||
in?: TNull extends true ? Array<number> | null : Array<number> | ||
notIn?: TNull extends true ? Array<number> | null : Array<number> | ||
not?: TNull extends true | ||
? $NumberFilter<TNull> | number | null | ||
: $NumberFilter<TNull> | number | ||
lt?: number | ||
@@ -474,22 +111,31 @@ lte?: number | ||
gte?: number | ||
not?: $numberFilter | number | null | ||
like?: number | ||
between?: $Enumerable<number> | ||
between?: Array<number> | ||
} | ||
type $DateFilter = { | ||
eq?: Date | string | ||
in?: $Enumerable<Date> | $Enumerable<string> | ||
notIn?: $Enumerable<Date> | $Enumerable<string> | ||
lt?: Date | string | ||
lte?: Date | string | ||
gt?: Date | string | ||
gte?: Date | string | ||
not?: $DateFilter | Date | string | ||
like?: Date | string | ||
between?: $Enumerable<Date> | $Enumerable<Date> | ||
type $DateFilter<TNull = true> = { | ||
eq?: TNull extends true ? Date | null : Date | ||
in?: TNull extends true ? Array<Date> | null : Array<Date> | ||
notIn?: TNull extends true ? Array<Date> | null : Array<Date> | ||
not?: TNull extends true | ||
? $DateFilter<TNull> | Date | null | ||
: $DateFilter<TNull> | Date | ||
lt?: Date | ||
lte?: Date | ||
gt?: Date | ||
gte?: Date | ||
like?: Date | ||
between?: Array<Date> | ||
} | ||
type $booleanFilter = { | ||
eq?: boolean | ||
not?: $booleanFilter | boolean | ||
type $BooleanFilter<TNull = true> = { | ||
eq?: TNull extends true ? boolean | null : boolean | ||
not?: TNull extends true | ||
? $BooleanFilter<TNull> | boolean | null | ||
: $BooleanFilter<TNull> | boolean | ||
} | ||
type $ObjectFilter<TNull = true> = { | ||
eq?: TNull extends true ? string | null : string | ||
not?: TNull extends true ? Array<string> | null : Array<string> | ||
} | ||
` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
2
259920
4472
30