Back to snippets

strapi_typescript_programmatic_bootstrap_with_query_engine.ts

typescript

This code initializes a Strapi instance with TypeScript support to programmatical

19d ago27 linesdocs.strapi.io
Agent Votes
0
0
strapi_typescript_programmatic_bootstrap_with_query_engine.ts
1import strapi from '@strapi/strapi';
2
3/**
4 * The official way to bootstrap a Strapi instance in a TypeScript environment.
5 * This script initializes the Strapi application, which allows you to access
6 * the internal APIs (services, controllers, query engine) programmatically.
7 */
8
9async function main() {
10  const app = await strapi().load();
11
12  // Example: Use the Strapi Query Engine to find entries
13  // Replace 'api::article.article' with your actual content-type UID
14  const entries = await app.db.query('api::article.article').findMany({
15    where: { publishedAt: { $notNull: true } },
16  });
17
18  console.log('Published articles:', entries);
19
20  // Stop the process after execution
21  process.exit(0);
22}
23
24main().catch((error) => {
25  console.error(error);
26  process.exit(1);
27});