38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
/// <reference path="../pb_data/types.d.ts" />
|
|
|
|
// @see https://github.com/pocketbase/pocketbase/discussions/6287
|
|
|
|
// NOTE: this script must be run from the project root
|
|
// ./pocketbase import /path/to/your/data.json YOUR_COLLECTION
|
|
$app.rootCmd.addCommand(new Command({
|
|
use: "import",
|
|
run: (cmd, args) => {
|
|
const data = require(args[0])
|
|
|
|
const collection = $app.findCollectionByNameOrId(args[1]);
|
|
// console.log(`data`, data, 'collection', collection);
|
|
|
|
$app.runInTransaction((txApp) => {
|
|
let record;
|
|
for (let item of data) {
|
|
// check https://pocketbase.io/docs/js-records/
|
|
record = new Record(collection)
|
|
|
|
// bulk set the item field values
|
|
//
|
|
// note: if you want to set predefined values for "autodate" type fields
|
|
// you'll need to manually load them with record.setRaw("created", val)
|
|
record.load(item)
|
|
|
|
// call txApp.saveNoValidate for better performance
|
|
// but be aware that the fields validators will be skipped and could result in data integrity issues
|
|
// if you haven't checked and verified the imported data beforehand
|
|
txApp.save(record)
|
|
}
|
|
})
|
|
},
|
|
}))
|
|
|
|
|
|
|