本文整理汇总了TypeScript中knex类的典型用法代码示例。如果您正苦于以下问题:TypeScript knex类的具体用法?TypeScript knex怎么用?TypeScript knex使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了knex类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: makeKnex
export function makeKnex({ host, user, password, database }) {
const knex = makeKnexConnection({
client: 'pg',
connection: {
database,
host,
password: 'postgres',
user,
},
})
// @TODO: Figure out how to make this work with async await or Promises.
// @TODO: See why migrations bugs out in tests.
if (process.env.NODE_ENV !== 'test') {
knex.migrate
.rollback()
.then(() => knex.migrate.latest())
.then(() =>
knex.seed
.run()
.then(() => knex)
// tslint:disable-next-line:no-console
.catch(e => console.error(e) && process.exit(1))
)
}
return knex
}
示例2: beforeAll
beforeAll(async () => {
knex = Knex(DEFAULT_DB_CONFIG);
await knex.migrate.latest();
await knex.seed.run();
const dbConfig = new DbConfig(DEFAULT_DB_CONFIG);
container = new Container();
container.bind<DbConfig>('DefaultDbConfig').toConstantValue(dbConfig);
// container...
container.bind<ICounterRepository>(TYPES.ICounterRepository).to(CounterRepository);
});
示例3: copyRunDbFixerScript
async function copyRunDbFixerScript(dbFileNamePath: string, backupDbPath: string): Promise<void> {
await promisify(fs.copyFile)(dbFileNamePath, backupDbPath);
// need to do this because cli runs migrations in .ts and augur-app runs migrations in .js
const db: Knex = Knex({
client: "sqlite3",
connection: {
filename: backupDbPath,
},
acquireConnectionTimeout: 5 * 60 * 1000,
useNullAsDefault: true,
});
await db.raw("update knex_migrations set name = substr(name,1, length(name)-2) || 'js';");
await db.destroy();
}
示例4: config
private config(): void {
// Sets the global header `Server` as a middleware. We
// are intentionally receiving and ignoring the `req`
// parameter, indicated by the underscore.
this.app.use((_, res, next): void => {
res.set("Server", "TypeScript-rest");
next();
});
// Initiatlize connection to the database and connect
// the knex query builder to our objection models.
const knex = Knex(Knexfile);
Model.knex(knex);
}
示例5: knex
knex(tableName?: string | Knex.Raw | Knex.QueryBuilder | undefined) {
if (!knexInstance) {
knexInstance = Knex({
client: 'mysql',
connection: {
host: DB_HOST,
user: DB_USER,
password: DB_PASS,
database: WORDPRESS_DB_NAME
}
})
}
return knexInstance(tableName)
}
示例6: test
test('should add a new record on #save()', async function (t) {
// startup...
const knex = Knex({ client: 'sqlite3', connection: { filename: ':memory:' } });
Model.knex(knex);
Models.One.app = 'myBlog';
const m = new Migration({ tables: [] }, { tables: [Models.One.toJSON()] });
Migration.run(knex, await m.up());
const one = new Models.One({ myBoolean: 1, myDate: Date.now(), myDateTime: Date.now() });
await one.save();
const found = await Models.One.query().where('id', 1);
// teardown...
await knex.destroy();
t.same(one, found[0]);
})
示例7: resetDatabase
static resetDatabase(done) {
let connection = knex({
client : 'mysql',
connection: {
user : 'root',
host : '127.0.0.1',
database: 'wetland_test',
},
});
connection.raw('drop database wetland_test').then(function () {
return connection.raw('create database wetland_test').then(() => {
connection.destroy().then(() => {
done();
});
});
});
}
示例8: knex
export function knex() {
if (!knexInstance) {
knexInstance = Knex({
client: 'mysql',
connection: {
host: DB_HOST,
user: DB_USER,
password: DB_PASS,
database: DB_NAME,
typeCast: (field: any, next: any) => {
if (field.type == 'TINY' && field.length == 1) {
return (field.string() == '1'); // 1 = true, 0 = false
}
return next();
}
}
})
}
return knexInstance
}
示例9: constructor
constructor(dataBaseInfo: IConnection) {
// PostgreSQL, MySQL, and SQLite3.
// XXX::: Dont know about port
let knex = Knex({
client: dataBaseInfo.driver,
connection: {
host: dataBaseInfo.host,
user: dataBaseInfo.username,
password: dataBaseInfo.password,
database: dataBaseInfo.database,
charset: 'utf8'
}
});
knex['_config'] = dataBaseInfo;
// XXX::: todo for mongo
// XXX::: todo for elastic
this._connection = Bookshelf(knex);
}
示例10: knexFactory
import config from './index';
import * as knexFactory from 'knex';
import Knex from 'knex';
const env = process.env['NODE_ENV'] || 'development',
MIGRATIONS_TABLE = 'migrations',
{ DB_CONNECTION, PG_POOL_MIN, PG_POOL_MAX, PG_SEARCH_PATH } = config[env];
export const dbConfig = {
client: 'pg',
connection: DB_CONNECTION,
pool: {
min: PG_POOL_MIN,
max: PG_POOL_MAX
},
searchPath: PG_SEARCH_PATH,
migrations: {
directory: './migrations',
tableName: MIGRATIONS_TABLE
},
seeds: {
directory: './seeds',
}
};
const db: Knex = knexFactory(dbConfig);
export default db;
示例11: initDb
export async function initDb(knexConfig: knex.Config): Promise<any> {
return await knex(knexConfig)
}
示例12: setupDb
export async function setupDb(): Promise<any> {
const config = await get('knex')
const db = knex(config)
await db.migrate.latest()
}
示例13: knexLib
ďťż
import * as knexLib from "knex";
import {dbConfig} from "../../config"
export var knex = knexLib(dbConfig);
示例14: Knex
import * as GeoJSON from 'geojson';
import * as Knex from 'knex';
import * as KPG from 'knex-postgis';
const knex: Knex = Knex({ dialect: 'pg' });
const st: KPG.KnexPostgis = KPG(knex);
const point: GeoJSON.Point = {
type: 'Point',
coordinates: [23.773206, 61.506005],
crs: {
type: 'name',
properties: {
name: 'EPSG:4326'
}
}
};
const wktPoint = 'POINT(23.773206 61.506005)';
knex('geometries')
.select(st.area('wkb_geometry').as('area'))
.then(console.log);
// Can't be done because of Knex typings
// knex('geometries')
// .select('name')
// .where(st.area('wkb_geometry'), '>', 0.1)
knex('geometries')
.select(st.asText('wkb_geometry'))
示例15: Knex
const configInit = (knexConfig) => {
knex = Knex(knexConfig);
Model.knex(knex);
return Promise.resolve(Model);
};