Created
January 18, 2020 00:25
-
-
Save zew13/a545088bcb9db19367a474fc669eeb8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| path = require('path') | |
| TABLE_SCHEMA = \shein | |
| _pg = require('pg') | |
| require('pg-parse-float')(_pg) | |
| _pg.types.setTypeParser(20, parseInt) | |
| _pg.types.setTypeParser(1016, (v)-> | |
| if v=="{}" | |
| return [] | |
| v = v.replace(/{/g,"[").replace(/}/g,"]") | |
| return JSON.parse(v) | |
| ) | |
| DB = require('./_knex')({ | |
| client: 'pg', | |
| connection: 'postgresql://'+require('./config/postgresql.ls'), | |
| searchPath: [TABLE_SCHEMA, 'public'], | |
| useNullAsDefault: true | |
| pool: { min: 1, max: 7 } | |
| acquireConnectionTimeout: 60000 | |
| }) | |
| # DB = require('./_knex')({ | |
| # client: 'sqlite3', | |
| # connection: { | |
| # filename: path.join(__dirname,"../db/shein.db") | |
| # } | |
| # useNullAsDefault: true | |
| # }) | |
| QueryBuilder = require('knex/lib/query/builder') | |
| _FUNC_LIST = do -> | |
| r = [] | |
| for k,v of QueryBuilder.prototype | |
| if typeof(v)=='function' | |
| r.push k | |
| return r | |
| _warp = (self, name, func)-> | |
| get : -> | |
| o = self(name) | |
| return o[func].bind(o) | |
| _table = (knex, name)-> | |
| table = new Function() | |
| for func in _FUNC_LIST | |
| Object.defineProperty( | |
| table.prototype | |
| func | |
| _warp(knex, name, func) | |
| ) | |
| r = new table() | |
| return r | |
| exports = -> | |
| DB.apply DB,arguments | |
| promise = new Promise( | |
| (resolve, reject)->> | |
| table_li = await DB.li1( | |
| "SELECT table_name as name FROM information_schema.tables WHERE table_schema='#TABLE_SCHEMA' AND table_type='BASE TABLE'" | |
| # "SELECT name FROM sqlite_master WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' UNION ALL SELECT name FROM sqlite_temp_master WHERE type IN ('table','view') ORDER BY 1" | |
| ) | |
| # 不能创建为name的表名, 会被function的name覆盖 | |
| for name in table_li | |
| exports[name] = _table(DB, name) | |
| resolve(exports) | |
| ) | |
| exports.$ = (func)-> | |
| promise.then(func) | |
| do -> | |
| for k,v of DB | |
| if typeof(v) == 'function' | |
| exports["$"+k]=v.bind(DB) | |
| Knex = require('knex') | |
| module.exports = exports |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment