sqlite
This commit is contained in:
@@ -1,6 +1,14 @@
|
||||
import { CREATE, GET_NEXT_SORT } from '../../providers/mssql/queries/entitycontact.js';
|
||||
import {
|
||||
CREATE as MSSQL_CREATE,
|
||||
GET_NEXT_SORT as MSSQL_GET_NEXT_SORT,
|
||||
} from '../../providers/mssql/queries/entitycontact.js';
|
||||
import {
|
||||
CREATE as SQLITE_CREATE,
|
||||
GET_NEXT_SORT as SQLITE_GET_NEXT_SORT,
|
||||
} from '../../providers/sqlite3/queries/entitycontact.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import resolveEntityAccess from '../entity/resolveEntityAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
function shape(row) {
|
||||
return {
|
||||
@@ -12,9 +20,12 @@ function shape(row) {
|
||||
};
|
||||
}
|
||||
|
||||
export default function create(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
const entityAccess = resolveEntityAccess(pool);
|
||||
export default function create(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const entityAccess = resolveEntityAccess(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
const getSortSql = isSqlite ? SQLITE_GET_NEXT_SORT : MSSQL_GET_NEXT_SORT;
|
||||
const createSql = isSqlite ? SQLITE_CREATE : MSSQL_CREATE;
|
||||
|
||||
/**
|
||||
* Creates a new contact for an entity.
|
||||
@@ -25,6 +36,7 @@ export default function create(pool) {
|
||||
* @param {string} input.contactType - The contact type (e.g. `'email'`).
|
||||
* @param {string} input.contact - The contact value.
|
||||
* @param {number} [input.sort] - Optional sort order.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{id: number, entityId: number, contactType: string, contact: string, sort: number}>} The created contact.
|
||||
* @throws {Error} If the contact type or value is missing.
|
||||
*
|
||||
@@ -32,7 +44,7 @@ export default function create(pool) {
|
||||
* const contact = await store.entitycontact.create(42, 5, { contactType: 'email', contact: 'a@b.c' });
|
||||
* // => { id, entityId, contactType, contact, sort }
|
||||
*/
|
||||
return async (googleAuthId, entityId, input = {}) => {
|
||||
return async (googleAuthId, entityId, input = {}, tx) => {
|
||||
const contactType = input.contactType?.trim?.()
|
||||
? input.contactType.trim()
|
||||
: null;
|
||||
@@ -41,28 +53,33 @@ export default function create(pool) {
|
||||
throw new Error('Contact required');
|
||||
}
|
||||
|
||||
const { companyId } = await access(googleAuthId);
|
||||
await entityAccess(companyId, entityId);
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, tx);
|
||||
await entityAccess(companyId, entityId, tx);
|
||||
|
||||
let sort = input.sort;
|
||||
if (sort === undefined || sort === null) {
|
||||
const next = await pool
|
||||
.request()
|
||||
.input('entityId', entityId)
|
||||
.query(GET_NEXT_SORT);
|
||||
sort = next.recordset[0].sort;
|
||||
const next = await runner.query(getSortSql, { entityId });
|
||||
sort = next.rows[0].sort;
|
||||
} else if (!Number.isFinite(sort)) {
|
||||
throw new Error('Invalid sort');
|
||||
}
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('entityId', entityId)
|
||||
.input('contactType', contactType)
|
||||
.input('contact', contact)
|
||||
.input('sort', sort)
|
||||
.query(CREATE);
|
||||
const result = await runner.query(createSql, {
|
||||
entityId,
|
||||
contactType,
|
||||
contact,
|
||||
sort,
|
||||
});
|
||||
|
||||
return shape(result.recordset[0]);
|
||||
const row = result.rows[0] ?? {
|
||||
id: result.insertId,
|
||||
entityId,
|
||||
contactType,
|
||||
contact,
|
||||
sort,
|
||||
};
|
||||
|
||||
return shape(row);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { DELETE_BY_ENTITY_AND_ID } from '../../providers/mssql/queries/entitycontact.js';
|
||||
import { DELETE_BY_ENTITY_AND_ID as MSSQL_DELETE } from '../../providers/mssql/queries/entitycontact.js';
|
||||
import { DELETE_BY_ENTITY_AND_ID as SQLITE_DELETE } from '../../providers/sqlite3/queries/entitycontact.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import resolveEntityAccess from '../entity/resolveEntityAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function del(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
const entityAccess = resolveEntityAccess(pool);
|
||||
export default function del(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const entityAccess = resolveEntityAccess(provider);
|
||||
const sql = provider?.dialect === 'sqlite3' ? SQLITE_DELETE : MSSQL_DELETE;
|
||||
|
||||
/**
|
||||
* Deletes a contact of an entity.
|
||||
@@ -12,27 +15,28 @@ export default function del(pool) {
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} entityId - The id of the entity.
|
||||
* @param {number} id - The id of the contact to delete.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<void>}
|
||||
* @throws {Error} If the contact is not found.
|
||||
*
|
||||
* @example
|
||||
* await store.entitycontact.delete(42, 5, 9);
|
||||
*/
|
||||
return async (googleAuthId, entityId, id) => {
|
||||
return async (googleAuthId, entityId, id, tx) => {
|
||||
if (!Number.isFinite(id)) {
|
||||
throw new Error('Contact not found');
|
||||
}
|
||||
|
||||
const { companyId } = await access(googleAuthId);
|
||||
await entityAccess(companyId, entityId);
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, tx);
|
||||
await entityAccess(companyId, entityId, tx);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('id', id)
|
||||
.input('entityId', entityId)
|
||||
.query(DELETE_BY_ENTITY_AND_ID);
|
||||
const result = await runner.query(sql, {
|
||||
id,
|
||||
entityId,
|
||||
});
|
||||
|
||||
if (!result.rowsAffected[0]) {
|
||||
if (!result.rowsAffected) {
|
||||
throw new Error('Contact not found');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { LIST_BY_ENTITY } from '../../providers/mssql/queries/entitycontact.js';
|
||||
import { LIST_BY_ENTITY as MSSQL_LIST_BY_ENTITY } from '../../providers/mssql/queries/entitycontact.js';
|
||||
import { LIST_BY_ENTITY as SQLITE_LIST_BY_ENTITY } from '../../providers/sqlite3/queries/entitycontact.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import resolveEntityAccess from '../entity/resolveEntityAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
function shape(row) {
|
||||
return {
|
||||
@@ -12,30 +14,33 @@ function shape(row) {
|
||||
};
|
||||
}
|
||||
|
||||
export default function list(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
const entityAccess = resolveEntityAccess(pool);
|
||||
export default function list(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const entityAccess = resolveEntityAccess(provider);
|
||||
const sql =
|
||||
provider?.dialect === 'sqlite3'
|
||||
? SQLITE_LIST_BY_ENTITY
|
||||
: MSSQL_LIST_BY_ENTITY;
|
||||
|
||||
/**
|
||||
* Lists the contacts of an entity.
|
||||
*
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} entityId - The id of the entity.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<Array<{id: number, entityId: number, contactType: string, contact: string, sort: number}>>} The contacts.
|
||||
*
|
||||
* @example
|
||||
* const contacts = await store.entitycontact.list(42, 5);
|
||||
* // => [{ id, entityId, contactType, contact, sort }]
|
||||
*/
|
||||
return async (googleAuthId, entityId) => {
|
||||
const { companyId } = await access(googleAuthId);
|
||||
await entityAccess(companyId, entityId);
|
||||
return async (googleAuthId, entityId, tx) => {
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, tx);
|
||||
await entityAccess(companyId, entityId, tx);
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('entityId', entityId)
|
||||
.query(LIST_BY_ENTITY);
|
||||
const result = await runner.query(sql, { entityId });
|
||||
|
||||
return result.recordset.map(shape);
|
||||
return result.rows.map(shape);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
import { GET_BY_ENTITY_AND_ID, UPDATE } from '../../providers/mssql/queries/entitycontact.js';
|
||||
import {
|
||||
GET_BY_ENTITY_AND_ID as MSSQL_GET_BY_ENTITY_AND_ID,
|
||||
UPDATE as MSSQL_UPDATE,
|
||||
} from '../../providers/mssql/queries/entitycontact.js';
|
||||
import {
|
||||
GET_BY_ENTITY_AND_ID as SQLITE_GET_BY_ENTITY_AND_ID,
|
||||
UPDATE as SQLITE_UPDATE,
|
||||
} from '../../providers/sqlite3/queries/entitycontact.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import resolveEntityAccess from '../entity/resolveEntityAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
function shape(row) {
|
||||
return {
|
||||
@@ -12,9 +20,14 @@ function shape(row) {
|
||||
};
|
||||
}
|
||||
|
||||
export default function update(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
const entityAccess = resolveEntityAccess(pool);
|
||||
export default function update(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const entityAccess = resolveEntityAccess(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
const getSql = isSqlite
|
||||
? SQLITE_GET_BY_ENTITY_AND_ID
|
||||
: MSSQL_GET_BY_ENTITY_AND_ID;
|
||||
const updateSql = isSqlite ? SQLITE_UPDATE : MSSQL_UPDATE;
|
||||
|
||||
/**
|
||||
* Updates a contact of an entity.
|
||||
@@ -23,6 +36,7 @@ export default function update(pool) {
|
||||
* @param {number} entityId - The id of the entity.
|
||||
* @param {number} id - The id of the contact to update.
|
||||
* @param {object} patch - The update patch (any subset of contact fields).
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{id: number, entityId: number, contactType: string, contact: string, sort: number}>} The updated contact.
|
||||
* @throws {Error} If the contact is not found.
|
||||
*
|
||||
@@ -30,21 +44,18 @@ export default function update(pool) {
|
||||
* const contact = await store.entitycontact.update(42, 5, 9, { contact: 'b@c.d' });
|
||||
* // => { id, entityId, contactType, contact, sort }
|
||||
*/
|
||||
return async (googleAuthId, entityId, id, patch = {}) => {
|
||||
return async (googleAuthId, entityId, id, patch = {}, tx) => {
|
||||
if (!Number.isFinite(id)) {
|
||||
throw new Error('Contact not found');
|
||||
}
|
||||
|
||||
const { companyId } = await access(googleAuthId);
|
||||
await entityAccess(companyId, entityId);
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, tx);
|
||||
await entityAccess(companyId, entityId, tx);
|
||||
|
||||
const existing = await pool
|
||||
.request()
|
||||
.input('id', id)
|
||||
.input('entityId', entityId)
|
||||
.query(GET_BY_ENTITY_AND_ID);
|
||||
const existing = await runner.query(getSql, { id, entityId });
|
||||
|
||||
const current = existing.recordset[0];
|
||||
const current = existing.rows[0];
|
||||
if (!current) {
|
||||
throw new Error('Contact not found');
|
||||
}
|
||||
@@ -73,19 +84,22 @@ export default function update(pool) {
|
||||
sort = patch.sort;
|
||||
}
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('id', id)
|
||||
.input('entityId', entityId)
|
||||
.input('contactType', contactType)
|
||||
.input('contact', contact)
|
||||
.input('sort', sort)
|
||||
.query(UPDATE);
|
||||
const result = await runner.query(updateSql, {
|
||||
id,
|
||||
entityId,
|
||||
contactType,
|
||||
contact,
|
||||
sort,
|
||||
});
|
||||
|
||||
const row = result.rows[0] ?? {
|
||||
id,
|
||||
entityId,
|
||||
contactType,
|
||||
contact,
|
||||
sort,
|
||||
};
|
||||
|
||||
const row = result.recordset[0];
|
||||
if (!row) {
|
||||
throw new Error('Contact not found');
|
||||
}
|
||||
return shape(row);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user