agentlang 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +83 -0
- package/README.md +120 -0
- package/bin/cli.js +4 -0
- package/out/api/http.d.ts +3 -0
- package/out/api/http.d.ts.map +1 -0
- package/out/api/http.js +290 -0
- package/out/api/http.js.map +1 -0
- package/out/cli/cli-util.d.ts +7 -0
- package/out/cli/cli-util.d.ts.map +1 -0
- package/out/cli/cli-util.js +9 -0
- package/out/cli/cli-util.js.map +1 -0
- package/out/cli/docs.d.ts +2 -0
- package/out/cli/docs.d.ts.map +1 -0
- package/out/cli/docs.js +236 -0
- package/out/cli/docs.js.map +1 -0
- package/out/cli/main.d.ts +288 -0
- package/out/cli/main.d.ts.map +1 -0
- package/out/cli/main.js +119 -0
- package/out/cli/main.js.map +1 -0
- package/out/cli/openapi-docs.yml +695 -0
- package/out/extension/main.cjs +18093 -0
- package/out/extension/main.cjs.map +7 -0
- package/out/extension/main.d.ts +4 -0
- package/out/extension/main.d.ts.map +1 -0
- package/out/extension/main.js +42 -0
- package/out/extension/main.js.map +1 -0
- package/out/language/agentlang-module.d.ts +42 -0
- package/out/language/agentlang-module.d.ts.map +1 -0
- package/out/language/agentlang-module.js +42 -0
- package/out/language/agentlang-module.js.map +1 -0
- package/out/language/agentlang-validator.d.ts +15 -0
- package/out/language/agentlang-validator.d.ts.map +1 -0
- package/out/language/agentlang-validator.js +50 -0
- package/out/language/agentlang-validator.js.map +1 -0
- package/out/language/generated/ast.d.ts +491 -0
- package/out/language/generated/ast.d.ts.map +1 -0
- package/out/language/generated/ast.js +934 -0
- package/out/language/generated/ast.js.map +1 -0
- package/out/language/generated/grammar.d.ts +7 -0
- package/out/language/generated/grammar.d.ts.map +1 -0
- package/out/language/generated/grammar.js +4475 -0
- package/out/language/generated/grammar.js.map +1 -0
- package/out/language/generated/module.d.ts +14 -0
- package/out/language/generated/module.d.ts.map +1 -0
- package/out/language/generated/module.js +21 -0
- package/out/language/generated/module.js.map +1 -0
- package/out/language/main-browser.d.ts +2 -0
- package/out/language/main-browser.d.ts.map +1 -0
- package/out/language/main-browser.js +10 -0
- package/out/language/main-browser.js.map +1 -0
- package/out/language/main.cjs +36229 -0
- package/out/language/main.cjs.map +7 -0
- package/out/language/main.d.ts +2 -0
- package/out/language/main.d.ts.map +1 -0
- package/out/language/main.js +11 -0
- package/out/language/main.js.map +1 -0
- package/out/language/parser.d.ts +9 -0
- package/out/language/parser.d.ts.map +1 -0
- package/out/language/parser.js +273 -0
- package/out/language/parser.js.map +1 -0
- package/out/language/syntax.d.ts +155 -0
- package/out/language/syntax.d.ts.map +1 -0
- package/out/language/syntax.js +527 -0
- package/out/language/syntax.js.map +1 -0
- package/out/runtime/agents/common.d.ts +2 -0
- package/out/runtime/agents/common.d.ts.map +1 -0
- package/out/runtime/agents/common.js +178 -0
- package/out/runtime/agents/common.js.map +1 -0
- package/out/runtime/agents/impl/openai.d.ts +8 -0
- package/out/runtime/agents/impl/openai.d.ts.map +1 -0
- package/out/runtime/agents/impl/openai.js +15 -0
- package/out/runtime/agents/impl/openai.js.map +1 -0
- package/out/runtime/agents/provider.d.ts +21 -0
- package/out/runtime/agents/provider.d.ts.map +1 -0
- package/out/runtime/agents/provider.js +32 -0
- package/out/runtime/agents/provider.js.map +1 -0
- package/out/runtime/agents/registry.d.ts +2 -0
- package/out/runtime/agents/registry.d.ts.map +1 -0
- package/out/runtime/agents/registry.js +10 -0
- package/out/runtime/agents/registry.js.map +1 -0
- package/out/runtime/auth/cognito.d.ts +16 -0
- package/out/runtime/auth/cognito.d.ts.map +1 -0
- package/out/runtime/auth/cognito.js +186 -0
- package/out/runtime/auth/cognito.js.map +1 -0
- package/out/runtime/auth/defs.d.ts +11 -0
- package/out/runtime/auth/defs.d.ts.map +1 -0
- package/out/runtime/auth/defs.js +24 -0
- package/out/runtime/auth/defs.js.map +1 -0
- package/out/runtime/auth/interface.d.ts +22 -0
- package/out/runtime/auth/interface.d.ts.map +1 -0
- package/out/runtime/auth/interface.js +2 -0
- package/out/runtime/auth/interface.js.map +1 -0
- package/out/runtime/defs.js +24 -0
- package/out/runtime/defs.js.map +1 -0
- package/out/runtime/interpreter.d.ts +69 -0
- package/out/runtime/interpreter.d.ts.map +1 -0
- package/out/runtime/interpreter.js +1163 -0
- package/out/runtime/interpreter.js.map +1 -0
- package/out/runtime/loader.d.ts +25 -0
- package/out/runtime/loader.d.ts.map +1 -0
- package/out/runtime/loader.js +346 -0
- package/out/runtime/loader.js.map +1 -0
- package/out/runtime/logger.d.ts +2 -0
- package/out/runtime/logger.d.ts.map +1 -0
- package/out/runtime/logger.js +44 -0
- package/out/runtime/logger.js.map +1 -0
- package/out/runtime/module.d.ts +273 -0
- package/out/runtime/module.d.ts.map +1 -0
- package/out/runtime/module.js +1786 -0
- package/out/runtime/module.js.map +1 -0
- package/out/runtime/modules/ai.d.ts +26 -0
- package/out/runtime/modules/ai.d.ts.map +1 -0
- package/out/runtime/modules/ai.js +211 -0
- package/out/runtime/modules/ai.js.map +1 -0
- package/out/runtime/modules/auth.d.ts +39 -0
- package/out/runtime/modules/auth.d.ts.map +1 -0
- package/out/runtime/modules/auth.js +359 -0
- package/out/runtime/modules/auth.js.map +1 -0
- package/out/runtime/modules/core.d.ts +2 -0
- package/out/runtime/modules/core.d.ts.map +1 -0
- package/out/runtime/modules/core.js +67 -0
- package/out/runtime/modules/core.js.map +1 -0
- package/out/runtime/relgraph.d.ts +21 -0
- package/out/runtime/relgraph.d.ts.map +1 -0
- package/out/runtime/relgraph.js +156 -0
- package/out/runtime/relgraph.js.map +1 -0
- package/out/runtime/resolvers/interface.d.ts +59 -0
- package/out/runtime/resolvers/interface.d.ts.map +1 -0
- package/out/runtime/resolvers/interface.js +111 -0
- package/out/runtime/resolvers/interface.js.map +1 -0
- package/out/runtime/resolvers/registry.d.ts +8 -0
- package/out/runtime/resolvers/registry.d.ts.map +1 -0
- package/out/runtime/resolvers/registry.js +26 -0
- package/out/runtime/resolvers/registry.js.map +1 -0
- package/out/runtime/resolvers/sqldb/database.d.ts +50 -0
- package/out/runtime/resolvers/sqldb/database.d.ts.map +1 -0
- package/out/runtime/resolvers/sqldb/database.js +618 -0
- package/out/runtime/resolvers/sqldb/database.js.map +1 -0
- package/out/runtime/resolvers/sqldb/dbutil.d.ts +18 -0
- package/out/runtime/resolvers/sqldb/dbutil.d.ts.map +1 -0
- package/out/runtime/resolvers/sqldb/dbutil.js +221 -0
- package/out/runtime/resolvers/sqldb/dbutil.js.map +1 -0
- package/out/runtime/resolvers/sqldb/impl.d.ts +26 -0
- package/out/runtime/resolvers/sqldb/impl.d.ts.map +1 -0
- package/out/runtime/resolvers/sqldb/impl.js +300 -0
- package/out/runtime/resolvers/sqldb/impl.js.map +1 -0
- package/out/runtime/state.js +83 -0
- package/out/runtime/state.js.map +1 -0
- package/out/runtime/util.d.ts +43 -0
- package/out/runtime/util.d.ts.map +1 -0
- package/out/runtime/util.js +447 -0
- package/out/runtime/util.js.map +1 -0
- package/out/setupClassic.d.ts +98 -0
- package/out/setupClassic.d.ts.map +1 -0
- package/out/setupClassic.js +38 -0
- package/out/setupClassic.js.map +1 -0
- package/out/setupCommon.d.ts +2 -0
- package/out/setupCommon.d.ts.map +1 -0
- package/out/setupCommon.js +33 -0
- package/out/setupCommon.js.map +1 -0
- package/out/setupExtended.d.ts +40 -0
- package/out/setupExtended.d.ts.map +1 -0
- package/out/setupExtended.js +67 -0
- package/out/setupExtended.js.map +1 -0
- package/out/syntaxes/agentlang.monarch.d.ts +77 -0
- package/out/syntaxes/agentlang.monarch.d.ts.map +1 -0
- package/out/syntaxes/agentlang.monarch.js +31 -0
- package/out/syntaxes/agentlang.monarch.js.map +1 -0
- package/out/utils/fs/index.d.ts +14 -0
- package/out/utils/fs/index.d.ts.map +1 -0
- package/out/utils/fs/index.js +26 -0
- package/out/utils/fs/index.js.map +1 -0
- package/out/utils/fs/interfaces.d.ts +105 -0
- package/out/utils/fs/interfaces.d.ts.map +1 -0
- package/out/utils/fs/interfaces.js +5 -0
- package/out/utils/fs/interfaces.js.map +1 -0
- package/out/utils/fs/lightning-fs.d.ts +116 -0
- package/out/utils/fs/lightning-fs.d.ts.map +1 -0
- package/out/utils/fs/lightning-fs.js +243 -0
- package/out/utils/fs/lightning-fs.js.map +1 -0
- package/out/utils/fs/node-fs.d.ts +93 -0
- package/out/utils/fs/node-fs.d.ts.map +1 -0
- package/out/utils/fs/node-fs.js +169 -0
- package/out/utils/fs/node-fs.js.map +1 -0
- package/out/utils/fs-utils.d.ts +153 -0
- package/out/utils/fs-utils.d.ts.map +1 -0
- package/out/utils/fs-utils.js +271 -0
- package/out/utils/fs-utils.js.map +1 -0
- package/out/utils/runtime.d.ts +36 -0
- package/out/utils/runtime.d.ts.map +1 -0
- package/out/utils/runtime.js +39 -0
- package/out/utils/runtime.js.map +1 -0
- package/package.json +155 -0
- package/src/api/http.ts +361 -0
- package/src/cli/cli-util.ts +18 -0
- package/src/cli/main.ts +146 -0
- package/src/extension/main.ts +51 -0
- package/src/language/agentlang-module.ts +75 -0
- package/src/language/agentlang-validator.ts +60 -0
- package/src/language/agentlang.langium +178 -0
- package/src/language/generated/ast.ts +1698 -0
- package/src/language/generated/grammar.ts +4477 -0
- package/src/language/generated/module.ts +25 -0
- package/src/language/main-browser.ts +19 -0
- package/src/language/main.ts +13 -0
- package/src/language/parser.ts +329 -0
- package/src/language/syntax.ts +646 -0
- package/src/runtime/agents/common.ts +177 -0
- package/src/runtime/agents/impl/openai.ts +19 -0
- package/src/runtime/agents/provider.ts +58 -0
- package/src/runtime/agents/registry.ts +9 -0
- package/src/runtime/auth/cognito.ts +225 -0
- package/src/runtime/auth/defs.ts +33 -0
- package/src/runtime/auth/interface.ts +31 -0
- package/src/runtime/defs.ts +33 -0
- package/src/runtime/interpreter.ts +1352 -0
- package/src/runtime/loader.ts +450 -0
- package/src/runtime/logger.ts +51 -0
- package/src/runtime/module.ts +2188 -0
- package/src/runtime/modules/ai.ts +257 -0
- package/src/runtime/modules/auth.ts +489 -0
- package/src/runtime/modules/core.ts +95 -0
- package/src/runtime/relgraph.ts +195 -0
- package/src/runtime/resolvers/interface.ts +160 -0
- package/src/runtime/resolvers/registry.ts +30 -0
- package/src/runtime/resolvers/sqldb/database.ts +823 -0
- package/src/runtime/resolvers/sqldb/dbutil.ts +257 -0
- package/src/runtime/resolvers/sqldb/impl.ts +471 -0
- package/src/runtime/state.ts +87 -0
- package/src/runtime/util.ts +513 -0
- package/src/setupClassic.ts +43 -0
- package/src/setupCommon.ts +33 -0
- package/src/setupExtended.ts +79 -0
- package/src/syntaxes/agentlang.monarch.ts +31 -0
- package/src/utils/fs/index.ts +28 -0
- package/src/utils/fs/interfaces.ts +118 -0
- package/src/utils/fs/lightning-fs.ts +284 -0
- package/src/utils/fs/node-fs.ts +185 -0
- package/src/utils/fs-utils.ts +304 -0
- package/src/utils/runtime.ts +43 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { ColumnType, EntitySchema, EntitySchemaColumnOptions, EntitySchemaOptions, TableColumnOptions, TableForeignKey, TableIndexOptions } from 'typeorm';
|
|
2
|
+
import {
|
|
3
|
+
AttributeSpec,
|
|
4
|
+
fetchModule,
|
|
5
|
+
getAllBetweenRelationships,
|
|
6
|
+
getAllOneToOneRelationshipsForEntity,
|
|
7
|
+
getAttributeDefaultValue,
|
|
8
|
+
getAttributeLength,
|
|
9
|
+
getFkSpec,
|
|
10
|
+
getModuleNames,
|
|
11
|
+
isArrayAttribute,
|
|
12
|
+
isBuiltInType,
|
|
13
|
+
isIdAttribute,
|
|
14
|
+
isIndexedAttribute,
|
|
15
|
+
isOptionalAttribute,
|
|
16
|
+
isUniqueAttribute,
|
|
17
|
+
Record,
|
|
18
|
+
RecordSchema,
|
|
19
|
+
Relationship,
|
|
20
|
+
Module,
|
|
21
|
+
} from '../../module.js';
|
|
22
|
+
import { buildGraph } from '../../relgraph.js';
|
|
23
|
+
import { makeFqName } from '../../util.js';
|
|
24
|
+
import { DeletedFlagAttributeName, ParentAttributeName, PathAttributeName } from '../../defs.js';
|
|
25
|
+
|
|
26
|
+
export const DefaultVectorDimension = 1536
|
|
27
|
+
|
|
28
|
+
export type TableSchema = {
|
|
29
|
+
name: string;
|
|
30
|
+
columns: TableSpec;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export function asTableName(moduleName: string, entityName: string): string {
|
|
34
|
+
return `${moduleName}_${entityName}`.toLowerCase();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function modulesAsDbSchema(): TableSchema[] {
|
|
38
|
+
const result: TableSchema[] = new Array<TableSchema>();
|
|
39
|
+
getModuleNames().forEach((n: string) => {
|
|
40
|
+
buildGraph(n);
|
|
41
|
+
const mod: Module = fetchModule(n);
|
|
42
|
+
const entities: Record[] = mod.getEntityEntries();
|
|
43
|
+
const betRels: Record[] = mod
|
|
44
|
+
.getBetweenRelationshipEntries()
|
|
45
|
+
.filter((v: Relationship) => v.isManyToMany());
|
|
46
|
+
const allEntries: Record[] = entities.concat(betRels) as Record[];
|
|
47
|
+
allEntries.forEach((ent: Record) => {
|
|
48
|
+
const tspec: TableSchema = {
|
|
49
|
+
name: asTableName(n, ent.name),
|
|
50
|
+
columns: entitySchemaToTable(ent.schema),
|
|
51
|
+
};
|
|
52
|
+
result.push(tspec);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export type OrmSchema = {
|
|
59
|
+
entities: EntitySchema[],
|
|
60
|
+
vectorEntities: EntitySchema[]
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function modulesAsOrmSchema(): OrmSchema {
|
|
64
|
+
const ents: EntitySchema[] = [];
|
|
65
|
+
const vects: EntitySchema[] = []
|
|
66
|
+
getModuleNames().forEach((n: string) => {
|
|
67
|
+
buildGraph(n);
|
|
68
|
+
const mod: Module = fetchModule(n);
|
|
69
|
+
const entities: Record[] = mod.getEntityEntries()
|
|
70
|
+
const rels: Record[] = mod.getBetweenRelationshipEntriesThatNeedStore();
|
|
71
|
+
entities.concat(rels).forEach((entry: Record) => {
|
|
72
|
+
ents.push(new EntitySchema<any>(ormSchemaFromRecordSchema(n, entry)))
|
|
73
|
+
const ownerEntry = createOwnersEntity(entry)
|
|
74
|
+
ents.push(new EntitySchema<any>(ormSchemaFromRecordSchema(n, ownerEntry, true)))
|
|
75
|
+
if (entry.getFullTextSearchAttributes()) {
|
|
76
|
+
const vectorEntry = createVectorEntity(entry)
|
|
77
|
+
vects.push(new EntitySchema<any>(ormSchemaFromRecordSchema(n, vectorEntry, true)))
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
return { entities: ents, vectorEntities: vects }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function ormSchemaFromRecordSchema(moduleName: string, entry: Record, hasOwnPk?: boolean): EntitySchemaOptions<any> {
|
|
85
|
+
const entityName = entry.name
|
|
86
|
+
const scm: RecordSchema = entry.schema
|
|
87
|
+
const result = new EntitySchemaOptions<any>()
|
|
88
|
+
result.tableName = asTableName(moduleName, entityName)
|
|
89
|
+
result.name = result.tableName
|
|
90
|
+
const cols = new Map<string, any>()
|
|
91
|
+
const indices = new Array<any>()
|
|
92
|
+
const chkforpk: boolean = hasOwnPk == undefined ? false : true
|
|
93
|
+
let needPath = true
|
|
94
|
+
scm.forEach((attrSpec: AttributeSpec, attrName: string) => {
|
|
95
|
+
let d: any = getAttributeDefaultValue(attrSpec);
|
|
96
|
+
const autoUuid: boolean = d && d == 'uuid()' ? true : false;
|
|
97
|
+
const autoIncr: boolean = !autoUuid && d && d == 'autoincrement()' ? true : false;
|
|
98
|
+
if (autoUuid || autoIncr) d = undefined;
|
|
99
|
+
let genStrat: 'uuid' | 'increment' | undefined = undefined
|
|
100
|
+
if (autoIncr) genStrat = 'increment';
|
|
101
|
+
else if (autoUuid) genStrat = 'uuid';
|
|
102
|
+
const isuq: boolean = isUniqueAttribute(attrSpec)
|
|
103
|
+
const ispk: boolean = chkforpk && isIdAttribute(attrSpec)
|
|
104
|
+
const colDef: EntitySchemaColumnOptions = {
|
|
105
|
+
type: asSqlType(attrSpec.type),
|
|
106
|
+
generated: genStrat,
|
|
107
|
+
default: d,
|
|
108
|
+
unique: isuq,
|
|
109
|
+
primary: ispk,
|
|
110
|
+
nullable: isOptionalAttribute(attrSpec)
|
|
111
|
+
};
|
|
112
|
+
if (ispk) {
|
|
113
|
+
needPath = false
|
|
114
|
+
}
|
|
115
|
+
if (isIndexedAttribute(attrSpec)) {
|
|
116
|
+
indices.push(Object.fromEntries(new Map()
|
|
117
|
+
.set('name', `${result.tableName}_${attrName}_index`)
|
|
118
|
+
.set('columns', [attrName])
|
|
119
|
+
.set('unique', isuq)))
|
|
120
|
+
}
|
|
121
|
+
cols.set(attrName, colDef)
|
|
122
|
+
});
|
|
123
|
+
if (needPath) {
|
|
124
|
+
cols.set(PathAttributeName, { type: "varchar", primary: true })
|
|
125
|
+
cols.set(ParentAttributeName, { type: "varchar", default: '', indexed: true })
|
|
126
|
+
}
|
|
127
|
+
cols.set(DeletedFlagAttributeName, { type: "boolean", default: false })
|
|
128
|
+
const allBetRels = getAllBetweenRelationships()
|
|
129
|
+
const relsSpec = new Map()
|
|
130
|
+
const fqName = makeFqName(moduleName, entityName)
|
|
131
|
+
getAllOneToOneRelationshipsForEntity(moduleName, entityName, allBetRels)
|
|
132
|
+
.forEach((re: Relationship) => {
|
|
133
|
+
const colName = re.getInverseAliasForName(fqName)
|
|
134
|
+
if (cols.has(colName)) {
|
|
135
|
+
throw new Error(`Cannot establish relationship ${re.name}, ${entityName}.${colName} already exists`)
|
|
136
|
+
}
|
|
137
|
+
cols.set(colName, { type: "varchar", unique: true })
|
|
138
|
+
})
|
|
139
|
+
if (relsSpec.size > 0) {
|
|
140
|
+
result.relations = Object.fromEntries(relsSpec)
|
|
141
|
+
}
|
|
142
|
+
result.columns = Object.fromEntries(cols)
|
|
143
|
+
const compUqs = entry.getCompositeUniqueAttributes()
|
|
144
|
+
if (compUqs) {
|
|
145
|
+
indices.push(Object.fromEntries(new Map()
|
|
146
|
+
.set('name', `${result.tableName}__comp__index`)
|
|
147
|
+
.set('columns', compUqs)
|
|
148
|
+
.set('unique', true)))
|
|
149
|
+
}
|
|
150
|
+
if (indices.length > 0) {
|
|
151
|
+
result.indices = indices
|
|
152
|
+
}
|
|
153
|
+
return result
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export const OwnersSuffix = '_owners'
|
|
157
|
+
export const VectorSuffix = '_vector'
|
|
158
|
+
|
|
159
|
+
function createOwnersEntity(entry: Record): Record {
|
|
160
|
+
const ownersEntry = new Record(`${entry.name}${OwnersSuffix}`, entry.moduleName)
|
|
161
|
+
const permProps = new Map().set('default', true)
|
|
162
|
+
return ownersEntry.addAttribute('id', { type: 'UUID', properties: new Map().set('id', true) })
|
|
163
|
+
.addAttribute('user_id', { type: 'String' })
|
|
164
|
+
.addAttribute('type', { type: 'String', properties: new Map().set('default', 'o') })
|
|
165
|
+
.addAttribute('c', { type: 'Boolean', properties: permProps })
|
|
166
|
+
.addAttribute('r', { type: 'Boolean', properties: permProps })
|
|
167
|
+
.addAttribute('u', { type: 'Boolean', properties: permProps })
|
|
168
|
+
.addAttribute('d', { type: 'Boolean', properties: permProps })
|
|
169
|
+
.addAttribute('path', { type: 'String', properties: new Map().set('indexed', true) })
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function createVectorEntity(entry: Record): Record {
|
|
173
|
+
const ownersEntry = new Record(`${entry.name}${VectorSuffix}`, entry.moduleName)
|
|
174
|
+
return ownersEntry.addAttribute('id', { type: 'String', properties: new Map().set('id', true) })
|
|
175
|
+
.addAttribute('embedding', { type: `vector(${DefaultVectorDimension})` })
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export type TableSpec = {
|
|
179
|
+
columns: TableColumnOptions[];
|
|
180
|
+
indices: Array<TableIndexOptions>;
|
|
181
|
+
idColumns: Map<string, AttributeSpec>;
|
|
182
|
+
fks?: Array<TableForeignKey>;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
function entitySchemaToTable(scm: RecordSchema): TableSpec {
|
|
186
|
+
const cols: Array<TableColumnOptions> = new Array<TableColumnOptions>();
|
|
187
|
+
const indices: Array<TableIndexOptions> = new Array<TableIndexOptions>();
|
|
188
|
+
const idCols: Map<string, AttributeSpec> = new Map<string, AttributeSpec>();
|
|
189
|
+
let fkSpecs: Array<TableForeignKey> | undefined;
|
|
190
|
+
scm.forEach((attrSpec: AttributeSpec, attrName: string) => {
|
|
191
|
+
let d: any = getAttributeDefaultValue(attrSpec);
|
|
192
|
+
const autoUuid: boolean = d && d == 'uuid()' ? true : false;
|
|
193
|
+
const autoIncr: boolean = !autoUuid && d && d == 'autoincrement()' ? true : false;
|
|
194
|
+
if (autoUuid || autoIncr) d = undefined;
|
|
195
|
+
let genStrat: 'uuid' | 'increment' | 'rowid' | 'identity' = 'identity';
|
|
196
|
+
if (autoIncr) genStrat = 'increment';
|
|
197
|
+
else if (autoUuid) genStrat = 'uuid';
|
|
198
|
+
const fkSpec: string | undefined = getFkSpec(attrSpec);
|
|
199
|
+
if (fkSpec != undefined) {
|
|
200
|
+
const parts: string[] = fkSpec.split('.');
|
|
201
|
+
if (parts.length != 2) {
|
|
202
|
+
throw new Error(`Invalid reference - ${fkSpec}`);
|
|
203
|
+
}
|
|
204
|
+
if (fkSpecs == undefined) {
|
|
205
|
+
fkSpecs = new Array<TableForeignKey>();
|
|
206
|
+
}
|
|
207
|
+
const fk: TableForeignKey = new TableForeignKey({
|
|
208
|
+
columnNames: [attrName],
|
|
209
|
+
referencedColumnNames: [parts[1]],
|
|
210
|
+
referencedTableName: parts[0],
|
|
211
|
+
onDelete: 'CASCADE',
|
|
212
|
+
});
|
|
213
|
+
fkSpecs.push(fk);
|
|
214
|
+
}
|
|
215
|
+
const colOpt: TableColumnOptions = {
|
|
216
|
+
name: attrName,
|
|
217
|
+
type: asSqlType(attrSpec.type) as string,
|
|
218
|
+
isPrimary: genStrat == 'increment',
|
|
219
|
+
default: d,
|
|
220
|
+
isUnique: isUniqueAttribute(attrSpec),
|
|
221
|
+
isNullable: isOptionalAttribute(attrSpec),
|
|
222
|
+
isGenerated: autoUuid || autoIncr,
|
|
223
|
+
isArray: isArrayAttribute(attrSpec),
|
|
224
|
+
};
|
|
225
|
+
if (colOpt.isGenerated) {
|
|
226
|
+
colOpt.generationStrategy = genStrat;
|
|
227
|
+
}
|
|
228
|
+
const len: number | undefined = getAttributeLength(attrSpec);
|
|
229
|
+
if (len != undefined) {
|
|
230
|
+
colOpt.length = len.toString();
|
|
231
|
+
}
|
|
232
|
+
cols.push(colOpt);
|
|
233
|
+
const isId: boolean = isIdAttribute(attrSpec);
|
|
234
|
+
if (isId) {
|
|
235
|
+
idCols.set(attrName, attrSpec);
|
|
236
|
+
}
|
|
237
|
+
if (isIndexedAttribute(attrSpec) || isId) {
|
|
238
|
+
indices.push({ columnNames: [attrName] });
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
return { columns: cols, indices: indices, idColumns: idCols, fks: fkSpecs };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function asSqlType(type: string): ColumnType {
|
|
245
|
+
const t = type.toLowerCase()
|
|
246
|
+
if (t == 'string' || t == 'datetime' || t == 'email' || t == 'url'
|
|
247
|
+
|| t == 'map' || t == 'any' || t == 'path')
|
|
248
|
+
return 'varchar';
|
|
249
|
+
else if (t == 'int') return 'integer';
|
|
250
|
+
else if (t == 'number') return 'decimal'
|
|
251
|
+
else if (!isBuiltInType(type)) return 'varchar';
|
|
252
|
+
else return t as ColumnType;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export function isSqlTrue(v: true | false | 1 | 0): boolean {
|
|
256
|
+
return v == true || v == 1;
|
|
257
|
+
}
|