Comparing version 0.0.90 to 0.0.91
@@ -52,6 +52,6 @@ "use strict"; | ||
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) | ||
? 'number' | ||
const query = (Q) => `async (args${Cst.argOpts.includes(Q) ? '?' : ''}: ${tblType}.${Cst.aggregates.includes(Q) ? `$.${Q}` : Q === 'delete' ? 'del' : Q}): Promise<{data?: ${Q === 'aggregate' | ||
? `${tblType}.aggregateReturning` | ||
: ['deleteMany', 'updateMany'].includes(Q) | ||
? 'number' | ||
? `Array<{[P in keyof ${tblType}.scalar]?: ${tblType}.scalar[P]}>` | ||
: `${'findMany' === Q | ||
@@ -68,5 +68,3 @@ ? `Array<{[P in keyof ${tblType}]?: ${tblType}[P]}>` | ||
}`; | ||
return Cst.queries | ||
.map((v) => `${v}: ${v === '$' ? aggr() : query(v)}`) | ||
.join(`,\n`); | ||
return Cst.queries.map((v) => `${v}: ${v === '$' ? aggr() : query(v)}`).join(`,\n`); | ||
}; | ||
@@ -77,10 +75,7 @@ const tblIter = (subTbls) => Object.keys(subTbls).reduce((_, v) => { | ||
}, []); | ||
const tblApi = (tbl, key, spt) => `${tbl.props?.deprecated | ||
? (0, common_1.Deprecated)(tbl.props.deprecated) | ||
: ``}${key} ${spt} { | ||
const tblApi = (tbl, key, spt) => `${tbl.props?.deprecated ? (0, common_1.Deprecated)(tbl.props.deprecated) : ``}${key} ${spt} { | ||
${typeof tbl.kind === 'string' ? TblQuery(tbl) : tblIter(tbl)} | ||
}`; | ||
return Object.keys(tables) | ||
.reduce((_, v) => (_.push(tblApi(tables[v], v, api.startsWith('transaction') ? ':' : '=')), | ||
_), []) | ||
.reduce((_, v) => (_.push(tblApi(tables[v], v, api.startsWith('transaction') ? ':' : '=')), _), []) | ||
.join(`${api.startsWith('transaction') ? ',' : ''}\n\n`); | ||
@@ -122,3 +117,3 @@ }; | ||
foreign: {}, | ||
table: {}, | ||
table: {} | ||
}; | ||
@@ -139,3 +134,3 @@ for (const k in tables) { | ||
id: tbl.id, | ||
uniques: tbl.uniques, | ||
uniques: tbl.uniques | ||
}); | ||
@@ -147,5 +142,3 @@ tbl.uniques.forEach((v) => { | ||
else { | ||
typeDefine.unique[tbl.jsName].push(`{${v | ||
.map((v2) => ` ${v2}: $TB['${tbl.jsName}']['${v2}']`) | ||
.join('\n')}}`); | ||
typeDefine.unique[tbl.jsName].push(`{${v.map((v2) => ` ${v2}: $TB['${tbl.jsName}']['${v2}']`).join('\n')}}`); | ||
} | ||
@@ -159,3 +152,3 @@ }); | ||
Att[tbl.jsName].columns[colName] = Object.assign(Att[tbl.jsName].columns[colName] || {}, { | ||
name: colName, | ||
name: colName | ||
}); | ||
@@ -168,3 +161,3 @@ if (relColOpt) { | ||
type: typeTbl, | ||
optional: col.optional, | ||
optional: col.optional | ||
}); | ||
@@ -174,3 +167,3 @@ Att[relTbl.jsName].columns[relColName] = { | ||
type: tbl.jsName, | ||
optional: relCol.optional, | ||
optional: relCol.optional | ||
}; | ||
@@ -185,3 +178,3 @@ if (col.props.foreign) { | ||
keys: col.props.foreign.keys, | ||
references: col.props.foreign.references, | ||
references: col.props.foreign.references | ||
}; | ||
@@ -194,3 +187,3 @@ typeDefine.table[typeTbl].push(`${relColName}${'required' !== relCol.optional ? '?' : ''}: $Relation<'${tbl.jsName}', '${colName}', '${'array' === relCol.optional ? 'toMany' : 'toOne'}'>`); | ||
keys: col.props.foreign.keys, | ||
references: col.props.foreign.references, | ||
references: col.props.foreign.references | ||
}; | ||
@@ -206,4 +199,4 @@ } | ||
relColumn: relColName, | ||
mapTable, | ||
}, | ||
mapTable | ||
} | ||
}); | ||
@@ -219,3 +212,3 @@ } | ||
dbType: col.props.dbType, | ||
optional: col.optional, | ||
optional: col.optional | ||
}); | ||
@@ -283,4 +276,3 @@ switch (col.type) { | ||
`NOT?: $Enumerable<where>`, | ||
...fields | ||
.map((v) => `${v.fieldName}?: ${v.isRelation | ||
...fields.map((v) => `${v.fieldName}?: ${v.isRelation | ||
? v.isArray | ||
@@ -296,4 +288,3 @@ ? `{ | ||
: `$${v.fieldType === 'Date | string' ? 'Date' : titleCase(v.fieldType)}Filter<${v.isNull}>`} | ${v.fieldType} ${v.isNull ? ' | null' : ''}`}`) | ||
] | ||
.join('\n'); | ||
].join('\n'); | ||
case 'select': | ||
@@ -317,2 +308,48 @@ return ` | ||
`; | ||
case 'aggregateReturning': | ||
return ` | ||
count?: {[P in keyof scalar]?: number} | ||
countDistinct?: {[P in keyof scalar]?: number} | ||
${fields.filter((v) => v.fieldType === 'number').length | ||
? ` | ||
sum?: { | ||
${fields | ||
.filter((v) => v.fieldType === 'number') | ||
.map((v) => `${v.fieldName}?: number`) | ||
.join('\n')} | ||
} | ||
sumDistinct?: { | ||
${fields | ||
.filter((v) => v.fieldType === 'number') | ||
.map((v) => `${v.fieldName}?: number`) | ||
.join('\n')} | ||
} | ||
avg?: { | ||
${fields | ||
.filter((v) => v.fieldType === 'number') | ||
.map((v) => `${v.fieldName}?: number`) | ||
.join('\n')} | ||
} | ||
avgDistinct?: { | ||
${fields | ||
.filter((v) => v.fieldType === 'number') | ||
.map((v) => `${v.fieldName}?: number`) | ||
.join('\n')} | ||
} | ||
max?: { | ||
${fields | ||
.filter((v) => v.fieldType === 'number') | ||
.map((v) => `${v.fieldName}?: number`) | ||
.join('\n')} | ||
} | ||
min?: { | ||
${fields | ||
.filter((v) => v.fieldType === 'number') | ||
.map((v) => `${v.fieldName}?: number`) | ||
.join('\n')} | ||
} | ||
` | ||
: ''} | ||
sql?: boolean | ||
`; | ||
case 'insertInput': | ||
@@ -328,5 +365,12 @@ return ` | ||
: `Omit<${v.fieldType}.insertInput, ${v.relationKeys}>`} | ||
connect?: ${v.isArray | ||
? `$Enumerable<${v.fieldType}.uniqueWhere>` | ||
: `${v.fieldType}.uniqueWhere`} | ||
connect?: ${v.isArray ? `$Enumerable<${v.fieldType}.uniqueWhere>` : `${v.fieldType}.uniqueWhere`} | ||
connectOrInsert?: ${v.isArray | ||
? `$Enumerable<{ | ||
connect: ${v.fieldType}.uniqueWhere | ||
insert: Omit<${v.fieldType}.insertInput, ${v.relationKeys}> | ||
}>` | ||
: `{ | ||
connect: ${v.fieldType}.uniqueWhere | ||
insert: Omit<${v.fieldType}.insertInput, ${v.relationKeys}> | ||
}`} | ||
}` | ||
@@ -345,11 +389,20 @@ : v.fieldType}`) | ||
: `Omit<${v.fieldType}.insertInput, ${v.relationKeys}>`} | ||
connect?: ${v.isArray | ||
? `$Enumerable<${v.fieldType}.uniqueWhere>` | ||
: `${v.fieldType}.uniqueWhere`} | ||
connect?: ${v.isArray ? `$Enumerable<${v.fieldType}.uniqueWhere>` : `${v.fieldType}.uniqueWhere`} | ||
connectOrInsert?: ${v.isArray | ||
? `$Enumerable<{ | ||
connect: ${v.fieldType}.uniqueWhere | ||
insert: Omit<${v.fieldType}.insertInput, ${v.relationKeys}> | ||
}>` | ||
: `{ | ||
connect: ${v.fieldType}.uniqueWhere | ||
insert: Omit<${v.fieldType}.insertInput, ${v.relationKeys}> | ||
}`} | ||
disconnect?: ${v.isArray | ||
? `$Enumerable<${v.fieldType}.uniqueWhere>` | ||
: 'boolean'} | ||
: | ||
'boolean'} | ||
delete?: ${v.isArray | ||
? `$Enumerable<${v.fieldType}.uniqueWhere>` | ||
: 'boolean'} | ||
: | ||
'boolean'} | ||
upsert?: ${v.isArray | ||
@@ -451,23 +504,47 @@ ? `$Enumerable<{ | ||
`; | ||
case 'count': | ||
case 'countDistinct': | ||
case 'aggregate': | ||
return ` | ||
where?: where | ||
select: '*' | keyof scalar | ||
sql?: boolean | ||
`; | ||
case 'sum': | ||
case 'sumDistinct': | ||
case 'avg': | ||
case 'avgDistinct': | ||
case 'max': | ||
case 'mix': | ||
return ` | ||
where?: where | ||
select: ${fields.filter((v) => v.fieldType === 'number').length | ||
? fields | ||
count?: {[P in keyof scalar]?: boolean} | ||
countDistinct?: {[P in keyof scalar]?: boolean} | ||
${fields.filter((v) => v.fieldType === 'number').length | ||
? ` | ||
sum?: { | ||
${fields | ||
.filter((v) => v.fieldType === 'number') | ||
.map((v) => `'${v.fieldName}'`) | ||
.join('|') | ||
: 'never'} | ||
.map((v) => `${v.fieldName}?: boolean`) | ||
.join('\n')} | ||
} | ||
sumDistinct?: { | ||
${fields | ||
.filter((v) => v.fieldType === 'number') | ||
.map((v) => `${v.fieldName}?: boolean`) | ||
.join('\n')} | ||
} | ||
avg?: { | ||
${fields | ||
.filter((v) => v.fieldType === 'number') | ||
.map((v) => `${v.fieldName}?: boolean`) | ||
.join('\n')} | ||
} | ||
avgDistinct?: { | ||
${fields | ||
.filter((v) => v.fieldType === 'number') | ||
.map((v) => `${v.fieldName}?: boolean`) | ||
.join('\n')} | ||
} | ||
max?: { | ||
${fields | ||
.filter((v) => v.fieldType === 'number') | ||
.map((v) => `${v.fieldName}?: boolean`) | ||
.join('\n')} | ||
} | ||
min?: { | ||
${fields | ||
.filter((v) => v.fieldType === 'number') | ||
.map((v) => `${v.fieldName}?: boolean`) | ||
.join('\n')} | ||
} | ||
` | ||
: ''} | ||
sql?: boolean | ||
@@ -490,3 +567,3 @@ `; | ||
.join(' | '); | ||
const queries = ['where', 'scalar', 'orderBy', 'select', 'insertInput', 'updateInput', ...Cst.queries] | ||
const queries = ['where', 'scalar', 'orderBy', 'select', 'aggregateReturning', 'insertInput', 'updateInput', ...Cst.queries] | ||
.map((v) => `export ${v === '$' ? 'namespace' : 'type'} ${v === 'delete' ? 'del' : v} ${v === '$' ? '' : '='} { | ||
@@ -517,5 +594,3 @@ ${v === '$' ? aggr() : query(v)} | ||
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' ? '' : '?', | ||
@@ -528,3 +603,3 @@ isRelation: false, | ||
relationKeys: 'never', | ||
OmitSelectKeys: 'never', | ||
OmitSelectKeys: 'never' | ||
}; | ||
@@ -547,6 +622,4 @@ if (col.props.createdAt || col.props.updatedAt) { | ||
columns[col.jsName].fieldType = col.type.substring(0, col.type.lastIndexOf('.')); | ||
columns[col.jsName].isArray = | ||
relColOpt.endsWith(']') || col.optional === 'array' ? '[]' : ''; | ||
columns[col.jsName].required = | ||
relColOpt.endsWith(']') || col.optional !== 'required' ? '?' : ''; | ||
columns[col.jsName].isArray = relColOpt.endsWith(']') || col.optional === 'array' ? '[]' : ''; | ||
columns[col.jsName].required = relColOpt.endsWith(']') || col.optional !== 'required' ? '?' : ''; | ||
columns[col.jsName].relationKeys = `'${relColOpt.match(/^\w+/)[0]}'`; | ||
@@ -562,5 +635,3 @@ if (col.props.foreign) { | ||
} | ||
if (columns[col.jsName].fieldType | ||
.split('.') | ||
.reduce((_, v) => (_.columns ? _.columns[v] : _[v]), tables)) { | ||
if (columns[col.jsName].fieldType.split('.').reduce((_, v) => (_.columns ? _.columns[v] : _[v]), tables)) { | ||
const relationTbl = columns[col.jsName].fieldType | ||
@@ -591,5 +662,3 @@ .split('.') | ||
export namespace ${key} { | ||
${typeof tbl.kind === 'string' | ||
? TblType(columns, tbl.uniques) | ||
: tblIter(tbl)} | ||
${typeof tbl.kind === 'string' ? TblType(columns, tbl.uniques) : tblIter(tbl)} | ||
}`; | ||
@@ -613,3 +682,3 @@ }; | ||
CU: [], | ||
anno: {}, | ||
anno: {} | ||
}; | ||
@@ -632,4 +701,3 @@ for (const k in ast.dbs) { | ||
for (const k3 in orm.typeDefine[v2]) { | ||
tmp.push(` ${k3}: ${orm.typeDefine[v2][k3].map((v3) => `'${v3}'`).join(' | ') || | ||
'never'}`); | ||
tmp.push(` ${k3}: ${orm.typeDefine[v2][k3].map((v3) => `'${v3}'`).join(' | ') || 'never'}`); | ||
} | ||
@@ -644,5 +712,3 @@ dbType[{ foreign: 'FK', onTime: 'CU' }[v2]].push(...tmp); | ||
serverApi += fs_1.default.readFileSync(path_1.default.join(__dirname, `${templatePath}/handle-server`), 'utf-8'); | ||
clientApi += | ||
'\n\n' + | ||
fs_1.default.readFileSync(path_1.default.join(__dirname, `${templatePath}/client-request`), 'utf-8'); | ||
clientApi += '\n\n' + fs_1.default.readFileSync(path_1.default.join(__dirname, `${templatePath}/client-request`), 'utf-8'); | ||
for (const k in ast.dbs) { | ||
@@ -674,4 +740,3 @@ const db = ast.dbs[k]; | ||
const serverConfig = config.serverApps[k]; | ||
const resolveApiDir = path_1.default.join(resolveAcaDir, k, serverConfig.apiDir ?? | ||
path_1.default.join(Cst.DefaultTsDir, Cst.DefaultServerApiDir)); | ||
const resolveApiDir = path_1.default.join(resolveAcaDir, k, serverConfig.apiDir ?? path_1.default.join(Cst.DefaultTsDir, Cst.DefaultServerApiDir)); | ||
const apiIndex = path_1.default.join(resolveApiDir, Cst.ApiIndex); | ||
@@ -687,3 +752,3 @@ const RPCDir = path_1.default.join(resolveApiDir, Cst.ServerRPCDir); | ||
const allowRPCs = clientApps[k]?.allowRPCs || Object.keys(serverApps); | ||
const RPCs = allowRPCs.filter((v) => clientRPCApis[v] !== undefined ? true : false); | ||
const RPCs = allowRPCs.filter((v) => (clientRPCApis[v] !== undefined ? true : false)); | ||
const RPCApis = RPCs.map((v) => (clientRPCApis[v] ? nsRPCTpl(v) : '')).join('\n\n'); | ||
@@ -701,4 +766,3 @@ const reqInstance = () => { | ||
`; | ||
const apiDir = path_1.default.join(k, clientConfig.apiDir ?? | ||
path_1.default.join(Cst.DefaultTsDir, Cst.DefaultClientApiDir)); | ||
const apiDir = path_1.default.join(k, clientConfig.apiDir ?? path_1.default.join(Cst.DefaultTsDir, Cst.DefaultClientApiDir)); | ||
const api = path_1.default.join(resolveAcaDir, apiDir, Cst.ClientApi); | ||
@@ -705,0 +769,0 @@ const apiIndex = path_1.default.join(resolveAcaDir, apiDir, Cst.ClientApiIndex); |
@@ -52,4 +52,4 @@ "use strict"; | ||
'deleteMany', | ||
'$', | ||
'aggregate', | ||
]; | ||
exports.argOpts = ['findFirst', 'findMany', 'deleteMany'].concat(exports.aggregates); |
@@ -56,3 +56,3 @@ "use strict"; | ||
case typescript_1.default.SyntaxKind.TypeAliasDeclaration: | ||
rtn2 += '\n export ' + tsContent.slice(node.pos, node.end); | ||
rtn2 += '\n ' + tsContent.slice(node.pos, node.end); | ||
break; | ||
@@ -59,0 +59,0 @@ case typescript_1.default.SyntaxKind.ExportDeclaration: |
{ | ||
"name": "aca.ts", | ||
"version": "0.0.90", | ||
"version": "0.0.91", | ||
"description": "ACA.ts is a fullstack development toolkit, which can automatically synchronize database schema and generate database query APIs according to a typescript ORM document; Automatically generate the client access server APIs according to the server functions and database query APIs, so that the client code can be developed on the client.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
module.exports = `\n | ||
/* eslint-disable @typescript-eslint/no-namespace */ | ||
/* eslint-disable @typescript-eslint/no-use-before-define */ | ||
/* eslint-disable @typescript-eslint/no-unused-expressions */ | ||
/* eslint-disable @typescript-eslint/no-redeclare */ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
// @ts-nocheck | ||
type $EnumKeys = keyof $Enum | ||
@@ -4,0 +10,0 @@ |
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
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
283006
4680