fewer
Version:
A minimal ORM for Node.js.
86 lines • 2.65 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Adapter {
constructor(config, impl) {
this.connected = false;
this.connection = null;
this.client = null;
this.config = config;
this.impl = impl;
this.ColumnTypes = impl.columnTypes;
}
async connect() {
// If we're already connected, just resolve:
if (this.connected) {
return;
}
// If a connect is in-progress, wait for it to finish, then resolve:
if (this.connection) {
await this.connection;
return;
}
// Connect:
this.connection = this.impl.connect(this.config);
this.client = await this.connection;
this.connection = null;
this.connected = true;
}
async disconnect() {
if (!this.connected)
return;
const oldClient = this.client;
this.client = null;
this.connected = false;
await this.impl.disconnect(oldClient);
}
async select(query) {
const context = query.context;
return this.impl.select(this.client, context);
}
/**
* Inserts a new record into the database. Returns the id of the newly-inserted item.
*/
insert(query) {
const context = query.context;
return this.impl.insert(this.client, context);
}
/**
* Updates a record in the database. Returns the id of the updated item.
*/
update(query) {
const context = query.context;
return this.impl.update(this.client, context);
}
// HOOKS FOR MIGRATIONS:
migrate(direction, migration) {
return this.impl.migrate(this.client, direction, migration);
}
migrateAddVersion(version) {
return this.impl.migrateAddVersion(this.client, version);
}
migrateRemoveVersion(version) {
return this.impl.migrateRemoveVersion(this.client, version);
}
migrateGetVersions() {
return this.impl.migrateGetVersions(this.client);
}
migrateHasVersion(version) {
return this.impl.migrateHasVersion(this.client, version);
}
getInfos() {
if (!this.impl.getInfos) {
throw new Error('Database Adapter does not implement method for online schema generation.');
}
return this.impl.getInfos(this.client);
}
}
exports.Adapter = Adapter;
function createAdapter(impl) {
return class AdapterImpl extends Adapter {
constructor(config) {
super(config, impl);
}
};
}
exports.createAdapter = createAdapter;
//# sourceMappingURL=index.js.map