airtable
Version:
The official Airtable JavaScript library.
105 lines • 3.56 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var callback_to_promise_1 = __importDefault(require("./callback_to_promise"));
var Record = /** @class */ (function () {
function Record(table, recordId, recordJson) {
this._table = table;
this.id = recordId || recordJson.id;
if (recordJson) {
this.commentCount = recordJson.commentCount;
}
this.setRawJson(recordJson);
this.save = callback_to_promise_1.default(save, this);
this.patchUpdate = callback_to_promise_1.default(patchUpdate, this);
this.putUpdate = callback_to_promise_1.default(putUpdate, this);
this.destroy = callback_to_promise_1.default(destroy, this);
this.fetch = callback_to_promise_1.default(fetch, this);
this.updateFields = this.patchUpdate;
this.replaceFields = this.putUpdate;
}
Record.prototype.getId = function () {
return this.id;
};
Record.prototype.get = function (columnName) {
return this.fields[columnName];
};
Record.prototype.set = function (columnName, columnValue) {
this.fields[columnName] = columnValue;
};
Record.prototype.setRawJson = function (rawJson) {
this._rawJson = rawJson;
this.fields = (this._rawJson && this._rawJson.fields) || {};
};
return Record;
}());
function save(done) {
this.putUpdate(this.fields, done);
}
function patchUpdate(cellValuesByName, opts, done) {
var _this = this;
if (!done) {
done = opts;
opts = {};
}
var updateBody = __assign({ fields: cellValuesByName }, opts);
this._table._base.runAction('patch', "/" + this._table._urlEncodedNameOrId() + "/" + this.id, {}, updateBody, function (err, response, results) {
if (err) {
done(err);
return;
}
_this.setRawJson(results);
done(null, _this);
});
}
function putUpdate(cellValuesByName, opts, done) {
var _this = this;
if (!done) {
done = opts;
opts = {};
}
var updateBody = __assign({ fields: cellValuesByName }, opts);
this._table._base.runAction('put', "/" + this._table._urlEncodedNameOrId() + "/" + this.id, {}, updateBody, function (err, response, results) {
if (err) {
done(err);
return;
}
_this.setRawJson(results);
done(null, _this);
});
}
function destroy(done) {
var _this = this;
this._table._base.runAction('delete', "/" + this._table._urlEncodedNameOrId() + "/" + this.id, {}, null, function (err) {
if (err) {
done(err);
return;
}
done(null, _this);
});
}
function fetch(done) {
var _this = this;
this._table._base.runAction('get', "/" + this._table._urlEncodedNameOrId() + "/" + this.id, {}, null, function (err, response, results) {
if (err) {
done(err);
return;
}
_this.setRawJson(results);
done(null, _this);
});
}
module.exports = Record;
//# sourceMappingURL=record.js.map