Comparing version
@@ -5,2 +5,4 @@ # Changelog | ||
## [0.8.0](https://github.com/waitingsong/kmore/compare/v0.7.0...v0.8.0) (2019-08-15) | ||
## [0.7.0](https://github.com/waitingsong/kmore/compare/v0.6.0...v0.7.0) (2019-08-12) | ||
@@ -7,0 +9,0 @@ |
@@ -5,3 +5,3 @@ /** | ||
* | ||
* @version 0.7.0 | ||
* @version 0.8.0 | ||
* @author waiting | ||
@@ -20,12 +20,2 @@ * @license MIT | ||
const initOptions = { | ||
callerFuncNames: ['genTbListFromType', 'kmore'], | ||
exportVarPrefix: 'tbs', | ||
forceLoadTbListJs: false, | ||
forceLoadTbListJsPathReplaceRules: null, | ||
outputBanner: '/* eslint-disable */', | ||
outputFileNameSuffix: '__built-tables', | ||
refTablesPrefix: 'reftb_', | ||
}; | ||
const initBuildSrcOpts = Object.assign({}, initOptions, { path: [], concurrent: 5 }); | ||
var DbPropKeys; | ||
@@ -43,6 +33,5 @@ (function (DbPropKeys) { | ||
function loadTbListParamFromCallerInfo(options) { | ||
const caller = kmoreTypes.getCallerStack(2); | ||
function loadTbListParamFromCallerInfo(options, caller) { | ||
if (!options.forceLoadTbListJs && kmoreTypes.isTsFile(caller.path)) { | ||
return loadTbListFromTsTypeFile(options.callerFuncNames); | ||
return loadTbListFromTsTypeFile(options.callerDistance + 3); | ||
} | ||
@@ -53,7 +42,4 @@ else { // run in js or debug in ts | ||
} | ||
function loadTbListFromTsTypeFile(callerFuncNames) { | ||
const ret = kmoreTypes.genTbListFromType({ | ||
callerFuncNames, | ||
stackDepth: 4, | ||
}); | ||
function loadTbListFromTsTypeFile(callerDistance) { | ||
const ret = kmoreTypes.genTbListFromType({ callerDistance }); | ||
return ret; | ||
@@ -84,10 +70,21 @@ } | ||
* eg. db.rb.user() => Knex.QueryBuilder<{id: number, name: string}> | ||
* tables will be generated from generics automaitically when passing undefined or null value | ||
*/ | ||
function kmore(config, options) { | ||
function kmore(config, | ||
/** Auto generate tables from generics, if value is undefined or null */ | ||
tables, options) { | ||
const opts = options | ||
? Object.assign({}, initOptions, options) : Object.assign({}, initOptions); | ||
const tables = loadTbListParamFromCallerInfo(opts); | ||
? Object.assign({}, kmoreTypes.initOptions, options) : Object.assign({}, kmoreTypes.initOptions); | ||
let tbs = {}; | ||
if (typeof tables === 'undefined' || tables === null) { | ||
// detect running env of the caller | ||
const caller = kmoreTypes.getCallerStack(opts.callerDistance); | ||
tbs = loadTbListParamFromCallerInfo(opts, caller); | ||
} | ||
else { | ||
tbs = tables ? Object.assign({}, tables) : kmoreTypes.createNullObject(); | ||
} | ||
let db = kmoreTypes.createNullObject(); | ||
db = bindDbh(defaultPropDescriptor, db, config); | ||
db = bindTables(defaultPropDescriptor, db, tables); | ||
db = bindTables(defaultPropDescriptor, db, tbs); | ||
db = bindRefTables(opts, defaultPropDescriptor, db); | ||
@@ -118,6 +115,6 @@ return Object.freeze(db); | ||
Object.defineProperty(exports, 'buildSource', { | ||
Object.defineProperty(exports, 'genTbListFromType', { | ||
enumerable: true, | ||
get: function () { | ||
return kmoreTypes.buildSource; | ||
return kmoreTypes.genTbListFromType; | ||
} | ||
@@ -124,0 +121,0 @@ }); |
export * from './lib/index'; | ||
export * from './lib/model'; | ||
export { buildSource } from './lib/build'; | ||
export { genTbListFromType } from 'kmore-types'; |
export * from './lib/index'; | ||
export { buildSource } from './lib/build'; | ||
export { genTbListFromType } from 'kmore-types'; |
@@ -1,6 +0,3 @@ | ||
import { CacheMap, Options, GenTbListFromTypeOpts, BuildSrcOpts } from './model'; | ||
export declare const initOptions: Options; | ||
export declare const initBuildSrcOpts: Required<BuildSrcOpts>; | ||
export declare const initGenTbListFromTypeOpts: GenTbListFromTypeOpts; | ||
export declare const reservedTbListKeys: string[]; | ||
export { globalCallerFuncNameSet } from 'kmore-types'; | ||
export { cacheMap, initBuildSrcOpts, initGenTbListFromTypeOpts, initOptions, } from 'kmore-types'; | ||
export declare enum DbPropKeys { | ||
@@ -12,2 +9,1 @@ 'dbh' = "dbh", | ||
export declare const defaultPropDescriptor: PropertyDescriptor; | ||
export declare const cacheMap: CacheMap; |
@@ -1,20 +0,3 @@ | ||
export const initOptions = { | ||
callerFuncNames: ['genTbListFromType', 'kmore'], | ||
exportVarPrefix: 'tbs', | ||
forceLoadTbListJs: false, | ||
forceLoadTbListJsPathReplaceRules: null, | ||
outputBanner: '/* eslint-disable */', | ||
outputFileNameSuffix: '__built-tables', | ||
refTablesPrefix: 'reftb_', | ||
}; | ||
export const initBuildSrcOpts = Object.assign({}, initOptions, { path: [], concurrent: 5 }); | ||
export const initGenTbListFromTypeOpts = { | ||
callerFuncNames: initOptions.callerFuncNames, | ||
includePathKeyWords: [], | ||
stackDepth: 1, | ||
}; | ||
export const reservedTbListKeys = [ | ||
'constructor', | ||
'__proto__', | ||
]; | ||
export { globalCallerFuncNameSet } from 'kmore-types'; | ||
export { cacheMap, initBuildSrcOpts, initGenTbListFromTypeOpts, initOptions, } from 'kmore-types'; | ||
export var DbPropKeys; | ||
@@ -31,9 +14,1 @@ (function (DbPropKeys) { | ||
}; | ||
export const cacheMap = { | ||
/** CallerId -> TbListParam */ | ||
tbListMap: new Map(), | ||
/** CallerId -> LocalTypeId */ | ||
callerIdToLocalTypeIdMap: new Map(), | ||
/** LocalTypeId -> TableListTagMap */ | ||
localTypeMap: new Map(), | ||
}; |
@@ -1,2 +0,2 @@ | ||
import { DbModel, Options, TTableListModel, Config } from './model'; | ||
import { DbModel, DbTables, Options, TTableListModel, Config } from './model'; | ||
/** | ||
@@ -13,3 +13,6 @@ * Knex factory with type-safe tables accessor | ||
* eg. db.rb.user() => Knex.QueryBuilder<{id: number, name: string}> | ||
* tables will be generated from generics automaitically when passing undefined or null value | ||
*/ | ||
export declare function kmore<T extends TTableListModel>(config: Config, options?: Partial<Options>): DbModel<T>; | ||
export declare function kmore<T extends TTableListModel>(config: Config, | ||
/** Auto generate tables from generics, if value is undefined or null */ | ||
tables?: DbTables<T> | null, options?: Partial<Options>): DbModel<T>; |
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import * as Knex from 'knex'; | ||
import { validateParamTables, createNullObject, getCallerStack } from 'kmore-types'; | ||
import { defaultPropDescriptor, DbPropKeys, initOptions } from './config'; | ||
import { validateParamTables, createNullObject } from './util'; | ||
import { loadTbListParamFromCallerInfo } from './tables'; | ||
@@ -19,10 +19,21 @@ // workaround for rollup | ||
* eg. db.rb.user() => Knex.QueryBuilder<{id: number, name: string}> | ||
* tables will be generated from generics automaitically when passing undefined or null value | ||
*/ | ||
export function kmore(config, options) { | ||
export function kmore(config, | ||
/** Auto generate tables from generics, if value is undefined or null */ | ||
tables, options) { | ||
const opts = options | ||
? Object.assign({}, initOptions, options) : Object.assign({}, initOptions); | ||
const tables = loadTbListParamFromCallerInfo(opts); | ||
let tbs = {}; | ||
if (typeof tables === 'undefined' || tables === null) { | ||
// detect running env of the caller | ||
const caller = getCallerStack(opts.callerDistance); | ||
tbs = loadTbListParamFromCallerInfo(opts, caller); | ||
} | ||
else { | ||
tbs = tables ? Object.assign({}, tables) : createNullObject(); | ||
} | ||
let db = createNullObject(); | ||
db = bindDbh(defaultPropDescriptor, db, config); | ||
db = bindTables(defaultPropDescriptor, db, tables); | ||
db = bindTables(defaultPropDescriptor, db, tbs); | ||
db = bindRefTables(opts, defaultPropDescriptor, db); | ||
@@ -29,0 +40,0 @@ return Object.freeze(db); |
import * as Knex from 'knex'; | ||
export { Options, PathReWriteRule, BuildSrcOpts, CacheMap, CallerIdToLocalTypeIdMap, LocalTypeId, CallerId, CallerTypeId, CallerTbListMap, LocalTypeMap, CallerTypeMap, TbListTagMap, TbListMap, GenericsArgName, CallerInfo, CallerTypeIdInfo, GenTbListFromFileOpts, GenTbListFromTypeOpts, RetrieveInfoFromTypeOpts, CallerFuncName, GenGenericsArgMapOpts, ColumnType, BaseTbType, BaseTbListType, PlainJsonValueType, JsonType, TableAlias, TableName, FilePath, FileName, } from 'kmore-types'; | ||
export { Options, PathReWriteRule, BuildSrcOpts, CacheMap, CallerFuncNameSet, CallerIdToLocalTypeIdMap, LocalTypeId, CallerId, CallerTypeId, CallerTbListMap, LocalTypeMap, CallerTypeMap, TbListTagMap, TbListMap, GenericsArgName, CallerInfo, CallerTypeIdInfo, GenTbListFromTypeOpts, RetrieveInfoFromTypeOpts, CallerFuncName, GenGenericsArgMapOpts, ColumnType, BaseTbType, BaseTbListType, PlainJsonValueType, JsonType, TableAlias, TableName, FilePath, FileName, } from 'kmore-types'; | ||
export declare type Config = Knex.Config; | ||
@@ -4,0 +4,0 @@ /** |
@@ -0,4 +1,5 @@ | ||
import { BuildSrcOpts } from 'kmore-types'; | ||
import { CallerInfo, DbTables, Options, TTableListModel } from './model'; | ||
export declare function loadTbListParamFromCallerInfo<T extends TTableListModel>(options: Options): DbTables<T>; | ||
export declare function loadTbListFromTsTypeFile<T extends TTableListModel>(callerFuncNames: Options['callerFuncNames']): DbTables<T>; | ||
export declare function loadTbListParamFromCallerInfo<T extends TTableListModel>(options: Options, caller: CallerInfo): DbTables<T>; | ||
export declare function loadTbListFromTsTypeFile<T extends TTableListModel>(callerDistance: BuildSrcOpts['callerDistance']): DbTables<T>; | ||
export declare function loadTbListFromJsBuiltFile<T extends TTableListModel>(options: Options, caller: CallerInfo): DbTables<T>; |
import { accessSync, constants } from 'fs'; | ||
import { getCallerStack, isTsFile, reWriteLoadingPath, loadVarFromFile, } from './util'; | ||
import { genTbListFromType } from './compiler'; | ||
export function loadTbListParamFromCallerInfo(options) { | ||
const caller = getCallerStack(2); | ||
import { isTsFile, genTbListFromType, loadVarFromFile, reWriteLoadingPath, } from 'kmore-types'; | ||
export function loadTbListParamFromCallerInfo(options, caller) { | ||
if (!options.forceLoadTbListJs && isTsFile(caller.path)) { | ||
return loadTbListFromTsTypeFile(options.callerFuncNames); | ||
return loadTbListFromTsTypeFile(options.callerDistance + 3); | ||
} | ||
@@ -13,7 +11,4 @@ else { // run in js or debug in ts | ||
} | ||
export function loadTbListFromTsTypeFile(callerFuncNames) { | ||
const ret = genTbListFromType({ | ||
callerFuncNames, | ||
stackDepth: 4, | ||
}); | ||
export function loadTbListFromTsTypeFile(callerDistance) { | ||
const ret = genTbListFromType({ callerDistance }); | ||
return ret; | ||
@@ -20,0 +15,0 @@ } |
{ | ||
"name": "kmore", | ||
"author": "waiting", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"description": "A Knex little more factory of SQL query builder, with auto-generated type-safe tables accessor for Node.js", | ||
@@ -59,3 +59,3 @@ "keywords": [ | ||
"dependencies": { | ||
"kmore-types": "^0.2.0" | ||
"kmore-types": "^0.6.0" | ||
}, | ||
@@ -69,3 +69,3 @@ "devDependencies": { | ||
"@types/yargs": "^13.0.0", | ||
"@waiting/eslint-config": "^1.10.0", | ||
"@waiting/eslint-config": "^2.1.0", | ||
"coveralls": "^3.0.5", | ||
@@ -72,0 +72,0 @@ "cross-env": "^5.2.0", |
@@ -70,4 +70,8 @@ # [kmore](https://waitingsong.github.io/kmore/) | ||
* eg. db.rb.user() => Knex.QueryBuilder<{id: number, name: string}> | ||
* tables will be generated from generics automaitically when passing undefined or null value | ||
*/ | ||
const db = kmore<TbListModel>(config) | ||
// or | ||
const tbList = genTbListFromType<TbListModel>() | ||
const db = kmore<TbListModel>(config, tbList) | ||
@@ -74,0 +78,0 @@ ``` |
@@ -71,4 +71,8 @@ # [kmore](https://waitingsong.github.io/kmore/) | ||
* eg. db.rb.user() => Knex.QueryBuilder<{id: number, name: string}> | ||
* tables will be generated from generics automaitically when passing undefined or null value | ||
*/ | ||
const db = kmore<TbListModel>(config) | ||
// or | ||
const tbList = genTbListFromType<TbListModel>() | ||
const db = kmore<TbListModel>(config, tbList) | ||
@@ -75,0 +79,0 @@ ``` |
Sorry, the diff of this file is not supported yet
164
2.5%33750
-2.33%18
-25%271
-9.36%+ Added
- Removed
Updated