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AAK,GAA4B,iBAAX,GAAuB,MAAM,QAAS,GACxD,MAAM,IAAI,MAAO,4BAGrB,IAAK,EAAO,eAAgB,SACxB,MAAM,IAAI,MAAO,gCAGrB,KAAK,GAAK,EACV,KAAK,MAAQ,EAAO,MACpB,KAAK,WAAa,EAAO,YAAc,IAC3C,CAEA,WAAA,GACI,OAAO,IAAI,YAAa,KAAK,GAAI,KAAK,MAC1C,CAEA,GAAA,IAAQ,GACJ,OAAO,KAAK,GAAG,SAAU,EAC7B,CAEA,MAAA,IAAW,GACP,OAAO,KAAK,cAAc,UAAW,EACzC,CAEA,IAAA,CAAM,EAAO,EAAQ,GACjB,OAAwB,OAApB,KAAK,WACE,QAAQ,OAAQ,8BAGL,mBAAX,IACP,EAAW,EACX,EAAS,IAEb,EAAS,GAAU,GACZ,KAAK,UAAW,GAAS,MAAO,KAAK,WAAY,GAAQ,OAAO,MAAM,IACzE,IAAI,EAAS,EAAK,IAAO,KACzB,OAAO,EAAW,EAAU,KAAM,GAAW,CAAM,IACpD,OAAO,GAAO,EAAU,IAAS,IACxC,CAEA,MAAA,IAAW,GACP,OAAO,KAAK,cAAc,UAAW,EACzC,CAEA,MAAA,IAAW,GACP,OAAO,KAAK,cAAc,SAAS,OAAQ,EAC/C,CAEA,UAAW,GACP,OAAO,KAAK,cAAc,SAAS,SAAU,EACjD,EAIJ,MAAM,WACF,WAAA,CAAa,GACT,KAAK,GAAK,CACd,CAEA,MAAA,CAAQ,GACJ,OAAO,IAAI,MAAO,KAAK,GAAI,EAC/B,EAGJ,OAAO,QAAU,CAAE,sBAAY","file":"/npm/@soulmaneller-dev/nodejs-model-mysql@0.0.6/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@soulmaneller-dev/nodejs-model-mysql@0.0.6/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","class QueryString {\n    constructor( db, table ) {\n        this.db = db;\n        this.table = table;\n        this._update = [];\n        this.add = [];\n        this.conditions = \"\";\n        this.val = [];\n        this.orderType = 'ASC';\n        this.limitNo = '';\n        this.type = null;\n    }\n\n    select( ...fields ) {\n        if( this.type !== null ) throw new Error( `Type cannot be changed` );\n        this.type = 'select';\n        let columns = fields.join( ', ' ) || '*';\n        this.begin = `SELECT ${ columns } FROM ${ this.table }`;\n        return this;\n    }\n\n    insert( ...fields ) {\n        if( this.type !== null ) throw new Error( `Type cannot be changed` );\n        this.type = 'insert';\n        let str = `INSERT INTO ${ this.table }`;\n        if( fields.length > 0 ) {\n            str = str + ` ( ${ fields.join( ', ' )} )`;\n        }\n        str = str + ' VALUES';\n        this.begin = str;\n\n        return this;\n    }\n\n    values( ...values ) {\n        if( this.type !== 'insert' ) {\n            throw new Error( `Type is not matched` );\n        }\n        this.add.push( `( ${ values.map(() => '?' ).join( ', ' )} )` );\n        this.val = this.val.concat( values );\n        return this;\n    }\n\n    update() {\n        if( this.type !== null ) throw new Error( `Type cannot be changed` );\n        this.type = 'update';\n        this.begin = `UPDATE ${ this.table } SET`;\n        return this;\n    }\n\n    set( key, val ) {\n        if( this.type !== 'update' ) {\n            throw new Error( `Type is not matched` );\n        }\n        this._update.push( `${ key } = ?` );\n        this.val.push( val );\n        return this;\n    }\n\n    delete() {\n        if( this.type !== null ) throw new Error( `Type cannot be changed` );\n        this.type = 'delete';\n        this.begin = `DELETE FROM ${ this.table }`;\n        return this;\n    }\n\n    setWhere( type, key, op, val ) {\n        if( val === undefined || val === null  ) {\n            val = op;\n            op = '=';\n        }\n        let cond = this.conditions;\n        cond = cond ? `${ cond } `: cond;\n        if( type !== 'IN' ) {\n            if( cond ) {\n                cond = cond + `${ type } `;\n            }\n            cond = `${ cond }${ key } ${ op } ?`;\n            this.val.push( val );\n        } else {\n            cond = cond + `${ key } IN ( ${ val.map(() => '?' ).join( ', ' )} )`;\n            this.val = this.val.concat( val );\n        }\n        this.conditions = cond;\n        return this;\n    }\n\n    where( ...params ) {\n        return this.setWhere( 'AND', ...params );\n    }\n\n    whereOr( ...params ) {\n        return this.setWhere( 'OR', ...params );\n    }\n\n    whereIn( ...params ) {\n        if( this.conditions ) {\n            throw new Error( `whereIn must be the first condition` );\n        }\n        return this.setWhere( 'IN', ...params );\n    }\n\n    orderBy( ...keys ) {\n        this.order = `ORDER BY ${ keys.join( ',' ) }`;\n        return this;\n    }\n\n    limit() {\n        let params = Array.prototype.slice.call( arguments );\n        this.limitNo = `LIMIT ${ params.join( \",\" ) }`;\n        return this;\n    }\n\n    exec( callback ) {\n        let conditions = this.conditions ? `WHERE ${ this.conditions }` : '';\n        let order = this.order ? `${ this.order } ${ this.orderType }` : '';\n        let queryString = `${ this.begin }${ getStr( this._update.join( ', ' ))}${ getStr( conditions || this.add.join( \", \" ))}${ getStr( order )}${ getStr( this.limitNo )}`.trim();\n        function getStr( str ) {\n            return str ? ` ${ str }` : '';\n        }\n        return this.db.query( queryString, this.val, callback );\n    }\n}\n\nclass Model {\n    constructor( db, config ) {\n        if( typeof db.query !== 'function' ) {\n            throw new Error( 'db.query is not a function' );\n        }\n\n        if( !config || typeof config !== 'object' || Array.isArray( config )) {\n            throw new Error( 'Config must be an object' );\n        }\n\n        if( !config.hasOwnProperty( 'table' )) {\n            throw new Error( 'Attribute \"table\" not exists' );\n        }\n\n        this.db = db;\n        this.table = config.table;\n        this.primaryKey = config.primaryKey || null;\n    }\n\n    queryString() {\n        return new QueryString( this.db, this.table );\n    }\n\n    sql( ...data ) {\n        return this.db.query( ...data );\n    }\n\n    select( ...fields ) {\n        return this.queryString().select( ...fields );\n    }\n\n    find( value, fields, callback ) {\n        if( this.primaryKey === null ) {\n            return Promise.reject( 'Primary keys is undefined' );\n        }\n\n        if( typeof fields === 'function' ) {\n            callback = fields;\n            fields = [];\n        }\n        fields = fields || [];\n        return this.select( ...fields ).where( this.primaryKey, value ).exec().then( res => {\n            let output = res[ 0 ] || null;\n            return callback ? callback( null, output ) : output;\n        }).catch( err => callback( err ) || err );\n    }\n\n    insert( ...fields ) {\n        return this.queryString().insert( ...fields );\n    }\n\n    update( ...params ) {\n        return this.queryString().update().set( ...params );\n    }\n\n    delete( ...params ) {\n        return this.queryString().delete().where( ...params );\n    }\n}\n\n\nclass MySQLModel {\n    constructor( db ) {\n        this.db = db;\n    }\n\n    extend( config ) {\n        return new Model( this.db, config );\n    }\n}\n\nmodule.exports = { MySQLModel, Model };\n"]}