@stacksjs/database 0.75.556 → 0.75.558
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/dist/model-sources.d.ts +22 -0
- package/dist/model-sources.js +1 -1
- package/package.json +16 -16
package/dist/model-sources.d.ts
CHANGED
|
@@ -30,6 +30,28 @@ export declare function declaredTableName(source: ModelSource): string;
|
|
|
30
30
|
* (stacksjs/stacks#2943).
|
|
31
31
|
*/
|
|
32
32
|
export declare function excludedFrameworkModels(options?: { userRoot?: string, frameworkRoot?: string, includeFrameworkDefaults?: boolean }): ModelSource[];
|
|
33
|
+
/**
|
|
34
|
+
* The models in this app's schema: their names, files and where each comes
|
|
35
|
+
* from. The same list as {@link resolveModelSources}'s `models`, by the same
|
|
36
|
+
* rules, read from directory listings alone.
|
|
37
|
+
*
|
|
38
|
+
* For callers that ask which models exist while serving a request, such as
|
|
39
|
+
* the dashboard. {@link resolveModelSources} answers that too, but it is the
|
|
40
|
+
* migration generator's entry point: on the merge path it deletes and
|
|
41
|
+
* rebuilds the staging directory (a symlink per model) on every call, and
|
|
42
|
+
* reads model files to report what is out of scope. A page listing 110 models
|
|
43
|
+
* that asked it once per model spent over a second there, synchronously, and
|
|
44
|
+
* rewrote a directory the generator may be reading.
|
|
45
|
+
*
|
|
46
|
+
* Returns `null` when neither root holds a model. Installed packages' models
|
|
47
|
+
* are not listed, as they are not in `resolveModelSources().models`.
|
|
48
|
+
*/
|
|
49
|
+
export declare function listModelSources(options?: {
|
|
50
|
+
include?: readonly string[]
|
|
51
|
+
userRoot?: string
|
|
52
|
+
frameworkRoot?: string
|
|
53
|
+
includeFrameworkDefaults?: boolean
|
|
54
|
+
}): ModelSource[] | null;
|
|
33
55
|
/**
|
|
34
56
|
* Resolve models from userland, falling back to the framework defaults.
|
|
35
57
|
*
|
package/dist/model-sources.js
CHANGED
|
@@ -4,4 +4,4 @@ import{existsSync,lstatSync,mkdirSync,readdirSync,readFileSync,rmSync,symlinkSyn
|
|
|
4
4
|
`+` ${model.package}: ${model.file}
|
|
5
5
|
`+`Only one can win, so neither package can be trusted to own its table. `+`Rename one, or have both depend on a shared package that owns the model.`)}seen.set(model.name,model)}const frameworkNames=new Set(framework.map((model)=>model.name));for(const model of packaged){if(!frameworkNames.has(model.name))continue;throw new Error(`The package '${model.package}' ships a '${model.name}' model, which the framework already owns.
|
|
6
6
|
`+` ${model.file}
|
|
7
|
-
`+`The framework's own code reads that table, so a package cannot redefine it. `+`A package uses the host application's model instead: this is why a package never ships its own User.`)}}export function modelStagingDir(){return path.frameworkRuntimePath("model-sources")}function stage(models){const dir=modelStagingDir();try{rmSync(dir,{recursive:true,force:true})}catch{}mkdirSync(dir,{recursive:true});for(const model of models){const target=join(dir,`${model.name}.ts`);try{symlinkSync(model.file,target)}catch{try{writeFileSync(target,readFileSync(model.file))}catch{}}}return dir}export function declaredTableName(source){let contents="";try{contents=readFileSync(source.file,"utf8")}catch{}const declared=contents.match(/^\s*table:\s*['"`]([^'"`]+)['"`]/m);if(declared?.[1])return declared[1].toLowerCase();const named=contents.match(/^\s*name:\s*['"`]([^'"`]+)['"`]/m);return plural(snakeCase(named?.[1]??source.name)).toLowerCase()}function shouldIncludeFrameworkDefaults(){const flag=process.env.STACKS_INCLUDE_FRAMEWORK_MODELS;if(flag==="1"||flag==="true")return true;if(flag==="0"||flag==="false")return false;return config?.database?.models?.includeFrameworkDefaults===true}function frameworkDefaultsContribute(userModelCount,includeFrameworkDefaults){return(includeFrameworkDefaults??shouldIncludeFrameworkDefaults())||userModelCount===0}export function excludedFrameworkModels(options={}){const user=collectModels(options.userRoot??path.userModelsPath(),"user");if(frameworkDefaultsContribute(user.length,options.includeFrameworkDefaults))return[];return collectModels(options.frameworkRoot??path.frameworkPath("defaults/app/Models"),"framework")}
|
|
7
|
+
`+`The framework's own code reads that table, so a package cannot redefine it. `+`A package uses the host application's model instead: this is why a package never ships its own User.`)}}export function modelStagingDir(){return path.frameworkRuntimePath("model-sources")}function stage(models){const dir=modelStagingDir();try{rmSync(dir,{recursive:true,force:true})}catch{}mkdirSync(dir,{recursive:true});for(const model of models){const target=join(dir,`${model.name}.ts`);try{symlinkSync(model.file,target)}catch{try{writeFileSync(target,readFileSync(model.file))}catch{}}}return dir}export function declaredTableName(source){let contents="";try{contents=readFileSync(source.file,"utf8")}catch{}const declared=contents.match(/^\s*table:\s*['"`]([^'"`]+)['"`]/m);if(declared?.[1])return declared[1].toLowerCase();const named=contents.match(/^\s*name:\s*['"`]([^'"`]+)['"`]/m);return plural(snakeCase(named?.[1]??source.name)).toLowerCase()}function shouldIncludeFrameworkDefaults(){const flag=process.env.STACKS_INCLUDE_FRAMEWORK_MODELS;if(flag==="1"||flag==="true")return true;if(flag==="0"||flag==="false")return false;return config?.database?.models?.includeFrameworkDefaults===true}function frameworkDefaultsContribute(userModelCount,includeFrameworkDefaults){return(includeFrameworkDefaults??shouldIncludeFrameworkDefaults())||userModelCount===0}export function excludedFrameworkModels(options={}){const user=collectModels(options.userRoot??path.userModelsPath(),"user");if(frameworkDefaultsContribute(user.length,options.includeFrameworkDefaults))return[];return collectModels(options.frameworkRoot??path.frameworkPath("defaults/app/Models"),"framework")}function collectScope(options){const userRoot=options.userRoot??path.userModelsPath();const frameworkRoot=options.frameworkRoot??path.frameworkPath("defaults/app/Models");const user=collectModels(userRoot,"user");const framework=collectModels(frameworkRoot,"framework");const useFramework=frameworkDefaultsContribute(user.length,options.includeFrameworkDefaults);const contributing=useFramework?framework:[];const excluded=useFramework?[]:framework;const byName=new Map;for(const model of contributing)byName.set(model.name,model);for(const model of user)byName.set(model.name,model);const allModels=[...byName.values()].sort((a,b)=>a.name.localeCompare(b.name));return{userRoot,frameworkRoot,user,framework,contributing,excluded,allModels}}function selectIncluded(allModels,include){if(include===undefined)return{models:allModels,left:[]};if(!Array.isArray(include)||include.length===0||include.some((name)=>typeof name!=="string"||!name.trim()))throw new Error("database.models.include must name at least one model");const selected=new Set(include.map((name)=>name.trim().toLowerCase()));const known=new Set(allModels.map((model)=>model.name.toLowerCase()));const unknown=[...selected].filter((name)=>!known.has(name));if(unknown.length)throw new Error(`database.models.include names unknown models: ${unknown.join(", ")}`);return{models:allModels.filter((model)=>selected.has(model.name.toLowerCase())),left:allModels.filter((model)=>!selected.has(model.name.toLowerCase()))}}export function listModelSources(options={}){const{user,framework,allModels}=collectScope(options);if(user.length===0&&framework.length===0)return null;return selectIncluded(allModels,options.include??config?.database?.models?.include).models}export function resolveModelSources(options={}){const{userRoot,frameworkRoot,user,framework,contributing,excluded,allModels}=collectScope(options);const packaged=(options.packageRoots??packageModelRoots()).flatMap((root)=>collectModels(root.dir,"package",root.package));assertPackageModelsAreUsable(packaged,framework);if(user.length===0&&framework.length===0)return null;const frameworkByName=new Map;for(const model of framework)frameworkByName.set(model.name,model);const shadowed=[];for(const model of user){const replaced=frameworkByName.get(model.name);if(replaced)shadowed.push({name:model.name,userFile:model.file,frameworkFile:replaced.file})}const include=options.include??config?.database?.models?.include;const selection=selectIncluded(allModels,include);const models=selection.models;excluded.push(...selection.left);const roots=[];if(user.length>0)roots.push(userRoot);if(contributing.length>0)roots.push(frameworkRoot);const excludedTables=[...new Set([...excluded,...packaged].map(declaredTableName))].sort();const onlyUser=contributing.length===0;const allFlat=models.every((m)=>basename(join(m.file,".."))===basename(onlyUser?userRoot:frameworkRoot));const outOfScopeRelations=findOutOfScopeRelations(models.filter((model)=>model.origin==="user"),excluded);if(include===undefined&&!options.forceStage&&roots.length===1&&allFlat){return{dir:roots[0],models,roots,staged:false,shadowed,excluded,excludedTables,outOfScopeRelations}}return{dir:stage(models),models,roots,staged:true,shadowed,excluded,excludedTables,outOfScopeRelations}}export function cleanupModelStaging(){const dir=modelStagingDir();try{if(existsSync(dir)&&lstatSync(dir).isDirectory())rmSync(dir,{recursive:true,force:true})}catch{}}export function declaredModelTables(options={}){let packageRoots=options.packageRoots;if(!packageRoots){try{packageRoots=packageModelRoots()}catch{packageRoots=[]}}const models=[...collectModels(options.userRoot??path.userModelsPath(),"user"),...collectModels(options.frameworkRoot??path.frameworkPath("defaults/app/Models"),"framework"),...packageRoots.flatMap((root)=>collectModels(root.dir,"package",root.package))];return new Set(models.map(declaredTableName))}export async function appModelTables(root,templateRoot){const tables=new Set;for(const file of appModelFiles(root,templateRoot)){const definition=(await import(file)).default;if(typeof definition?.table==="string")tables.add(definition.table.toLowerCase())}return tables}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/database",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.75.
|
|
5
|
+
"version": "0.75.558",
|
|
6
6
|
"description": "The Stacks database integration.",
|
|
7
7
|
"author": "Chris Breuer",
|
|
8
8
|
"contributors": [
|
|
@@ -66,26 +66,26 @@
|
|
|
66
66
|
"prepublishOnly": "bun run build"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"@stacksjs/config": "0.75.
|
|
70
|
-
"@stacksjs/env": "0.75.
|
|
71
|
-
"@stacksjs/error-handling": "0.75.
|
|
72
|
-
"@stacksjs/faker": "0.75.
|
|
73
|
-
"@stacksjs/features": "0.75.
|
|
74
|
-
"@stacksjs/logging": "0.75.
|
|
75
|
-
"@stacksjs/model-meta": "0.75.
|
|
76
|
-
"@stacksjs/path": "0.75.
|
|
77
|
-
"@stacksjs/query-builder": "0.75.
|
|
78
|
-
"@stacksjs/security": "0.75.
|
|
79
|
-
"@stacksjs/storage": "0.75.
|
|
80
|
-
"@stacksjs/strings": "0.75.
|
|
69
|
+
"@stacksjs/config": "0.75.558",
|
|
70
|
+
"@stacksjs/env": "0.75.558",
|
|
71
|
+
"@stacksjs/error-handling": "0.75.558",
|
|
72
|
+
"@stacksjs/faker": "0.75.558",
|
|
73
|
+
"@stacksjs/features": "0.75.558",
|
|
74
|
+
"@stacksjs/logging": "0.75.558",
|
|
75
|
+
"@stacksjs/model-meta": "0.75.558",
|
|
76
|
+
"@stacksjs/path": "0.75.558",
|
|
77
|
+
"@stacksjs/query-builder": "0.75.558",
|
|
78
|
+
"@stacksjs/security": "0.75.558",
|
|
79
|
+
"@stacksjs/storage": "0.75.558",
|
|
80
|
+
"@stacksjs/strings": "0.75.558",
|
|
81
81
|
"@stacksjs/ts-validation": "^0.5.7",
|
|
82
82
|
"bun-query-builder": "^0.3.44",
|
|
83
83
|
"dynamodb-tooling": "^0.3.6"
|
|
84
84
|
},
|
|
85
85
|
"devDependencies": {
|
|
86
|
-
"@stacksjs/cli": "0.75.
|
|
87
|
-
"@stacksjs/router": "0.75.
|
|
88
|
-
"@stacksjs/utils": "0.75.
|
|
86
|
+
"@stacksjs/cli": "0.75.558",
|
|
87
|
+
"@stacksjs/router": "0.75.558",
|
|
88
|
+
"@stacksjs/utils": "0.75.558",
|
|
89
89
|
"better-dx": "^0.2.24"
|
|
90
90
|
}
|
|
91
91
|
}
|