Public
Edited
Mar 10, 2024
1 star
Insert cell
Insert cell
Insert cell
Insert cell
PGlite = import ("https://cdn.jsdelivr.net/npm/@electric-sql/pglite/dist/index.js").then(d => d.PGlite)
Insert cell
db = new PGlite()
Insert cell
randomMessage = await db.query("select 'Hello world' as message;")
Insert cell
randomMessage
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
pgTypes = await db.query(`
SELECT * FROM pg_catalog.pg_type
`)
Insert cell
pgTypes
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
/**
* Create the table
**/
async function createTable() {
await db.query(`
CREATE TABLE BabyNames(
uid SERIAL
, year INT
, gender VARCHAR(50)
, rank INT
, name VARCHAR(50)
, count INT
, PRIMARY KEY (year, gender, name)
)
`);
}
Insert cell
q = insert1(data[0])
Insert cell
function insert1({ Year, Gender, Rank, Name, Count }) {
return `
INSERT INTO BabyNames(Year, Gender, Rank, Name, Count)
VALUES(${Year}, '${Gender}', ${Rank}, '${Name}', ${Count})
`;
}
Insert cell
/**
* Insert new record
**/
async function insert({ Year, Gender, Rank, Name, Count }) {
return await db.query(`
INSERT INTO BabyNames(Year, Gender, Rank, Name, Count)
VALUES(${Year}, '${Gender}', ${Rank}, '${Name}', ${Count})
`);
}
Insert cell
{
var tableNames, records;

// Create the table
await createTable();

// Check if my table is inserted
tableNames = await db.query(`
SELECT relname FROM pg_class
JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
WHERE pg_class.relkind = 'r'
AND relname NOT LIKE 'pg_%'
`);

// Insert some record
for (let i = 0; i < data.length; ++i) {
await insert(data[i]);
}

// Checkout the records
records = await db.query(`
SELECT *
FROM BabyNames
`);

return { tableNames, records };
}
Insert cell
Insert cell
Insert cell
data = FileAttachment("baby_names.csv").csv({typed: true})
Insert cell
Inputs.table(search, {
format: {
Year: d3.format("d") // format as "1960" rather than "1,960"
}
})
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more