使用 NodeJS 和 ObjectionJs 进行分页查询
Paging query with NodeJS and ObjectionJs
您好,我是 NodeJS 的新手,正在使用 ObjectionJS 作为 ORM。
我想为我的用户 table 做一个迁移脚本,以便更改每行的某些字段。
我做了这样的东西
export default class UserMigration {
constructor(firstId, lastId = 9000000) {
this.firstId = firstId;
this.lastId = lastId;
}
migrate() {
let more = true;
let page = 0;
let batchSize = 100;
while(more) {
UserModel.query().where('id', '>=', this.firstId)
.where('id', '<=', this.lastId)
.page(page, batchSize)
.then((result) => {
let users = result.results;
debug('Page: ', page);
debug('PageSize: ', users.length)
users.forEach((user) => {
// Do something here
});
if(result.results.length < batchSize) {
more = false
} else {
page++;
}
})
}
}
}
但后来我意识到查询是异步执行的,而 while
块是同步执行的,对吗?
如何在不一次查询 returns 所有用户的情况下实现迁移?
提前致谢!!
我使用 async/await
实现了这一点
export default class UserMigration {
constructor(firstId, lastId = 9000000) {
this.firstId = firstId;
this.lastId = lastId;
}
run() {
this.migrateV3().then((data) => {
debug('All migrated');
debug('data: ', data);
}).catch((data) => {
debug('Error');
debug('data: ', data);
});
}
async migrateV3() {
let more = true;
let page = 0;
let batchSize = 100;
while(more) {
try {
let result = await UserModel.query().where('id', '>=', this.firstId)
.where('id', '<=', this.lastId)
.page(page, batchSize);
let users = result.results;
debug('Page: ', page);
debug('PageSize: ', users.length)
for(let user of users) {
debug(`User ${user.id} migration start`);
// Do something
};
if(result.results.length < batchSize) {
more = false
} else {
page++;
}
}
catch (err) {
throw err;
}
}
}
}
您好,我是 NodeJS 的新手,正在使用 ObjectionJS 作为 ORM。 我想为我的用户 table 做一个迁移脚本,以便更改每行的某些字段。
我做了这样的东西
export default class UserMigration {
constructor(firstId, lastId = 9000000) {
this.firstId = firstId;
this.lastId = lastId;
}
migrate() {
let more = true;
let page = 0;
let batchSize = 100;
while(more) {
UserModel.query().where('id', '>=', this.firstId)
.where('id', '<=', this.lastId)
.page(page, batchSize)
.then((result) => {
let users = result.results;
debug('Page: ', page);
debug('PageSize: ', users.length)
users.forEach((user) => {
// Do something here
});
if(result.results.length < batchSize) {
more = false
} else {
page++;
}
})
}
}
}
但后来我意识到查询是异步执行的,而 while
块是同步执行的,对吗?
如何在不一次查询 returns 所有用户的情况下实现迁移?
提前致谢!!
我使用 async/await
实现了这一点export default class UserMigration {
constructor(firstId, lastId = 9000000) {
this.firstId = firstId;
this.lastId = lastId;
}
run() {
this.migrateV3().then((data) => {
debug('All migrated');
debug('data: ', data);
}).catch((data) => {
debug('Error');
debug('data: ', data);
});
}
async migrateV3() {
let more = true;
let page = 0;
let batchSize = 100;
while(more) {
try {
let result = await UserModel.query().where('id', '>=', this.firstId)
.where('id', '<=', this.lastId)
.page(page, batchSize);
let users = result.results;
debug('Page: ', page);
debug('PageSize: ', users.length)
for(let user of users) {
debug(`User ${user.id} migration start`);
// Do something
};
if(result.results.length < batchSize) {
more = false
} else {
page++;
}
}
catch (err) {
throw err;
}
}
}
}