Comparing version 1.0.229 to 1.0.230
import { OrmaMutation } from '../types/mutation/mutation_types'; | ||
import { OrmaQuery } from '../types/query/query_types'; | ||
import { OrmaSchema } from '../types/schema/schema_types'; | ||
export declare const prepopulate: (orma_query: (query: OrmaQuery<any, any>) => Promise<any>, orma_mutate: (mutation: OrmaMutation<any>) => Promise<any>, orma_schema: OrmaSchema) => Promise<void>; | ||
type OrmaQueryFunction = (query: OrmaQuery<any, any>) => Promise<any>; | ||
type OrmaMutateFunction = (mutation: OrmaMutation<any>) => Promise<any>; | ||
export declare const prepopulate: (orma_query: OrmaQueryFunction, orma_mutate: OrmaMutateFunction, orma_schema: OrmaSchema) => Promise<void>; | ||
export {}; |
@@ -6,4 +6,5 @@ "use strict"; | ||
const diff_mutation_1 = require("../mutate/diff/diff_mutation"); | ||
const prepopulate = async (orma_query, orma_mutate, orma_schema) => { | ||
const get_prepopulate_query = async (orma_query, orma_schema) => { | ||
var _a, _b; | ||
let query = {}; | ||
const table_names = (0, schema_helpers_1.get_entity_names)(orma_schema); | ||
@@ -26,24 +27,28 @@ for (const table_name of table_names) { | ||
if (((_b = diff[table_name]) === null || _b === void 0 ? void 0 : _b.length) > 0) { | ||
const create_count = diff[table_name].filter(el => el.$operation === 'create').length; | ||
const update_count = diff[table_name].filter(el => el.$operation === 'update').length; | ||
const delete_count = diff[table_name].filter(el => el.$operation === 'delete').length; | ||
try { | ||
await orma_mutate(diff); | ||
// console.log( | ||
// `✏️✏️✏️ Prepopulated ${table_name} with ${create_count} new rows and ${update_count} updated rows.` | ||
// ) | ||
create_count > 0 && | ||
console.log(`Prepopulate: 🌱 ${create_count} rows added to ${table_name}`); | ||
update_count > 0 && | ||
console.log(`Prepopulate: ✏️ ${update_count} rows update in ${table_name}`); | ||
delete_count > 0 && | ||
console.log(`Prepopulate: ✂️ ${delete_count} rows deleted in ${table_name}`); | ||
} | ||
catch (error) { | ||
console.error(`❌❌❌ Prepopulate failed for ${table_name}`); | ||
throw error; | ||
} | ||
query[table_name] = diff[table_name]; | ||
} | ||
} | ||
return query; | ||
}; | ||
const prepopulate = async (orma_query, orma_mutate, orma_schema) => { | ||
const prepopulate_query = await get_prepopulate_query(orma_query, orma_schema); | ||
try { | ||
await orma_mutate(prepopulate_query); | ||
Object.entries(prepopulate_query).forEach(async ([table_name, rows]) => { | ||
const create_count = rows.filter(el => el.$operation === 'create').length; | ||
const update_count = rows.filter(el => el.$operation === 'update').length; | ||
const delete_count = rows.filter(el => el.$operation === 'delete').length; | ||
create_count > 0 && | ||
console.log(`Prepopulate: 🌱 ${create_count} rows added to ${table_name}`); | ||
update_count > 0 && | ||
console.log(`Prepopulate: ✏️ ${update_count} rows updated in ${table_name}`); | ||
delete_count > 0 && | ||
console.log(`Prepopulate: ✂️ ${delete_count} rows deleted in ${table_name}`); | ||
}); | ||
} | ||
catch (error) { | ||
console.error(`Prepopulate: ❌ failed`); | ||
throw error; | ||
} | ||
}; | ||
exports.prepopulate = prepopulate; |
{ | ||
"name": "orma", | ||
"version": "1.0.229", | ||
"version": "1.0.230", | ||
"description": "A declarative relational syncronous orm", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -7,7 +7,11 @@ import { get_entity_names } from '../helpers/schema_helpers' | ||
export const prepopulate = async ( | ||
orma_query: (query: OrmaQuery<any, any>) => Promise<any>, | ||
orma_mutate: (mutation: OrmaMutation<any>) => Promise<any>, | ||
type OrmaQueryFunction = (query: OrmaQuery<any, any>) => Promise<any> | ||
type OrmaMutateFunction = (mutation: OrmaMutation<any>) => Promise<any> | ||
const get_prepopulate_query = async ( | ||
orma_query: OrmaQueryFunction, | ||
orma_schema: OrmaSchema | ||
) => { | ||
let query = {} | ||
const table_names = get_entity_names(orma_schema) | ||
@@ -38,18 +42,33 @@ | ||
if (diff[table_name]?.length > 0) { | ||
const create_count = diff[table_name].filter( | ||
el => el.$operation === 'create' | ||
).length | ||
const update_count = diff[table_name].filter( | ||
el => el.$operation === 'update' | ||
).length | ||
const delete_count = diff[table_name].filter( | ||
el => el.$operation === 'delete' | ||
).length | ||
query[table_name] = diff[table_name] | ||
} | ||
} | ||
try { | ||
await orma_mutate(diff) | ||
// console.log( | ||
// `✏️✏️✏️ Prepopulated ${table_name} with ${create_count} new rows and ${update_count} updated rows.` | ||
// ) | ||
return query | ||
} | ||
export const prepopulate = async ( | ||
orma_query: OrmaQueryFunction, | ||
orma_mutate: OrmaMutateFunction, | ||
orma_schema: OrmaSchema | ||
) => { | ||
const prepopulate_query = await get_prepopulate_query( | ||
orma_query, | ||
orma_schema | ||
) | ||
try { | ||
await orma_mutate(prepopulate_query) | ||
Object.entries(prepopulate_query).forEach( | ||
async ([table_name, rows]: any) => { | ||
const create_count = rows.filter( | ||
el => el.$operation === 'create' | ||
).length | ||
const update_count = rows.filter( | ||
el => el.$operation === 'update' | ||
).length | ||
const delete_count = rows.filter( | ||
el => el.$operation === 'delete' | ||
).length | ||
create_count > 0 && | ||
@@ -61,3 +80,3 @@ console.log( | ||
console.log( | ||
`Prepopulate: ✏️ ${update_count} rows update in ${table_name}` | ||
`Prepopulate: ✏️ ${update_count} rows updated in ${table_name}` | ||
) | ||
@@ -68,8 +87,8 @@ delete_count > 0 && | ||
) | ||
} catch (error) { | ||
console.error(`❌❌❌ Prepopulate failed for ${table_name}`) | ||
throw error | ||
} | ||
} | ||
) | ||
} catch (error) { | ||
console.error(`Prepopulate: ❌ failed`) | ||
throw error | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1616040
39404