nocobase2/benchmark/koa-database/index.js
YANG QIA 474b09c7f2
perf(server): optimize performance of APIs (#3079)
* perf: add perf_hooks

* perf: add cache

* fix: test

* feat: support bloom filter

* feat: caching token black list

* perf: caching i18n instance

* fix: test

* fix: test

* chore: remove prePerfHooks on app

* chore: improve i18n instances cache

* chore: remove performance measure

* fix: package.json

* perf: optimize cache strategy

* fix: test

* fix: bug

* test: storer of auth-manager

* fix: afterDestroy hook when updating null value

* fix: version

* chore: fix bug and add test

* fix: test

* fix: test
2023-12-12 23:02:09 +08:00

89 lines
1.7 KiB
JavaScript

const Koa = require('koa');
const { Database } = require('@nocobase/database');
const dotenv = require('dotenv');
dotenv.config();
const db = new Database({
logging: false,
dialect: process.env.DB_DIALECT,
storage: process.env.DB_STORAGE,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
timezone: process.env.DB_TIMEZONE,
tablePrefix: process.env.DB_TABLE_PREFIX,
schema: process.env.DB_SCHEMA,
underscored: process.env.DB_UNDERSCORED === 'true',
});
db.collection({
name: 'users',
sortable: 'sort',
fields: [
{
name: 'id',
type: 'bigInt',
autoIncrement: true,
primaryKey: true,
allowNull: false,
},
{
type: 'string',
name: 'nickname',
},
{
type: 'string',
name: 'username',
unique: true,
},
{
type: 'string',
name: 'email',
unique: true,
},
{
type: 'string',
name: 'phone',
unique: true,
},
{
type: 'password',
name: 'password',
hidden: true,
},
{
type: 'string',
name: 'appLang',
},
{
type: 'string',
name: 'resetToken',
unique: true,
hidden: true,
},
{
type: 'json',
name: 'systemSettings',
defaultValue: {},
},
],
});
const app = new Koa();
app.use(async (ctx, next) => {
const repository = db.getRepository('users');
ctx.body = await repository.findAndCount({
limit: 20,
offset: 0,
});
await next();
});
app.listen(13010, () => {
console.log('koa-database: http://localhost:13010/');
});