File size: 1,893 Bytes
61d39e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { safeHasOwnProperty } from '../lib/objectfn.js';
import BaseOperation from './BaseOperation.js';

export default class extends BaseOperation {
    static requiredForCreate = [
        'uuid',
        'parent_uid',
    ];

    static allowedForCreate = [
        ...this.requiredForCreate,
        'name',
        'user_id',
        'is_dir',
        'created',
        'modified',
        'immutable',
        'shortcut_to',
        'is_shortcut',
        'metadata',
        'bucket',
        'bucket_region',
        'thumbnail',
        'accessed',
        'size',
        'symlink_path',
        'is_symlink',
        'associated_app_id',
        'path',
    ];

    constructor (entry) {
        super();
        const requiredForCreate = this.constructor.requiredForCreate;
        const allowedForCreate = this.constructor.allowedForCreate;

        {
            const sanitized_entry = {};
            for ( const k of allowedForCreate ) {
                if ( safeHasOwnProperty(entry, k) ) {
                    sanitized_entry[k] = entry[k];
                }
            }
            entry = sanitized_entry;
        }

        for ( const k of requiredForCreate ) {
            if ( ! safeHasOwnProperty(entry, k) ) {
                throw new Error(`Missing required property: ${k}`);
            }
        }

        this.entry = entry;
    }

    getStatement () {
        const fields = Object.keys(this.entry);
        const statement = 'INSERT INTO fsentries ' +
            `(${fields.join(', ')}) ` +
            `VALUES (${fields.map(() => '?').join(', ')})`;
        const values = fields.map(k => this.entry[k]);
        return { statement, values };
    }

    apply (answer) {
        answer.entry = { ...this.entry };
    }

    get uuid () {
        return this.entry.uuid;
    }
};