-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
111 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,36 @@ | ||
import { FieldResolveInput } from 'stucco-js'; | ||
import { prepareModel, prepare_id, prepareSourceParameters } from '../data.js'; | ||
import { DB } from '../db/mongo.js'; | ||
import { DB } from '../db/orm.js'; | ||
|
||
export const handler = async (input: FieldResolveInput) => | ||
DB().then(async (db) => { | ||
const _id = prepare_id(input) || (prepareSourceParameters(input)._id as string); | ||
if (!_id) throw new Error('_id not found'); | ||
const entries = Object.entries(input.arguments || {}); | ||
const reconstructedObject: Record<string, any> = {}; | ||
|
||
const entriesWithOutId = entries.filter((e) => e[0] !== '_id'); | ||
if (!entriesWithOutId) { | ||
throw new Error(`You need update input argument for this resolver to work`); | ||
} | ||
|
||
if (typeof entriesWithOutId[0][1] ==='object' ) { | ||
if(entriesWithOutId[1]) throw new Error( | ||
'There should be only string arguments or _id argument and one argument of "input" type to update this model', | ||
) | ||
} else{ | ||
(entriesWithOutId as [string, any][]).forEach((entry: [string, any]) => { | ||
|
||
if (typeof entriesWithOutId[0][1] === 'object') { | ||
if (entriesWithOutId[1]) | ||
throw new Error( | ||
'There should be only string arguments or _id argument and one argument of "input" type to update this model', | ||
); | ||
} else { | ||
(entriesWithOutId as [string, any][]).forEach((entry: [string, any]) => { | ||
const [key, value] = entry; | ||
reconstructedObject[key] = value; | ||
}); | ||
} | ||
const setter = typeof entriesWithOutId[0][1] ==='object' ? entriesWithOutId[0][1] : reconstructedObject | ||
const setter = typeof entriesWithOutId[0][1] === 'object' ? entriesWithOutId[0][1] : reconstructedObject; | ||
const filterInput: Record<string, any> = { _id, ...prepareSourceParameters(input) }; | ||
const res = await db | ||
.collection(prepareModel(input)) | ||
.updateOne(filterInput, { $set: { ...(setter), updatedAt: new Date().toISOString() } }) | ||
if(res.matchedCount<1) throw new Error(`Object for update not found. Please check parameters: ${ JSON.stringify(filterInput)}`) | ||
return res.modifiedCount>=1 | ||
const res = await db(prepareModel(input)).collection.updateOne(filterInput, { | ||
$set: { ...setter, updatedAt: new Date().toISOString() }, | ||
}); | ||
if (res.matchedCount < 1) | ||
throw new Error(`Object for update not found. Please check parameters: ${JSON.stringify(filterInput)}`); | ||
return res.modifiedCount >= 1; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,77 @@ | ||
import { FieldResolveInput } from 'stucco-js'; | ||
import { prepareModel, prepareSourceParameters } from '../data.js'; | ||
import { DB } from '../db/mongo.js'; | ||
import { DB } from '../db/orm.js'; | ||
|
||
export const handler = async (input: FieldResolveInput) => { | ||
return DB().then((db) => { | ||
const sortArg = input.arguments?.sortByField || input.arguments?.sort | ||
const sort = (typeof sortArg === 'object') ? sortArg as {field: string, order?: boolean} : undefined | ||
const field = snakeCaseToCamelCase(sort?.field as unknown as string) | ||
const fieldFilter = input.arguments?.fieldFilter | ||
const dateFilter =input.arguments?.dateFilter | ||
const fieldRegexFilter: any = input.arguments?.fieldRegexFilter ? input.arguments?.fieldRegexFilter : fieldFilter ? {} : checkStringFields(input.arguments)? input.arguments : {} | ||
if (fieldRegexFilter?.sortByField) delete fieldRegexFilter?.sortByField | ||
if (fieldRegexFilter?.sort) delete fieldRegexFilter?.sort | ||
if (fieldRegexFilter?.dateFilter) delete fieldRegexFilter?.dateFilter | ||
const sortArg = input.arguments?.sortByField || input.arguments?.sort; | ||
const sort = typeof sortArg === 'object' ? (sortArg as { field: string; order?: boolean }) : undefined; | ||
const field = snakeCaseToCamelCase(sort?.field as unknown as string); | ||
const fieldFilter = input.arguments?.fieldFilter; | ||
const dateFilter = input.arguments?.dateFilter; | ||
const fieldRegexFilter: any = input.arguments?.fieldRegexFilter | ||
? input.arguments?.fieldRegexFilter | ||
: fieldFilter | ||
? {} | ||
: checkStringFields(input.arguments) | ||
? input.arguments | ||
: {}; | ||
if (fieldRegexFilter?.sortByField) delete fieldRegexFilter?.sortByField; | ||
if (fieldRegexFilter?.sort) delete fieldRegexFilter?.sort; | ||
if (fieldRegexFilter?.dateFilter) delete fieldRegexFilter?.dateFilter; | ||
const filterInput = { | ||
...prepareSourceParameters(input), | ||
...convertDateFilter(dateFilter as QueryObject), | ||
...(ifValueIsArray(fieldFilter as QueryObject)), | ||
...ifValueIsArray(fieldFilter as QueryObject), | ||
...convertObjectToRegexFormat(ifValueIsArray(fieldRegexFilter) as QueryObject), | ||
}; | ||
|
||
return db.collection(prepareModel(input)).find(filterInput).sort(field ? { [field]: sort?.order === false ? -1 : 1 } : { _id: 1 }).toArray(); | ||
|
||
return db(prepareModel(input)) | ||
.collection.find(filterInput) | ||
.sort(field ? { [field]: sort?.order === false ? -1 : 1 } : { _id: 1 }) | ||
.toArray(); | ||
}); | ||
}; | ||
|
||
interface QueryObject { | ||
[key: string]: unknown; | ||
} | ||
|
||
|
||
// Function to convert the object to the desired format | ||
function convertObjectToRegexFormat(obj: QueryObject): QueryObject | undefined { | ||
|
||
|
||
for (const key in obj) { | ||
if (Array.isArray(obj[key])) obj[key] = { $regex: { $in: obj[key]}, $options: 'i' } | ||
if (obj[key]&& typeof obj[key] === 'string' ) obj[key] = { $regex: obj[key], $options: 'i' }; | ||
if (Array.isArray(obj[key])) obj[key] = { $regex: { $in: obj[key] }, $options: 'i' }; | ||
if (obj[key] && typeof obj[key] === 'string') obj[key] = { $regex: obj[key], $options: 'i' }; | ||
} | ||
return obj; | ||
} | ||
|
||
|
||
function ifValueIsArray(obj: QueryObject): QueryObject | undefined { | ||
for (const key in obj) { | ||
if (Array.isArray(obj[key]) ) obj[key] = { $in: obj[key]} | ||
if (Array.isArray(obj[key])) obj[key] = { $in: obj[key] }; | ||
} | ||
return obj; | ||
} | ||
|
||
|
||
function snakeCaseToCamelCase(input: string | null | undefined) { | ||
function snakeCaseToCamelCase(input: string | null | undefined) { | ||
return input?.toLowerCase().replace(/_([a-z])/g, (match, group1) => group1.toUpperCase()); | ||
} | ||
|
||
|
||
function checkStringFields(obj: Record<string, unknown> | undefined): boolean { | ||
for (const key in obj) { | ||
if (typeof obj[key] === 'string' || Array.isArray(obj[key])) { | ||
return true; | ||
if (typeof obj[key] === 'string' || Array.isArray(obj[key])) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
return false; | ||
} | ||
|
||
|
||
function convertDateFilter(obj: QueryObject): QueryObject | undefined { | ||
if(!obj) return {} | ||
return { [obj.dateFieldName? obj.dateFieldName as string : 'createdAt'] : { | ||
$gte: obj.from || "", | ||
...(obj.to ? { $lte: obj.to } : {}), | ||
}} | ||
if (!obj) return {}; | ||
return { | ||
[obj.dateFieldName ? (obj.dateFieldName as string) : 'createdAt']: { | ||
$gte: obj.from || '', | ||
...(obj.to ? { $lte: obj.to } : {}), | ||
}, | ||
}; | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { FieldResolveInput } from 'stucco-js'; | ||
import { prepareModel, prepareRequired_id, prepareSourceParameters } from '../data.js'; | ||
import { DB } from '../db/mongo.js'; | ||
import { DB } from '../db/orm.js'; | ||
|
||
export const handler = async (input: FieldResolveInput) => | ||
DB().then(async (db) => { | ||
const _id = prepareRequired_id(input); | ||
const filterInput: Record<string, any> = { _id, ...prepareSourceParameters(input) }; | ||
return db.collection(prepareModel(input)).findOne(filterInput); | ||
return db(prepareModel(input)).collection.findOne(filterInput); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { iGraphQL } from 'i-graphql'; | ||
import { ObjectId } from 'mongodb'; | ||
|
||
export const orm = async () => { | ||
console.log('IGraphQl'); | ||
//const modell = collection.replace("Collection", "Model") | ||
|
||
return iGraphQL<Record<string, any>, { _id: () => string; createdAt: () => string }>({ | ||
_id: () => new ObjectId().toHexString(), | ||
createdAt: () => new Date().toISOString(), | ||
}); | ||
}; | ||
|
||
export async function DB() { | ||
return orm(); | ||
} |